-
Notifications
You must be signed in to change notification settings - Fork 6
/
MMM-RemoteTemperature.js
116 lines (95 loc) · 2.95 KB
/
MMM-RemoteTemperature.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
/* global Module, moment */
/* Magic Mirror Module: MMM-RemoteTemperature (https://github.com/balassy/MMM-RemoteTemperature)
* By György Balássy (https://www.linkedin.com/in/balassy)
* MIT Licensed.
*/
Module.register('MMM-RemoteTemperature', {
defaults: {
sensorId: null,
icon: 'home',
showMore: true
},
requiresVersion: '2.1.0',
getScripts() {
return [
'moment.js'
];
},
getStyles() {
return [
'MMM-RemoteTemperature.css',
'font-awesome.css',
'font-awesome5.css'
];
},
getTranslations() {
return {
en: 'translations/en.json',
hu: 'translations/hu.json'
};
},
start() {
this.viewModel = null;
this._initCommunication();
},
getDom() {
const wrapper = document.createElement('div');
if (this.viewModel) {
const firstLineEl = document.createElement('div');
if (this.config.icon) {
const iconEl = document.createElement('span');
iconEl.classList = `symbol fa fa-${this.config.icon}`;
firstLineEl.appendChild(iconEl);
}
if (this.viewModel.temp) {
const tempEl = document.createElement('span');
tempEl.classList = 'temp';
tempEl.innerHTML = `${this.viewModel.temp}°`;
firstLineEl.appendChild(tempEl);
}
if (this.viewModel.humidity) {
const humidityEl = document.createElement('span');
humidityEl.classList = 'humidity';
humidityEl.innerHTML = `${this.viewModel.humidity}%`;
firstLineEl.appendChild(humidityEl);
}
wrapper.appendChild(firstLineEl);
if (this.config.showMore) {
const secondLineEl = document.createElement('div');
secondLineEl.classList = 'more dimmed small';
secondLineEl.innerHTML = `<span class="fa fa-refresh"></span> ${this._formatTimestamp(this.viewModel.timestamp)}`;
if (this.viewModel.battery) {
secondLineEl.innerHTML += `<span class="fa fa-battery-half"></span> ${this.viewModel.battery}%`;
}
wrapper.appendChild(secondLineEl);
}
} else {
const loadingEl = document.createElement('span');
loadingEl.innerHTML = this.translate('LOADING');
loadingEl.classList = 'dimmed small';
wrapper.appendChild(loadingEl);
}
return wrapper;
},
socketNotificationReceived(notificationName, payload) {
if (notificationName === 'MMM-RemoteTemperature.VALUE_RECEIVED' && payload) {
if (!this.config.sensorId || (this.config.sensorId && this.config.sensorId === payload.sensorId)) {
this.viewModel = {
temp: payload.temp,
humidity: payload.humidity,
battery: payload.battery,
timestamp: Date.now()
};
this.updateDom();
}
}
},
_initCommunication() {
this.sendSocketNotification('MMM-RemoteTemperature.INIT', {
sensorId: this.config.sensorId
});
},
_formatTimestamp(timestamp) {
return moment(timestamp).format('HH:mm');
}
});