forked from f00d4tehg0dz/MMM-TeslaFi
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMMM-Tesla.js
251 lines (229 loc) · 6.93 KB
/
MMM-Tesla.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Magic Mirror
* Module: MMM-Tesla
*
* Originally By Adrian Chrysanthou
* Updated by Matt Dyson
* MIT Licensed.
*/
Module.register("MMM-Tesla", {
defaults: {
animationSpeed: 1000,
refreshInterval: 1000 * 60, // Refresh DOM every 60 seconds
updateInterval: 1000 * 60 * 5, // Load data every 5 minutes
unitDistance: "miles",
unitTemperature: "c",
batteryDanger: 30,
batteryWarning: 50,
dataTimeout: 0,
source: {},
maps: {
apiKey: "",
width: 300,
height: 150,
zoom: 13,
exclude: []
},
precision: 1, // How many decimal places to round values to
items: [
"state",
"speed",
"heading",
"battery",
"range",
"range-estimated",
"power-connected",
"charge-time",
"charge-added",
"charge-power",
"locked",
"odometer",
"temperature",
"map",
"version",
"version-new",
"location",
"data-time"
]
},
// Define required scripts.
getScripts: function () {
return [
"moment.js",
this.file("node_modules/build-url/src/build-url.js"),
this.file("DataItemProvider.js"),
this.file("dataitems/battery.js"),
this.file("dataitems/charge.js"),
this.file("dataitems/driving.js"),
this.file("dataitems/location.js"),
this.file("dataitems/range.js"),
this.file("dataitems/software.js"),
this.file("dataitems/state.js"),
this.file("dataitems/temperature.js")
];
},
getStyles: function () {
return [
"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0/css/material-design-iconic-font.min.css",
"MMM-Tesla.css"
];
},
start: function () {
Log.info("Starting module: " + this.name);
this.loaded = false;
this.sendSocketNotification("CONFIG", this.config);
this.providers = [];
for (var identifier in DataItemProvider.providers) {
this.providers[identifier] = new DataItemProvider.providers[identifier](
this
);
}
this.resetDomUpdate();
},
resetDomUpdate: function () {
var self = this;
// Reset any previously allocated timer to avoid double-refreshes
clearInterval(this.domTimer);
// Refresh the DOM at the given interval
this.domTimer = setInterval(function () {
self.updateDom(self.config.animationSpeed);
}, this.config.refreshInterval);
},
getDom: function () {
var wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.parsedData) {
wrapper.innerHTML = "No data";
wrapper.className = "dimmed light small";
return wrapper;
}
var t = this.parsedData;
var content = document.createElement("div");
content.innerHTML = "";
var table = `
<h2 class="car-name"><span class="zmdi zmdi-car zmdi-hc-1x icon"></span> ${t.display_name}</h2>
<table class="small">
`;
for (var index in this.config.items) {
dataItem = this.config.items[index];
if (!this.providers.hasOwnProperty(dataItem)) {
Log.error("Could not find " + dataItem + " in list of valid providers");
continue;
}
if (!this.providers[dataItem].display) {
// This provider doesn't want us to display it right now, so skip
Log.info(
"Provider " + dataItem + " doesn't want to be shown right now"
);
continue;
}
var icon = this.providers[dataItem].icon;
var field = this.providers[dataItem].field;
var value = this.providers[dataItem].value;
if (field === null && value === null) {
table += `
<tr>
<td class="icon" colspan="3">${icon}</td>
</tr>
`;
} else {
var colspan = 1;
if (value === null) {
colspan = 2;
}
table += `
<tr>
<td class="icon">${icon}</td>
<td class="field" colspan="${colspan}">${field}</td>
`;
if (value !== null) {
table += `<td class="value">${value}</td>`;
}
table += `</tr>`;
}
} // end foreach loop of items
table += "</table>";
wrapper.innerHTML = table;
wrapper.className = "light small";
wrapper.appendChild(content);
return wrapper;
},
socketNotificationReceived: function (notification, payload) {
if (notification === "STARTED") {
// If the node_helper socket has only just opened, refresh the DOM to make sure we're displaying a loading message
this.updateDom();
} else if (notification === "DATA") {
Log.info("Tesla received new data");
// We've received data, so parse and display it
var data = JSON.parse(payload);
if (!data) {
return;
}
this.parsedData = data;
this.loaded = true;
// Tell all of our data item providers about the new data
for (var identifier in this.providers) {
this.providers[identifier].onDataUpdate(data);
}
this.updateDom(this.config.animationSpeed);
this.resetDomUpdate();
}
},
// Return a number with the precision specified in our config
numberFormat: function (number) {
return parseFloat(number).toFixed(this.config.precision);
},
// Converts the given temperature (assumes C input) into the configured output, with appropriate units appended
convertTemperature: function (valueC) {
if (this.config.unitTemperature === "f") {
var valueF = valueC * (9 / 5) + 32;
return this.numberFormat(valueF) + "°F";
} else {
return this.numberFormat(valueC) + "°C";
}
},
// Converts the given distance (assumes miles input) into the configured output, with appropriate units appended
convertDistance: function (valueMiles) {
if (this.config.unitDistance === "km") {
var valueKm = valueMiles * 1.60934;
return this.numberFormat(valueKm) + " km";
} else {
return this.numberFormat(valueMiles) + " miles";
}
},
// Converts given speed (assumes miles input) to configured output with approprate units appened
convertSpeed: function (valueMiles) {
if (this.config.unitDistance === "km") {
return this.numberFormat(valueMiles * 1.60934) + " km/h";
} else {
return this.numberFormat(valueMiles) + " mph";
}
},
// Converts heading int to nearest bearing by 45deg
convertHeading: function (heading) {
const bearing = {
0: "North",
45: "North East",
90: "East",
135: "South East",
180: "South",
225: "South West",
270: "West",
315: "North West",
360: "North"
};
const direction = (heading) => {
return Object.keys(bearing)
.map(Number)
.reduce(function (prev, curr) {
return Math.abs(curr - heading) < Math.abs(prev - heading)
? curr
: prev;
});
};
return bearing[direction(heading)];
}
});