forked from ottopaulsen/MMM-MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-MQTT.js
117 lines (98 loc) · 3.92 KB
/
MMM-MQTT.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
Module.register("MMM-MQTT",{
getScripts: function() {
return [
this.file('node_modules/jsonpointer/jsonpointer.js')
];
},
// Default module config
defaults: {
mqttServer: 'localhost',
subscriptions: []
},
start: function() {
console.log(this.name + ' started.');
this.subscriptions = [];
console.log(this.name + ': Setting up ' + this.config.subscriptions.length + ' topics');
for(i = 0; i < this.config.subscriptions.length; i++){
console.log(this.name + ': Adding config ' + this.config.subscriptions[i].label + ' = ' + this.config.subscriptions[i].topic);
this.subscriptions[i] = {
label: this.config.subscriptions[i].label,
topic: this.config.subscriptions[i].topic,
decimals: this.config.subscriptions[i].decimals,
jsonpointer: this.config.subscriptions[i].jsonpointer,
suffix: typeof(this.config.subscriptions[i].suffix) == 'undefined' ? '' : this.config.subscriptions[i].suffix,
value: ''
}
}
this.openMqttConnection();
var self = this;
setInterval(function() {
self.updateDom(1000);
}, 10000);
},
openMqttConnection: function() {
this.sendSocketNotification('MQTT_CONFIG', this.config);
},
socketNotificationReceived: function(notification, payload) {
if(notification === 'MQTT_PAYLOAD'){
if(payload != null) {
for(i = 0; i < this.subscriptions.length; i++){
if(this.subscriptions[i].topic == payload.topic){
var value = payload.value;
// Extract value if JSON Pointer is configured
if(this.subscriptions[i].jsonpointer) {
value = get(JSON.parse(value), this.subscriptions[i].jsonpointer);
}
// Round if decimals is configured
if(isNaN(this.subscriptions[i].decimals) == false) {
if (isNaN(value) == false){
value = Number(value).toFixed(this.subscriptions[i].decimals);
}
}
this.subscriptions[i].value = value;
}
}
this.updateDom();
} else {
console.log(this.name + ': MQTT_PAYLOAD - No payload');
}
}
},
getStyles: function() {
return [
'MQTT.css'
];
},
getDom: function() {
self = this;
var wrapper = document.createElement("table");
wrapper.className = "small";
var first = true;
if (self.subscriptions.length === 0) {
wrapper.innerHTML = (self.loaded) ? self.translate("EMPTY") : self.translate("LOADING");
wrapper.className = "small dimmed";
console.log(self.name + ': No values');
return wrapper;
}
self.subscriptions.forEach(function(sub){
var subWrapper = document.createElement("tr");
// Label
var labelWrapper = document.createElement("td");
labelWrapper.innerHTML = sub.label;
labelWrapper.className = "align-left";
subWrapper.appendChild(labelWrapper);
// Value
var valueWrapper = document.createElement("td");
valueWrapper.innerHTML = sub.value;
valueWrapper.className = "align-right bright medium";
subWrapper.appendChild(valueWrapper);
// Suffix
var suffixWrapper = document.createElement("td");
suffixWrapper.innerHTML = sub.suffix;
suffixWrapper.className = "align-left";
subWrapper.appendChild(suffixWrapper);
wrapper.appendChild(subWrapper);
});
return wrapper;
}
});