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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const Buffer = require('buffer').Buffer;
const util = require('util');
const aliyunIot = require('../../lib');
const fixtures = require('../fixtures');
 
const device = aliyunIot.device({...fixtures.sdk_device1});
// base_sdk_device2
// const device = aliyunIot.device({ ...fixtures.base_sdk_device2})
 
// const deviceConfig = {
//     "productKey": "a1FVHNuAmvq",
//     "deviceName": "testdevice1",
//     "deviceSecret": "QNNcinhgbY5VgpnQT0DtIUXAlV4jYthO",
//     "region":"cn-shanghai"
//   }
// const device = aliyunIot.device(deviceConfig);
 
// device.subscribe(`${device.productKey}/${device.deviceName}/user/get`);
// device.on('message', (topic, payload) => {
//     console.log(topic, payload);
//     console.log("^:message",topic);
//     device.end();
// });
 
// 测试上报一条设备标签数据
device.on('connect', () => {
 
  // devicebeok() //测试动态注册
  console.log('>>>>>connect');
  // 上报设备属性
  device.onService('wakeup_sync', function (res) {
    // 处理服务参数
    console.log('1^:device.serve',res);
  });
 
 
});
 
// 测试直连设备动态注册功能 ok
function testDirectRegiest(){
  const registerDeviceInfo = {
    productKey:"a15YDgQGhU0",
    productSecret:"AP4HnuqhNqqArIkH",
    deviceName:"device1"
  }
  // 动态注册ok
  aliyunIot.register(registerDeviceInfo,(error,res)=>{
    if(error){console.log("register faild",error);return}
    console.log("register succeed,data:",res);
  })
}
 
 
// 测试直连设备动态注册功能 ok
function testSubDeivceRegiest(){
  const registerDeviceInfo = {
    productKey:"a15YDgQGhU0",
    productSecret:"AP4HnuqhNqqArIkH",
    deviceName:"device1"
  }
  // 动态注册ok
  aliyunIot.register(registerDeviceInfo,(error,res)=>{
    if(error){console.log("register faild",error);return}
    console.log("register succeed,data:",res);
  })
}
 
 
// 测试ok的
function devicebeok() {
  //连接ok
  console.log('>>>>>connect');
  
  // 上报设备属性
  device.postProps({
    LightSwitch: 0
  }, (res) => {
    console.log(res);
  });
  //订阅可以收到服务调用下发
  device.onService('wakeup_async', function (res) {
    // 处理服务参数
    console.log('1^:device.serve');
  });
  //重复订阅不会多次执行回调函数
  device.onService('wakeup_async', function (res) {
    // 处理服务参数
    console.log('1^:device.serve');
  });
 
  // postevent 事件上班 ok
  device.postEvent("lowpower", {
    power: 10,
  }, (res) => {
    console.log(`1postEvent:${res}`);
  })
  //回调函数不重复
  device.postEvent("lowpower", {
    power: 20,
  }, (res) => {
    console.log(`2postEvent:${res}`);
  })
 
  //删除标签ok
  device.deleteTags(['a'], (res) => {
    console.log(`tagid:${res.id}^ delete succeed`);
  });
  //删除多个标签回调函数不冲突
  device.deleteTags(['b'], (res) => {
    console.log(`tagid:${res.id}^ delete succeed`);
  });
  // 添加标签ok
  device.postTags(
    [{
        "attrKey": "Temperature",
        "attrValue": "311"
      },
      {
        "attrKey": "a",
        "attrValue": "avalue"
      },
      {
        "attrKey": "b",
        "attrValue": "bvalue"
      }
    ],
    (res) => {
      console.log(res);
    }
  );
  
  // 获取远程配置功能ok 
  device.getConfig((res) => {
    console.log(`getConfig:${res.data.toString()}`);
  });
 
  // 获取远程接收云端下发 todo
  device.onConfig((res) => {
    console.log(`onConfig:${res.data.toString()}`);
  });
 
 
  //订阅影子设备返回值
  device.onShadow((res) => {
    console.log('获取最新设备影子,%o', res);
  })
  // 设备主动获取影子 ok
  device.getShadow();
  // 设备上报实际值
  device.postShadow({
    a: "avalue11"
  });
 
  // 删除影子属性 ok
  // 参数 属性key,若为空则删除全部
  device.deleteShadow("a");
  device.deleteShadow(["a","b"]);
  device.deleteShadow()
  
}