-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.js
More file actions
401 lines (369 loc) · 11.6 KB
/
device.js
File metadata and controls
401 lines (369 loc) · 11.6 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
Author: LawrenceDon
Mail: lawrencedon@163.com
QQ: 20515042
Website: www.espruino.cn
Github: github.com/LawrenceDon/nodejs-device-hekr
MIT License
Copyright (c) 2017 LawrenceDon
*/
var debugOutputFlag = true; //设置是否输出debug信息,本脚本运行之后,在CMD窗口输入eval:logOn()或者eval:logOff()来打开或者关闭debug信息输出
function getDevTid()
{
return "NODEJS_" + "SN20170725001"; //使用前请修改,devTid代表设备ID,可以自己定义设备ID规则,devTid的最大长度是32
}
var device = { //本脚本运行之后,在CMD窗口输入eval:device,会显示该对象的内容,其中的devTid,prodKey,token,ctrlKey,bindKey我们需要记录下来,其中的devTid和bindKey将用于生成绑定设备时需要的二维码
devTid:"",
prodKey:"xxxxxx", //需要填写自己产品的prodKey。更改prodKey之前应该先运行本脚本,在CMD窗口输入eval:clearDevToken(localStorage,devTokenKey)清除之前的token,如果该设备已经被绑定,那需要先在APP中将该设备删除掉
token:"",
ctrlKey:"",
bindKey:"",
mainTCPLink:null,
mainTCPLinkReady:false,
getProdInfoTCPLink:null,
heartbeatIntervalID:0
};
var devInfo = {
devTid:"",
mid:"",
workMode:0,
tokenType:2,
serviceHost:"",
servicePort:0,
binVer:"0.1.0",
binType:"NA",
sdkver:"2.0.0",
SDKVer:"2.0.0",
mac:"",
MAC:"",
ssid:"",
SSID:"",
lanIp:""
};
var infoServer = {
host:'info-dev.hekr.me',
port:91
};
var server = {
host:"",
port:0
};
var net = require('net');
var qr = require('qr-image');
var LocalStorage = require('node-localstorage').LocalStorage;
var localStorage = new LocalStorage('./devParams');
var devTokenKey = "devToken";
var msgId = -1;
var respStrTemp = "";
var heartbeatRespFlag = 0;
function getMsgId()
{
if(msgId == 65535)
{
msgId = 0;
}
else
{
msgId++;
}
return msgId;
}
function isRespComplete(data)
{
var index0 = data.indexOf("\n");
if(index0 != -1)
{
return 1;
}
return 0;
}
function getDevToken(localStorage,key)
{
var lsDevToken = localStorage.getItem(key);
if(lsDevToken == null)
{
localStorage.setItem(key, '');
lsDevToken = '';
}
device.token = lsDevToken;
return lsDevToken;
}
function setDevToken(localStorage,key,data)
{
localStorage.setItem(key, data);
}
function clearDevToken(localStorage,key)
{
localStorage.setItem(key, '');
}
function getProdInfo()
{
var getInfo_tpl='{"msgId" : "{msgId}","action" : "getProdInfo","params" : {"devTid" : "{devTid}","prodKey" : "{prodKey}"}}\n';
var getInfo_str=getInfo_tpl.replace('{msgId}',getMsgId()).replace('{devTid}',device.devTid).replace('{prodKey}',device.prodKey);
device.getProdInfoTCPLink = net.connect(infoServer, function(){
debugOutput("getProdInfo TCP connected!");
debugOutput('getProdInfo send : ' + getInfo_str);
device.getProdInfoTCPLink.write(getInfo_str);
});
getProdInfoBind(device.getProdInfoTCPLink);
}
function getProdInfoBind(client)
{
client.on('data', function(data){
respStrTemp += data;
if(isRespComplete(respStrTemp) == 1)
{
debugOutput('getProdInfo recv : ' + respStrTemp);
var jsonData = JSON.parse(respStrTemp);
if(jsonData.code == 200)
{
getProdInfoCallback(jsonData.params);
}
else
{
debugOutput("getProdInfo recv : " + jsonData.action + " with error {code:" + jsonData.code + ", desc:" + jsonData.desc + "}");
}
respStrTemp = "";
}
});
client.on('error', function(e){
debugOutput("getProdInfo connection error!");
debugOutput(e);
debugOutput("reconnect for getProdInfo");
setTimeout(getProdInfo,5000);
});
client.on('close', function(){
debugOutput('getProdInfo connection closed');
});
}
function getProdInfoCallback(obj)
{
server.host = obj.serviceHost;
server.port = obj.servicePort;
devInfo.mid = obj.mid;
devInfo.workMode = obj.workMode;
devInfo.tokenType = obj.tokenType;
devInfo.serviceHost = obj.serviceHost;
devInfo.servicePort = obj.servicePort;
login();
}
function login()
{
var login_tpl='{"msgId" : "{msgId}","action" : "devLogin","params" : {"devTid" : "{devTid}","prodKey" : "{prodKey}","token" : "{token}"}}\n';
var login_str=login_tpl.replace('{msgId}',getMsgId()).replace('{devTid}',device.devTid).replace('{prodKey}',device.prodKey).replace('{token}',getDevToken(localStorage,devTokenKey));
device.mainTCPLink = net.connect(server, function(){
debugOutput("main TCP connected!");
debugOutput('CONNECTED TO : ' + server.host + ':' + server.port);
debugOutput('main send devLogin : ' + login_str);
device.mainTCPLink.write(login_str);
});
loginBind(device.mainTCPLink);
}
function loginBind(client)
{
client.on('data', function(data){
respStrTemp += data;
if(isRespComplete(respStrTemp) == 1)
{
var jsonData = JSON.parse(respStrTemp);
if(jsonData != undefined)
{
parseData(jsonData);
}
respStrTemp = "";
}
});
client.on('error', function(e){
debugOutput("main connection error!");
debugOutput(e);
});
client.on('close', function(){
debugOutput('main connection closed');
clearInterval(device.heartbeatIntervalID);
device.mainTCPLinkReady = false;
heartbeatRespFlag = 0;
debugOutput("reconnect for login");
setTimeout(login,5000);
});
}
function reportDevInfo(devinfo,tcplink)
{
var reportDevInfo_tpl='{"msgId" : "{msgId}","action" : "reportDevInfo","params" : {devInfo}}\n';
var reportDevInfo_str=reportDevInfo_tpl.replace('{msgId}',getMsgId()).replace('{devInfo}',JSON.stringify(devinfo));
debugOutput('main send reportDevInfo : ' + reportDevInfo_str);
tcplink.write(reportDevInfo_str);
}
function parseData(jsonData)
{
switch(jsonData.action){
case "heartbeatResp":
if(jsonData.code == 200)
{
heartbeatRespFlag = 0;
debugOutput("main receive : " + jsonData.action + " with msgId " + jsonData.msgId);
}
else
{
debugOutput("main receive : " + jsonData.action + " with error {code:" + jsonData.code + ", desc:" + jsonData.desc + "}");
}
break;
case "devLoginResp":
debugOutput("main receive : " + JSON.stringify(jsonData));
if(jsonData.code == 200)
{
debugOutput("main receive : " + jsonData.action);
if(jsonData.params.token != getDevToken(localStorage,devTokenKey))
{
setDevToken(localStorage,devTokenKey,jsonData.params.token);
device.token = jsonData.params.token;
var qrDevBind = qr.image('http://www.hekr.me?action=bind&devTid=' + jsonData.params.devTid + '&bindKey=' + jsonData.params.bindKey, {type: 'png'});
qrDevBind.pipe(require('fs').createWriteStream('qrDevBind.png'));
}
device.ctrlKey = jsonData.params.ctrlKey;
device.bindKey = jsonData.params.bindKey;
device.mainTCPLinkReady = true;
reportDevInfo(devInfo,device.mainTCPLink);
device.heartbeatIntervalID = setInterval(function(){
if(heartbeatRespFlag == 0)
{
heartbeatRespFlag = 1;
var msgId = getMsgId();
debugOutput("main send : heartbeat with msgId " + msgId);
device.mainTCPLink.write('{"msgId" : ' + msgId + ',"action" : "heartbeat"}\n');
}
else
{
device.mainTCPLinkReady = false;
debugOutput("no heartbeat response");
}
}, 25000);
}
else
{
debugOutput("main receive : " + jsonData.action + " with error {code:" + jsonData.code + ", desc:" + jsonData.desc + "}");
}
break;
case "devSendResp":
if(jsonData.code == 200)
{
debugOutput("main receive : " + jsonData.action);
}
else
{
debugOutput("main receive : " + jsonData.action + " with error {code:" + jsonData.code + ", desc:" + jsonData.desc + "}");
}
break;
case "reportDevInfoResp":
debugOutput("main receive : " + JSON.stringify(jsonData));
if(jsonData.code == 200)
{
debugOutput("main receive : " + jsonData.action);
}
else
{
debugOutput("main receive : " + jsonData.action + " with error {code:" + jsonData.code + ", desc:" + jsonData.desc + "}");
}
break;
case "errorResp":
debugOutput("main receive : " + jsonData.action + " with error {code:" + jsonData.code + ", desc:" + jsonData.desc + "}");
break;
case "appSend":
debugOutput("main receive : " + jsonData.action);
var jsonAppSendResp = jsonData;
jsonAppSendResp.action = "appSendResp";
jsonAppSendResp.code = 200;
jsonAppSendResp.desc = "success";
sendData(JSON.stringify(jsonAppSendResp) + '\n',device.mainTCPLink);
parseAppSend(jsonData);
break;
default:
debugOutput("main receive : " + JSON.stringify(jsonData));
}
}
function sendData(data,tcplink)
{
if(device.mainTCPLinkReady == true)
{
debugOutput("sendData : " + data);
tcplink.write(data);
}
else
{
debugOutput("TCP link is not ready!");
}
}
function sendParamsData(data,tcplink)
{
var send_tpl='{"msgId" : "{msgId}","action" : "devSend","params" : {"devTid" : "{devTid}","appTid" : [],"data" : {{data}}}}\n';
var send_str=send_tpl.replace('{msgId}',getMsgId()).replace('{devTid}',device.devTid).replace('{data}',data);
if(device.mainTCPLinkReady == true)
{
debugOutput("sendParamsData : " + send_str);
tcplink.write(send_str);
}
else
{
debugOutput("TCP link is not ready!");
}
}
function getCurrentDateTime()
{
var date=new Date();
return date.toLocaleString();
}
function debugOutput(content)
{
if(debugOutputFlag == true)
console.log(getCurrentDateTime() + " : " + content);
}
function logOn()
{
debugOutputFlag = true;
}
function logOff()
{
debugOutputFlag = false;
}
function startDevice()
{
device.devTid = getDevTid();
devInfo.devTid = getDevTid();
getProdInfo();
deviceFunction();
}
//deviceFunction,parseAppSend,reportStatus这3个函数的内容需要开发者自己完成
function deviceFunction() //具体的设备功能在此函数中完成
{
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function(){
var data = process.stdin.read();
if(data !== null){
data=data.substring(0,data.length-2); //去除\r\n
if(data[0]=='e' && data[1]=='v' && data[2]=='a' && data[3]=='l' && data[4]==':')
{
console.log(eval(data.substr(5))); //示例:直接在cmd窗口输入或ctrl+v eval:1+1
}
else if(data[0]=='d' && data[1]=='a' && data[2]=='t' && data[3]=='a' && data[4]==':')
{
sendParamsData(data.substr(5),device.mainTCPLink); //示例:直接在cmd窗口输入或ctrl+v data:"cmdId":1,"power":1
}
else
{
if(data.length >0 && device.mainTCPLinkReady == true)
{
data=data + '\n';
sendData(data,device.mainTCPLink); //示例:直接在cmd窗口输入或ctrl+v {"msgId" : 1,"action" : "heartbeat"}
}
}
}
});
}
function parseAppSend(jsonData) //根据产品通信协议,解析从云端下发的协议数据
{
//jsonData是一个对象,可以从中获得协议数据,示例: jsonData.params.data.cmdId, jsonData.params.data.power
}
function reportStatus(tcplink) //根据产品通信协议,上报设备当前状态
{
//可以使用sendParamsData发送协议数据给云端,示例: sendParamsData("\"cmdId\":1,\"power\":0",tcplink);
}
startDevice();