-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
142 lines (124 loc) · 4.02 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var NodeHelper = require("node_helper");
const request = require('request');
var authToken = null;
const authUrl = "https://api.tronity.tech/authentication"
const vehicleDataUrl = "https://api.tronity.tech/tronity/vehicles/VEHICLE_ID/last_record"
module.exports = NodeHelper.create({
defaults: {
debug: false
},
config: {
clientId: '',
clientSecret: '',
vehicleId: ''
},
start: function () {
console.log('Starting node_helper for MMM-Tronity');
},
getAuth: function (vehicleId) {
var self = this;
console.log('Authenticating via url ' + authUrl);
console.log('Using client ID ' + this.config.clientId);
console.log('and client secret ' + this.config.clientSecret);
const options = {
url: authUrl,
method: 'POST',
json: true,
body: {
'client_id': self.config.clientId,
'client_secret': self.config.clientSecret,
'grant_type': 'app'
}
};
request(options, function (error, response, body) {
if (!error) {
console.log('Response status code ' + response.statusCode);
//self.debug('Received response ' + JSON.stringify(response));
if (response.statusCode == 201) {
//const obj = JSON.parse(response);
authToken = response.body.access_token;
console.log('Got auth token ' + authToken);
self.getVehicleData(vehicleId);
}
else {
console.log(response.statusCode + ' - ' + response.statusMessage);
}
} else if (error) {
console.log(error);
}
});
console.log('Completed auth token request');
return;
},
socketNotificationReceived: function (notification, payload) {
var self = this;
console.log('Socket notification received ' + notification);
if (notification == 'SET_CONFIG') {
this.config = Object.assign(this.defaults, payload);
self.sendSocketNotification('MMM_TRONITY_READY');
return;
}
if (self.config.clientId.length < 2) {
console.log('Config info missing!')
self.sendSocketNotification('GET_CONFIG');
return;
}
else if (notification == 'GET_CAR_DATA') {
if (authToken == null) {
console.log('No auth token!');
self.getAuth(payload);
} else {
self.getVehicleData(payload);
}
return;
}
},
getVehicleDataUrl: function (config) {
console.log('vehicle ID' + this.config.vehicleId);
if (this.config && !this.config.vehicleId) return '';
if (!config) return '';
var url = vehicleDataUrl.replace('VEHICLE_ID', this.config.vehicleId);
return url;
},
getVehicleData: function (vehicleId) {
var self = this;
var apiUrl = this.getVehicleDataUrl(vehicleId);
console.log('Vehicle data url ' + apiUrl);
if (apiUrl.length == 0) {
self.sendSocketNotification('GET_CONFIG');
return;
}
const options = {
url: apiUrl,
method: 'GET',
json: true,
headers: {
'Authorization': 'Bearer ' + authToken
}
};
console.log('Sending GET request with auth token: ' + authToken);
request(options, function (error, response, body) {
if (!error) {
console.log('Response status code ' + response.statusCode);
if (response.statusCode == 200) {
self.sendSocketNotification('UPDATE_CAR_DATA', response);
//self.debug('Received response: ' + JSON.stringify(response));
}
else if (response.statusCode == 401) {
console.log('Unauthorised! Renewing auth token.');
self.getAuth();
return;
} else {
console.log(response.statusCode + ' - ' + response.statusMessage);
}
} else if (error) {
console.log(error);
}
});
},
debug: function (msg) {
var shouldLog = true;
if (this.config && typeof this.config.debug !== 'undefined') shouldLog = this.config.debug;
if (shouldLog) console.log(msg);
}
});