forked from kstolk/MMM-Bluelinky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
76 lines (62 loc) · 2.13 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
'use strict';
/* Magic Mirror
* Module: MMM-Tesla2
*
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Bluelinky = require('bluelinky');
module.exports = NodeHelper.create({
start: function() {
this.started = false;
this.config = null;
this.vehicle_data = null;
},
getData: function () {
var self = this;
const config = {
username: this.config.username,
password: this.config.password,
brand: 'hyundai',
region: this.config.region,
pin: this.config.pin,
};
const client = new Bluelinky(config);
client.on('ready', async (vehicles) => {
// Check if we should wake the car to refresh the data:
// This happens when this is the first time after the module started and the config allowes it
const refresh = this.config.wakeOnRefresh || (!this.started && this.config.wakeOnModuleLoad);
//Get first vehicle using the VIN id
const vehicle = client.getVehicle(vehicles[0].vehicleConfig.vin);
// Get non-parsed bluelinky data
const vehicleData = await vehicle.status({parsed: false, refresh});
// Get location
const location = await vehicle.location();
const newData = {
...vehicleData,
...location,
name: vehicle.vehicleConfig.name + ' (' + vehicle.vehicleConfig.generation + ')',
};
self.vehicle_data = newData;
self.sendSocketNotification("CAR_DATA", newData);
if (vehicleData.evStatus.batteryCharge) {
setTimeout(function() { self.getData(); }, this.config.refreshIntervalWhileCharging);
} else {
setTimeout(function() { self.getData(); }, this.config.refreshInterval);
}
});
},
socketNotificationReceived: async function(notification, payload) {
var self = this;
if (notification === 'BLUELINKY_CONFIG' && self.started == false) {
self.config = payload;
self.sendSocketNotification("STARTED", true);
self.getData();
self.started = true;
} else if (notification == 'REFRESH') {
self.getData();
} else if (notification == 'BLUELINKY_CONFIG') {
self.sendSocketNotification("CAR_DATA", self.vehicle_data);
}
}
});