forked from philmavedzenge/MMM-BMWConnected
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-BMWConnected.js
261 lines (215 loc) · 8.06 KB
/
MMM-BMWConnected.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
252
253
254
255
256
257
258
259
260
261
Module.register('MMM-BMWConnected', {
defaults: {
apiBase: "www.bmw-connecteddrive.co.za",
refresh: 20,
vehicleAngle: 300,
distance: "miles",
debug: false
},
getStyles: function () {
return ["MMM-BMWConnected.css"];
},
getScripts: function () {
return ["moment.js"];
},
capFirst: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
start: function () {
Log.info('Starting module: ' + this.name);
this.sendSocketNotification('MMM-BMWCONNECTED-CONFIG', this.config);
this.bmwInfo = {};
this.getInfo();
this.timer = null;
},
getInfo: function () {
clearTimeout(this.timer);
this.timer = null;
//added this part to rotate the car image
this.config.vehicleAngle = (this.config.vehicleAngle+50)%360;
console.log("Car angle "+this.config.vehicleAngle);
console.log("Region "+this.config.region);
this.sendSocketNotification('MMM-BMWCONNECTED-CONFIG', this.config);
//end rotate car image
this.sendSocketNotification("MMM-BMWCONNECTED-GET", {
instanceId: this.identifier
});
var self = this;
this.timer = setTimeout(function () {
self.getInfo();
}, this.config.refresh * 60 * 1000);
},
socketNotificationReceived: function (notification, payload) {
if (notification == "MMM-BMWCONNECTED-RESPONSE" + this.identifier && Object.keys(payload).length > 0) {
this.bmwInfo = payload;
this.updateDom(1000);
}
},
faIconFactory: function (icon) {
var faIcon = document.createElement("i");
faIcon.classList.add("fas");
faIcon.classList.add(icon);
return (faIcon);
},
getDom: function () {
var distanceSuffix = "mi";
if (this.config.distance === "km") {
distanceSuffix = "km";
}
var wrapper = document.createElement("div");
if (this.config.email === "" || this.config.password === "") {
wrapper.innerHTML = "Missing configuration.";
return wrapper;
}
if (Object.keys(this.bmwInfo).length == 0) {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "dimmed light small";
return wrapper;
}
var info = this.bmwInfo;
var carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
var locked = document.createElement("span");
locked.classList.add("locked");
if (info.doorLock === "SECURED" || info.doorLock === "LOCKED") {
locked.appendChild(this.faIconFactory("fa-lock"));
} else {
locked.appendChild(this.faIconFactory("fa-lock-open"));
}
carContainer.appendChild(locked);
var mileage = document.createElement("span");
mileage.classList.add("mileage");
mileage.appendChild(this.faIconFactory("fa-road"));
mileage.appendChild(document.createTextNode(info.mileage + " " + distanceSuffix));
carContainer.appendChild(mileage);
wrapper.appendChild(carContainer);
//
carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
var plugged = document.createElement("span");
plugged.classList.add("plugged");
if (info.connectorStatus == "CONNECTED") {
plugged.appendChild(this.faIconFactory("fa-bolt"));
} else {
plugged.appendChild(this.faIconFactory("fa-plugjk"));//added an icon that's not available so that it doesnt show on screen
}
carContainer.appendChild(plugged);
wrapper.appendChild(carContainer);
carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
var battery = document.createElement("span");
battery.classList.add("battery");
switch (true) {
case (info.chargingLevelHv < 25):
battery.appendChild(this.faIconFactory("fa-battery-empty"));
break;
case (info.chargingLevelHv < 50):
battery.appendChild(this.faIconFactory("fa-battery-quarter"));
break;
case (info.chargingLevelHv < 75):
battery.appendChild(this.faIconFactory("fa-battery-half"));
break;
case (info.chargingLevelHv < 100):
battery.appendChild(this.faIconFactory("fa-battery-three-quarters"));
break;
default:
battery.appendChild(this.faIconFactory("fa-battery-fullz"));//added an icon that's not available so that it doesnt show on screen
break;
}
carContainer.appendChild(battery);
wrapper.appendChild(carContainer);
var fuelcolor = "";
if(info.fuelLitres > 30){
fuelcolor = "green";
}else {
if(info.fuelLitres <= 30 && info.fuelLitres > 10){
fuelcolor = "orange";
}else{
fuelcolor = "red";
}
}
carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
var fuelLeft = document.createElement("span");
fuelLeft.classList.add(fuelcolor);
fuelLeft.appendChild(this.faIconFactory("fa-tachometer-alt"));
fuelLeft.appendChild(document.createTextNode(info.fuelLitres + " litres" ));
carContainer.appendChild(fuelLeft);
var fuelRange = document.createElement("span");
fuelRange.classList.add("fuelRange");
fuelRange.appendChild(this.faIconFactory("fa-gas-pump"));
fuelRange.appendChild(document.createTextNode(info.fuelRange + " " + distanceSuffix));
carContainer.appendChild(fuelRange);
wrapper.appendChild(carContainer);
//display if windows or sunroof are open
var windows = "windows : ";
if (info.windowDriverFront === "CLOSED" && info.windowPassengerFront === "CLOSED" && info.windowDriverRear === "CLOSED" && info.windowPassengerRear === "CLOSED"){
windows = "closed";
}
else{
windows = "open";
}
var sunroof = "sunroof : ";
if (info.sunroof === "CLOSED"){
sunroof = "closed";
}else{
sunroof = "open";
}
var boot = "";
if (info.boot === "CLOSED"){
boot = "closed";
}else{
boot = "open";
}
//
carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
carContainer.classList.add("updated");
var updated = document.createElement("span");
updated.classList.add("updated");
updated.appendChild(this.faIconFactory("fa-info"));
var lastUpdateText = "last updated " + moment(info.updateTime).fromNow();
if (this.config.debug) {
lastUpdateText += " [" + info.unitOfLength + "]";
}
switch (true) {
case (windows === "open" && sunroof === "open" && boot === "open"):
lastUpdateText = "sunroof , boot and windows open";
break;
case (windows === "open" && sunroof === "open" && boot === "closed"):
lastUpdateText = "sunroof and windows open";
break;
case (windows === "open" && sunroof === "closed" && boot === "open"):
lastUpdateText = "boot and windows open";
break;
case (windows === "closed" && sunroof === "open" && boot === "open"):
lastUpdateText = "boot and sunroof open";
break;
case (windows === "open" && sunroof === "closed" && boot === "closed"):
lastUpdateText = "windows open";
break;
case (windows === "closed" && sunroof === "closed" && boot === "open"):
lastUpdateText = "boot open";
break;
case (windows === "closed" && sunroof === "open" && boot === "closed"):
lastUpdateText = "sunroof open";
break;
default:
break;
}
updated.appendChild(document.createTextNode(lastUpdateText));
carContainer.appendChild(updated);
wrapper.appendChild(carContainer);
//
//carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
var imageContainer = document.createElement("span");
var imageObject = document.createElement("img");
imageObject.setAttribute('src', info.imageUrl);
//imageObject.setAttribute('src', 'https://www.openstreetmap.org/?mlat=-26.14244&mlon=27.94451#map=16/-26.14244/27.94451');
imageContainer.appendChild(imageObject);
carContainer.appendChild(imageContainer);
wrapper.appendChild(carContainer);
return wrapper;
}
});