-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabrp.js
288 lines (268 loc) · 12.4 KB
/
abrp.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
module.exports = function(RED) {
const fs = require('fs');
const path = require('path');
const https = require('https');
//const request = require('sync-request');
function AbrpconfigNode(config) {
RED.nodes.createNode(this, config);
this.usertoken = config.usertoken;
this.carmodel = config.carmodel;
this.apikey = config.apikey;
this.apiurl = config.apiurl;
var nodeContext = this.context();
}
function AbrpSendTlm(config) {
RED.nodes.createNode(this, config);
var node = this;
this.abrpconfig = RED.nodes.getNode(config.abrpconfig);
//this.usertoken = RED.nodes.getNode(config.usertoken);
//this.carmodel = RED.nodes.getNode(config.carmodel);
//this.apikey = RED.nodes.getNode(config.apikey);
//this.apiurl = RED.nodes.getNode(config.apiurl);
var nodeContext = this.context();
var throttleKey = this.abrpconfig.apiurl + '#' + this.abrpconfig.usertoken;
this.on('input', async function(msg) {
node.status({
fill: 'grey',
shape: 'ring',
text: "Called..."
});
var tlm = msg.payload;
// check if it's a real object, not an array and not null (null is also an object)
if (typeof msg.payload === 'object' && !Array.isArray(msg.payload) && msg.payload !== null) {
var tlm = {
vars: {},
discarded: {}
};
for (const [key, value] of Object.entries(msg.payload)) {
//console.log(`${key}: ${value}`);
switch (key) {
case 'utc': // High Priority (UTC Epoch Timestamp in seconds)
// Let's make sure it's not too much in the futur
if (value < ((new Date()).getTime() / 1000 + 300)) {
tlm.vars[key] = value;
} else {
tlm.discarded[key] = value;
RED.log.warn(`'${key}' has an invalid value`);
}
break;
case 'soc': // High Priority (%, the value displayed on dashboard, not from BMS)
// Let's make sure it's normal
if (value > -5 && value < 110) {
tlm.vars[key] = value;
} else {
tlm.discarded[key] = value;
RED.log.warn(`'${key}' has an invalid value`);
}
break;
case 'power': // High Priority (kW, positive during use, negative during charge)
// Let's make sure it's normal
if (value > -1000 && value < 2000) { // Yes, 1MW charging and 2700HP
tlm.vars[key] = value;
} else {
tlm.discarded[key] = value;
RED.log.warn(`'${key}' has an invalid value`);
}
break;
case 'speed': // High Priority
case 'lat': // High Priority
case 'lon': // High Priority
case 'is_charging': // High Priority
case 'is_dcfc': // High Priority
case 'is_parked': // High Priority
case 'capacity': // Low Prio
case 'soe':
case 'soh':
case 'heading':
case 'elevation':
case 'ext_temp':
case 'batt_temp':
case 'voltage':
case 'current':
case 'odometer':
case 'est_battery_range':
case 'hvac_power':
case 'hvac_setpoint':
case 'cabin_temp':
case 'tire_pressure_fl': // in kPa
case 'tire_pressure_fr':
case 'tire_pressure_rl':
case 'tire_pressure_rr':
tlm.vars[key] = value;
break;
default:
tlm.discarded[key] = value;
//RED.log.warn(`sendtlm input payload contained unknown key '${key}'`);
}
}
if (!tlm.vars.hasOwnProperty('utc')) {
tlm.vars['utc'] = Math.floor((new Date()).getTime() / 1000);
}
/*
*/
try {
https.get(
this.abrpconfig.apiurl + 'tlm/send?api_key=' + this.abrpconfig.apikey + '&token=' + this.abrpconfig.usertoken + '&tlm=' + encodeURI(JSON.stringify(tlm.vars)), {
timeout: 3000,
method: 'POST'
},
(getres) => {
let data = '';
// A chunk of data has been received.
getres.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received.
getres.on('end', () => {
try {
const jsonData = JSON.parse(data);
msg.payload = jsonData;
if (jsonData.status == 'ok') {
RED.log.debug("sendtlm jsonData.status == 'ok'");
node.status({
fill: 'green',
shape: 'dot',
text: jsonData.status
});
node.send({
payload: {
tlm: tlm,
result: jsonData
}
});
} else {
RED.log.error("sendtlm jsonData.status != 'ok'", jsonData);
node.status({
fill: 'red',
shape: 'dot',
text: jsonData.status
});
node.send(msg);
}
} catch (err) {
RED.log.error("sendtlm jsonData decode error", jsonData);
node.status({
fill: 'red',
shape: 'dot',
text: err
});
node.send(msg);
}
});
});
} catch (err) {
node.status({
fill: 'red',
shape: 'dot',
text: err
});
node.send({
payload: err,
});
}
} else {
node.status({
fill: 'red',
shape: 'dot',
text: 'Incorrect msg.payload given'
});
node.send({
payload: msg.payload,
});
}
});
}
function writeAbrpDataFile(path, data) {
fs.writeFile(path, JSON.stringify(data, null, 2), (err) => {
if (err) {
RED.log.error("Failed to write file: " + err.message);
} else {
RED.log.info("Wrote file !");
}
});
}
// Local Route to get/refresh carmodels_list from given apiurl
// Does not use config node for apiurl in case one changes it from the node before saving
RED.httpAdmin.get('/_abrp/get_carmodels_list/:refresh/:apikey/:apiurl', function(req, res) {
var abrpdatafile = path.join(RED.settings.userDir, 'abrpdata.json');
let refreshlist = req.params.refresh == 'true' ? true : false;
let apiurl = req.params.apiurl
let apikey = req.params.apikey
let abrpdata = false;
try {
// Read the file content synchronously
const data = fs.readFileSync(abrpdatafile, 'utf8');
try {
// Parse the JSON string to an object
abrpdata = JSON.parse(data);
} catch (parseErr) {
// Handle JSON parsing errors
RED.log.error('Error parsing JSON:', parseErr);
}
} catch (err) {
if (err.code === 'ENOENT') {
RED.log.warn("Failed to read file: " + err.message + " ... might create it");
abrpdata = {};
//writeAbrpDataFile(abrpdatafile,abrpdata);
} else {
RED.log.error("Unhandled error while trying to read file: " + err.message);
}
}
// ensure abrpdata has carmodels_list
if (!abrpdata.hasOwnProperty('carmodels_lists')) {
abrpdata.carmodels_lists = {};
// Don't, next check should trigger adding info anyways
//writeAbrpDataFile(abrpdatafile,abrpdata);
}
if (!abrpdata.carmodels_lists.hasOwnProperty(apiurl) || refreshlist) {
let newData;
RED.log.debug("Car lists from " + apiurl + " don't exist, or refresh requested");
// This is async, and it's a pain sometimes
https.get(apiurl + 'tlm/get_carmodels_list?api_key=' + apikey, (getres) => {
let data = '';
// A chunk of data has been received.
getres.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received.
getres.on('end', () => {
try {
const jsonData = JSON.parse(data);
if (jsonData.status == 'ok') {
RED.log.debug("jsonData.status == 'ok'");
const transformedCarList = jsonData.result.map(obj => {
const key = Object.keys(obj)[0];
const value = obj[key];
return {
value,
label: key
};
});
abrpdata.carmodels_lists[apiurl] = transformedCarList;
writeAbrpDataFile(abrpdatafile, abrpdata);
res.json(abrpdata.carmodels_lists[apiurl]);
} else {
RED.log.error("jsonData.status != 'ok'", jsonData);
}
//console.log(jsonData);
} catch (err) {
console.error('Error parsing JSON:', err);
res.status(500).json({
error: "Failed Parsing Json, check API Base URL"
});
}
});
}).on('error', (err) => {
console.error('Error:', err);
res.status(500).json({
error: err
});
});
} else {
RED.log.debug("Car lists from " + apiurl + " exists, and no refresh requested");
res.json(abrpdata.carmodels_lists[apiurl]);
}
});
RED.nodes.registerType("abrpconfig", AbrpconfigNode);
RED.nodes.registerType("ABRP Send", AbrpSendTlm);
}