-
Notifications
You must be signed in to change notification settings - Fork 19
/
leaf.js
176 lines (159 loc) · 4.87 KB
/
leaf.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
"use strict";
let https = require("https");
// Require encryption.js to encrypt the password.
var Encryption = require('./encryption.js');
// Do not change this value, it is static.
let initial_app_strings = "geORNtsZe5I4lRGjG9GZiA";
// Possible value are NE (Europe), NNA (North America) and NCI (Canada).
let region_code = process.env.regioncode;
// You should store your username and password as environment variables.
// If you don't you can hard code them in the following variables.
let username = process.env.username; // Your NissanConnect username or email address.
let password = encrypt(process.env.password); // Your NissanConnect account password.
let sessionid, vin, loginFailureCallback;
/**
* Sends a request to the Nissan API.
*
* action - The API endpoint to call, like UserLoginRequest.php.
* requestData - The URL encoded parameter string for the current call.
* successCallback
* failureCallback
**/
function sendRequest(action, requestData, successCallback, failureCallback) {
const options = {
hostname: "gdcportalgw.its-mo.com",
port: 443,
path: "/gworchest_160803EC/gdc/" + action,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(requestData),
}
};
const req = https.request(options, resp => {
if (resp.statusCode < 200 || resp.statusCode > 300) {
console.log(`Failed to send request ${action} (${resp.statusCode}: ${resp.statusMessage})`);
if (failureCallback)
failureCallback();
return;
}
console.log(`Successful request ${action} (${resp.statusCode}: ${resp.statusMessage})`);
let respData = "";
resp.on("data", c => {
respData += c.toString();
});
resp.on("end", () => {
let json = respData && respData.length ? JSON.parse(respData) : null;
if (json.status == 200) {
successCallback(respData && respData.length ? JSON.parse(respData) : null);
}else {
console.log(json);
}
});
});
req.write(requestData);
req.end();
}
/**
* Log the current user in to retrieve a valid session token.
*
* successCallback
**/
function login(successCallback) {
sendRequest("UserLoginRequest.php",
"UserId=" + username +
"&initial_app_strings=" + initial_app_strings +
"&RegionCode=" + region_code +
"&Password=" + password,
loginResponse => {
// Get the session id and VIN for future API calls.
// Sometimes the results from the API include a VehicleInfoList array, sometimes they omit it!
if (loginResponse.VehicleInfoList) {
sessionid = encodeURIComponent(loginResponse.VehicleInfoList.vehicleInfo[0].custom_sessionid);
vin = encodeURIComponent(loginResponse.VehicleInfoList.vehicleInfo[0].vin);
} else {
sessionid = encodeURIComponent(loginResponse.vehicleInfo[0].custom_sessionid);
vin = encodeURIComponent(loginResponse.vehicleInfo[0].vin);
}
successCallback();
},
loginFailureCallback);
}
/**
* Get the battery information from the API.
**/
exports.getBatteryStatus = (successCallback, failureCallback) => {
login(() => sendRequest("BatteryStatusRecordsRequest.php",
"custom_sessionid=" + sessionid +
"&RegionCode=" + region_code +
"&VIN=" + vin,
successCallback,
failureCallback));
}
/**
* Enable the climate control in the car.
**/
exports.sendPreheatCommand = (successCallback, failureCallback) => {
login(() => sendRequest("ACRemoteRequest.php",
"UserId=" + username +
"&custom_sessionid=" + sessionid +
"&RegionCode=" + region_code +
"&VIN=" + vin,
successCallback,
failureCallback));
}
/**
* Enable the climate control in the car.
**/
exports.sendCoolingCommand = (successCallback, failureCallback) => {
login(() => sendRequest("ACRemoteRequest.php",
"UserId=" + username +
"&custom_sessionid=" + sessionid +
"&RegionCode=" + region_code +
"&VIN=" + vin,
successCallback,
failureCallback));
}
/**
* Disable the climate control in the car.
**/
exports.sendClimateControlOffCommand = (successCallback, failureCallback) => {
login(() => sendRequest("ACRemoteOffRequest.php",
"UserId=" + username +
"&custom_sessionid=" + sessionid +
"&RegionCode=" + region_code +
"&VIN=" + vin,
successCallback,
failureCallback));
}
/**
* Start charging the car.
**/
exports.sendStartChargingCommand = (successCallback, failureCallback) => {
login(() => sendRequest("BatteryRemoteChargingRequest.php",
"UserId=" + username +
"&custom_sessionid=" + sessionid +
"&RegionCode=" + region_code +
"&VIN=" + vin,
successCallback,
failureCallback));
}
/**
* Request the API fetch updated data from the car.
**/
exports.sendUpdateCommand = (successCallback, failureCallback) => {
login(() => sendRequest("BatteryStatusCheckRequest.php",
"UserId=" + username +
"&custom_sessionid=" + sessionid +
"&RegionCode=" + region_code +
"&VIN=" + vin,
successCallback,
failureCallback));
}
/**
* Encrypt the password for use with API calls.
**/
function encrypt(password) {
var e = new Encryption();
return e.encrypt(password, "uyI5Dj9g8VCOFDnBRUbr3g");
}