-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-CaravanPiGasWeight.js
244 lines (205 loc) · 6.6 KB
/
MMM-CaravanPiGasWeight.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
/* ---------------------------------------------------------------------
* Magic Mirror
* Module: MMM-CaravanPiGasWeight
*
* CaravanPi Module
* see https://github.com/spitzlbergerj/CaravanPi for more Information
* about the DIY project
*
* By Josef Spitzlberger http://spitzlberger.de
* MIT Licensed.
*/
Module.register("MMM-CaravanPiGasWeight",{
defaults:{
valueDir: "/home/pi/CaravanPi/values",
updateInterval: 60000, // milliseconds
weightUnit: " Gramm",
weightPrecision: 2,
levelUnit: " %",
levelPrecision: 2,
showDate: true,
sensors: [
{
name: "Gasflasche 1",
file: "gasScale1",
},
{
name: "Gasflasche 2",
file: "gasScale2",
},
],
localeStr: 'de-DE',
style: "lines",
},
valueList:[],
start: function (){
Log.log('Starting module: ' + this.name);
this.valueList = new Array();
var i = 0;
// Log.log('sensors: ', this.config.sensors.length, this.config.sensors);
while(i<this.config.sensors.length){
this.valueList[i] = new Object();
this.valueList[i]["name"] = this.config.sensors[i]["name"];
this.valueList[i]["file"] = this.config.sensors[i]["file"];
this.valueList[i]["datetime"] = this.translate('LOADING');
this.valueList[i]["weight"] = "0";
this.valueList[i]["level"] = "0";
i+=1;
}
Log.log('valueList: ', this.valueList);
this.sendSocketNotification(
'CONFIG',
{
config: this.config,
valueList: this.valueList,
});
},
// Get translations
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json"
}
},
// Get the Module CSS
getStyles: function() {
return ["MMM-CaravanPiGasWeight.css"];
},
getDom: function(){
var table = document.createElement("table");
table.border = 0;
if (this.config.style == "boxes" || this.config.style == "boxlines" ) {
var boxRow = document.createElement("tr");
boxRow.className = 'sensorBoxRow';
boxRow.vAlign = 'top';
}
var i = 0;
while (i<this.config.sensors.length) {
var weightStr = this.prepareAttribute("WEIGHT", this.valueList[i]["weight"], this.config.weightPrecision, this.config.weightUnit);
var levelStr = this.prepareAttribute("LEVEL", this.valueList[i]["level"], this.config.levelPrecision, this.config.levelUnit);
var rowSensor = document.createElement("td");
rowSensor.className = 'sensorName';
rowSensor.style.borderBottom = '1px dotted #ffffff';
rowSensor.appendChild(document.createTextNode(this.valueList[i]["name"]));
var rowWeight = document.createElement("td");
rowWeight.className = 'sensorWeight';
rowWeight.appendChild(document.createTextNode(weightStr));
var rowLevel = document.createElement("td");
rowLevel.className = 'sensorLevel';
rowLevel.appendChild(document.createTextNode(levelStr));
var rowDate = document.createElement("td");
rowDate.className = 'sensorDate';
rowDate.appendChild(document.createTextNode(this.valueList[i]["datetime"]));
if (this.config.style == "lines") {
var row = document.createElement("tr");
row.className = 'sensorContainer';
row.vAlign = 'top';
rowSensor.width = '120px';
rowWeight.width = '60px';
rowLevel.width = '60px';
rowDate.width = '60px';
// Building of the table row
row.appendChild(rowSensor);
row.appendChild(rowWeight);
row.appendChild(rowLevel);
if(this.config.showDate === true) {
row.appendChild(rowDate);
}
table.appendChild(row);
}
else if (this.config.style == "boxes" || this.config.style == "boxlines" ) {
var boxRowElement = document.createElement("td");
boxRowElement.className = 'sensorBoxRowElement';
var tableInner = document.createElement("table");
tableInner.style.border= '1px solid #ffffff';
var row1 = document.createElement("tr");
row1.className = 'sensorContainer';
row1.align = 'center';
row1.vAlign = 'top';
if (this.config.style == "boxlines" )
{
rowSensor.colSpan = '2';
}
row1.appendChild(rowSensor);
var row2 = document.createElement("tr");
row2.className = 'sensorContainer';
row2.align = 'center';
row2.vAlign = 'top';
if (this.config.style == "boxes" )
{
row2.appendChild(rowWeight);
var row3 = document.createElement("tr");
row3.className = 'sensorContainer';
row3.align = 'center';
row3.vAlign = 'top';
row3.appendChild(rowLevel);
// Building of the table rows
tableInner.appendChild(row1);
tableInner.appendChild(row2);
tableInner.appendChild(row3);
}
else
{
rowWeight.width = '60px';
rowLevel.width = '60px';
rowDate.width = '60px';
// Building of the table row
row2.appendChild(rowWeight);
row2.appendChild(rowLevel);
// Building the table
tableInner.appendChild(row1);
tableInner.appendChild(row2);
}
if(this.config.showDate === true)
{
var row4 = document.createElement("tr");
row4.className = 'sensorContainer';
row4.align = 'center';
row4.vAlign = 'top';
if (this.config.style == "boxlines" )
{
rowDate.colSpan = '3';
}
row4.appendChild(rowDate);
tableInner.appendChild(row4);
}
boxRowElement.appendChild(tableInner)
boxRow.appendChild(boxRowElement);
}
i+=1;
}
if (this.config.style == "boxes" || this.config.style == "boxlines" ) {
table.appendChild(boxRow);
}
var wrapper = document.createElement("div")
wrapper.className = "MMM-CaravanPiGasWeight";
wrapper.innerHTML = table.outerHTML;
return wrapper
},
socketNotificationReceived: function(notification, payload){
Log.log('MMM-Systemvalues: socketNotificationReceived ' + notification + payload);
switch(notification) {
case "VALUES":
this.valueList = payload;
Log.log('valueList in socketNotificationReceived: ', this.valueList);
this.updateDom();
break
}
},
/**
* Prepare the output of the given attribute. Reads the attributeName from the
* settingsArray and do further processing on it, i.e. to display the value with the
* unit (temperature, valve state) or anything else.
* Can be used in the future to prepare any other attributes for output.
*/
prepareAttribute: function(attributeName, strValue, precision, unit){
var preparedAttributeValue = "";
switch(attributeName){
case "WEIGHT":
case "LEVEL":
preparedAttributeValue = Number(parseFloat(strValue)).toLocaleString(this.config.localeStr, {minimumFractionDigits: precision, maximumFractionDigits: precision}) + unit;
break;
}
return preparedAttributeValue;
},
})