-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
413 lines (392 loc) · 12.7 KB
/
Copy pathmain.js
File metadata and controls
413 lines (392 loc) · 12.7 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
'use strict';
/*
* Created with @iobroker/create-adapter v1.34.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const axios = require('axios').default;
const Json2iob = require('json2iob');
const crypto = require('crypto');
const qs = require('qs');
// Load your modules here, e.g.:
// const fs = require("fs");
class Niu extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'niu',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
this.deviceArray = [];
this.json2iob = new Json2iob(this);
this.requestClient = axios.create();
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
if (this.config.interval < 0.5) {
this.log.info('Set interval to minimum 0.5');
this.config.interval = 0.5;
}
if (!this.config.username || !this.config.password) {
this.log.error('Please set username and password in the instance settings');
return;
}
this.userAgent =
'manager/4.7.16 (iPhone; iOS 14.8; Scale/3.00);deviceName=iPhone;timezone=Europe/Berlin;model=iPhone;lang=de-DE;ostype=iOS;clientIdentifier=Overseas';
this.updateInterval = null;
this.reLoginTimeout = null;
this.refreshTokenTimeout = null;
this.session = {};
this.subscribeStates('*');
await this.login();
if (this.session.access_token) {
await this.getDeviceList();
await this.updateDevices();
this.updateInterval = setInterval(async () => {
await this.updateDevices();
}, this.config.interval * 60 * 1000);
this.refreshTokenInterval = setInterval(() => {
this.refreshToken();
}, 22 * 60 * 60 * 1000);
}
}
async login() {
await this.requestClient({
method: 'post',
url: 'https://account-fk.niu.com/v3/api/oauth2/token',
headers: {
accept: '*/*',
token: '',
'user-agent':
'manager/4.7.12 (iPhone; iOS 14.8; Scale/3.00);deviceName=iPhone;timezone=Europe/Berlin;model=iPhone 8 Plus;lang=de-DE;ostype=iOS;clientIdentifier=Overseas',
'accept-language': 'de-DE;q=1, uk-DE;q=0.9, en-DE;q=0.8',
},
data: qs.stringify({
account: this.config.username,
app_id: 'niu_fksss2ws',
grant_type: 'password',
password: crypto.createHash('md5').update(this.config.password).digest('hex'),
scope: 'base',
}),
})
.then((res) => {
this.log.debug(JSON.stringify(res.data));
if (res.data.data && res.data.data.token) {
this.session = res.data.data.token;
this.setState('info.connection', true, true);
return;
}
this.log.error(JSON.stringify(res.data));
})
.catch((error) => {
this.log.error(error);
if (error.response) {
this.log.error(JSON.stringify(error.response.data));
}
});
}
async getDeviceList() {
await this.requestClient({
method: 'get',
url: 'https://app-api-fk.niu.com/v5/scooter/list',
headers: {
accept: '*/*',
token: this.session.access_token,
'accept-language': 'de',
},
})
.then(async (res) => {
this.log.debug(JSON.stringify(res.data));
if (!res.data.data.items) {
this.log.error('No devices found');
return;
}
for (const device of res.data.data.items) {
let vin = device.sn_id; // Alternative datapoint for serial number
if (device.sn) {
vin = device.sn; // original serial number
}
this.deviceArray.push(vin);
let name = device.scooter_name;
if (device.sku_name) {
name += ' ' + device.sku_name;
}
await this.setObjectNotExistsAsync(vin, {
type: 'device',
common: {
name: name,
},
native: {},
});
await this.setObjectNotExistsAsync(vin + '.remote', {
type: 'channel',
common: {
name: 'Remote Controls',
},
native: {},
});
await this.setObjectNotExistsAsync(vin + '.general', {
type: 'channel',
common: {
name: 'General Information',
},
native: {},
});
const remoteArray = [{ command: 'Refresh', name: 'True = Refresh' }];
remoteArray.forEach((remote) => {
this.setObjectNotExists(vin + '.remote.' + remote.command, {
type: 'state',
common: {
name: remote.name || '',
type: remote.type || 'boolean',
role: remote.role || 'boolean',
write: true,
read: true,
},
native: {},
});
});
this.json2iob.parse(vin + '.general', device);
}
})
.catch((error) => {
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
}
async updateDevices() {
const statusArray = [
{
path: 'battery_info_health',
url: 'https://app-api-fk.niu.com/v3/motor_data/battery_info/health?sn=$vin',
desc: 'Status of the battery health',
},
{
path: 'battery_info',
url: 'https://app-api-fk.niu.com/v3/motor_data/battery_info?sn=$vin',
desc: 'Status of the battery info',
},
{
path: 'status',
url: 'https://app-api-fk.niu.com/v5/scooter/motor_data/index_info?sn=$vin',
desc: 'Status of the scooter',
},
{
path: 'scooter_info', // A lot of Infos about the scooter; e.g. position, battery, last track and much more
url: 'https://app-api-fk.niu.com/v3/motor_data/index_info?sn=$vin',
desc: 'More Information of the scooter',
},
{
path: 'track',
url: 'https://app-api-fk.niu.com/v5/track/list/v2',
desc: 'Tracks of the scooter',
method: 'post',
},
{
path: 'overallTally',
url: 'https://app-api-fk.niu.com/motoinfo/overallTally',
desc: 'overall Tally of the scooter',
method: 'post',
},
{
path: 'cycling_statistics.day', // daily statistics
url: 'https://app-api-fk.niu.com/v3/motor_data/cycling_statistics?sortby=1&sn=$vin',
desc: 'Cycling statistics by day of the scooter',
},
{
path: 'cycling_statistics.week', // weekly statistics
url: 'https://app-api-fk.niu.com/v3/motor_data/cycling_statistics?sortby=2&sn=$vin',
desc: 'Cycling statistics by week of the scooter',
},
{
path: 'cycling_statistics.month', // monthly statistics
url: 'https://app-api-fk.niu.com/v3/motor_data/cycling_statistics?sortby=3&sn=$vin',
desc: 'Cycling statistics by month of the scooter',
},
];
const headers = {
accept: '*/*',
token: this.session.access_token,
'accept-language': 'de',
'user-agent': this.userAgent,
};
for (const vin of this.deviceArray) {
for (const element of statusArray) {
const url = element.url.replace('$vin', vin);
const data = {
sn: vin,
index: '0',
token: this.session.access_token,
pagesize: 10,
};
await this.requestClient({
method: element.method || 'get',
url: url,
headers: headers,
data: data,
})
.then((res) => {
this.log.debug(JSON.stringify(res.data));
if (!res.data) {
return;
}
let data = res.data;
if (data.data) {
data = data.data;
}
if (element.path === 'battery_info') {
delete data.batteries.compartmentA;
}
const forceIndex = true;
const preferedArrayName = null;
this.json2iob.parse(vin + '.' + element.path, data, {
forceIndex: forceIndex,
preferedArrayName: preferedArrayName,
channelName: element.desc,
});
})
.catch((error) => {
if (error.response) {
if (error.response.status === 401) {
error.response && this.log.debug(JSON.stringify(error.response.data));
this.log.info(element.path + ' receive 401 error. Refresh Token in 60 seconds');
this.refreshTokenTimeout && clearTimeout(this.refreshTokenTimeout);
this.refreshTokenTimeout = setTimeout(() => {
this.refreshToken();
}, 1000 * 60);
return;
}
}
this.log.error(url);
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
}
}
}
async refreshToken() {
if (!this.session) {
this.log.error('No session found relogin');
await this.login();
return;
}
await this.requestClient({
method: 'post',
url: 'https://account-fk.niu.com/v3/api/oauth2/token',
headers: {
Accept: '*/*',
'Accept-Language': 'de',
'Content-Type': 'application/x-www-form-urlencoded',
},
data: qs.stringify({
app_id: 'niu_fksss2ws',
grant_type: 'refresh_token',
refresh_token: this.session.refresh_token,
}),
})
.then((res) => {
this.log.debug(JSON.stringify(res.data));
if (res.data.data && res.data.data.token) {
this.session = res.data.data.token;
this.setState('info.connection', true, true);
return;
} else {
this.log.warn('no refresh token found');
}
})
.catch((error) => {
this.log.error('refresh token failed');
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
this.log.error('Start relogin in 1min');
this.reLoginTimeout && clearTimeout(this.reLoginTimeout);
this.reLoginTimeout = setTimeout(() => {
this.login();
}, 1000 * 60 * 1);
});
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.setState('info.connection', false, true);
this.refreshTimeout && clearTimeout(this.refreshTimeout);
this.reLoginTimeout && clearTimeout(this.reLoginTimeout);
this.refreshTokenTimeout && clearTimeout(this.refreshTokenTimeout);
this.updateInterval && clearInterval(this.updateInterval);
this.refreshTokenInterval && clearInterval(this.refreshTokenInterval);
callback();
} catch (e) {
this.log.error('Error in onUnload: ' + e);
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (state) {
if (!state.ack) {
// const deviceId = id.split('.')[2];
const command = id.split('.')[4];
if (id.split('.')[3] !== 'remote') {
return;
}
if (command === 'Refresh') {
this.updateDevices();
}
const data = {};
this.log.debug(JSON.stringify(data));
await this.requestClient({
method: 'post',
url: '',
headers: {
accept: '*/*',
'content-type': 'application/json',
'accept-language': 'de',
authorization: 'Bearer ' + this.session.access_token,
},
data: data,
})
.then((res) => {
this.log.info(JSON.stringify(res.data));
return res.data;
})
.catch((error) => {
this.log.error(error);
if (error.response) {
this.log.error(JSON.stringify(error.response.data));
}
});
this.refreshTimeout && clearTimeout(this.refreshTimeout);
this.refreshTimeout = setTimeout(async () => {
await this.updateDevices();
}, 10 * 1000);
}
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Niu(options);
} else {
// otherwise start the instance directly
new Niu();
}