-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-mqtt_command.js
More file actions
73 lines (60 loc) · 1.8 KB
/
MMM-mqtt_command.js
File metadata and controls
73 lines (60 loc) · 1.8 KB
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
'use strict';
/* global Module */
/* Magic Mirror
* Module: MMM-mqtt_command
*
* By Paul Langdon
* MIT Licensed.
*/
Module.register('MMM-mqtt_command', {
defaults: {
mqttServer: 'mqtt://test.mosquitto.org',
loadingText: 'Loading MQTT Data...',
topic: '',
showTitle: false,
title: 'MQTT Data',
interval: 300000,
postText: ''
},
// Define required scripts.
getStyles: function() {
return ["MMM-mqtt_command.css"];
},
start: function() {
Log.info('Starting module: ' + this.name);
this.loaded = false;
this.mqttVal = '';
this.updateMqtt(this);
},
updateMqtt: function(self) {
self.sendSocketNotification('MQTT_SERVER', { mqttServer: self.config.mqttServer, topic: self.config.topic });
setTimeout(self.updateMqtt, self.config.interval, self);
},
getDom: function() {
var wrapper = document.createElement('div');
if (!this.loaded) {
wrapper.innerHTML = this.config.loadingText;
return wrapper;
}
if (this.config.showTitle) {
var titleDiv = document.createElement('div');
titleDiv.innerHTML = this.config.title;
wrapper.appendChild(titleDiv);
}
var mqttDiv = document.createElement('div');
mqttDiv.innerHTML = this.mqttVal.toString().concat(this.config.postText);
wrapper.appendChild(mqttDiv);
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'MQTT_DATA' && payload.topic === this.config.topic) {
this.mqttVal = payload.data.toString();
this.loaded = true;
this.sendNotification(this.mqttVal, payload);
this.updateDom();
}
if (notification === 'ERROR') {
this.sendNotification('SHOW_ALERT', payload);
}
}
});