This repository has been archived by the owner on May 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
MMM-BMWConnected.js
188 lines (153 loc) · 5.63 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
Module.register('MMM-BMWConnected', {
defaults: {
apiBase: "www.bmw-connecteddrive.co.uk",
refresh: 15,
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;
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-plug"));
}
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-full"));
break;
}
carContainer.appendChild(battery);
wrapper.appendChild(carContainer);
carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
var elecRange = document.createElement("span");
elecRange.classList.add("elecRange");
elecRange.appendChild(this.faIconFactory("fa-charging-station"));
elecRange.appendChild(document.createTextNode(info.electricRange + " " + distanceSuffix));
carContainer.appendChild(elecRange);
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);
//
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 + "]";
}
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);
imageContainer.appendChild(imageObject);
carContainer.appendChild(imageContainer);
wrapper.appendChild(carContainer);
return wrapper;
}
});