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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const Buffer = require('buffer').Buffer;
const util = require('util');
const aliyunIot = require('../../lib');
const Device = require('../../lib/device');
const fixtures = require('../fixtures');
 
let device;
beforeAll(()=> {
  return new Promise((resolve, reject)=>{
    device = aliyunIot.device({
      ...fixtures.sdk_device2
    });
    device.on('connect', () => {
      resolve();
    });
  })
},3000)
 
afterAll(() => {
  device.end();
});
 
describe('device tags ability test', () => {
  let tags = [
    {"attrKey": "Temperature", "attrValue": "311"},
    {"attrKey": "a","attrValue": "avalue"},
    {"attrKey": "b","attrValue": "bvalue"},
    {"attrKey": "c","attrValue": "bvalue"}
  ];
 
  test('add tags should be ok', done => {
    try {
      device.postTags(
        tags,
        (res) => {
          console.log(`add tag ok res:${res.id}`);
          done()
        }
      );
    } 
    catch (e) {console.error(e);}
  });
  
  test('delete tags should be ok', done => {
    try {
      device.deleteTags(
        ['a'],
        (res) => {
          console.log(`tagid:${res.id}^ delete succeed`);
          done()
        }
      );
    }
    catch (e) {console.error(e);}
  });
 
});
 
// 远程配置功能
// remote configuration
describe('device remote configuration ability test', () => {
  test('get remote configuration should be ok', done => {
    try {
      // 获取远程配置功能
      device.getConfig((res) => {
        console.log("getConfig:%o,",res.data);
        done()
      });
    } 
    catch (e) {console.error(e);}
  });
});
 
 
// 远程配置功能
// remote configuration
describe('device shadow ability test', () => {
  const random = Math.random().toString(36).substr(2);
  test('shadow get should be ok', done => {
    try {
      device.onShadow((res) => {
        // { method: 'reply',
        //   payload:{ status: 'success',
        //   state: { reported: { a: 'avalue11' } },}
        console.log('@@getShadow:,%o', res);
        done()
      })
      device.getShadow();
    } 
    catch (e) {console.error(e);}
  });
  test('shadow post should be ok', done => {
    try {
      device.onShadow((res) => {
        console.log('@@postShadow,%o', res);
        done()
      })
      device.postShadow({
        a: "random"
      });
    } 
    catch (e) {console.error(e);}
  });
  test('shadow delete should be ok', done => {
    try {
      device.onShadow((res) => {
        console.log('@@deleteShadow,%o', res);
        done()
      })
      device.deleteShadow("a");
    } 
    catch (e) {console.error(e);}
  });
},5000);