chenyc
2025-12-09 65e034683b28d799e73c7d7e5e4769fab5b9bc9c
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
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_device1
    });
    device.on('connect', () => {
      resolve();
    });
  })
},3000)
 
afterAll(() => {
  device.end();
});
 
describe('device test', () => {
  test('device connect linkPlatform should be ok', done => {
    if(device.connected){
      done();
    }
  });
 
  test('productKey should not be empty', done => {
    try {
      const device = aliyunIot.device();
    } catch (e) {
      done();
    }
  });
 
  test('deviceName should not be empty', done => {
    try {
      const device = aliyunIot.device({
        productKey: '1'
      });
    } catch (e) {
      done();
    }
  });
 
  test('deviceSecret should not be empty', done => {
    try {
      const device = aliyunIot.device({
        productKey: '1',
        deviceName: '2'
      });
    } catch (e) {
      done();
    }
  });
 
  test('postProps should not be empty', done => {
    try {
      // 上报设备属性
      device.postProps({
        LightSwitch: 0
      }, (res) => {
        done();
      });
    } catch (e) {}
  });
 
  test('postEvent should not be empty', done => {
    try {
      // 上报设备事件ok
      device.postEvent("lowpower", {
        power: 20,
      }, (res) => {
        console.log(`postEvent:${res}`);
        done();
      })
    } catch (e) {}
  });
 
});