forked from javiergayala/MMM-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMMM-IndoorTemp.js
77 lines (62 loc) · 1.87 KB
/
MMM-IndoorTemp.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
'use strict';
/* global Module */
/* Magic Mirror
* Module: MMM-IndoorTemp
*
* By Sebastian Hodapp https://www.sebastian-hodapp.de,
*
* based on Javier Ayala's module MMM-mqtt http://www.javierayala.com/
* MIT Licensed.
*/
Module.register('MMM-IndoorTemp', {
defaults: {
mqttServer: 'mqtt://test.mosquitto.org',
topic: '',
interval: 150000,
debug: false,
showAlerts: true,
showTitle: false,
title: 'Indoor Temperature',
loadingText: 'Loading MQTT Data...',
postText: ''
},
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.config.debug) {
if (!this.loaded) {
wrapper.innerHTML = this.config.loadingText;
return wrapper;
}
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('INDOOR_TEMPERATURE', this.mqttVal);
if (this.config.debug) {
this.updateDom();
}
}
if (notification === 'ERROR' && this.config.showAlerts){
this.sendNotification('SHOW_ALERT', payload);
}
}
});