-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
117 lines (108 loc) · 3.97 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
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
/* Magic Mirror
* Module: MMM-NeviWebTemp
*
* By Julien Stroheker
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var request = require("request");
module.exports = NodeHelper.create({
start: function() {
console.log("MMM-NeviWebTemp helper started ...");
},
//{'dataServer' 'email' 'password' 'gatewayname' 'room'});
getTemp: function(api_url) {
var self = this;
var uri = api_url.dataServer;
var path = uri + "api/login";
var result = { gatewayID: 1, gatewayName: "", thermostats: [] };
dataPayload = {
email: api_url.email,
password: api_url.password,
stayConnected: "0"
};
request({ url: path, method: "POST", form: dataPayload }, function(
error,
response,
body
) {
if (!error && response.statusCode == 200) {
if (JSON.parse(body).session != null) {
var sessionId = JSON.parse(body).session;
var path = uri + "api/gateway";
var dataHeaders = { "Session-Id": sessionId };
request({ url: path, method: "GET", headers: dataHeaders }, function(
error,
response,
body
) {
if (!error && response.statusCode == 200) {
var gatewayId = JSON.parse(body)[0].id;
result.gatewayID = gatewayId;
result.gatewayName = JSON.parse(body)[0].name;
var path = uri + "api/device";
var params = { gatewayId: gatewayId };
request(
{ url: path, method: "GET", headers: dataHeaders, qs: params },
function(error, response, body) {
if (!error && response.statusCode == 200) {
JSON.parse(body).forEach(function(device) {
var thermoinfos = {
name: "",
id: null,
active: null,
setpoint: null,
temperature: null
};
var path =
uri + "api/device/" + device.id + "/data?force=1";
thermoinfos.name = device.name;
thermoinfos.id = device.id;
request(
{ url: path, method: "GET", headers: dataHeaders },
function(error, response, body) {
if (!error && response.statusCode == 200) {
thermoinfos.temperature = JSON.parse(
body
).temperature;
thermoinfos.setpoint = JSON.parse(body).setpoint;
if (JSON.parse(body).heatLevel >= 1) {
thermoinfos.active = 1;
} else {
thermoinfos.active = 0;
}
result.thermostats.push(thermoinfos);
} else {
console.log("Error to fetch infos from devices");
}
}
);
}, this);
setTimeout(function() {
result.thermostats.sort(function(a, b) {
return parseInt(a.id) - parseInt(b.id);
});
self.sendSocketNotification("NEVI_MYTEMP", result);
}, 2000);
} else {
console.log("Error with the devices");
}
}
);
} else {
console.log("Error with the gateway");
}
});
} else {
console.log("Error with session ID");
}
}
});
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === "NEVI_TEMP") {
this.getTemp(payload);
}
}
});