This repository has been archived by the owner on Nov 22, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
213 lines (194 loc) · 7.17 KB
/
index.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
var request = require('request')
, xml2js = require('xml2js')
, util = require('util')
, js2xml = require('data2xml')()
, parser = new xml2js.Parser({explicitArray: false});
var PLUGIN_STATUS_DISCONNECTED = 'NOT_CONNECTED'
, PLUGIN_STATUS_CONNECTED = 'CONNECTED';
/**
* HTTP POST to the Carwings SOAP webservice
*
* @param {String} service
* @param {Object} payload
* @param {Function} callback
* @api private
*/
var _post = function(service, payload, callback) {
request.post({
url: 'https://nissan-na-smartphone-biz.viaaq.com/aqPortal/smartphoneProxy/' + service,
headers: {
'User-Agent': 'NissanLEAF/1.40 CFNetwork/485.13.9 Darwin/11.0.0 carwingsjs',
'Content-Type': 'text/xml'
},
jar: true,
body: js2xml(payload[0], payload[1]) },
function(error, response, body) {
if (error) {
//console.log(error);
callback(error);
}
else {
//console.log(response);
//console.log(body);
parser.parseString(body, callback);
}
});
};
/**
* Extracts key bits of info from a <ns4:SmartphoneLatestBatteryStatusResponse> node
*
* @param {Object} node
* @return {Object}
* @api private
*/
var _getVehicle = function(node) {
vehicleResponse = {};
vehicleResponse.vin = node['SmartphoneBatteryStatusResponseType']['VehicleInfo']['Vin'];
vehicleResponse.batteryChargingStatus = node['SmartphoneBatteryStatusResponseType']['ns3:BatteryStatusRecords']['ns3:BatteryStatus']['ns3:BatteryChargingStatus'];
vehicleResponse.batteryCapacity = node['SmartphoneBatteryStatusResponseType']['ns3:BatteryStatusRecords']['ns3:BatteryStatus']['ns3:BatteryCapacity'];
vehicleResponse.batteryRemainingAmount = node['SmartphoneBatteryStatusResponseType']['ns3:BatteryStatusRecords']['ns3:BatteryStatus']['ns3:BatteryRemainingAmount'];
vehicleResponse.lastBatteryStatusCheckExecutionTime = node['SmartphoneBatteryStatusResponseType']['lastBatteryStatusCheckExecutionTime']
vehicleResponse.pluginState = node['SmartphoneBatteryStatusResponseType']['ns3:BatteryStatusRecords']['ns3:PluginState'];
if (vehicleResponse.pluginState != PLUGIN_STATUS_DISCONNECTED) {
if (node['SmartphoneBatteryStatusResponseType']['ns3:BatteryStatusRecords']['ns3:TimeRequiredToFull']) {
vehicleResponse.hoursRequiredToFull = node['SmartphoneBatteryStatusResponseType']['ns3:BatteryStatusRecords']['ns3:TimeRequiredToFull']['ns3:HourRequiredToFull'];
vehicleResponse.minutesRequiredToFull = node['SmartphoneBatteryStatusResponseType']['ns3:BatteryStatusRecords']['ns3:TimeRequiredToFull']['ns3:MinutesRequiredToFull'];
}
}
return vehicleResponse;
};
/**
* Login to the Carwings service. Please note that the vehicle data
* returned is generally stale. You will need to call `requestUpdate` if you
* want the most current data from the car.
*
* @param username
* @param password
* @param callback
* @api public
*/
exports.login = function(username, password, callback) {
var payload = [
'ns2:SmartphoneLoginWithAdditionalOperationRequest', {
_attr: { 'xmlns:ns2' : 'urn:com:airbiquity:smartphone.userservices:v1' },
'SmartphoneLoginInfo' : {
'UserLoginInfo' : {
'userId' : username,
'userPassword' : password },
'DeviceToken': util.format('DUMMY%s', (new Date()).getTime()),
'UUID': util.format('carwingsjs:%s', username),
'Locale': 'US',
'AppVersion': '1.40',
'SmartphoneType': 'IPHONE'},
'SmartphoneOperationType': 'SmartphoneLatestBatteryStatusRequest'
}];
_post('userService', payload, function(error, result) {
if (error) {
callback(error);
}
else {
var loginResponse = _getVehicle(result['ns2:SmartphoneLoginWithAdditionalOperationResponse']['ns4:SmartphoneLatestBatteryStatusResponse']);
loginResponse._full = result;
callback(null, loginResponse);
}
});
};
/**
* Tell Carwings to ping the car from a data refresh. This method
* return no data. It will take anywhere from 30 seconds to 5 minutes
* for the data in Carwings to get refreshed and cached.
*
* @param vin
* @param callback
* @api public
*/
exports.requestUpdate = function(vin, callback) {
var payload = ['ns4:SmartphoneRemoteBatteryStatusCheckRequest', {
_attr: {
'xmlns:ns4' : 'urn:com:airbiquity:smartphone.vehicleservice:v1',
'xmlns:ns3' : 'urn:com:hitachi:gdc:type:vehicle:v1',
'xmlns:ns2' : 'urn:com:hitachi:gdc:type:portalcommon:v1' },
'ns3:BatteryStatusCheckRequest' : {
'ns3:VehicleServiceRequestHeader': {
'ns2:VIN': vin }
}
}];
_post('vehicleService', payload, callback);
};
/**
* Return information about the car. Please note that the vehicle data
* returned is generally stale. You will need to call `requestUpdate` if you
* want the most current data from the car.
*
* @param vin
* @param callback
* @api public
*/
exports.vehicleStatus = function(vin, callback) {
var payload = ['ns2:SmartphoneGetVehicleInfoRequest', {
_attr: { 'xmlns:ns2' : 'urn:com:airbiquity:smartphone.userservices:v1' },
'VehicleInfo' : {
'Vin' : vin },
'SmartphoneOperationType': 'SmartphoneLatestBatteryStatusRequest',
'changeVehicle': 'false'
}];
_post('userService', payload, function(error, result) {
if (error) {
callback(error);
}
else {
var vehicleResponse = _getVehicle(result['ns2:SmartphoneGetVehicleInfoResponse']['ns4:SmartphoneLatestBatteryStatusResponse']);
vehicleResponse._full = result;
callback(null, vehicleResponse);
}
});
};
/**
* Tell Carwings to signal the car to start the climate control system
* at the specified datetime (pass null for the date argument to start
* the climate control immediately). This method returns no data.
* It will take anywhere from 30 seconds to 5 minutes for Carwings
* to process this request.
*
* @param vin
* @param date
* @param callback
* @api public
*/
exports.startClimateControl = function(vin, date, callback) {
var payload = ['ns4:SmartphoneRemoteACTimerRequest', {
_attr: {
'xmlns:ns4' : 'urn:com:airbiquity:smartphone.vehicleservice:v1',
'xmlns:ns3' : 'urn:com:hitachi:gdc:type:vehicle:v1',
'xmlns:ns2' : 'urn:com:hitachi:gdc:type:portalcommon:v1' },
'ns3:ACRemoteRequest' : {
'ns3:VehicleServiceRequestHeader': {
'ns2:VIN': vin },
'ns3:NewACRemoteRequest': {
'ns3:ExecuteTime': (date || new Date()).toISOString() }
}
}];
_post('vehicleService', payload, callback);
}
/**
* Tell Carwings to signal the car to stop the climate control system.
* This method returns no data. It will take anywhere from 30 seconds
* to 5 minutes for Carwings to process this request.
*
* @param vin
* @param callback
* @api public
*/
exports.stopClimateControl = function(vin, callback) {
var payload = ['ns4:SmartphoneRemoteACOffRequest', {
_attr: {
'xmlns:ns4' : 'urn:com:airbiquity:smartphone.vehicleservice:v1',
'xmlns:ns3' : 'urn:com:hitachi:gdc:type:vehicle:v1',
'xmlns:ns2' : 'urn:com:hitachi:gdc:type:portalcommon:v1' },
'ns3:ACRemoteOffRequest' : {
'ns3:VehicleServiceRequestHeader': {
'ns2:VIN': vin }
}
}];
_post('vehicleService', payload, callback);
}