-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiSpindel-Forwarder.js
executable file
·396 lines (323 loc) · 10.3 KB
/
iSpindel-Forwarder.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
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
#!/usr/bin/node
const net = require("net");
const request = require('request');
const querystring = require('querystring');
var config = require("./config.json");
/* Possible return values from a forwarder
Success: Data has been successfully transmitted
Error: Either configuration error or a non-recoverable error during transmission. Data will be discarded
Buffer: Error during transmission that can potentially be recovered (i.e. timeout, server cannot be reached)
Data will be buffered and tried to be sent again periodically
*/
const forwardReturnVal = {
Success: 1,
Error: 2,
Buffer: 3
};
const color = {
reset: "\033[0m\033[39m",
black: "\033[0m\033[30m",
red:"\033[0m\033[31m",
green: "\033[0m\033[32m",
yellow: "\033[0m\033[33m",
blue: "\033[0m\033[34m",
magenta: "\033[0m\033[35m",
cyan: "\033[0m\033[36m",
white: "\033[0m\033[37m",
light_black: "\033[1m\033[30m",
light_red: "\033[1m\033[31m",
light_green: "\033[1m\033[32m",
light_yellow: "\033[1m\033[33m",
light_blue: "\033[1m\033[34m",
light_magenta: "\033[1m\033[35m",
light_cyan: "\033[1m\033[36m",
light_white: "\033[1m\033[37m"
};
function log(sys, msg) {
var d = new Date();
console.log(color.reset + "[" + color.yellow + sys + color.reset + "]", d.toLocaleString(), msg, color.reset);
}
function logs(sys, msg, status) {
var statusStr;
if (!status) {
statusStr = color.red + "Fail" + color.reset;
}
else {
statusStr = color.green + "OK" + color.reset;
}
var d = new Date();
console.log(color.reset + "[" + color.yellow + sys + color.reset + "]", d.toLocaleString(), msg, "[" + statusStr + "]");
}
const server = net.createServer(function(socket) {
log("Main", "new connection from " + socket.address().address + ":" + socket.address().port);
var json = "";
socket.on("data", function(data) {
var s = data.toString();
json += s;
});
socket.on('error', function() {
console.log("error");
});
socket.on('close', function() {
try {
var obj = JSON.parse(json.toString());
handleData(obj);
}
catch (err) {
log("Main", color.red + "invalid JSON data received: " + err);
}
console.log("connection closed");
});
});
server.listen(config.port ? config.port : 9502, function() {
log("Main", "iSpindel-Forwarder listening on port " + server.address().port);
});
function forwardCraftbeerpi3(forwarder) {
var rec = forwarder.buffer[0];
var data = forwarder.buffer[0].data;
if (data.type === "eManometer") {
log(forwarder.type, color.light_red + "Currently eManometer data cannot be forwarded to Craftbeerpi3");
forwardCallback(rec, forwarder, forwardReturnVal.Error);
return;
}
if (forwarder.ip == undefined) {
log(forwarder.type, color.red + "config error: ip field missing for craftbeerpi3 forwarder");
forwardCallback(rec, forwarder, forwardReturnVal.Error);
return;
}
var url = "http://" + forwarder.ip + "/api/hydrometer/v1/data";
var data = {
"name": data.name,
"angle": forwarder["send-angle"] === true ? data.angle : data.gravity,
"temperature": data.temperature,
"battery": data.battery
};
log(forwarder.type, "forwarding to craftbeerpi3 at " + url);
request.post(url, { json: data }, function(error, res, body) {
if (error) {
log(forwarder.type, color.red + error)
forwardCallback(rec, forwarder, forwardReturnVal.Error);
return
}
var success = res.statusCode == 200 || res.statusCode == 204;
logs(forwarder.type, "returned: " + res.statusCode, success);
console.log(body);
if (success)
forwardCallback(rec, forwarder, forwardReturnVal.Success);
else
forwardCallback(rec, forwarder, forwardReturnVal.Error);
});
}
function forwardUbidots(forwarder) {
var rec = forwarder.buffer[0];
var data = forwarder.buffer[0].data;
var token;
if (forwarder.token == undefined || forwarder.token == null) {
token = data.token;
}
else {
token = forwarder.token;
}
if (token == undefined || token == null) {
log(forwarder.type, color.red + "Don't have any ubidots token");
forwardCallback(rec, forwarder, forwardReturnVal.Error);
return;
}
var url = "http://things.ubidots.com/api/v1.6/devices/" + data.name + "?token=" + token;
var data = {
"tilt": {
"value": data.angle,
"timestamp": data.timestamp
},
"temperature": {
"value": data.temperature,
"timestamp": data.timestamp
},
"battery": {
"value": data.battery,
"timestamp": data.timestamp
},
"gravity": {
"value": data.gravity,
"timestamp": data.timestamp
},
"interval": {
"value": data.interval,
"timestamp": data.timestamp
},
"rssi": {
"value": data.RSSI,
"timestamp": data.timestamp
}
};
log(forwarder.type, "forwarding to ubidots at " + url + " (" + forwarder.buffer.length + " in queue)");
request.post(url, { json: data }, function(error, res, body) {
if (error) {
console.error(error)
forwardCallback(rec, forwarder, forwardReturnVal.Buffer);
return
}
log(forwarder.type, "returned: " + res.statusCode, res.statusCode == 200);
console.log(body);
forwardCallback(rec, forwarder, res.statusCode == 200 ? forwardReturnVal.Success : forwardReturnVal.Error);
});
}
function forwardGenericTCP(forwarder) {
var rec = forwarder.buffer[0];
var data = forwarder.buffer[0].data;
if (forwarder.ip == undefined) {
log(forwarder.type, color.red + "config error for iSpindel TCP server: missing ip field");
forwardCallback(rec, forwarder, forwardReturnVal.Error);
return;
}
if (forwarder.port == undefined) {
log(forwarder.type, color.red + "config error for generic TCP server: missing port field");
forwardCallback(rec, forwarder, forwardReturnVal.Error);
return;
}
log(forwarder.type, "forwarding to generic TCP Server " + forwarder.ip + ":" + forwarder.port + " (" + forwarder.buffer.length + " in queue)");
var sock = new net.Socket();
sock.setTimeout(5000);
sock.on('error', function(err) {
log(forwarder.type, color.red + "error connecting to " + forwarder.ip + ":" + forwarder.port + ": " + err);
forwardCallback(rec, forwarder, forwardReturnVal.Buffer);
});
sock.connect(forwarder.port, forwarder.ip, function() {
sock.write(JSON.stringify(data), function() {
});
});
sock.on('close', function(hadErr) {
logs(forwarder.type, "socket closed", !hadErr);
forwardCallback(rec, forwarder, hadErr ? forwardReturnVal.Buffer : forwardReturnVal.Success);
})
sock.on('timeout', function() {
forwardCallback(rec, forwarder, forwardReturnVal.Buffer);
log(forwarder.type, color.light_red + "Timeout waiting for reply");
sock.end();
});
sock.on('data', function(data) {
if (data == String.fromCharCode(6)) {
log(forwarder.type, "received ACK");
forwardCallback(rec, forwarder, forwardReturnVal.Success);
}
else if (data == String.fromCharCode(21)) {
log(forwarder.type, color.red + "received NACK, dropping data");
forwardCallback(rec, forwarder, forwardReturnVal.Error);
}
else {
log(forwarder.type, color.red + "invalid reply received from generic TCP Server");
forwardCallback(rec, forwarder, forwardReturnVal.Error);
}
});
}
function forwardThingspeak(forwarder) {
var rec = forwarder.buffer[0];
var data = forwarder.buffer[0].data;
if (forwarder.token == undefined || forwarder.token == null) {
log(forwarder.type, color.red + "Config error: missing token");
forwardCallback(rec, forwarder, forwardReturnVal.Error);
return;
}
var url = "http://api.thingspeak.com/update";
var outdata = {};
/* Helper function to add a field if it exists in the data and if it's defined in the config */
var addField = function(fieldname) {
var num = forwarder['field-' + fieldname];
if (data[fieldname] && num && num > 0 && num <= 8) {
outdata["field" + num] = data[fieldname];
}
};
addField('gravity');
addField('temperature');
addField('angle');
addField('battery');
addField('rssi');
addField('interval');
addField('pressure');
addField('co2');
outdata['api_key'] = forwarder.token;
outdata['created_at'] = new Date(data.timestamp).toISOString();
var str = querystring.stringify(outdata);
request.get(url + "?" + str, function(error, res, body) {
if (error) {
console.error(error)
forwardCallback(rec, forwarder, forwardReturnVal.Buffer);
return
}
log(forwarder.type, "returned: " + res.statusCode, res.statusCode == 200);
console.log(body);
forwardCallback(rec, forwarder, res.statusCode == 200 ? forwardReturnVal.Success : forwardReturnVal.Error);
});
}
function forwardCallback(data, forwarder, retval)
{
if (data.invoked) {
return;
}
data.invoked = true;
if (retval == forwardReturnVal.Success) {
logs(forwarder.type, "successfully forwarded", true);
forwarder.buffer.shift();
}
else if (retval == forwardReturnVal.Error) {
log(forwarder.type, color.red + "error during forwarding, but not buffering");
forwarder.buffer.shift();
}
else if (retval == forwardReturnVal.Buffer) {
log(forwarder.type, color.light_red + "error during forwarding, buffered to be sent later");
}
if (forwarder.buffer.length > 0) {
if (retval == forwardReturnVal.Success)
forwardData(forwarder);
else
setTimeout(function() {
forwardData(forwarder);
}, 3000);
}
}
function forwardData(forwarder) {
log("Main", "forwarding to " + forwarder.type);
forwarder.buffer[0].invoked = false;
switch(forwarder.type) {
case "craftbeerpi3":
forwardCraftbeerpi3(forwarder);
break;
case "ubidots":
forwardUbidots(forwarder);
break;
case "generic-tcp":
forwardGenericTCP(forwarder);
break;
case "thingspeak":
forwardThingspeak(forwarder);
break;
}
}
function handleData(data) {
if (data.ID == undefined) {
log("Main", color.red + "invalid json data (missing id)");
return;
}
if (data.name == undefined) {
log("Main", color.red + "invalid json data (missing name)");
return;
}
log("Main", "got data " + JSON.stringify(data));
if (config.devices[data.name] == undefined || config.devices[data.name].forwarders == undefined) {
log("Main", color.light_red + "no forwarders defined for device " + data.name);
return;
}
if (data.timestamp == undefined)
data.timestamp = Date.now();
for (var i = 0; i < config.devices[data.name].forwarders.length; ++i) {
var forwarder = config.devices[data.name].forwarders[i];
if (forwarder.buffer == undefined)
forwarder.buffer = new Array;
forwarder.buffer.push( {
'invoked' : false,
'data': data
});
if (forwarder.buffer.length == 1)
forwardData(forwarder);
}
}