homebridge-tasmota-control 1.6.15-beta.9 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/deviceinfo.js CHANGED
@@ -3,7 +3,7 @@ import EventEmitter from 'events';
3
3
  import { ApiCommands, LightKeys, SensorKeys } from './constants.js';
4
4
 
5
5
  class DeviceInfo extends EventEmitter {
6
- constructor(url, auth, user, passwd, deviceName, loadNameFromDevice, enableDebugMode, refreshInterval) {
6
+ constructor(url, auth, user, passwd, deviceName, loadNameFromDevice, enableDebugMode) {
7
7
  super();
8
8
  this.name = deviceName
9
9
  this.loadNameFromDevice = loadNameFromDevice;
@@ -11,7 +11,6 @@ class DeviceInfo extends EventEmitter {
11
11
 
12
12
  //axios instance
13
13
  this.axiosInstance = axios.create({
14
- method: 'GET',
15
14
  baseURL: url,
16
15
  timeout: 10000,
17
16
  withCredentials: auth,
@@ -24,11 +23,11 @@ class DeviceInfo extends EventEmitter {
24
23
  }
25
24
 
26
25
  async getInfo() {
27
- const debug = this.enableDebugMode ? this.emit('debug', `Requesting info`) : false;
26
+ if (this.enableDebugMode) this.emit('debug', `Requesting info`);
28
27
  try {
29
- const deviceInfoData = await this.axiosInstance(ApiCommands.Status);
28
+ const deviceInfoData = await this.axiosInstance.get(ApiCommands.Status);
30
29
  const deviceInfo = deviceInfoData.data ?? {};
31
- const debug = this.enableDebugMode ? this.emit('debug', `Info: ${JSON.stringify(deviceInfo, null, 2)}`) : false;
30
+ if (this.enableDebugMode) this.emit('debug', `Info: ${JSON.stringify(deviceInfo, null, 2)}`);
32
31
  await new Promise(resolve => setTimeout(resolve, 250));
33
32
 
34
33
  //status
package/src/fans.js CHANGED
@@ -1,7 +1,7 @@
1
- import { promises as fsPromises } from 'fs';
2
1
  import axios from 'axios';
3
2
  import EventEmitter from 'events';
4
3
  import ImpulseGenerator from './impulsegenerator.js';
4
+ import Functions from './functions.js';
5
5
  import { ApiCommands } from './constants.js';
6
6
  let Accessory, Characteristic, Service, Categories, AccessoryUUID;
7
7
 
@@ -27,16 +27,13 @@ class Fans extends EventEmitter {
27
27
  this.disableLogInfo = config.disableLogInfo || false;
28
28
  this.disableLogDeviceInfo = config.disableLogDeviceInfo || false;
29
29
  this.refreshInterval = refreshInterval;
30
-
31
- //variable
32
- this.startPrepareAccessory = true;
30
+ this.functions = new Functions();
33
31
 
34
32
  //axios instance
35
33
  const url = `http://${config.host}/cm?cmnd=`;
36
34
  this.axiosInstance = axios.create({
37
- method: 'GET',
38
35
  baseURL: url,
39
- timeout: 6000,
36
+ timeout: 15000,
40
37
  withCredentials: config.auth,
41
38
  auth: {
42
39
  username: config.user,
@@ -44,38 +41,45 @@ class Fans extends EventEmitter {
44
41
  }
45
42
  });
46
43
 
47
- //impulse generator
48
- this.call = false;
49
- this.impulseGenerator = new ImpulseGenerator();
50
- this.impulseGenerator.on('checkDeviceState', async () => {
51
- if (this.call) return;
52
-
53
- try {
54
- this.call = true;
55
- await this.checkDeviceState();
56
- this.call = false;
57
- } catch (error) {
58
- this.call = false;
59
- this.emit('error', `Inpulse generator error: ${error}`);
60
- };
61
- }).on('state', (state) => {
62
- const emitState = state ? this.emit('success', `Impulse generator started`) : this.emit('warn', `Impulse generator stopped`);
63
- });
44
+ //lock flags
45
+ this.locks = {
46
+ checkState: false,
47
+ };
48
+ this.impulseGenerator = new ImpulseGenerator()
49
+ .on('checkState', () => this.handleWithLock('checkState', async () => {
50
+ await this.checkState();
51
+ }))
52
+ .on('state', (state) => {
53
+ this.emit('success', `Impulse generator ${state ? 'started' : 'stopped'}.`);
54
+ });
55
+ }
56
+
57
+ async handleWithLock(lockKey, fn) {
58
+ if (this.locks[lockKey]) return;
59
+
60
+ this.locks[lockKey] = true;
61
+ try {
62
+ await fn();
63
+ } catch (error) {
64
+ this.emit('error', `Inpulse generator error: ${error}`);
65
+ } finally {
66
+ this.locks[lockKey] = false;
67
+ }
64
68
  }
65
69
 
66
- async checkDeviceState() {
67
- const debug = this.enableDebugMode ? this.emit('debug', `Requesting status`) : false;
70
+ async checkState() {
71
+ if (this.enableDebugMode) this.emit('debug', `Requesting status`);
68
72
  try {
69
73
  //power status
70
- const powerStatusData = await this.axiosInstance(ApiCommands.PowerStatus);
74
+ const powerStatusData = await this.axiosInstance.get(ApiCommands.PowerStatus);
71
75
  const powerStatus = powerStatusData.data ?? {};
72
76
  const powerStatusKeys = Object.keys(powerStatus);
73
- const debug = this.enableDebugMode ? this.emit('debug', `Power status: ${JSON.stringify(powerStatus, null, 2)}`) : false;
77
+ if (this.enableDebugMode) this.emit('debug', `Power status: ${JSON.stringify(powerStatus, null, 2)}`);
74
78
 
75
79
  //sensor status
76
- const sensorStatusData = await this.axiosInstance(ApiCommands.Status);
80
+ const sensorStatusData = await this.axiosInstance.get(ApiCommands.Status);
77
81
  const sensorStatus = sensorStatusData.data ?? {};
78
- const debug1 = this.enableDebugMode ? this.emit('debug', `Sensors status: ${JSON.stringify(sensorStatus, null, 2)}`) : false;
82
+ if (this.enableDebugMode) this.emit('debug', `Sensors status: ${JSON.stringify(sensorStatus, null, 2)}`);
79
83
 
80
84
  //sensor status keys
81
85
  const sensorStatusKeys = Object.keys(sensorStatus);
@@ -154,30 +158,10 @@ class Fans extends EventEmitter {
154
158
  }
155
159
  }
156
160
 
157
- async saveData(path, data) {
158
- try {
159
- data = JSON.stringify(data, null, 2);
160
- await fsPromises.writeFile(path, data);
161
- const debug = !this.enableDebugMode ? false : this.emit('debug', `Saved data: ${data}`);
162
- return true;
163
- } catch (error) {
164
- throw new Error(`Save data error: ${error}`);
165
- }
166
- }
167
-
168
- async readData(path) {
169
- try {
170
- const data = await fsPromises.readFile(path);
171
- return data;
172
- } catch (error) {
173
- throw new Error(`Read data error: ${error}`);
174
- }
175
- }
176
-
177
161
  async startImpulseGenerator() {
178
162
  try {
179
163
  //start impulse generator
180
- const timers = [{ name: 'checkDeviceState', sampling: this.refreshInterval }];
164
+ const timers = [{ name: 'checkState', sampling: this.refreshInterval }];
181
165
  await this.impulseGenerator.start(timers);
182
166
  return true;
183
167
  } catch (error) {
@@ -198,7 +182,7 @@ class Fans extends EventEmitter {
198
182
 
199
183
  //prepare accessory
200
184
  async prepareAccessory() {
201
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Accessory`) : false;
185
+ if (this.enableDebugMode) this.emit('debug', `Prepare Accessory`);
202
186
 
203
187
  try {
204
188
  //accessory
@@ -208,7 +192,7 @@ class Fans extends EventEmitter {
208
192
  const accessory = new Accessory(accessoryName, accessoryUUID, accessoryCategory);
209
193
 
210
194
  //Prepare information service
211
- const debug1 = this.enableDebugMode ? this.emit('debug', `Prepare Information Service`) : false;
195
+ if (this.enableDebugMode) this.emit('debug', `Prepare Information Service`);
212
196
  accessory.getService(Service.AccessoryInformation)
213
197
  .setCharacteristic(Characteristic.Manufacturer, 'Tasmota')
214
198
  .setCharacteristic(Characteristic.Model, this.info.modelName ?? 'Model Name')
@@ -217,9 +201,9 @@ class Fans extends EventEmitter {
217
201
  .setCharacteristic(Characteristic.ConfiguredName, accessoryName);
218
202
 
219
203
  //Prepare services
220
- const debug2 = this.enableDebugMode ? this.emit('debug', `Prepare Services`) : false;
204
+ if (this.enableDebugMode) this.emit('debug', `Prepare Services`);
221
205
  if (this.fans.length > 0) {
222
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Fan Services`) : false;
206
+ if (this.enableDebugMode) this.emit('debug', `Prepare Fan Services`);
223
207
  this.fanServices = [];
224
208
 
225
209
  for (let i = 0; i < this.fans.length; i++) {
@@ -238,8 +222,8 @@ class Fans extends EventEmitter {
238
222
  try {
239
223
  state = state ? 1 : 0;
240
224
  const speed = `${ApiCommands.FanSpeed}${state}`;
241
- await this.axiosInstance(speed);
242
- const logInfo = this.disableLogInfo ? false : this.emit('info', `${friendlyName}, set state: ${state ? 'ON' : 'OFF'}`);
225
+ await this.axiosInstance.get(speed);
226
+ if (!this.disableLogInfo) this.emit('info', `${friendlyName}, set state: ${state ? 'ON' : 'OFF'}`);
243
227
  } catch (error) {
244
228
  this.emit('warn', `${friendlyName}, set state error: ${error}`);
245
229
  }
@@ -252,8 +236,8 @@ class Fans extends EventEmitter {
252
236
  // .onSet(async (value) => {
253
237
  // try {
254
238
  // const direction = `${ApiCommands.FanDirection}${value}`;
255
- // await this.axiosInstance(direction);
256
- // const logInfo = this.disableLogInfo ? false : this.emit('info', `${friendlyName}, set direction: ${value}`);
239
+ // await this.axiosInstance.get(direction);
240
+ // if (!this.disableLogInfo) this.emit('info', `${friendlyName}, set direction: ${value}`);
257
241
  // } catch (error) {
258
242
  // this.emit('warn', `${friendlyName}, set direction error: ${error}`);
259
243
  // }
@@ -271,8 +255,8 @@ class Fans extends EventEmitter {
271
255
  .onSet(async (value) => {
272
256
  try {
273
257
  const speed = `${ApiCommands.FanSpeed}${value}`;
274
- await this.axiosInstance(speed);
275
- const logInfo = this.disableLogInfo ? false : this.emit('info', `${friendlyName}, set speed: ${value}`);
258
+ await this.axiosInstance.get(speed);
259
+ if (!this.disableLogInfo) this.emit('info', `${friendlyName}, set speed: ${value}`);
276
260
  } catch (error) {
277
261
  this.emit('warn', `${friendlyName}, set rotation speed error: ${error}`);
278
262
  }
@@ -301,8 +285,8 @@ class Fans extends EventEmitter {
301
285
  const powerOn = this.lights.length === 1 ? (this.lights[i].power1 ? `${ApiCommands.Power}${relayNr}${ApiCommands.On}` : ApiCommands.PowerOn) : `${ApiCommands.Power}${relayNr}${ApiCommands.On}`;
302
286
  const powerOff = this.lights.length === 1 ? (this.lights[i].power1 ? `${ApiCommands.Power}${relayNr}${ApiCommands.Off}` : ApiCommands.PowerOff) : `${ApiCommands.Power}${relayNr}${ApiCommands.Off}`;
303
287
  state = state ? powerOn : powerOff;
304
- await this.axiosInstance(state);
305
- const logInfo = this.disableLogInfo ? false : this.emit('info', `${friendlyName}, set state: ${state ? 'ON' : 'OFF'}`);
288
+ await this.axiosInstance.get(state);
289
+ if (!this.disableLogInfo) this.emit('info', `${friendlyName}, set state: ${state ? 'ON' : 'OFF'}`);
306
290
  } catch (error) {
307
291
  this.emit('warn', `${friendlyName}, set state error: ${error}`);
308
292
  }
@@ -313,12 +297,12 @@ class Fans extends EventEmitter {
313
297
 
314
298
  //sensors
315
299
  if (this.sensorsCount > 0) {
316
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Sensor Services`) : false;
300
+ if (this.enableDebugMode) this.emit('debug', `Prepare Sensor Services`);
317
301
 
318
302
  //temperature
319
303
  const sensorsTemperatureCount = this.sensorsTemperatureCount;
320
304
  if (sensorsTemperatureCount > 0) {
321
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Temperature Sensor Services`) : false;
305
+ if (this.enableDebugMode) this.emit('debug', `Prepare Temperature Sensor Services`);
322
306
  this.sensorTemperatureServices = [];
323
307
  for (let i = 0; i < sensorsTemperatureCount; i++) {
324
308
  const sensorName = this.sensorsName[i];
@@ -329,7 +313,7 @@ class Fans extends EventEmitter {
329
313
  sensorTemperatureService.getCharacteristic(Characteristic.CurrentTemperature)
330
314
  .onGet(async () => {
331
315
  const value = this.sensorsTemperature[i];
332
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} temperature: ${value} °${this.tempUnit}`);
316
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} temperature: ${value} °${this.tempUnit}`);
333
317
  return value;
334
318
  });
335
319
  this.sensorTemperatureServices.push(sensorTemperatureService);
@@ -339,7 +323,7 @@ class Fans extends EventEmitter {
339
323
  //reference temperature
340
324
  const sensorsReferenceTemperatureCount = this.sensorsReferenceTemperatureCount;
341
325
  if (sensorsReferenceTemperatureCount > 0) {
342
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Reference Temperature Sensor Services`) : false;
326
+ if (this.enableDebugMode) this.emit('debug', `Prepare Reference Temperature Sensor Services`);
343
327
  this.sensorReferenceTemperatureServices = [];
344
328
  for (let i = 0; i < sensorsReferenceTemperatureCount; i++) {
345
329
  const sensorName = this.sensorsName[i];
@@ -350,7 +334,7 @@ class Fans extends EventEmitter {
350
334
  sensorReferenceTemperatureService.getCharacteristic(Characteristic.CurrentTemperature)
351
335
  .onGet(async () => {
352
336
  const value = this.sensorsReferenceTemperature[i];
353
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} reference temperature: ${value} °${this.tempUnit}`);
337
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} reference temperature: ${value} °${this.tempUnit}`);
354
338
  return value;
355
339
  });
356
340
  this.sensorReferenceTemperatureServices.push(sensorReferenceTemperatureService);
@@ -360,7 +344,7 @@ class Fans extends EventEmitter {
360
344
  //object temperature
361
345
  const sensorsObjTemperatureCount = this.sensorsObjTemperatureCount;
362
346
  if (sensorsObjTemperatureCount > 0) {
363
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Obj Temperature Sensor Services`) : false;
347
+ if (this.enableDebugMode) this.emit('debug', `Prepare Obj Temperature Sensor Services`);
364
348
  this.sensorObjTemperatureServices = [];
365
349
  for (let i = 0; i < sensorsObjTemperatureCount; i++) {
366
350
  const sensorName = this.sensorsName[i];
@@ -371,7 +355,7 @@ class Fans extends EventEmitter {
371
355
  sensorObjTemperatureService.getCharacteristic(Characteristic.CurrentTemperature)
372
356
  .onGet(async () => {
373
357
  const value = this.sensorsObjTemperature[i];
374
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} obj temperature: ${value} °${this.tempUnit}`);
358
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} obj temperature: ${value} °${this.tempUnit}`);
375
359
  return value;
376
360
  });
377
361
  this.sensorObjTemperatureServices.push(sensorObjTemperatureService);
@@ -381,7 +365,7 @@ class Fans extends EventEmitter {
381
365
  //ambient temperature
382
366
  const sensorsAmbTemperatureCount = this.sensorsAmbTemperatureCount;
383
367
  if (sensorsAmbTemperatureCount > 0) {
384
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Amb Temperature Sensor Services`) : false;
368
+ if (this.enableDebugMode) this.emit('debug', `Prepare Amb Temperature Sensor Services`);
385
369
  this.sensorAmbTemperatureServices = [];
386
370
  for (let i = 0; i < sensorsAmbTemperatureCount; i++) {
387
371
  const sensorName = this.sensorsName[i];
@@ -392,7 +376,7 @@ class Fans extends EventEmitter {
392
376
  sensorAmbTemperatureService.getCharacteristic(Characteristic.CurrentTemperature)
393
377
  .onGet(async () => {
394
378
  const value = this.sensorsAmbTemperature[i];
395
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} amb temperature: ${value} °${this.tempUnit}`);
379
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} amb temperature: ${value} °${this.tempUnit}`);
396
380
  return value;
397
381
  });
398
382
  this.sensorAmbTemperatureServices.push(sensorAmbTemperatureService);
@@ -402,7 +386,7 @@ class Fans extends EventEmitter {
402
386
  //dew point temperature
403
387
  const sensorsDewPointTemperatureCount = this.sensorsDewPointTemperatureCount;
404
388
  if (sensorsDewPointTemperatureCount > 0) {
405
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Dew Point Temperature Sensor Services`) : false;
389
+ if (this.enableDebugMode) this.emit('debug', `Prepare Dew Point Temperature Sensor Services`);
406
390
  this.sensorDewPointTemperatureServices = [];
407
391
  for (let i = 0; i < sensorsDewPointTemperatureCount; i++) {
408
392
  const sensorName = this.sensorsName[i];
@@ -413,7 +397,7 @@ class Fans extends EventEmitter {
413
397
  sensorDewPointTemperatureService.getCharacteristic(Characteristic.CurrentTemperature)
414
398
  .onGet(async () => {
415
399
  const value = this.sensorsDewPointTemperature[i];
416
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} dew point: ${value} °${this.tempUnit}`);
400
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} dew point: ${value} °${this.tempUnit}`);
417
401
  return value;
418
402
  });
419
403
  this.sensorDewPointTemperatureServices.push(sensorDewPointTemperatureService);
@@ -423,7 +407,7 @@ class Fans extends EventEmitter {
423
407
  //humidity
424
408
  const sensorsHumidityCount = this.sensorsHumidityCount;
425
409
  if (sensorsHumidityCount > 0) {
426
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Humidity Sensor Services`) : false;
410
+ if (this.enableDebugMode) this.emit('debug', `Prepare Humidity Sensor Services`);
427
411
  this.sensorHumidityServices = [];
428
412
  for (let i = 0; i < sensorsHumidityCount; i++) {
429
413
  const sensorName = this.sensorsName[i];
@@ -434,7 +418,7 @@ class Fans extends EventEmitter {
434
418
  sensorHumidityService.getCharacteristic(Characteristic.CurrentRelativeHumidity)
435
419
  .onGet(async () => {
436
420
  const value = this.sensorsHumidity[i];
437
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} humidity: ${value} %`);
421
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} humidity: ${value} %`);
438
422
  return value;
439
423
  });
440
424
  this.sensorHumidityServices.push(sensorHumidityService);
@@ -448,7 +432,7 @@ class Fans extends EventEmitter {
448
432
  //carbon dioxyde
449
433
  const sensorsCarbonDioxydeCount = this.sensorsCarbonDioxydeCount;
450
434
  if (sensorsCarbonDioxydeCount > 0) {
451
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Carbon Dioxyde Sensor Services`) : false;
435
+ if (this.enableDebugMode) this.emit('debug', `Prepare Carbon Dioxyde Sensor Services`);
452
436
  this.sensorCarbonDioxydeServices = [];
453
437
  for (let i = 0; i < sensorsCarbonDioxydeCount; i++) {
454
438
  const sensorName = this.sensorsName[i];
@@ -459,19 +443,19 @@ class Fans extends EventEmitter {
459
443
  sensorCarbonDioxydeService.getCharacteristic(Characteristic.CarbonDioxideDetected)
460
444
  .onGet(async () => {
461
445
  const state = this.sensorsCarbonDioxyde[i] > 1000;
462
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} carbon dioxyde detected: ${state ? 'Yes' : 'No'}`);
446
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} carbon dioxyde detected: ${state ? 'Yes' : 'No'}`);
463
447
  return state;
464
448
  });
465
449
  sensorCarbonDioxydeService.getCharacteristic(Characteristic.CarbonDioxideLevel)
466
450
  .onGet(async () => {
467
451
  const value = this.sensorsCarbonDioxyde[i];
468
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} carbon dioxyde level: ${value} ppm`);
452
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} carbon dioxyde level: ${value} ppm`);
469
453
  return value;
470
454
  });
471
455
  sensorCarbonDioxydeService.getCharacteristic(Characteristic.CarbonDioxidePeakLevel)
472
456
  .onGet(async () => {
473
457
  const value = this.sensorsCarbonDioxyde[i];
474
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} carbon dioxyde peak level: ${value} ppm`);
458
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} carbon dioxyde peak level: ${value} ppm`);
475
459
  return value;
476
460
  });
477
461
  this.sensorCarbonDioxydeServices.push(sensorCarbonDioxydeService);
@@ -481,7 +465,7 @@ class Fans extends EventEmitter {
481
465
  //ambient light
482
466
  const sensorsAmbientLightCount = this.sensorsAmbientLightCount;
483
467
  if (sensorsAmbientLightCount > 0) {
484
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Ambient Light Sensor Services`) : false;
468
+ if (this.enableDebugMode) this.emit('debug', `Prepare Ambient Light Sensor Services`);
485
469
  this.sensorAmbientLightServices = [];
486
470
  for (let i = 0; i < sensorsAmbientLightCount; i++) {
487
471
  const sensorName = this.sensorsName[i];
@@ -492,7 +476,7 @@ class Fans extends EventEmitter {
492
476
  sensorAmbientLightService.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
493
477
  .onGet(async () => {
494
478
  const value = this.sensorsAmbientLight[i];
495
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} ambient light: ${value} lx`);
479
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} ambient light: ${value} lx`);
496
480
  return value;
497
481
  });
498
482
  this.sensorAmbientLightServices.push(sensorAmbientLightService);
@@ -502,7 +486,7 @@ class Fans extends EventEmitter {
502
486
  //motion
503
487
  const sensorsMotionCount = this.sensorsMotionCount;
504
488
  if (sensorsMotionCount > 0) {
505
- const debug = this.enableDebugMode ? this.emit('debug', `Prepare Motion Sensor Services`) : false;
489
+ if (this.enableDebugMode) this.emit('debug', `Prepare Motion Sensor Services`);
506
490
  this.sensorMotionServices = [];
507
491
  for (let i = 0; i < sensorsMotionCount; i++) {
508
492
  const sensorName = this.sensorsName[i];
@@ -513,7 +497,7 @@ class Fans extends EventEmitter {
513
497
  sensorMotionService.getCharacteristic(Characteristic.MotionDetected)
514
498
  .onGet(async () => {
515
499
  const state = this.sensorsMotion[i];
516
- const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} motion: ${state ? 'ON' : 'OFF'}`);
500
+ if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} motion: ${state ? 'ON' : 'OFF'}`);
517
501
  return state;
518
502
  });
519
503
  this.sensorMotionServices.push(sensorMotionService);
@@ -531,22 +515,17 @@ class Fans extends EventEmitter {
531
515
  async start() {
532
516
  try {
533
517
  //check device state
534
- await this.checkDeviceState();
518
+ await this.checkState();
535
519
 
536
520
  //connect to deice success
537
521
  this.emit('success', `Connect Success`)
538
522
 
539
523
  //check device info
540
- const devInfo = !this.disableLogDeviceInfo ? await this.deviceInfo() : false;
524
+ if (!this.disableLogDeviceInfo) await this.deviceInfo();
541
525
 
542
526
  //start prepare accessory
543
- if (this.startPrepareAccessory) {
544
- const accessory = await this.prepareAccessory();
545
- const publishAccessory = this.emit('publishAccessory', accessory);
546
- this.startPrepareAccessory = false;
547
- }
548
-
549
- return true;
527
+ const accessory = await this.prepareAccessory();
528
+ return accessory;
550
529
  } catch (error) {
551
530
  throw new Error(`Start error: ${error}`);
552
531
  }
@@ -0,0 +1,31 @@
1
+ import { promises as fsPromises } from 'fs';
2
+
3
+ class Functions {
4
+ constructor(config) {
5
+ }
6
+
7
+ async saveData(path, data) {
8
+ try {
9
+ data = JSON.stringify(data, null, 2);
10
+ await fsPromises.writeFile(path, data);
11
+ return true;
12
+ } catch (error) {
13
+ throw new Error(`Save data error: ${error}`);
14
+ }
15
+ }
16
+
17
+ async readData(path) {
18
+ try {
19
+ const data = await fsPromises.readFile(path);
20
+ return data;
21
+ } catch (error) {
22
+ throw new Error(`Read data error: ${error}`);
23
+ }
24
+ }
25
+
26
+ async scaleValue(value, inMin, inMax, outMin, outMax) {
27
+ const scaledValue = parseFloat((((Math.max(inMin, Math.min(inMax, value)) - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin).toFixed(0));
28
+ return scaledValue;
29
+ }
30
+ }
31
+ export default Functions
@@ -4,6 +4,7 @@ class ImpulseGenerator extends EventEmitter {
4
4
  constructor() {
5
5
  super();
6
6
  this.timersState = false;
7
+ this.timers = [];
7
8
  }
8
9
 
9
10
  async start(timers) {
@@ -12,20 +13,19 @@ class ImpulseGenerator extends EventEmitter {
12
13
  return true;
13
14
  }
14
15
 
15
- //update state
16
- const updateState = timers.length > 0 ? this.state(true) : false;
17
-
18
- //add timers
19
16
  this.timers = [];
17
+
20
18
  for (const timer of timers) {
21
19
  this.emit(timer.name);
22
20
 
23
- const newTimer = setInterval(() => {
21
+ const interval = setInterval(() => {
24
22
  this.emit(timer.name);
25
23
  }, timer.sampling);
26
- this.timers.push(newTimer);
24
+
25
+ this.timers.push(interval);
27
26
  }
28
27
 
28
+ this.state(true);
29
29
  return true;
30
30
  }
31
31
 
@@ -35,12 +35,13 @@ class ImpulseGenerator extends EventEmitter {
35
35
  return true;
36
36
  }
37
37
 
38
- //update state
39
- this.timers.forEach(timer => clearInterval(timer));
40
- this.timers = [];
38
+ for (const timer of this.timers) {
39
+ clearInterval(timer);
40
+ }
41
41
 
42
+ this.timers = [];
42
43
  this.state(false);
43
- return true
44
+ return true;
44
45
  }
45
46
 
46
47
  state(state) {
@@ -48,4 +49,5 @@ class ImpulseGenerator extends EventEmitter {
48
49
  this.emit('state', state);
49
50
  }
50
51
  }
52
+
51
53
  export default ImpulseGenerator;