-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_helper.js
More file actions
73 lines (69 loc) · 3.01 KB
/
Copy pathnode_helper.js
File metadata and controls
73 lines (69 loc) · 3.01 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
62
63
64
65
66
67
68
69
70
71
72
73
var NodeHelper = require("node_helper");
const request = require('request');
module.exports = NodeHelper.create({
start: function() {
},
// Frontend module pings the node helper to fetch data from Shelly HT
socketNotificationReceived: function (notification, payload) {
if (notification == "GetShelly_temp"){
//Parameters: notification can be anything (not used), payload must be the URL of the Shelly HT status api
self = this;
request(payload, {json: true }, (err, res, body) => {
if (err) { return console.log(err); }
currentdate = new Date();
var options = { month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' };
var printed_date = new Intl.DateTimeFormat('de', options).format(currentdate);
payload= {
tmp: body.tC,
updated: printed_date
}
console.log("Sending Shelly data to FE module", payload);
//Only sending back temperature in Celsius and Humidity %.
//Also, since Shelly HT is mostly asleep (see Shelly documentation),
//Adding a "last updated" timestamp as well for displaying
//TODO: add battery percentage maybe as well
self.sendSocketNotification('ShellyHTData_temp',payload)
});
}
if (notification == "GetShelly_hum"){
//Parameters: notification can be anything (not used), payload must be the URL of the Shelly HT status api
self = this;
request(payload, {json: true }, (err, res, body) => {
if (err) { return console.log(err); }
currentdate = new Date();
var options = { month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' };
var printed_date = new Intl.DateTimeFormat('de', options).format(currentdate);
payload= {
hum: body.rh,
updated: printed_date
}
console.log("Sending Shelly data to FE module", payload);
//Only sending back temperature in Celsius and Humidity %.
//Also, since Shelly HT is mostly asleep (see Shelly documentation),
//Adding a "last updated" timestamp as well for displaying
//TODO: add battery percentage maybe as well
self.sendSocketNotification('ShellyHTData_hum',payload)
});
}
if (notification == "GetShelly_pow"){
//Parameters: notification can be anything (not used), payload must be the URL of the Shelly HT status api
self = this;
request(payload, {json: true }, (err, res, body) => {
if (err) { return console.log(err); }
currentdate = new Date();
var options = { month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' };
var printed_date = new Intl.DateTimeFormat('de', options).format(currentdate);
payload= {
bat: body.battery.percent + "%(" + body.battery.V + " V)",
updated: printed_date
}
console.log("Sending Shelly data to FE module", payload);
//Only sending back temperature in Celsius and Humidity %.
//Also, since Shelly HT is mostly asleep (see Shelly documentation),
//Adding a "last updated" timestamp as well for displaying
//TODO: add battery percentage maybe as well
self.sendSocketNotification('ShellyHTData_pow',payload)
});
}
}
});