-
Notifications
You must be signed in to change notification settings - Fork 6
/
node_helper.js
78 lines (68 loc) · 2.14 KB
/
node_helper.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
/* Magic Mirror
* Node Helper: MMM-TracCar
*
* By asimhsidd
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const RQ = require('request');
const WSO = require('ws');
module.exports = NodeHelper.create({
start: function() {
console.log("Starting NodeHelper for " + this.name + "module.");
},
socketNotificationReceived: function(notification, payload) {
var self = this;
var urlstring = payload.url;
switch(notification) {
case "INITIATEDEVICES":
var auth = {'Authorization': "Basic " + Buffer.from(payload.username + ":" + payload.pass).toString('base64')};
RQ.get(
{
headers: auth,
url: urlstring+"/api/devices"
},
function(error, response){
if(response.body.substring(0, 1) === '['){ // I am quite ashamed of this, but this is just to make things work!
self.sendSocketNotification("Devices",response.body);
return;
}
self.sendSocketNotification("Error","Error");
}
);
break;
case "SETUP":
var body = "email="+payload.username+"&password="+payload.pass;
RQ.post(
{
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
url: urlstring+"/api/session",
body: body
},
function(error, response){
if(response.body.substring(0, 1) === '['){ // I am quite ashamed of this, but this is just to make things work!
self.sendSocketNotification("Error","Error");
return;
}
self.socket = new WSO(urlstring.replace("http","ws")+'/api/socket', [],
{
'headers': { 'Cookie': response.headers['set-cookie'][0] }
});
self.socket.on('error', function (e) {
self.sendSocketNotification("Error","Error");
});
self.socket.on('open', function () {
self.sendSocketNotification("Update","Connected");
});
self.socket.on('disconnect', function(){
self.sendSocketNotification("Error","Error");
});
self.socket.on('message', function(message, flags){
self.sendSocketNotification("Position",message);
});
}
);
break;
}
}
});