-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
545 lines (512 loc) · 15.2 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
'use strict';
const utils = require('@iobroker/adapter-core');
const fetch = require('fetch').fetchUrl;
const meaterUrl = 'https://public-api.cloud.meater.com/v1/devices';
const meaterUrlLogin = 'https://public-api.cloud.meater.com/v1/login';
class Meater extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'meater',
});
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.states = new Array();
this.path = new Array();
this.token = '';
this.statusCode = 0;
this.expire = 0;
this.updateTimer = 60;
this.loginFailure = false;
this.timeoutReadFromCloud = null;
this.timeoutLogin = null;
}
async onReady() {
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
// Subscribe to states
this.subscribeStates('update');
// Reset states (just to be safe)
//await this.setStateAsync('update', { val: false, ack: true });
// Connect to cloud
await this.login();
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
if (this.timeoutReadFromCloud) {
this.clearTimeout(this.timeoutReadFromCloud);
}
if (this.timeoutLogin) {
this.clearTimeout(this.timeoutLogin);
}
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (state && !state.ack) {
// The state was changed
const idParts = id.split('.');
try {
// trigger an update
if (idParts[2] === 'update' && state.val === true) {
this.log.debug('Update triggered manually');
this.readFromCloud();
//await this.setStateAsync('update', { val: false, ack: true });
}
} catch (error) {
this.log.error(`Got an error on handling state change: ${error}.`);
}
}
}
// Login
async login() {
if (this.config.username == '' || this.config.password == '') {
this.log.error('Credentials for cloud access missing. Please go to adapter settings.');
} else {
this.log.debug('Send login to Meater cloud');
try {
fetch(
meaterUrlLogin,
{
method: 'POST',
headers: { 'content-type': 'application/json' },
payload: JSON.stringify({
email: this.config.username,
password: this.config.password,
}),
},
async (error, response, result) => {
// Error handling
// Remark:
// response.status == Statuscode of fetch/webserver
// result.status == Statuscode of MEATER Cloud API
if (response && 'status' in response && response.status == 200) {
// Log received data
this.log.debug('result from login: ' + result);
// Save into states
await this.setStateAsync('rawData', result.toString(), true);
// Handle status code
await this.handleStatusCode(JSON.parse(result));
if (this.statusCode == 200) {
this.loginFailure = false;
this.token = JSON.parse(result).data.token;
await this.setStateAsync('info.token', this.token, true);
await this.setStateAsync('info.userId', JSON.parse(result).data.userId, true);
await this.setStateAsync('status', JSON.parse(result).status, true);
this.readFromCloud();
} else {
// If something went wrong...
this.loginFailure = true;
// try again in <updateTimer> seconds
this.timeoutLogin = this.setTimeout(() => {
this.login();
}, this.updateTimer * 1000);
}
} else {
this.updateTimer = 600; //sec
this.log.error(
`Failed reading data from cloud. This seems to be an error of MEATER Cloud server or missing internet connection. Try again in ${this.updateTimer} seconds.`,
);
if (error) {
this.log.debug('Got following error from server: ' + JSON.stringify(error));
}
this.log.debug('Got following response from server: ' + JSON.stringify(response));
// If something went wrong...
this.loginFailure = true;
// try again in <updateTimer> seconds
this.timeoutLogin = this.setTimeout(() => {
this.login();
}, this.updateTimer * 1000);
}
},
);
} catch (error) {
this.updateTimer = 600; //sec
this.log.error(
`Got an error while logging in: ${error}. This seems to be an unhandled error of the adapter. Try again in ${this.updateTimer} seconds.`,
);
// If something went wrong...
this.loginFailure = true;
// try again in <updateTimer> seconds
this.timeoutLogin = this.setTimeout(() => {
this.login();
}, this.updateTimer * 1000);
}
}
}
// Check and handle status code of API response
async handleStatusCode(jsonObj) {
this.statusCode = jsonObj.statusCode;
this.setStateAsync('statusCode', this.statusCode, true);
switch (this.statusCode) {
case 200: // OK
this.log.debug('Statuscode 200 --> OK');
this.setState('info.connection', true, true);
break;
case 400: // Bad Request
this.updateTimer = 600; //sec
this.log.warn(`Statuscode 400 --> Bad Request. Try again in ${this.updateTimer} seconds.`);
this.setState('info.connection', false, true);
break;
case 401: // Unauthorized
this.log.info('Statuscode 401 --> Unauthorized --> login');
this.setState('info.connection', false, true);
// If login went wrong raise updateTimer
if (this.loginFailure) {
this.updateTimer = 600; //sec
this.log.error(`Try again in ${this.updateTimer} seconds.`);
// login is selfcalling
} else {
await this.login();
}
break;
case 404: // Not Found
this.updateTimer = 600; //sec
this.log.warn(`Statuscode 404 --> Not Found. Try again in ${this.updateTimer} seconds.`);
this.setState('info.connection', false, true);
break;
case 429: // Too Many Requests
this.updateTimer = 600; //sec
this.log.warn(`Statuscode 429 --> Too Many Requests. Try again in ${this.updateTimer} seconds.`);
break;
case 500: // Internal Server Error
this.updateTimer = 600; //sec
this.log.warn(`Statuscode 500 --> Internal Server Error. Try again in ${this.updateTimer} seconds.`);
this.setState('info.connection', false, true);
break;
}
}
// Read data from Meater cloud
async readFromCloud() {
// clear timeout to prevent loop in case of call from login if last call failed
if (this.timeoutReadFromCloud) {
this.clearTimeout(this.timeoutReadFromCloud);
}
this.log.debug('fetch data from cloud');
try {
fetch(
meaterUrl,
{
headers: { Authorization: 'Bearer ' + this.token, 'Accept-Language': this.config.language },
},
async (error, response, result) => {
// Error handling
// Remark:
// response.status == Statuscode of fetch/webserver
// result.status == Statuscode of MEATER Cloud API
if (response && 'status' in response && response.status == 200) {
// Log received data
this.log.debug('result from readFromCloud: ' + result);
// Save states
this.setStateAsync('rawData', result.toString(), true);
this.setStateAsync('status', JSON.parse(result).status, true);
await this.handleStatusCode(JSON.parse(result));
if (this.statusCode == 200) {
await this.readDeviceData(JSON.parse(result));
}
} else {
this.updateTimer = 600; //sec
this.log.error(
`Failed reading data from cloud. This seems to be an error of MEATER Cloud server. Try again in ${this.updateTimer} seconds.`,
);
if (error) {
this.log.debug('got following error from server: ' + JSON.stringify(error));
}
this.log.debug('got following response from server: ' + JSON.stringify(response));
}
},
);
} catch (error) {
this.updateTimer = 600; //sec
this.log.error(
`Got an error while fetching data from cloud: ${error}. This seems to be an unhandled error of the adapter. Try again in ${this.updateTimer} seconds.`,
);
}
// If everthing is done run again in <updateTimer> seconds
this.timeoutReadFromCloud = this.setTimeout(() => {
this.readFromCloud();
}, this.updateTimer * 1000);
}
// Create new device
async createNewDevice(deviceName) {
// Create device
await this.setObjectNotExistsAsync(deviceName, {
type: 'device',
common: {
name: deviceName,
role: '',
},
native: {},
});
// Create state for last update
await this.setObjectNotExistsAsync(deviceName + '.last_update', {
type: 'state',
common: {
name: 'date/time of last transmitted value',
type: 'number',
role: 'date',
read: true,
write: false,
},
native: {},
});
// Create channel "temperature"
await this.setObjectNotExistsAsync(deviceName + '.temperature', {
type: 'channel',
common: {
name: 'temperature',
role: '',
},
native: {},
});
// Create state for internal temperature
await this.setObjectNotExistsAsync(deviceName + '.temperature.internal', {
type: 'state',
common: {
name: 'temperature of meat',
type: 'number',
unit: this.config.tempUnit,
role: 'value.temperature',
read: true,
write: false,
},
native: {},
});
// Create state for ambient temperature
await this.setObjectNotExistsAsync(deviceName + '.temperature.ambient', {
type: 'state',
common: {
name: 'temperature of ambient',
type: 'number',
unit: this.config.tempUnit,
role: 'value.temperature',
read: true,
write: false,
},
native: {},
});
// Create state for target temperature
await this.setObjectNotExistsAsync(deviceName + '.temperature.target', {
type: 'state',
common: {
name: 'target temperature of cook session',
type: 'number',
unit: this.config.tempUnit,
role: 'value.temperature',
read: true,
write: false,
},
native: {},
});
// Create state for peak temperature
await this.setObjectNotExistsAsync(deviceName + '.temperature.peak', {
type: 'state',
common: {
name: 'peak temperature of cook session',
type: 'number',
unit: this.config.tempUnit,
role: 'value.temperature.max',
read: true,
write: false,
},
native: {},
});
// Create channel "cook"
await this.setObjectNotExistsAsync(deviceName + '.cook', {
type: 'channel',
common: {
name: 'cook',
role: '',
},
native: {},
});
// Create state for cook ID
await this.setObjectNotExistsAsync(deviceName + '.cook.id', {
type: 'state',
common: {
name: 'ID of cook session',
type: 'string',
role: 'state',
read: true,
write: false,
},
native: {},
});
// Create state for cook name
await this.setObjectNotExistsAsync(deviceName + '.cook.name', {
type: 'state',
common: {
name: 'name of selected meat',
type: 'string',
role: 'state',
read: true,
write: false,
},
native: {},
});
// Create state for cook state
await this.setObjectNotExistsAsync(deviceName + '.cook.state', {
type: 'state',
common: {
name: 'state of cook session',
type: 'string',
role: 'state',
read: true,
write: false,
},
native: {},
});
// Create state for elapsed time of cook session
await this.setObjectNotExistsAsync(deviceName + '.cook.time_elapsed', {
type: 'state',
common: {
name: 'elapsed time of cook session',
type: 'number',
unit: 'sec',
role: 'value.interval',
read: true,
write: false,
},
native: {},
});
// Create state for remaining time of cook session
await this.setObjectNotExistsAsync(deviceName + '.cook.time_remaining', {
type: 'state',
common: {
name: 'remaining time of cook session',
type: 'number',
unit: 'sec',
role: 'value.interval',
read: true,
write: false,
},
native: {},
});
}
// Read device data
async readDeviceData(jsonObj) {
// get exitsing devices
const devices = [];
const existingDevices = await this.getDevicesAsync();
for (const dev in existingDevices) {
devices.push(existingDevices[dev].common.name);
}
// cook states
let numCooking = 0;
// Set expire time of values
if (this.config.clearOldValues) {
this.expire = 2 * this.updateTimer;
}
// check if device data has been sent
if (jsonObj.data.devices !== 'undefined') {
this.log.debug('got device data from cloud');
// data from cloud
for (const dev in jsonObj.data.devices) {
const deviceData = jsonObj.data.devices[dev];
const deviceName = deviceData.id;
// Check if device allready exists or has to be created
if (!devices.includes(deviceName)) {
this.log.info('Found new probe --> creating device: ' + deviceName);
await this.createNewDevice(deviceName);
}
// save states
await this.setStateAsync(deviceName + '.last_update', {
val: deviceData.updated_at,
ack: true,
expire: this.expire,
});
await this.setStateAsync(deviceName + '.temperature.internal', {
val: deviceData.temperature.internal,
ack: true,
expire: this.expire,
});
await this.setStateAsync(deviceName + '.temperature.ambient', {
val: deviceData.temperature.ambient,
ack: true,
expire: this.expire,
});
// the following values will only appear while cooking is active
if (
deviceData.cook !== null &&
deviceData.cook !== 'undefined' &&
deviceData.cook !== '' &&
deviceData.cook.state !== null &&
deviceData.cook.state !== 'undefined' &&
deviceData.cook.state !== ''
) {
numCooking += 1;
await this.setStateAsync(deviceName + '.temperature.target', {
val: deviceData.cook.temperature.target,
ack: true,
expire: this.expire,
});
await this.setStateAsync(deviceName + '.temperature.peak', {
val: deviceData.cook.temperature.peak,
ack: true,
expire: this.expire,
});
await this.setStateAsync(deviceName + '.cook.id', {
val: deviceData.cook.id,
ack: true,
expire: this.expire,
});
await this.setStateAsync(deviceName + '.cook.name', {
val: deviceData.cook.name,
ack: true,
expire: this.expire,
});
await this.setStateAsync(deviceName + '.cook.state', {
val: deviceData.cook.state,
ack: true,
expire: this.expire,
});
await this.setStateAsync(deviceName + '.cook.time_elapsed', {
val: deviceData.cook.time.elapsed,
ack: true,
expire: this.expire,
});
await this.setStateAsync(deviceName + '.cook.time_remaining', {
val: deviceData.cook.time.remaining,
ack: true,
expire: this.expire,
});
}
}
} else {
this.log.debug('no device data from cloud has been sent');
}
// set updateTimer
if (numCooking > 0) {
this.updateTimer = this.config.updateCook;
this.setStateAsync('cookingActive', true, true);
} else {
this.updateTimer = this.config.updateIdle;
this.setStateAsync('cookingActive', false, true);
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Meater(options);
} else {
// otherwise start the instance directly
new Meater();
}