forked from Medve01/Shelly-HT
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-Shelly-PM.js
104 lines (99 loc) · 3.46 KB
/
MMM-Shelly-PM.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
Module.register("MMM-Shelly-PM",{
// Default module config.
defaults: {
//Just a mock API I used for development
displayUpdated: true,
horizontalView: true,
negativeDisplay: false
},
// Initialize data after startup
ShellyPDData: {
tmp: "--",
apower: "--",
updated: "--",
total: "--"
},
getStyles: function () {
return ["MMM-Shelly-PM.css"]; //, "font-awesome.css"];
},
start: function() {
var self = this;
function setIntervalImmediately(func, interval) {
func();
return setInterval(func, interval);
}
// Schedule update timer.
setIntervalImmediately(function() {
var payload = {
uri: self.config.ShellyApiPath
}
self.sendSocketNotification("GetShelly", payload);
self.updateDom();
}, this.config.refreshIntervalLAN);
setIntervalImmediately(function() {
var payload = {
uri: self.config.cloudServerPath,
deviceId: self.config.deviceId,
authKey: self.config.authKey
}
self.sendSocketNotification("GetShellyCloud", payload);
self.updateDom();
}, this.config.refreshIntervalCloud);
// update initial data
self.sendSocketNotification("GetShelly");
// self.sendSocketNotification("GetShellyCloud");
},
socketNotificationReceived: function (notification, payload) {
if (notification == "ShellyPDData"){
// Log.log(this.name + " received a socket notification: " + notification + " - Temp: " + payload.tmp + " Hum: " + payload.hum + "Updated: " + payload.updated);
this.ShellyPDData.tmp = payload.tmp
this.ShellyPDData.apower = payload.apower
this.ShellyPDData.updated = payload.updated
}
if (notification == "ShellyCloudData") {
this.ShellyPDData.total = payload.total
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
var apower = this.translate("APOWER");
var totalday = this.translate("TOTALDAY");
if (this.config.negativeDisplay && this.ShellyPDData.apower > 0.4) {
var apower_unit = this.translate("APOWER_UNIT", {"apower": -this.ShellyPDData.apower});
var csstype = "generation"
} else {
var apower_unit = this.translate("APOWER_UNIT", {"apower": this.ShellyPDData.apower});
var csstype = "consumption"
}
if (this.config.negativeDisplay && this.ShellyPDData.total > 0.004) {
var total_unit = this.translate("TOTALDAY_UNIT", {"apower": -this.ShellyPDData.total})
var totalcsstype = "generation"
} else {
var total_unit = this.translate("TOTALDAY_UNIT", {"apower": this.ShellyPDData.total})
var totalcsstype = "consumption"
}
var tmp = this.translate("TEMPERATURE", {"tmp": this.ShellyPDData.tmp});
var updated = this.translate("UPDATED", {"upd": this.ShellyPDData.updated})
ihtml = "<div class='container'>"
if (this.config.horizontalView) {
ihtml += " <div class='right " + csstype + "'><sup>" + apower + "</sup> " + apower_unit + "</div>"
ihtml += " <div class='newline right " + totalcsstype + "'><sup>" + totalday + "</sup> " + total_unit + "</div>"
} else {
ihtml += " <div class='newline " + csstype + "'><sup>" + apower + "</sup>" + apower_unit + "</div>"
ihtml += " <div class='newline " + totalcsstype + "'><sup>" + totalday + "</sup> " + total_unit + "</div>"
}
if (this.config.displayUpdated){
ihtml += " <p class='bottom'>" + tmp + "°C " + updated + "</p>"
}
ihtml += "</div><div class='newline'></div>"
wrapper.innerHTML = ihtml
return wrapper
},
getTranslations: function() {
return {
en: 'translations/en.json',
de: 'translations/de.json',
};
}
});