-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Luftdaten.js
129 lines (121 loc) · 3.35 KB
/
MMM-Luftdaten.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
Module.register("MMM-Luftdaten",{
//default module config
defaults: {
sensors: [],
sensorData: {},
fetchInterval: 5, // update intervall in minutes
timeOnly: false,
withBorder: true,
borderClass: "border",
displayTendency: true,
connected: false,
error: false
},
// Define required scripts.
getStyles: function () {
return ["MMM-Luftdaten.css","font-awesome.css"];
},
// Define required scripts.
getScripts: function () {
return ["moment.js"];
},
// Define required translations.
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json",
};
},
getTemplate: function () {
return "MMM-Luftdaten.njk";
},
// Override start method.
start: function () {
this.defaults = {
...this.defaults,
...this.config
}
const {sensorHost} = this.defaults
if(sensorHost){
const sensorIsHost = sensorHost ? true : false
this.addSensor(sensorHost,this.defaults.fetchInterval, sensorIsHost)
} else {
for(let index of this.defaults.sensors){
//inital fetch of sensor data
this.addSensor(index,this.defaults.fetchInterval)
}
}
},
addSensor: function(sensorId, fetchInterval, sensorIsHost){
this.sendSocketNotification("ADD_SENSOR", {
sensorId, fetchInterval, sensorIsHost
});
},
// Override socket notification handler.
socketNotificationReceived: function (notification, payload) {
if (notification === "SENSOR_DATA_RECEIVED") {
if(payload.sensorData){
this.defaults.error = false
this.defaults.connected = true
this.defaults.lastUpdate = payload.sensorData.lastUpdate
this.defaults.sensorData = this.createSensorTemplateData(payload.sensorData)
}
} else if(notification === "SENSOR_DATA_CONNECTION_ERROR"){
this.defaults.error = true
if(payload.lastUpdate) {
this.defaults.lastUpdate = payload.lastUpdate
}
} else {
Log.log("MMM-Luftdatan received an unknown socket notification: " + notification);
}
this.updateDom(this.config.animationSpeed);
},
createSensorTemplateData: function (data){
const sensors = {}
delete data.lastUpdate;
for(let key in data){
const sensor = {}
sensor.value = parseFloat(data[key])
switch(key){
case "pressure":
sensor.label = this.translate("PRESSURE");
sensor.value = Math.round(parseFloat(sensor.value)) / 100;
break;
case "humidity":
sensor.label = this.translate("HUMIDITY");
break;
default:
break;
}
if(this.defaults.displayTendency && this.defaults.sensorData[key]){
sensor.tendency = this.getTendency(this.defaults.sensorData[key].value, sensor.value)
}
sensors[key] = sensor
}
return sensors
},
getTendency: function(oldValue, newValue){
if(oldValue === newValue) return false;
if(oldValue < newValue) return "up"
return "down"
},
getTemplateData: function () {
const data = {
...this.defaults.sensorData,
lastUpdate: this.formatDate(this.defaults.lastUpdate),
borderClass: this.defaults.withBorder ? this.defaults.borderClass : '',
connected: this.defaults.connected,
error: this.defaults.error,
text: {
CONNECTING: this.translate("CONNECTING"),
CONNECTION_ERROR: this.translate("CONNECTION_ERROR")
}
}
return data;
},
formatDate: function (dateString){
const format = this.defaults.timeOnly ? "LT" : "L LT"
const date = moment.utc(dateString).local()
return date.format(format)
}
});