incyclist-services 1.0.67 → 1.0.68

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.
@@ -39,7 +39,7 @@ export declare class DeviceRideService extends EventEmitter {
39
39
  startAdapters(adapters: AdapterRideInfo[], startType: 'start' | 'check' | 'pair', props?: RideServiceDeviceProperties): Promise<boolean>;
40
40
  startHealthCheck(ai: AdapterRideInfo): void;
41
41
  stopHealthCheck(ai: AdapterRideInfo): void;
42
- prepareReconnect(ai: AdapterRideInfo): Promise<void>;
42
+ prepareReconnect(down: AdapterRideInfo): Promise<void>;
43
43
  start(props: RideServiceDeviceProperties): Promise<boolean>;
44
44
  startRetry(props: RideServiceDeviceProperties): Promise<boolean>;
45
45
  cancelStart(): Promise<boolean>;
@@ -482,29 +482,27 @@ class DeviceRideService extends events_1.default {
482
482
  return;
483
483
  }
484
484
  const prevStatus = ai.dataStatus;
485
- const isAmber = (tsNow - ai.tsLastData) < NO_DATA_THRESHOLD;
486
- const isRed = (tsNow - ai.tsLastData) < UNHEALTHY_THRESHOLD;
485
+ const isAmber = (tsNow - ai.tsLastData) > NO_DATA_THRESHOLD;
486
+ const isRed = (tsNow - ai.tsLastData) > UNHEALTHY_THRESHOLD;
487
487
  ai.dataStatus = 'green';
488
488
  if (isAmber)
489
489
  ai.dataStatus = 'amber';
490
490
  if (isRed)
491
491
  ai.dataStatus = 'red';
492
- if (ai.isHealthy && !isAmber) {
492
+ if (ai.isHealthy && isAmber) {
493
493
  ai.isHealthy = false;
494
494
  this.logEvent({ message: 'device unhealthy', device: ai.adapter.getUniqueName(), udid: ai.udid });
495
495
  const { enabledCapabilities } = this.getEnabledCapabilities(ai);
496
496
  this.emit('unhealthy', ai.udid, ai.dataStatus, enabledCapabilities);
497
+ console.log('~~~~ health check: prepare reconnect');
498
+ this.prepareReconnect(ai);
497
499
  }
498
- else if (!ai.isHealthy && isAmber) {
500
+ else if (!ai.isHealthy && !isAmber) {
499
501
  const { enabledCapabilities } = this.getEnabledCapabilities(ai);
500
502
  ai.isHealthy = true;
501
503
  this.logEvent({ message: 'device healthy', device: ai.adapter.getUniqueName(), udid: ai.udid });
502
504
  this.emit('healthy', ai.udid, ai.dataStatus, enabledCapabilities);
503
505
  }
504
- if ((isAmber || isRed) && prevStatus === 'green') {
505
- console.log('~~~~ health check: prepare reconnect');
506
- this.prepareReconnect(ai);
507
- }
508
506
  };
509
507
  ai.ivToCheck = (0, timers_1.setInterval)(check, 1000);
510
508
  ai.isHealthy = true;
@@ -516,18 +514,18 @@ class DeviceRideService extends events_1.default {
516
514
  delete ai.isHealthy;
517
515
  }
518
516
  }
519
- prepareReconnect(ai) {
517
+ prepareReconnect(down) {
520
518
  return __awaiter(this, void 0, void 0, function* () {
521
- if (ai.isRestarting)
519
+ if (down.isRestarting)
522
520
  return;
523
- console.log('~~~~ health check: wait 45s', ai.udid);
521
+ console.log('~~~~ health check: wait 45s', down.udid);
524
522
  yield (0, sleep_1.sleep)(UNHEALTHY_THRESHOLD - NO_DATA_THRESHOLD - 5000);
525
- console.log('~~~~ health check: check if still unhealthy', ai.udid, ai.isHealthy, ai.isRestarting);
526
- if (ai.isHealthy || ai.isRestarting)
523
+ console.log('~~~~ health check: check if still unhealthy', down.udid, down.isHealthy, down.isRestarting);
524
+ if (down.isHealthy || down.isRestarting)
527
525
  return;
528
- const ifName = ai.adapter.getInterface();
526
+ const ifName = down.adapter.getInterface();
529
527
  const adapters = this.getAdapterList().filter(ai => ai.adapter.getInterface() === ifName);
530
- ai.isRestarting = true;
528
+ down.isRestarting = true;
531
529
  if (!adapters.find(ai => ai.isHealthy)) {
532
530
  console.log('~~~~ health check: restart interface', ifName);
533
531
  this.logger.logEvent({ message: 'restart interface', interface: ifName });
@@ -555,24 +553,36 @@ class DeviceRideService extends events_1.default {
555
553
  this.logger.logEvent({ message: 'restart interface failed', interface: ifName, reason: err.message });
556
554
  }
557
555
  adapters.map(ai => {
556
+ if (ai.adapter.isStarted()) {
557
+ ai.isHealthy = true;
558
+ ai.dataStatus = 'green';
559
+ }
560
+ else {
561
+ ai.isHealthy = false;
562
+ ai.dataStatus = 'red';
563
+ this.prepareReconnect(ai);
564
+ }
558
565
  ai.adapter.on('data', this.deviceDataHandler);
559
566
  ai.isRestarting = false;
560
567
  });
561
568
  }
562
569
  else {
563
- console.log('~~~~ health check: restart adapter', ai.udid);
564
- this.logger.logEvent({ message: 'restart adapter', device: ai.udid });
565
- ai.adapter.off('data', this.deviceDataHandler);
566
- const adapter = ai.adapter;
570
+ console.log('~~~~ health check: restart adapter', down.udid);
571
+ this.logger.logEvent({ message: 'restart adapter', device: down.udid });
572
+ down.adapter.off('data', this.deviceDataHandler);
573
+ const adapter = down.adapter;
567
574
  try {
568
575
  yield adapter.restart();
576
+ down.isHealthy = true;
577
+ down.dataStatus = 'green';
569
578
  }
570
579
  catch (err) {
571
- this.logger.logEvent({ message: 'restart adapter failed', device: ai.udid, reason: err.message });
580
+ this.logger.logEvent({ message: 'restart adapter failed', device: down.udid, reason: err.message });
581
+ this.prepareReconnect(down);
572
582
  }
573
- ai.adapter.on('data', this.deviceDataHandler);
583
+ down.adapter.on('data', this.deviceDataHandler);
574
584
  }
575
- ai.isRestarting = false;
585
+ down.isRestarting = false;
576
586
  });
577
587
  }
578
588
  start(props) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-services",
3
- "version": "1.0.67",
3
+ "version": "1.0.68",
4
4
  "peerDependencies": {
5
5
  "gd-eventlog": "^0.1.26"
6
6
  },