-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
executable file
·389 lines (337 loc) · 10.7 KB
/
main.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
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
"use strict";
/*
* Created with @iobroker/create-adapter v1.15.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");
// Custom modules
const uuid = require('uuid/v4');
// const util = require('util');
const http_request = require('request-promise-native');
// for debug url and randomness
const debug = false;
class Ryd extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "ryd",
});
this.on("ready", this.onReady.bind(this));
this.on("unload", this.onUnload.bind(this));
this._shutdown = false;
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// config
this._adapter_randomness = Math.floor(Math.random() * 60000 + 1); // add max 60 seconds randomness
this._adapter_timeout = parseInt(this.config.adapterTimeout, 10) || 10000;
this._adapter_delay = 10 * 60 * 1000; // in ms
// fallback
setTimeout(() => {
this.log.error("Adapter timeout reached. Stopping adapter now.");
this.stop(); // stop adapter right here (on schedule mode)
}, this._adapter_timeout + this._adapter_randomness);
this._think_properties = this.config.thinkProperties.split(',');
this._think_properties_ignore = this.config.thinkPropertiesIgnore.split(',');
if (!this.config.email) {
this.log.info("Setup missing: Email is empty. Stopping adapter now.");
this.stop();
return false;
} else if (!this.config.password) {
this.log.info("Setup missing: Password empty. Stopping adapter now.");
this.stop();
return false;
}
this._ryd_api_server = this.config.rydApiServer;
if(debug){
this._ryd_api_server = 'http://www.nemon.org/ryd'; // DEBUG
}
this._client_device_type = this.config.clientDeviceType;
this._client_device_id = this.config.clientDeviceId;
this._client_device_version = this.config.clientDeviceVersion;
this._client_device_resolution = this.config.clientDeviceResolution;
this._ryd_app_version = this.config.rydAppVersion;
this._ryd_app_locale = this.config.rydAppLocale;
this._ryd_app_platform = this._client_device_type + ' [' + this._client_device_id + ',' + this._client_device_version + ',' + this._client_device_resolution + ']';
this._ryd_app_user_agent = this.config.rydAppInternalName + '/' + this._ryd_app_version + '(' + this._client_device_id + '; ' + this._client_device_type + ' ' + this._client_device_version + ')';
try {
this._ryd_auth_token = (await this.getStateAsync('authToken')).val;
this._ryd_things = JSON.parse((await this.getStateAsync('rydThings')).val);
} catch (error) {
// do not stop adapter, will be resolved with login
this._resetUserStates();
this.log.debug("read user states:" + error);
}
this._base_request = http_request.defaults({
gzip: true,
timeout: 2000,
headers: {
'x-txn-platform': this._ryd_app_platform,
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': 0,
'x-txn-app-version': this._ryd_app_version,
'User-agent': this._ryd_app_user_agent,
'X-Txn-Request-Id': uuid(),
'X-Txn-Locale': this._ryd_app_locale,
'Content-Type': 'application/json; charset=utf-8'
}
})
this.log.info("Account: " + this.config.email);
this.log.debug("Adapter timeout: " + this._adapter_timeout + " ms");
this.log.debug("Think properties: " + this._think_properties);
this.log.debug("Think properties ignore: " + this._think_properties_ignore);
try {
const state = await this.getStateAsync('lastUpdate');
const now = new Date().getTime();
/*
if (now < (state.ts + this._adapter_delay)) {
this.log.info("Adapter ran less than " + (this._adapter_delay/1000) + " seconds earlier. Try it again later.");
this.stop(); // stop adapter right here (on schedule mode)
return false;
}
*/
} catch (e) {
// handle error here
}
await this._createUserStates();
this._queryRydServer();
}
async _createUserStates() {
await this.setObjectNotExistsAsync("authToken", {
type: "state",
common: {
name: "authToken",
type: "string",
role: "value",
read: true,
write: true,
},
native: {},
});
await this.setObjectNotExistsAsync("rydThings", {
type: "state",
common: {
name: "rydThings",
type: "json",
role: "value",
read: true,
write: true,
},
native: {},
});
await this.setObjectNotExistsAsync("lastUpdate", {
type: "state",
common: {
name: "lastUpdate",
type: "number",
role: "date",
read: true,
write: true,
},
native: {},
});
}
async _loginRydServer() {
this.log.debug("Trying to (re) log in");
try {
let response = await this._base_request({
method: 'POST',
// url: this._ryd_api_server + '/auth%2Flogin%2Flocal',
url: this._ryd_api_server + '/auth/login/local',
body: JSON.stringify({
email: this.config.email,
password: this.config.password
})
});
var userObj = JSON.parse(response);
} catch (error) {
await this._rydServerError(error);
return false;
}
this.log.debug("Login successful");
try {
this._ryd_auth_token = userObj.auth_token;
this._ryd_things = userObj.things;
this.log.debug("login: auth token: "+userObj.auth_token.substr(0,5) +", things: "+JSON.stringify(this._ryd_things),);
await this.setStateAsync("authToken", this._ryd_auth_token, true);
await this.setStateAsync("rydThings", JSON.stringify(this._ryd_things), true);
return true;
} catch (error) {
this._resetUserStates();
this._rydInternalError(error);
return false;
}
}
/**
* _queryRydServer
*/
async _queryRydServer() {
if(!debug){
this.log.debug("Adapter will wait " + this._adapter_randomness + " ms (to distribute server load)");
await this._sleep(this._adapter_randomness);
}
if ((this._ryd_auth_token == null || !(typeof this._ryd_auth_token == "string") || this._ryd_auth_token === "")
|| (this._ryd_things == null || this._ryd_things == undefined))
{
this.log.info("No auth token or things, triggering login");
await this._loginRydServer();
} else {
this.log.debug("Auth token and things present, continuing");
}
this.log.debug("_queryRydServer: auth token: " + this._ryd_auth_token.substr(0,5) + ", things:" + JSON.stringify(this._ryd_things));
// -- DEBUG
/*
this._ryd_things = [
{"id":"1","role":"THING_OWNER","type":"CAR"},
{"id":"2","role":"THING_OWNER","type":"CAR"},
{"id":"3","role":"THING_OWNER","type":"CAR"}
];
*/
this.log.debug('Things('+ this._ryd_things.length + '): ' + JSON.stringify(this._ryd_things));
let things_obj = [];
await Promise.all(this._ryd_things.map(async thing => {
let response = await this._base_request({
url: this._ryd_api_server + '/things/' + thing.id + '/status?auth_token=' + this._ryd_auth_token
});
let thing_obj = JSON.parse(response);
things_obj[thing.id] = thing_obj.data;
})).then(() => {
this._createThings(things_obj).then(() => {
this.setStateAsync("lastUpdate", new Date());
/**
try {
let response = this._base_request({
url: this._ryd_api_server + '/auth%2Flogout?auth_token=' + this._ryd_auth_token
});
this.log.debug("Done. Logout User.");
// this.log.debug(util.inspect(response));
} catch (error) {
this._rydServerError(error);
}
*/
this.stop(); // stop adapter right here (on shedule mode)
}).catch((error) => {
this._rydInternalError(error);
});
}).catch((error) => {
this._rydInternalError(error);
});
}
/**
* _createThings
*/
async _createThings(things_obj) {
for (let id in things_obj) {
// console.log("=== id " + id + " ===");
// console.log(things_obj[id]);
let thing_obj = things_obj[id];
for (let p in thing_obj) {
if (typeof thing_obj[p] !== 'object') {
// console.log('create things.' + id + '.' + p);
await this.setObjectNotExistsAsync('things.' + id + '.' + p, {
type: 'state',
common: {
'name': p,
'role': 'state',
'type': typeof thing_obj[p],
'write': false,
'read': true
},
native: {}
});
await this.setState('things.' + id + '.' + p, {val : thing_obj[p], ack : true});
} else {
let sub_thing_obj = thing_obj[p];
if (this._think_properties.includes(p)) {
for (let q in sub_thing_obj) {
// console.log("+ " + q);
await this.setObjectNotExistsAsync('things.' + id + '.' + p + '.' + q, {
type: 'state',
common: {
'name': p,
'role': 'state',
'type': typeof sub_thing_obj[q],
'write': false,
'read': true
},
native: {}
});
let value = (typeof sub_thing_obj[q] == 'object') ? JSON.stringify(sub_thing_obj[q]) : sub_thing_obj[q];
await this.setState('things.' + id + '.' + p + '.' + q, {val : value, ack : true});
}
} else {
if (!this._think_properties_ignore.includes(p)) {
this.log.debug('Unknown property: things.' + id + '.' + p);
this.log.debug(JSON.stringify(thing_obj[p]));
}
}
}
}
}
}
async _sleep(ms) {
return new Promise(resolve => setTimeout(() => resolve(), ms));
}
/**
* _rydServerError
*/
async _rydServerError(error) {
// console.log(error);
this.log.error('request (' + error.options.url + ') failed ' + error.name + ' (' + error.statusCode + ')');
if (error.statusCode == 401) {
this.log.error("Access denied. Please check Ryd username and password! Trying to login again next time");
await this._resetUserStates();
}
this.stop(); // stop adapter right here (on schedule mode)
}
/**
* _rydInternalError
*/
_rydInternalError(error) {
// console.log(error);
this.log.error(error);
this.stop(); // stop adapter right here (on schedule mode)
}
/**
* _resetUserStates
*/
async _resetUserStates() {
this.log.debug("resetting authToken and rydThings");
await this.setStateAsync("authToken", "", true);
await this.setStateAsync("rydThings", "", true);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
if (!this._shutdown) {
try {
this.log.debug(this.name + " stopped, cleaned everything up...");
this._shutdown = true;
clearTimeout();
callback();
} catch (e) {
callback();
}
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Ryd(options);
} else {
// otherwise start the instance directly
new Ryd();
}