-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomehub.js
200 lines (166 loc) · 4.92 KB
/
homehub.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
run command:
npm install
node sample.js
*/
import * as Mqtt from "mqtt.so";
import * as utils from "../res/utils.js";
var mqttParam = {
server: 'tcp://fuhai.gw:1883',
options: {
//protocolId: 'MQIsdp',
//protocolVersion: 3,
username: 'your_username',
password: 'your_password',
clientId: 'RaftLink_API'
},
sub_topic: 'rltele/+/v1' //侦听所有设备的消息
};
console.log(`mosquitto : ${Mqtt.version()}`);
console.log(`scriptArgs : ${scriptArgs}`);
var mqttClient = Mqtt.connect(mqttParam.server, mqttParam.options);
console.log('Connecting to broker: ' + mqttParam.server);
/*
发现子设备列表
*/
function doDiscovery(device_id) {
var body = {};
var topic = 'rlcommand/'+device_id+'/v1';
var d = new Date().getTime();
body.msg = 'DISCOVERY';
body.time = d+'';
//console.log("Pub DISCOVERY: " + topic);
mqttClient.publish(topic, JSON.stringify(body), {qos: 1, retain: false})
}
/*
查询单个子设备信息
*/
function doQuery(device_id, aid) {
var body = {};
var topic = 'rlcommand/'+device_id+'/v1';
var d = new Date().getTime();
body.msg = 'GET-STATE';
body.time = d+'';
body.aid = Number(aid);
body.seq = 122;
console.log("Pub GET-STATE: " + JSON.stringify(body));
mqttClient.publish(topic, JSON.stringify(body), {qos: 1, retain: false})
}
/*
设置子设备开关量
*/
function setSwitch(device_id, aid, value){
var body = {};
var topic = 'rlcommand/'+device_id+'/v1';
var d = new Date().getTime();
body.msg = 'SET-STATE';
body.time = d+'';
body.aid = Number(aid);
body.seq = 1;
if (value == true)
{
body.power = 'on';
}
else{
body.power = 'off';
}
//console.log("Pub SET-STATE: " + topic);
mqttClient.publish(topic, JSON.stringify(body), {qos: 1, retain: false});
}
/*
设置LED灯子设备亮度, value范围0-100
*/
function setBrightness(device_id, aid, value){
var body = {};
var topic = 'rlcommand/'+device_id+'/v1';
var d = new Date().getTime();
body.msg = 'SET-STATE';
body.time = d+'';
body.aid = Number(aid);
body.seq = 2;
body.brightness = value;
//console.log("Pub SET-STATE: " + topic);
mqttClient.publish(topic, JSON.stringify(body), {qos: 1, retain: false});
}
/*
设置LED灯子设备色温, value范围0-100
*/
function setColorTemperature(device_id,aid, value){
var body = {};
var topic = 'rlcommand/'+device_id+'/v1';
var d = new Date().getTime();
body.msg = 'SET-STATE';
body.time = d+'';
body.aid = Number(e.currentTarget.id);
body.seq = 3;
body.ct = e.detail.value;
//console.log("Pub SET-STATE: " + topic);
mqttClient.publish(topic, JSON.stringify(body), {qos: 1, retain: false});
}
/*
设置LED灯子设备色调, value范围0-100
*/
function setHue(device_id, aid, value){
var body = {};
var topic = 'rlcommand/'+device_id+'/v1';
var d = new Date().getTime();
body.msg = 'SET-STATE';
body.time = d+'';
body.aid = Number(aid);
body.seq = 4;
body.hue = value;
//console.log("Pub SET-STATE: " + topic);
mqttClient.publish(topic, JSON.stringify(body), {qos: 1, retain: false});
}
/*
设置LED灯子设备饱和度, value范围0-100
*/
function setSaturation(device_id, aid, value){
var body = {};
var topic = 'rlcommand/'+device_id+'/v1';
var d = new Date().getTime();
body.msg = 'SET-STATE';
body.time = d+'';
body.aid = Number(aid);
body.seq = 5;
body.saturation = value;
//console.log("Pub SET-STATE: " + topic);
mqttClient.publish(topic, JSON.stringify(body), {qos: 1, retain: false});
}
/*
获取设备的DHCP客户端信息列表
*/
function requestDhcpClients(device_id) {
var body = {};
var topic = 'rlcommand/'+device_id+'/v1';
var d = new Date().getTime();
body.msg = 'DHCP-REQUEST';
body.time = d+'';
console.log("Pub DHCP-REQUEST: " + JSON.stringify(body));
mqttClient.publish(topic, JSON.stringify(body), {qos: 1, retain: false});
}
mqttClient.on('error', function(error) {
console.error(error);
});
mqttClient.on('message', function(topic, data) {
console.log('MQTT topic: ' + topic);
var str = utils.decodeUtf8(data);
console.log(' data received: ' + str);
//if (data.toString() === 'exit') process.exit();
mqttClient.close();
});
mqttClient.on('pubmid', function(topic, data) {
console.log('MQTT mid: ' + topic);
console.log(' msg received: ' + data);
//if (data.toString() === 'exit') process.exit();
});
mqttClient.on('connect', function() {
console.log('Connected. Client id is: ' + mqttParam.options.clientId);
mqttClient.subscribe(mqttParam.sub_topic);
console.log('Subscribed to topic: ' + mqttParam.sub_topic)
doDiscovery('eg57447');
doQuery('eg57447',2);
requestDhcpClients('eg57447');
//mqttClient.publish(mqttParam.pub_topic, 'Message from Baidu IoT demo');
//console.log('MQTT message published.');
});