-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-RadioThermostat.js
179 lines (154 loc) · 4.89 KB
/
MMM-RadioThermostat.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
/* Magic Mirror
* Module: MMM-RadioThermostat
*
* By Jason 'JP' McCrory,
* MIT Licensed.
*
*/
Module.register("MMM-RadioThermostat", {
jsonData: null,
indoorTemp: null,
// Default module config.
defaults: {
url: "",
size: 0,
//tryFormatDate: false,
updateInterval: 15000,
debug: false,
showAlerts: true,
showTitle: false,
title: 'Indoor Temperature',
loadingText: 'Getting Indoor Temperature...',
postText: '',
showFontAwesomeIcons: true
},
getStyles: function () {
return ["font-awesome.css"];
},
start: function () {
this.config.url = "http://" + this.config.url + "/tstat";
this.loaded = false;
this.getJson();
this.scheduleUpdate();
},
scheduleUpdate: function () {
var self = this;
setInterval(function () {
self.getJson();
}, this.config.updateInterval);
},
// Request node_helper to get json from url
getJson: function () {
this.sendSocketNotification("MMM-RadioThermostat_GET_JSON", this.config.url);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-RadioThermostat_JSON_RESULT") {
// Only continue if the notification came from the request we made
// This way we can load the module more than once
if (payload.url === this.config.url)
{
this.jsonData = payload.data;
//var JSONObject = JSON.parse(this.jsonData);
this.indoorTemp = this.jsonData["temp"];
this.sendNotification('INDOOR_TEMPERATURE', this.indoorTemp);
this.loaded = true;
if (this.config.debug) {
this.updateDom();
}
//this.updateDom(500);
}
}
if (notification === 'ERROR' && this.config.showAlerts){
this.sendNotification('SHOW_ALERT', payload);
}
},
getTranslations: function() {
return {
en: "translations/en.json"
};
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement('div');
wrapper.className = "xsmall";
if (this.config.debug) {
if (!this.loaded) {
wrapper.innerHTML = this.config.loadingText;
return wrapper;
}
var table = document.createElement("table");
var tbody = document.createElement("tbody");
let processThese = ["temp", "tmode", "fmode", "t_heat", "t_cool"];
let translateThese = ["tmode", "fmode"];
for (var key in this.jsonData) {
if (processThese.includes(key)) {
var row = document.createElement("tr");
//Create Table Data Elements
var descriptionCell = document.createElement("td");
var valueCell = document.createElement("td");
//Create Description
var descriptionText = document.createTextNode(this.translate(key));
descriptionCell.appendChild(descriptionText);
row.appendChild(descriptionCell);
//Create Values
if (translateThese.includes(key)) {
if (this.config.showFontAwesomeIcons) {
valueCell.classList.add("fa", this.translate(key + "-icon=" + this.jsonData[key]));
}
else {
var valueText = document.createTextNode(this.translate(key + "=" + this.jsonData[key]));
valueCell.appendChild(valueText);
}
}
else
{
var valueText = document.createTextNode(this.jsonData[key]);
valueCell.appendChild(valueText);
}
row.appendChild(valueCell);
tbody.appendChild(row);
}
}
table.appendChild(tbody);
wrapper.appendChild(table);
//var titleDiv = document.createElement('div');
//titleDiv.innerHTML = this.config.title;
//wrapper.appendChild(titleDiv);
//var tempDiv = document.createElement('div');
//tempDiv.innerHTML = this.indoorTemp;
//wrapper.appendChild(tempDiv);
}
return wrapper;
},
getTableRow: function (jsonObject) {
var row = document.createElement("tr");
for (var key in jsonObject) {
var cell = document.createElement("td");
var valueToDisplay = "";
if (key === "icon") {
cell.classList.add("fa", jsonObject[key]);
}
else if (this.config.tryFormatDate) {
valueToDisplay = this.getFormattedValue(jsonObject[key]);
}
else {
if ( this.config.keepColumns.length == 0 || this.config.keepColumns.indexOf(key) >= 0 ){
valueToDisplay = jsonObject[key];
}
}
var cellText = document.createTextNode(valueToDisplay);
if ( this.config.size > 0 && this.config.size < 9 ){
var h = document.createElement("H" + this.config.size );
h.appendChild(cellText)
cell.appendChild(h);
}
else
{
cell.appendChild(cellText);
}
row.appendChild(cell);
}
return row;
}
});