-
Notifications
You must be signed in to change notification settings - Fork 0
/
btsTestCloud.js
170 lines (150 loc) · 6.6 KB
/
btsTestCloud.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
//
// BTS code to test connectivity to SK cloud
// ddp connect to cloud
//
var ejson = require('ejson');
var DDPClient = require('ddp');
var async = require('async');
console.log('Testing....')
var btsSensorList=[];
// ddp call
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; //need this to prevent "unable to verify leaf signature"
// var ddpclient = new DDPClient({
// host : "ezgrowr.com",
// port : 3010,
// ssl : true,
// autoReconnect : true,
// autoReconnectTimer : 500,
// maintainCollections : true,
// ddpVersion : '1', // ['1', 'pre2', 'pre1'] available
// useSockJs: true
// });
var ddpclient = new DDPClient({
// host : "ezgrowr.com",
// port : 3010, //443
autoReconnect : true,
autoReconnectTimer : 500,
maintainCollections : true,
ddpVersion : '1', // ['1', 'pre2', 'pre1'] available
url: 'wss://ezgrowr.com/websocket'
});
// bts and sensor serial number
var btsID = '4414BBBK0072';
var sensorID ='a0143d0c6007';
//Connect to the Meteor Server
ddpclient.connect(function(error) {
if (error) {
console.log('DDP connection error!',error);
return;
}
console.log('connected!');
//Subscribe to a Meteor Collection
ddpclient.subscribe( // name of Meteor Publish function to subscribe to
'BaseStationSensors',
[btsID], // any parameters used by the Publish function
function (result) { // callback when the subscription is complete
}
);
// //plant data to be pushed to Cloud;
// var plantData = {sn:sensorID, sT:25, aT:26, sEC:0.01, rssi:88, txP:0, bL:88, sL:1.0,sM:0.0,mD:Date.now()};
// //call cloud methods to push data
// ddpclient.call(
// 'resetBTSConfig', // name of Meteor Method being called
// [btsID], // parameters to send to Meteor Method
// function (err, result) { // callback which returns the method call results
// console.log('called resetBTSConfig: ', result, ' err: ', JSON.stringify(err) );
// },
// function () { // callback which fires when server has finished
// console.log('done resetBTSConfig'); // sending any updated documents as a result of
// }
// );
// ddpclient.call(
// 'resetSensorLED', // name of Meteor Method being called
// [sensorID], // parameters to send to Meteor Method
// function (err, result) { // callback which returns the method call results
// console.log('called resetSensorLED: ', result, ' err: ', JSON.stringify(err) );
// },
// function () { // callback which fires when server has finished
// console.log('done resetSensorLED'); // sending any updated documents as a result of
// }
// );
// ddpclient.call(
// 'addPlantData', // name of Meteor Method being called
// [plantData], // parameters to send to Meteor Method
// function (err, result) { // callback which returns the method call results
// console.log('called addPlantData: ', result, ' err: ', JSON.stringify(err) );
// },
// function () { // callback which fires when server has finished
// console.log('done addPlantdata'); // sending any updated documents as a result of
// }
// );
});
ddpclient.on('message', function (msg) {
var bts = ddpclient.collections["base-stations"];
var sensor = ddpclient.collections.sensors;
msg = ejson.parse(msg);
console.log('msg:',msg);
if(msg["msg"] === "changed"){
console.log("changed");
if(msg["collection"] === "base-stations"){
// console.log('fields:',msg["fields"]);
// console.log('btsSerialNo:',msg["fields"]["serialNo"]);
console.log('maxLedOn =', bts[msg["id"]]["maxLedOnTime"]);
console.log('scanTime = ', bts[msg["id"]]["scanTime"]);
console.log('upgradeFW = ',bts[msg["id"]]["upgradeFW"]);
// rebootBB = collection[msg["id"]]["rebootBB"];
// restartApp = collection[msg["id"]]["restartApp"];
// reverseSSH = collection[msg["id"]]["reverseSSH"];
// closeTunnel = collection[msg["id"]]["closeTunnel"];
// blinkInterval = collection[msg["id"]]["blinkInterval"];
// console.log('upgradeFW:',msg["fields"]["upgradeFW"]);
// console.log('scanTime:',msg["fields"]["scanTime"]);
// console.log('rebootBB:',msg["fields"]["rebootBB"]);
// console.log('restartApp:',msg["fields"]["restartApp"]);
// console.log('maxLedOnTime:',msg["fields"]["maxLedOnTime"]);
// console.log('blinkInterval:',msg["fields"]["blinkInterval"]);
// console.log('closeTunnel:',msg["fields"]["closeTunnel"]);
// console.log('reverseSSH:',msg["fields"]["reverseSSH"]);
}
if(msg["collection"] === "sensors"){
console.log('ledStatus = ', sensor[msg["id"]]["ledStatus"]);
// console.log('fields:',msg["fields"]);
// console.log('sensorSerialNo:',msg["fields"]["serialNo"]);
// console.log('ledStatus:',msg["fields"]["ledStatus"]);
}
}
if (msg["msg"] === "added") {
console.log("added");
if(msg["collection"] === "base-stations"){
// btsSensorList = Object.keys(btsSensorListObjects);
console.log('maxLedOn =', bts[msg["id"]]["maxLedOnTime"]);
console.log('scanTime = ', bts[msg["id"]]["scanTime"]);
console.log('upgradeFW = ',bts[msg["id"]]["upgradeFW"]);
// console.log('fields:',msg["fields"]);
// console.log('btsSerialNo:',msg["fields"]["serialNo"]);
// console.log('upgradeFW:',msg["fields"]["upgradeFW"]);
// console.log('scanTime:',msg["fields"]["scanTime"]);
// console.log('rebootBB:',msg["fields"]["rebootBB"]);
// console.log('restartApp:',msg["fields"]["restartApp"]);
// console.log('maxLedOnTime:',msg["fields"]["maxLedOnTime"]);
// console.log('blinkInterval:',msg["fields"]["blinkInterval"]);
// console.log('closeTunnel:',msg["fields"]["closeTunnel"]);
// console.log('reverseSSH:',msg["fields"]["reverseSSH"]);
}
if(msg["collection"] === "sensors"){
btsSensorList[msg["id"]]=sensor[msg["id"]]["serialNo"];
console.log("btsSensorList:", btsSensorList);
console.log('ledStatus = ', sensor[msg["id"]]["ledStatus"]);
// console.log('sensorSerialNo:',msg["fields"]["serialNo"]);
// console.log('ledStatus:',msg["fields"]["ledStatus"]); //get only led data
}
}
if (msg["msg"] === "removed"){
console.log("removed");
if(msg["collection"] === "sensors"){
delete btsSensorList[msg["id"]];
btsSensorList.splice( btsSensorList.indexOf(msg["id"]), 1 );
console.log("btsSensorList:", btsSensorList);
}
}
});