-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetBatteryStatus.js
More file actions
94 lines (74 loc) · 2.9 KB
/
GetBatteryStatus.js
File metadata and controls
94 lines (74 loc) · 2.9 KB
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
const request = require('request');
const { Client } = require('pg');
function loadConfig() {
console.log('loadConfig()')
var fs = require('fs');
return JSON.parse(fs.readFileSync('config.json', 'utf8'));
}
function loadUsers() {
console.log('loadUsers()');
client.query('SELECT Username, Password FROM Users')
.then(res => {
// TODO: loop for all users
let user = res.rows[0];
login(user);
})
.catch(e => console.error(e.stack));
}
function login(user) {
console.log('login()');
url = 'https://services.renault-ze.com/api/user/login';
method = 'POST';
requestbody = user;
requestTimestamp = new Date().toISOString();
request({
url: url,
method: method,
json: requestbody,
}, function(error, response, body) {
if (error)
console.log(error.stack);
else {
// Redact the password and activation code before logging
requestbody.password = '[redacted]';
body.user.vehicle_details.activation_code = '[redacted]';
for (i = 0; i < body.user.associated_vehicles.length; i++)
body.user.associated_vehicles[i].activation_code = '[redacted]';
client.query(
'INSERT INTO ZeServicesLog (username, requesturl, requestmethod, requestbody, requesttimestamp, responsebody, responsetimestamp) VALUES ($1::text, $2::text, $3::text, $4::json, $5::timestamp, $6::json, $7::timestamp)',
[user.username, url, method, JSON.stringify(requestbody), requestTimestamp, JSON.stringify(body), new Date().toISOString()],
(err, res) => {
if (err)
console.log(err ? err.stack : 'OK');
else
getBatteryStatus(user.username, body.token, body.user.vehicle_details.VIN);
}
);
}
});
}
function getBatteryStatus(username, token, vin) {
console.log('getBatteryStatus()');
url = 'https://www.services.renault-ze.com/api/vehicle/' + vin + '/battery';
method = 'GET';
requestTimestamp = new Date().toISOString();
request({
url: url,
method: method,
auth: { 'bearer': token }
}, function(error, response, body) {
console.log(body);
client.query(
'INSERT INTO ZeServicesLog (username, requesturl, requestmethod, requestbody, requesttimestamp, responsebody, responsetimestamp) VALUES ($1::text, $2::text, $3::text, $4::json, $5::timestamp, $6::json, $7::timestamp)',
[username, url, method, null, requestTimestamp, body, new Date().toISOString()],
(err, res) => {
console.log(err ? err.stack : 'OK');
client.end();
}
);
});
}
const config = loadConfig();
const client = new Client(config.databaseConfig);
client.connect();
loadUsers();