-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-ZAPTEC.js
182 lines (154 loc) · 5.82 KB
/
MMM-ZAPTEC.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
Module.register("MMM-ZAPTEC", {
// Default module config.
defaults: {
username: "",
password: "",
updateInterval: 60000, // update every minute
lang: "eng", // default language is Swedish
enableChargeHistory: false, // by default, charge history is not displayed
showHistoryEntries: 5 // define the max amount of charge history entries to show
},
// Define start sequence.
start: function() {
this.sendSocketNotification("SET_CONFIG", this.config);
Log.info("Starting module: " + this.name);
this.chargerData = [];
this.chargeHistoryData = [];
this.sendSocketNotification("GET_CHARGER_DATA", this.config);
this.scheduleUpdate();
this.translations = {};
this.getTranslations();
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "small align-left";
var chargerIndex = this.config.charger === "all" ? null : parseInt(this.config.charger) - 1;
// Check if charger data is empty
if (this.chargerData.length === 0) {
wrapper.innerHTML = "<span class='small fa fa-refresh fa-spin fa-fw'></span>";
wrapper.className = "small dimmed";
return wrapper;
}
// Display charger data
for (var i = 0; i < this.chargerData.length; i++) {
if (chargerIndex !== null && chargerIndex !== i) {
continue;
}
var charger = this.chargerData[i];
var chargerWrapper = document.createElement("div");
chargerWrapper.className = "chargerWrapper";
var lang = this.config.lang;
var translationKeys = {
1: "available",
2: "authorizing",
3: "charging",
5: "finishedCharging"
};
var operatingModeKey = translationKeys[charger.OperatingMode] || charger.OperatingMode;
var operatingMode = this.translations[lang][operatingModeKey];
var chargerText = this.translations[lang].charger;
chargerWrapper.innerHTML = chargerText + " " + (i + 1) + " " + operatingMode;
wrapper.appendChild(chargerWrapper);
if (chargerIndex !== null) {
break;
}
}
// Display charge history data
if (this.config.enableChargeHistory && this.chargeHistoryData.length > 0) {
var chargeHistoryWrapper = document.createElement("div");
chargeHistoryWrapper.className = "chargeHistoryWrapper";
var chargeHistoryHeader = document.createElement("h4");
if (this.translations[lang] && this.translations[lang].chargeHistory) {
chargeHistoryHeader.innerHTML = this.translations[lang].chargeHistory;
} else {
chargeHistoryHeader.innerHTML = "Charge History";
}
chargeHistoryWrapper.appendChild(chargeHistoryHeader);
var chargeHistoryList = document.createElement("ul");
// Loop through the charge history data in reverse order
var historyEntries = 0;
for (var i = this.chargeHistoryData.length - 1; i >= 0 && historyEntries < this.config.showHistoryEntries; i--) {
var charge = this.chargeHistoryData[i];
var listItem = document.createElement("li");
// Convert StartDateTime to a JavaScript Date object and adjust for timezone difference
var startDate = new Date(charge.StartDateTime);
startDate.setHours(startDate.getHours() + 2);
// Convert EndDateTime to a JavaScript Date object and adjust for timezone difference
var endDate = new Date(charge.EndDateTime);
endDate.setHours(endDate.getHours() + 2);
// Format the start date and time using 24-hour format
var formattedStartDate = new Intl.DateTimeFormat(undefined, {
month: 'numeric',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false
}).format(startDate);
// Format the end date and time using 24-hour format
var formattedEndDate = new Intl.DateTimeFormat(undefined, {
hour: '2-digit',
minute: '2-digit',
hour12: false
}).format(endDate);
// Display the formatted start and end date and time, and the energy consumed
listItem.innerHTML = `${formattedStartDate} - ${formattedEndDate}: ${charge.Energy} kWh`;
chargeHistoryList.appendChild(listItem);
historyEntries++;
}
chargeHistoryWrapper.appendChild(chargeHistoryList);
wrapper.appendChild(chargeHistoryWrapper);
}
return wrapper;
},
// Schedule module update.
scheduleUpdate: function(delay) {
var self = this;
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
setTimeout(function() {
self.sendSocketNotification("GET_CHARGER_DATA", self.config);
}, nextLoad);
// Request charge history data if enableChargeHistory is set to true
if (this.config.enableChargeHistory) {
setTimeout(function() {
self.sendSocketNotification("GET_CHARGE_HISTORY");
}, nextLoad);
}
},
// Handle notifications from node_helper
socketNotificationReceived: function(notification, payload) {
if (notification === "CHARGER_DATA_RESULT") {
if (payload.error) {
Log.error(`Error getting charger data: ${payload.error}`);
return;
}
Log.info("Received charger data");
this.chargerData = payload.chargerData;
this.updateDom(1000);
} else if (notification === "CHARGE_HISTORY_RESULT") {
if (payload.error) {
Log.error(`Error getting charge history data: ${payload.error}`);
return;
}
Log.info("Received charge history data");
this.chargeHistoryData = payload.chargeHistoryData;
this.updateDom(1000);
}
},
// Get translations
getTranslations: function() {
var self = this;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
self.translations = JSON.parse(this.responseText);
}
};
xhttp.open("GET", "/modules/MMM-ZAPTEC/lang.js", true);
xhttp.send();
},
});
//@Created By Pierre Gode