chenyc
2025-12-09 545c24c6a711d71b65f3d4e8122fee3837fb1edc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const Buffer = require('buffer').Buffer;
const aliyunIot = require('../../lib');
const Device = require('../../lib/device');
const fixtures = require('../fixtures');
 
let device;
let gateway;
beforeAll(()=> {
  return new Promise((resolve, reject)=>{
    device = aliyunIot.device({
      ...fixtures.sdk_device1
    });
    device.on('connect', () => {
      resolve();
    });
  })
},3000)
 
afterAll(() => {
  device.end();
  gateway.end();
});
 
const registerDeviceInfo = {
  productKey:"a15YDgQGhU0",
  productSecret:"AP4HnuqhNqqArIkH",
  deviceName:"device1"
}
 
describe('device dynamic register test', () => {
 
  test('direct devices register should be ok', done => {
    // 动态注册ok
    aliyunIot.register(registerDeviceInfo,(res)=>{
      console.log("direct devices register should be ok",res)
      if(res.code == '200'){
        done();
      }
    })
  });
 
  test('direct devices register use wrong info shold be error', done => {
    // 动态注册ok
    aliyunIot.register({
      productKey:"xxxxx",
      productSecret:"xxx",
      deviceName:"xxx"
    },(res)=>{
      console.log("direct devices register should be wrong",res)
      if(res.code != '200'){
        done();
      }
    })
  });
 
 
  test('gateway subdevice register should be ok', done => {   
    // 测试网关动态注册子设备 ok
    gateway = aliyunIot.gateway({...fixtures.sdk_gateway1});
    gateway.on('connect', () => {
      gateway.regiestSubDevice([{
        "deviceName": "device3",
        "productKey": "a15YDgQGhU0"
      }],(res)=>{
        if(res.message == 'success'){
          done();
        }
      });
    });
  });
 
});