-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-ScreenLogic.js
340 lines (297 loc) · 13.8 KB
/
MMM-ScreenLogic.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
let poolData = {};
let moduleObj;
Module.register('MMM-ScreenLogic',{
defaults: {
showPoolTemp: true,
showSpaTemp: true,
showPH: true,
showOrp: true,
showSaltLevel: true,
showSaturation: true,
showFreezeMode: true,
showControls: false,
controls: [],
colored: true,
coldTemp: 84,
hotTemp: 90,
columns: 3,
contentClass: 'light',
showPHTankLevel: true,
pHTankLevelMax: 6
},
start: function() {
// this isn't a great solution...is there a better one? needed to do stuff with buttons
moduleObj = this;
if (this.config.showControls && (!this.config.controls || this.config.controls.length === 0)) {
Log.warn('Controls are enabled, but no controls are configured. See README for info on setting up controls.');
this.config.showControls = false;
}
this.sendSocketNotification('SCREENLOGIC_CONFIG', this.config);
},
getStyles: function() {
return ['screenlogic.css'];
},
getDom: function() {
if (!poolData.status) {
let wrapper = document.createElement('div');
wrapper.innerHTML = 'Loading ScreenLogic...';
wrapper.className += 'dimmed light small text-center';
return wrapper;
} else {
let outermost = document.createElement('div');
outermost.classList.add('container');
let reconnectDiv = document.createElement('div');
reconnectDiv.classList.add('overlay', 'reconnecting', 'd-none');
let reconnectLabel = document.createElement('div');
reconnectLabel.classList.add('margin-auto', 'bg-blur');
reconnectLabel.innerHTML = 'Reconnecting...';
reconnectDiv.appendChild(reconnectLabel);
let table = document.createElement('table');
table.classList.add('base-content', 'small');
if (this.config.colored) {
table.classList.add('colored');
}
outermost.appendChild(reconnectDiv);
outermost.appendChild(table);
let contents = [];
if (this.config.showPoolTemp) {
let className = '';
if (poolData.status.currentTemp[0] <= this.config.coldTemp) {
className += ' cold-temp';
} else if (poolData.status.currentTemp[0] >= this.config.hotTemp) {
className += ' hot-temp';
}
contents.push({
header: 'Pool temp',
data: poolData.status.currentTemp[0] + '°' + (!isPoolActive(poolData.status) ? ' (last)' : ''),
class: this.config.contentClass + className
});
}
if (this.config.showSpaTemp) {
let className = '';
if (poolData.status.currentTemp[1] <= this.config.coldTemp) {
className = ' cold-temp';
} else if (poolData.status.currentTemp[1] >= this.config.hotTemp) {
className = ' hot-temp';
}
contents.push({
header: 'Spa temp',
data: poolData.status.currentTemp[1] + '°' + (!isSpaActive(poolData.status) ? ' (last)' : ''),
class: this.config.contentClass + className
});
}
if (this.config.showPH) {
let dataStr = poolData.status.pH
if (this.config.showPHTankLevel) {
let percent = Math.round(((poolData.status.pHTank - 1) / this.config.pHTankLevelMax) * 100)
let cls = ''
if (this.config.colored) {
if (percent <= 17) {
cls = 'progress-bar-danger'
} else if (percent <= 33) {
cls = 'progress-bar-warning'
} else {
cls = 'progress-bar-success'
}
}
let progBarDiv = `<div class="progress vertical">
<div class="progress-bar ${cls}" role="progressbar" aria-valuenow="${percent}" aria-valuemin="0" aria-valuemax="100" style="width: ${percent}%;">
</div>
</div>`
dataStr = `${dataStr} ${progBarDiv}`
}
contents.push({
header: 'pH',
data: dataStr,
class: this.config.contentClass
});
}
if (this.config.showOrp) {
contents.push({
header: 'ORP',
data: poolData.status.orp,
class: this.config.contentClass
});
}
if (this.config.showSaltLevel) {
contents.push({
header: 'Salt PPM',
data: poolData.status.saltPPM,
class: this.config.contentClass
});
}
if (this.config.showSaturation) {
contents.push({
header: 'Saturation',
data: poolData.status.saturation,
class: this.config.contentClass
});
}
if (this.config.showControls) {
for (let control in this.config.controls) {
let controlObj = this.config.controls[control];
if (controlObj.type === 'circuit') {
let name = controlObj.name;
for (let circuit in poolData.controllerConfig.bodyArray) {
if (poolData.controllerConfig.bodyArray[circuit].circuitId === controlObj.id) {
if (!name) {
name = poolData.controllerConfig.bodyArray[circuit].name;
}
}
}
let on = false;
for (let circuit in poolData.status.circuitArray) {
if (poolData.status.circuitArray[circuit].id === controlObj.id) {
on = poolData.status.circuitArray[circuit].state !== 0;
}
}
let cls = '';
if (this.config.colored) {
cls = on ? 'control-on' : 'control-off';
}
contents.push({
data: '<button id="sl-control-' + controlObj.id + '" class="control ' + cls + '" onclick="setCircuit(this)" data-circuit="' +
controlObj.id + '" data-state="' + (on ? '1' : '0') + '"><div class="content">' +
name + '</div></button>',
class: this.config.contentClass
});
} else if (controlObj.type === 'heatpoint') {
if (controlObj.body < 0 || controlObj.body > poolData.status.setPoint.length) {
Log.warn('Invalid body specified for heatpoint');
continue;
}
let temperature = poolData.status.setPoint[controlObj.body];
let dataHtml = '<div class="temperature-container">';
dataHtml += '<button id="sl-temp-up-'+controlObj.body+'" class="temperature control-off" onclick="setHeatpoint(this, 1)" data-body="'+controlObj.body+'" data-temperature="'+temperature+'"><div class="content">+</div></button>';
dataHtml += '<div class="temperature-label">'+controlObj.name+': '+temperature+'°</div>';
dataHtml += '<button id="sl-temp-down-'+controlObj.body+'" class="temperature control-off" onclick="setHeatpoint(this, -1)" data-body="'+controlObj.body+'" data-temperature="'+temperature+'"><div class="content">-</div></button>';
contents.push({
data: dataHtml,
class: this.config.contentClass
});
} else if (controlObj.type === 'heatmode') {
if (controlObj.body < 0 || controlObj.body > poolData.status.heatMode.length) {
Log.warn('Invalid body specified for heatmode');
continue;
}
let on = poolData.status.heatMode[controlObj.body] !== 0;
let mode = typeof controlObj.heatMode === 'number' ? controlObj.heatMode : 3;
let cls = '';
if (this.config.colored) {
cls = on ? 'control-on' : 'control-off';
}
contents.push({
data: '<button id="sl-heat-' + controlObj.body + '" class="control ' + cls + '" onclick="setHeatmode(this)" data-body="' +
controlObj.body + '" data-state="' + (on ? '1' : '0') + '" data-mode="' + mode.toString() + '"><div class="content">' +
controlObj.name + '</div></button>',
class: this.config.contentClass
});
} else {
Log.warn('circuit with unknown type, unable to display:');
Log.warn(controlObj);
}
}
}
let headerRow = null;
let contentRow = null;
if (this.config.showFreezeMode && poolData.status.freezeMode !== 0) {
let row = document.createElement('tr');
table.appendChild(row);
row.className = 'cold-temp';
let cell = document.createElement('th');
row.appendChild(cell);
cell.colSpan = this.config.columns;
cell.innerHTML = '<center>FREEZE MODE</center>';
}
let cols = -1;
for (let item in contents) {
cols++;
if (cols % this.config.columns === 0) {
headerRow = document.createElement('tr');
contentRow = document.createElement('tr');
table.appendChild(headerRow);
table.appendChild(contentRow);
}
if (contents[item].header) {
let headerCell = document.createElement('th');
headerCell.innerHTML = contents[item].header;
headerRow.appendChild(headerCell);
}
let contentCell = document.createElement('td');
contentCell.innerHTML = contents[item].data;
contentCell.className = contents[item].class;
contentRow.appendChild(contentCell);
}
return outermost;
}
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'SCREENLOGIC_RESULT') {
poolData = payload;
this.updateDom();
showReconnectOverlay(false);
} else if (notification === 'SCREENLOGIC_CIRCUIT_DONE'
|| notification === 'SCREENLOGIC_HEATSTATE_DONE'
|| notification === 'SCREENLOGIC_HEATPOINT_DONE') {
poolData.status = payload.status;
this.updateDom();
showReconnectOverlay(false);
} else if (notification === 'SCREENLOGIC_RECONNECTING') {
showReconnectOverlay(true);
}
},
});
function showReconnectOverlay(show) {
let element = document.querySelector('.MMM-ScreenLogic .reconnecting');
if (!element || !element.classList) {
return;
}
if (show) {
element.classList.remove('d-none');
} else {
element.classList.add('d-none');
}
}
const SPA_CIRCUIT_ID = 500;
const POOL_CIRCUIT_ID = 505;
function isPoolActive(status) {
for (let i = 0; i < status.circuitArray.length; i++) {
if (status.circuitArray[i].id === POOL_CIRCUIT_ID) {
return status.circuitArray[i].state === 1;
}
}
}
function hasSpa(status) {
for (let i = 0; i < status.circuitArray.length; i++) {
if (status.circuitArray[i].id === SPA_CIRCUIT_ID) {
return true;
}
}
return false;
}
function isSpaActive(status) {
for (let i = 0; i < status.circuitArray.length; i++) {
if (status.circuitArray[i].id === SPA_CIRCUIT_ID) {
return status.circuitArray[i].state === 1;
}
}
}
function setCircuit(e) {
let circuitId = parseInt(e.dataset.circuit);
let on = e.dataset.state !== '0';
moduleObj.sendSocketNotification('SCREENLOGIC_CIRCUIT', {id: circuitId, state: on ? 0 : 1});
e.classList.remove('control-on', 'control-off');
}
function setHeatmode(e) {
let bodyId = parseInt(e.dataset.body);
let on = e.dataset.state !== '0';
let mode = e.dataset.mode;
moduleObj.sendSocketNotification('SCREENLOGIC_HEATSTATE', {body: bodyId, state: on ? 0 : parseInt(mode)});
e.classList.remove('control-on', 'control-off');
}
function setHeatpoint(e, tempChange) {
let bodyId = parseInt(e.dataset.body);
let temp = parseInt(e.dataset.temperature) + tempChange;
moduleObj.sendSocketNotification('SCREENLOGIC_HEATPOINT', {body: bodyId, temperature: temp});
e.classList.remove('control-on', 'control-off');
}