incyclist-services 1.0.66 → 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>;
@@ -478,31 +478,31 @@ class DeviceRideService extends events_1.default {
478
478
  const isPaused = ai.adapter.isPaused();
479
479
  if (isPaused || !ai.tsLastData) {
480
480
  ai.tsLastData = tsNow;
481
+ ai.dataStatus = 'green';
481
482
  return;
482
483
  }
483
484
  const prevStatus = ai.dataStatus;
484
- const isAmber = (tsNow - ai.tsLastData) < NO_DATA_THRESHOLD;
485
- const isRed = (tsNow - ai.tsLastData) < UNHEALTHY_THRESHOLD;
485
+ const isAmber = (tsNow - ai.tsLastData) > NO_DATA_THRESHOLD;
486
+ const isRed = (tsNow - ai.tsLastData) > UNHEALTHY_THRESHOLD;
486
487
  ai.dataStatus = 'green';
487
488
  if (isAmber)
488
489
  ai.dataStatus = 'amber';
489
490
  if (isRed)
490
491
  ai.dataStatus = 'red';
491
- if (ai.isHealthy && !isAmber) {
492
+ if (ai.isHealthy && isAmber) {
492
493
  ai.isHealthy = false;
493
494
  this.logEvent({ message: 'device unhealthy', device: ai.adapter.getUniqueName(), udid: ai.udid });
494
495
  const { enabledCapabilities } = this.getEnabledCapabilities(ai);
495
496
  this.emit('unhealthy', ai.udid, ai.dataStatus, enabledCapabilities);
497
+ console.log('~~~~ health check: prepare reconnect');
498
+ this.prepareReconnect(ai);
496
499
  }
497
- else if (!ai.isHealthy && isAmber) {
500
+ else if (!ai.isHealthy && !isAmber) {
498
501
  const { enabledCapabilities } = this.getEnabledCapabilities(ai);
499
502
  ai.isHealthy = true;
500
503
  this.logEvent({ message: 'device healthy', device: ai.adapter.getUniqueName(), udid: ai.udid });
501
504
  this.emit('healthy', ai.udid, ai.dataStatus, enabledCapabilities);
502
505
  }
503
- if (isAmber && prevStatus === 'green') {
504
- this.prepareReconnect(ai);
505
- }
506
506
  };
507
507
  ai.ivToCheck = (0, timers_1.setInterval)(check, 1000);
508
508
  ai.isHealthy = true;
@@ -514,17 +514,20 @@ class DeviceRideService extends events_1.default {
514
514
  delete ai.isHealthy;
515
515
  }
516
516
  }
517
- prepareReconnect(ai) {
517
+ prepareReconnect(down) {
518
518
  return __awaiter(this, void 0, void 0, function* () {
519
- if (ai.isRestarting)
519
+ if (down.isRestarting)
520
520
  return;
521
+ console.log('~~~~ health check: wait 45s', down.udid);
521
522
  yield (0, sleep_1.sleep)(UNHEALTHY_THRESHOLD - NO_DATA_THRESHOLD - 5000);
522
- 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)
523
525
  return;
524
- const ifName = ai.adapter.getInterface();
526
+ const ifName = down.adapter.getInterface();
525
527
  const adapters = this.getAdapterList().filter(ai => ai.adapter.getInterface() === ifName);
526
- ai.isRestarting = true;
528
+ down.isRestarting = true;
527
529
  if (!adapters.find(ai => ai.isHealthy)) {
530
+ console.log('~~~~ health check: restart interface', ifName);
528
531
  this.logger.logEvent({ message: 'restart interface', interface: ifName });
529
532
  const i = incyclist_devices_1.InterfaceFactory.create(ifName);
530
533
  try {
@@ -550,23 +553,36 @@ class DeviceRideService extends events_1.default {
550
553
  this.logger.logEvent({ message: 'restart interface failed', interface: ifName, reason: err.message });
551
554
  }
552
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
+ }
553
565
  ai.adapter.on('data', this.deviceDataHandler);
554
566
  ai.isRestarting = false;
555
567
  });
556
568
  }
557
569
  else {
558
- this.logger.logEvent({ message: 'restart adapter', device: ai.udid });
559
- ai.adapter.off('data', this.deviceDataHandler);
560
- 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;
561
574
  try {
562
575
  yield adapter.restart();
576
+ down.isHealthy = true;
577
+ down.dataStatus = 'green';
563
578
  }
564
579
  catch (err) {
565
- 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);
566
582
  }
567
- ai.adapter.on('data', this.deviceDataHandler);
583
+ down.adapter.on('data', this.deviceDataHandler);
568
584
  }
569
- ai.isRestarting = false;
585
+ down.isRestarting = false;
570
586
  });
571
587
  }
572
588
  start(props) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-services",
3
- "version": "1.0.66",
3
+ "version": "1.0.68",
4
4
  "peerDependencies": {
5
5
  "gd-eventlog": "^0.1.26"
6
6
  },