incyclist-services 1.4.5 → 1.4.6

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.
@@ -14,6 +14,7 @@ export declare class DeviceRideService extends IncyclistService {
14
14
  protected originalMode: CyclingMode;
15
15
  protected deviceDataHandler: any;
16
16
  protected lazyInitDone: boolean;
17
+ protected lastDataInfo: Record<string, number>;
17
18
  constructor();
18
19
  protected waitForInit(): Promise<void>;
19
20
  lazyInit(): Promise<void>;
@@ -36,6 +37,8 @@ export declare class DeviceRideService extends IncyclistService {
36
37
  private handleStartFailure;
37
38
  private handleStartSuccess;
38
39
  startHealthCheck(ai: AdapterRideInfo): void;
40
+ private updateOnDatahandler;
41
+ private removeOnDatahandler;
39
42
  stopHealthCheck(ai: AdapterRideInfo): void;
40
43
  prepareReconnect(unhealthy: AdapterRideInfo): Promise<void>;
41
44
  private reconnectInterface;
@@ -55,6 +58,10 @@ export declare class DeviceRideService extends IncyclistService {
55
58
  resume(): void;
56
59
  protected verifySelected(selectedDevices: any, capability: IncyclistCapability): void;
57
60
  onData(deviceSettings: DeviceSettings, data: DeviceData): void;
61
+ protected registerData(adapterInfo: AdapterRideInfo, data: DeviceData): void;
62
+ protected getLastDataTS(adapterInfo: AdapterRideInfo): number;
63
+ protected setLastDataTS(adapterInfo: AdapterRideInfo, ts: number): number;
64
+ protected clearLastDataTS(adapterInfo: AdapterRideInfo): void;
58
65
  private getEnabledCapabilities;
59
66
  isUpdateBusy(): boolean;
60
67
  waitForUpdateFinish(): Promise<void>;
@@ -81,6 +81,7 @@ let DeviceRideService = (() => {
81
81
  this.initizialized = __runInitializers(this, _instanceExtraInitializers);
82
82
  this.data = {};
83
83
  this.deviceDataHandler = this.onData.bind(this);
84
+ this.lastDataInfo = {};
84
85
  this.initizialized = false;
85
86
  this.simulatorEnforced = false;
86
87
  this.lazyInitDone = false;
@@ -533,29 +534,28 @@ let DeviceRideService = (() => {
533
534
  yield ai.adapter.pause().catch(console.log);
534
535
  if (ai.adapter.isControllable())
535
536
  this.setSerialPortInUse(ai.adapter);
536
- if (startType === 'pair') {
537
- ai.adapter.on('data', this.deviceDataHandler);
538
- }
539
- else if (startType === 'start') {
537
+ if (startType === 'start') {
540
538
  this.startHealthCheck(ai);
541
539
  }
542
540
  ai.isStarted = true;
543
541
  });
544
542
  }
545
543
  startHealthCheck(ai) {
544
+ this.updateOnDatahandler(ai);
546
545
  const check = () => {
547
546
  if (!ai.ivToCheck)
548
547
  return;
549
548
  const tsNow = Date.now();
549
+ const tsLastData = this.getLastDataTS(ai);
550
550
  const isPaused = ai.adapter.isPaused();
551
551
  const prevStatus = ai.dataStatus;
552
- if (isPaused || !ai.tsLastData) {
553
- ai.tsLastData = tsNow;
552
+ if (isPaused || !tsLastData) {
553
+ this.setLastDataTS(ai, tsNow);
554
554
  ai.dataStatus = 'green';
555
555
  return;
556
556
  }
557
- const isAmber = (tsNow - ai.tsLastData) > NO_DATA_THRESHOLD;
558
- const isRed = (tsNow - ai.tsLastData) > UNHEALTHY_THRESHOLD;
557
+ const isAmber = (tsNow - tsLastData) > NO_DATA_THRESHOLD;
558
+ const isRed = (tsNow - tsLastData) > UNHEALTHY_THRESHOLD;
559
559
  ai.dataStatus = 'green';
560
560
  if (isAmber)
561
561
  ai.dataStatus = 'amber';
@@ -563,7 +563,7 @@ let DeviceRideService = (() => {
563
563
  ai.dataStatus = 'red';
564
564
  if (ai.isHealthy && (isAmber || isRed)) {
565
565
  ai.isHealthy = false;
566
- this.logEvent({ message: 'device unhealthy', device: ai.adapter.getUniqueName(), udid: ai.udid, noDataSince: (tsNow - ai.tsLastData), tsLastData: ai.tsLastData });
566
+ this.logEvent({ message: 'device unhealthy', device: ai.adapter.getUniqueName(), udid: ai.udid, noDataSince: (tsNow - tsLastData), tsLastData: new Date(tsLastData).toISOString() });
567
567
  this.prepareReconnect(ai);
568
568
  }
569
569
  else if (!ai.isHealthy && !isAmber && !isRed) {
@@ -583,11 +583,22 @@ let DeviceRideService = (() => {
583
583
  ai.ivToCheck = (0, timers_1.setInterval)(() => { check(); }, 1000);
584
584
  ai.isHealthy = true;
585
585
  }
586
+ updateOnDatahandler(ai) {
587
+ this.logEvent({ message: 'init health check', device: ai.adapter.getName(), udid: ai.udid });
588
+ ai.adapter.off('data', this.deviceDataHandler);
589
+ ai.adapter.on('data', this.deviceDataHandler);
590
+ }
591
+ removeOnDatahandler(ai) {
592
+ this.logEvent({ message: 'cleanup health check', device: ai.adapter.getName(), udid: ai.udid });
593
+ ai.adapter.off('data', this.deviceDataHandler);
594
+ }
586
595
  stopHealthCheck(ai) {
587
596
  if (ai.ivToCheck) {
588
597
  clearInterval(ai.ivToCheck);
589
598
  delete ai.ivToCheck;
590
599
  delete ai.isHealthy;
600
+ this.removeOnDatahandler(ai);
601
+ this.lastDataInfo = {};
591
602
  }
592
603
  }
593
604
  prepareReconnect(unhealthy) {
@@ -870,7 +881,7 @@ let DeviceRideService = (() => {
870
881
  const adapterInfo = adapters === null || adapters === void 0 ? void 0 : adapters.find(ai => ai.adapter.isEqual(deviceSettings));
871
882
  if (!adapterInfo)
872
883
  return;
873
- adapterInfo.tsLastData = data.timestamp || Date.now();
884
+ this.registerData(adapterInfo, data);
874
885
  adapters === null || adapters === void 0 ? void 0 : adapters.forEach(ai => ai.capabilities = ai.adapter.getCapabilities());
875
886
  const selectedDevices = config.getSelectedDevices();
876
887
  this.verifySelected(selectedDevices, incyclist_devices_1.IncyclistCapability.Speed);
@@ -909,6 +920,23 @@ let DeviceRideService = (() => {
909
920
  });
910
921
  }
911
922
  }
923
+ registerData(adapterInfo, data) {
924
+ var _a;
925
+ const udid = adapterInfo.udid;
926
+ this.lastDataInfo[udid] = (_a = data.timestamp) !== null && _a !== void 0 ? _a : Date.now();
927
+ }
928
+ getLastDataTS(adapterInfo) {
929
+ const udid = adapterInfo.udid;
930
+ return this.lastDataInfo[udid];
931
+ }
932
+ setLastDataTS(adapterInfo, ts) {
933
+ const udid = adapterInfo.udid;
934
+ return this.lastDataInfo[udid] = ts;
935
+ }
936
+ clearLastDataTS(adapterInfo) {
937
+ const udid = adapterInfo.udid;
938
+ delete this.lastDataInfo[udid];
939
+ }
912
940
  getEnabledCapabilities(adapterInfo, selected) {
913
941
  const adapters = this.getSelectedAdapters();
914
942
  const config = this.getDeviceConfiguration();
@@ -988,7 +1016,6 @@ let DeviceRideService = (() => {
988
1016
  toggleCyclingMode() {
989
1017
  return __awaiter(this, void 0, void 0, function* () {
990
1018
  var _a, _b;
991
- console.log('~~~ toggle CyclingMode');
992
1019
  if (!this.isToggleEnabled())
993
1020
  return;
994
1021
  const { adapter } = (_a = this.getControlAdapter()) !== null && _a !== void 0 ? _a : {};
@@ -1014,7 +1041,6 @@ let DeviceRideService = (() => {
1014
1041
  }
1015
1042
  resetCyclingMode() {
1016
1043
  return __awaiter(this, arguments, void 0, function* (sendInit = false) {
1017
- console.log('~~~ reset CyclingMode');
1018
1044
  try {
1019
1045
  const adapterInfo = this.getControlAdapter();
1020
1046
  if (!(adapterInfo === null || adapterInfo === void 0 ? void 0 : adapterInfo.adapter))
@@ -1043,7 +1069,6 @@ let DeviceRideService = (() => {
1043
1069
  }
1044
1070
  enforceERG() {
1045
1071
  return __awaiter(this, void 0, void 0, function* () {
1046
- console.log('~~~ enforce ERG');
1047
1072
  try {
1048
1073
  const adapters = this.getSelectedAdapters();
1049
1074
  const adapterInfo = adapters === null || adapters === void 0 ? void 0 : adapters.find(ai => ai.adapter.hasCapability(incyclist_devices_1.IncyclistCapability.Control));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-services",
3
- "version": "1.4.5",
3
+ "version": "1.4.6",
4
4
  "peerDependencies": {
5
5
  "gd-eventlog": "^0.1.26"
6
6
  },
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "axios": "^1.7.7",
45
- "incyclist-devices": "^2.3.6",
45
+ "incyclist-devices": "^2.3.7",
46
46
  "promise.any": "^2.0.6",
47
47
  "semver": "^7.6.3",
48
48
  "tcx-builder": "^1.1.1",