-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestSensorNode.js
More file actions
62 lines (51 loc) · 1.7 KB
/
testSensorNode.js
File metadata and controls
62 lines (51 loc) · 1.7 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
var mqtt = require('mqtt');
var localUrl = 'mqtt://127.0.0.1';
var PiUrl = 'mqtt://192.168.199.170';//树莓派ip
var heweiUrl = 'mqtt://192.168.199.231';//hewei ip
//Sclient :server端为代理时,Dclient :device端为代理时
// --------------------- Server Sub --------------------------------
// var Sclient = mqtt.connect(heweiUrl);
// var x=11,y=22;
// Sclient.on('connect', function () {
// Sclient.subscribe('toDevice');
// });
// Sclient.on('message', function (topic, message) {
// if(topic === 'toDevice'){
// console.log('\n-- Device Sub:');
// eval(message.toString());
// }
// else
// console.log('\n-- Device Sub:'+ message.toString());
// });
// --------------------- Device Sub --------------------------------
var x=11,y=22;
var Dclient = mqtt.connect(localUrl);
var scriptStr='';
Dclient.on('connect', function () {
Dclient.subscribe('rawEnvironment');
Dclient.subscribe('rawRaspberryPiInfo');
Dclient.subscribe('toDevice');
});
Dclient.on('message', function (topic, message) {
if(topic === 'toDevice'){
console.log('\n-- Device Sub:');
// eval(message.toString());
scriptStr = message.toString();
}
else{
var messageStr = message.toString();
console.log('\n## Device Sub:'+ messageStr);
if(scriptStr === ''){
Dclient.publish(topic.replace("raw",""),messageStr);
}
else{
if(scriptSolve(scriptStr,topic,messageStr)!=''){
Dclient.publish(topic.replace("raw",""),scriptSolve(scriptStr,topic,messageStr));
}
}
}
});
function scriptSolve(scriptStr,topic,deviceData){
eval(scriptStr);//预定义好,脚本方面必须是以 scriptFunc 形式进行写
return scriptFunc(topic,deviceData);
}