forked from philmavedzenge/MMM-BMWConnected
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
121 lines (100 loc) · 4.8 KB
/
node_helper.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
var NodeHelper = require("node_helper");
const moment = require('moment');
var tokenmanager = require('./lib/tokenmanager.js');
var bmwrequest = require('./lib/bmwrequest.js');
module.exports = NodeHelper.create({
start: function () {
console.log("Starting node_helper for module: " + this.name);
this.bmwInfo = null;
},
socketNotificationReceived: function (notification, payload) {
var self = this;
if (notification == "MMM-BMWCONNECTED-CONFIG") {
self.config = payload;
} else if (notification == "MMM-BMWCONNECTED-GET") {
var credConfig = {
'username': self.config.email,
'password': self.config.password,
'country' : self.config.country,
'region' : self.config.region
}
tokenmanager.initialize(credConfig,
function onSuccess(token, tokenType) {
var vin;
console.log("Token init completed: " + "\nToken: " + token + "\nTokenType: " + tokenType);
bmwrequest.call(self.config.apiBase, '/api/me/vehicles/v2', '', token, tokenType,
function (data) {
try {
var json = JSON.parse(data);
vin = json[0].vin;
} catch (err) {
console.error("Failed to parse data " + data + ", error " + err);
}
var getInfoUri = '/api/vehicle/dynamic/v1/' + vin
bmwrequest.call(self.config.apiBase, getInfoUri, '', token, tokenType,
function (data) {
try {
var json = JSON.parse(data);
var attributes = json.attributesMap;
console.log(attributes);
self.bmwInfo = {
updateTime: moment.unix(attributes.updateTime_converted_timestamp / 1000).format(),
doorLock: attributes.door_lock_state,
electricRange: Number(attributes.beRemainingRangeElectricMile).toFixed(),
fuelRange: Number(attributes.beRemainingRangeFuelMile).toFixed(),
mileage: Number(attributes.mileage).toFixed(),
connectorStatus: attributes.connectorStatus,
vin: vin,
chargingLevelHv: Number(attributes.chargingLevelHv).toFixed(),
fuelLitres: Number(attributes.remaining_fuel).toFixed(),
sunroof: attributes.sunroof_state,
windowDriverFront: attributes.window_driver_front,
windowPassengerFront: attributes.window_passenger_front,
windowDriverRear: attributes.window_driver_rear,
windowPassengerRear: attributes.window_passenger_rear,
boot: attributes.trunk_state,
imageUrl: null,
unitOfLength: attributes.unitOfLength
}
if (self.config.distance === "km") {
self.bmwInfo.electricRange = Number(attributes.beRemainingRangeElectricKm).toFixed();
self.bmwInfo.fuelRange = Number(attributes.beRemainingRangeFuelKm).toFixed();
}
var getImagesUri = '/api/vehicle/image/v1/' + vin + "?startAngle=0&stepAngle=10&width=640"
bmwrequest.call(self.config.apiBase, getImagesUri, '', token, tokenType, function (data) {
try {
var json = JSON.parse(data);
var angleUrls = json.angleUrls;
var picked = angleUrls.find(o => o.angle === self.config.vehicleAngle);
} catch (err) {
console.error("Failed to parse data " + data + ", error " + err);
}
self.bmwInfo.imageUrl = picked.url;
self.bmwInfo.instanceId = payload.instanceId;
self.parseCarInfo(payload);
},
function onError(err) {
console.error("Failed to read list of vehicle images:" + err);
});
} catch (err) {
console.error("Failed to parse data " + data + ", error " + err);
}
},
function onError(err) {
console.error("Failed to read vehicle info:" + err);
});
},
function onError(err) {
console.error("Failed to read list of vehicles:" + err);
});
},
function onError(err) {
console.error("Failed to read token:" + err);
}
);
}
},
parseCarInfo: function (payload) {
this.sendSocketNotification("MMM-BMWCONNECTED-RESPONSE" + payload.instanceId, this.bmwInfo);
},
});