incyclist-services 1.9.21 → 1.9.22

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.
Files changed (46) hide show
  1. package/lib/cjs/activities/base/repo/db.js +7 -1
  2. package/lib/cjs/devices/page/base/guidance/guidance.js +277 -16
  3. package/lib/cjs/devices/page/base/service.js +245 -16
  4. package/lib/cjs/devices/page/desktop/matrix.js +2 -1
  5. package/lib/cjs/devices/page/desktop/service.js +4 -11
  6. package/lib/cjs/devices/page/mobile/service.js +1 -0
  7. package/lib/cjs/devices/pairing/conflict.js +47 -0
  8. package/lib/cjs/devices/pairing/guidance.js +40 -4
  9. package/lib/cjs/devices/pairing/helper.js +3 -1
  10. package/lib/cjs/devices/pairing/index.js +1 -0
  11. package/lib/cjs/devices/pairing/service.js +228 -35
  12. package/lib/cjs/devices/pairing/status.js +1 -0
  13. package/lib/cjs/devices/ride/service.js +20 -4
  14. package/lib/cjs/devices/ride/timeouts.js +9 -0
  15. package/lib/cjs/ride/display/service.js +2 -2
  16. package/lib/cjs/routes/base/api/api.js +1 -0
  17. package/lib/esm/activities/base/repo/db.js +7 -1
  18. package/lib/esm/devices/page/base/guidance/guidance.js +276 -15
  19. package/lib/esm/devices/page/base/service.js +246 -17
  20. package/lib/esm/devices/page/desktop/matrix.js +2 -1
  21. package/lib/esm/devices/page/desktop/service.js +4 -11
  22. package/lib/esm/devices/page/mobile/service.js +1 -0
  23. package/lib/esm/devices/pairing/conflict.js +9 -4
  24. package/lib/esm/devices/pairing/guidance.js +40 -4
  25. package/lib/esm/devices/pairing/helper.js +1 -0
  26. package/lib/esm/devices/pairing/index.js +1 -0
  27. package/lib/esm/devices/pairing/service.js +228 -35
  28. package/lib/esm/devices/pairing/status.js +1 -0
  29. package/lib/esm/devices/ride/service.js +20 -4
  30. package/lib/esm/ride/display/service.js +2 -2
  31. package/lib/esm/routes/base/api/api.js +1 -0
  32. package/lib/types/activities/base/repo/db.d.ts +1 -0
  33. package/lib/types/devices/page/base/guidance/guidance.d.ts +49 -2
  34. package/lib/types/devices/page/base/guidance/types.d.ts +20 -2
  35. package/lib/types/devices/page/base/service.d.ts +36 -2
  36. package/lib/types/devices/page/base/types.d.ts +31 -0
  37. package/lib/types/devices/page/desktop/service.d.ts +1 -2
  38. package/lib/types/devices/pairing/conflict.d.ts +4 -1
  39. package/lib/types/devices/pairing/guidance.d.ts +9 -4
  40. package/lib/types/devices/pairing/helper.d.ts +1 -0
  41. package/lib/types/devices/pairing/index.d.ts +1 -0
  42. package/lib/types/devices/pairing/model.d.ts +1 -0
  43. package/lib/types/devices/pairing/service.d.ts +43 -4
  44. package/lib/types/devices/pairing/status.d.ts +2 -1
  45. package/lib/types/devices/ride/service.d.ts +1 -0
  46. package/package.json +2 -2
@@ -365,7 +365,9 @@ let ActivitiesRepository = (() => {
365
365
  if (this.isDirty && (this.tsLastWrite === undefined || Date.now() - this.tsLastWrite >= 1000)) {
366
366
  this.isDirty = false;
367
367
  this.tsLastWrite = Date.now();
368
- this.writeRepo().catch(err => { this.logError(err, 'write'); });
368
+ this.writeRepo().catch(err => {
369
+ this.logWarning(err, 'write');
370
+ });
369
371
  }
370
372
  if (this.isDirty && Date.now() - this.tsLastWrite < 1000) {
371
373
  setTimeout(() => { this.write(); }, this.tsLastWrite + 1000 - Date.now());
@@ -519,6 +521,10 @@ let ActivitiesRepository = (() => {
519
521
  const props = logProps ?? {};
520
522
  this.logger.logEvent({ message: 'error', fn, error: err.message, ...props, stack: err.stack });
521
523
  }
524
+ logWarning(err, fn, logProps) {
525
+ const props = logProps ?? {};
526
+ this.logger.logEvent({ message: 'warning', fn, error: err.message, ...props });
527
+ }
522
528
  getRepo() {
523
529
  if (!this.repo)
524
530
  this.repo = api_1.JsonRepository.create('activities');
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PairingGuidance = exports.TRAINER_ATTEMPT_TIMEOUT_MS = exports.HELPER_RESOLVED_DISPLAY_MS = exports.HELPER_THRESHOLD_MS = exports.HELPER_CLOSED_EVENT = exports.HELPER_RESOLVED_EVENT = exports.HELPER_ACTION_EVENT = exports.HELPER_REASON_EVENT = exports.HELPER_SHOWN_EVENT = exports.PARTIAL_OUTAGE_EVENT = exports.TRAINER_PHASE_EVENT = void 0;
3
+ exports.PairingGuidance = exports.TRAINER_ATTEMPT_TIMEOUT_MS = exports.HELPER_RESOLVED_DISPLAY_MS = exports.HELPER_THRESHOLD_MS = exports.FALLBACK_REVERTED_DISPLAY_MS = exports.RIDER_RETRY_ENDED_EVENT = exports.RIDER_RETRY_EVENT = exports.FALLBACK_CANCELLED_EVENT = exports.FALLBACK_CHOSEN_EVENT = exports.HELPER_CLOSED_EVENT = exports.HELPER_RESOLVED_EVENT = exports.HELPER_ACTION_EVENT = exports.HELPER_REASON_EVENT = exports.HELPER_SHOWN_EVENT = exports.PARTIAL_OUTAGE_EVENT = exports.TRAINER_PHASE_EVENT = void 0;
4
4
  const incyclist_devices_1 = require("incyclist-devices");
5
5
  const capability_1 = require("../../../pairing/capability");
6
6
  const guidance_1 = require("../../../pairing/guidance");
@@ -13,6 +13,12 @@ exports.HELPER_REASON_EVENT = 'pairing helper reason selected';
13
13
  exports.HELPER_ACTION_EVENT = 'pairing helper action';
14
14
  exports.HELPER_RESOLVED_EVENT = 'pairing helper resolved';
15
15
  exports.HELPER_CLOSED_EVENT = 'pairing helper closed';
16
+ exports.FALLBACK_CHOSEN_EVENT = 'pairing fallback chosen';
17
+ exports.FALLBACK_CANCELLED_EVENT = 'pairing fallback cancelled';
18
+ exports.RIDER_RETRY_EVENT = 'pairing trainer retry';
19
+ exports.RIDER_RETRY_ENDED_EVENT = 'pairing trainer retry ended';
20
+ exports.FALLBACK_REVERTED_DISPLAY_MS = 3000;
21
+ const NOT_AVAILABLE = { reason: 'not-available', scope: 'until-next-pairing' };
16
22
  exports.HELPER_THRESHOLD_MS = {
17
23
  mobile: 30000,
18
24
  desktop: 45000,
@@ -57,6 +63,19 @@ class PairingGuidance {
57
63
  attemptElapsedMs = 0;
58
64
  attemptResumedAt;
59
65
  attemptTimer;
66
+ unavailableSince;
67
+ trainerRetry;
68
+ retryIndex = 0;
69
+ retryCase;
70
+ retrySawConnecting = false;
71
+ retryStartFields;
72
+ retryTimeoutMs;
73
+ retryElapsedMs = 0;
74
+ retryResumedAt;
75
+ retryTimer;
76
+ fallback;
77
+ fallbackReverted = false;
78
+ fallbackRevertedTimer;
60
79
  visitId;
61
80
  openedAt = 0;
62
81
  partialOutageSince;
@@ -100,6 +119,8 @@ class PairingGuidance {
100
119
  closeVisit() {
101
120
  if (this.visitOpen && this.helperPhase === 'open')
102
121
  this.logHelperClosed('page_left');
122
+ if (this.visitOpen && this.trainerRetry === 'running')
123
+ this.endRiderRetry('left_page');
103
124
  this.reset();
104
125
  }
105
126
  pauseVisit() {
@@ -108,6 +129,8 @@ class PairingGuidance {
108
129
  this.paused = true;
109
130
  this.stopAttemptClock();
110
131
  this.clearAttemptTimer();
132
+ this.stopRetryClock();
133
+ this.clearRetryTimer();
111
134
  this.stopHelperClock();
112
135
  this.clearHelperTimer();
113
136
  if (this.helperPhase === 'resolved')
@@ -121,40 +144,130 @@ class PairingGuidance {
121
144
  if (this.attemptTimeoutMs !== undefined)
122
145
  this.attemptResumedAt = Date.now();
123
146
  this.armAttemptTimer();
147
+ if (this.retryTimeoutMs !== undefined)
148
+ this.retryResumedAt = Date.now();
149
+ this.armRetryTimer();
124
150
  this.helperResumedAt = Date.now();
125
151
  this.armHelperTimer();
126
152
  }
127
153
  onPairingState(state) {
128
154
  if (!this.visitOpen || this.paused)
129
155
  return;
130
- const control = state?.capabilities?.find(c => c.capability === incyclist_devices_1.IncyclistCapability.Control);
131
- this.trainerConfigured = (0, capability_1.isCapabilityActive)(control);
132
- if (!this.trainerConfigured) {
133
- this.endTrainer();
134
- return;
135
- }
136
- if (this.phase === undefined || control.selected !== this.trainerUdid) {
137
- this.startTrainer(control);
138
- return;
139
- }
140
- this.trainerInterface = control.interface ?? this.trainerInterface;
141
- this.onTrainerConnectState(control.connectState);
156
+ const previousConnectState = this.lastConnectState;
157
+ this.followTrainer(state);
158
+ this.followRiderRetry(previousConnectState);
142
159
  }
143
160
  onTrainerUnavailable() {
144
161
  if (this.visitOpen && this.phase === 'connecting')
145
162
  this.moveTo('unavailable', 'rider');
146
163
  }
164
+ getResistanceUnselectAction() {
165
+ const { trainerPhase } = this.getState();
166
+ const waitingForTrainer = trainerPhase === 'connecting' || trainerPhase === 'unavailable';
167
+ return this.visitOpen && !this.fallback && waitingForTrainer ? 'open-helper' : 'unselect';
168
+ }
169
+ openHelperForUnavailableTrainer(trigger) {
170
+ if (!this.visitOpen)
171
+ return;
172
+ this.onTrainerUnavailable();
173
+ if (this.helperPhase !== 'open')
174
+ this.showHelper(trigger);
175
+ this.preselectReason('trainer_unavailable');
176
+ }
147
177
  getState() {
148
- const trainerPhase = this.trainerConfigured ? this.phase : undefined;
178
+ const trainerPhase = this.trainerConfigured || this.fallback ? this.phase : undefined;
149
179
  const pairingCase = trainerPhase ? CASE_BY_PHASE[trainerPhase] : 'no-trainer';
150
- return { trainerConfigured: this.trainerConfigured, trainerPhase, pairingCase };
180
+ const state = { trainerConfigured: this.trainerConfigured, trainerPhase, pairingCase };
181
+ if (this.trainerRetry)
182
+ state.trainerRetry = this.trainerRetry;
183
+ if (this.fallback)
184
+ state.fallback = { capability: this.fallback.capability };
185
+ return state;
186
+ }
187
+ getFallbackCandidate() {
188
+ if (!this.visitOpen || this.fallback || this.getState().trainerPhase !== 'unavailable')
189
+ return undefined;
190
+ return this.separateSensor;
191
+ }
192
+ chooseFallback() {
193
+ const capability = this.getFallbackCandidate();
194
+ const configuration = this.deps.configuration?.();
195
+ const trainerUdid = this.trainerUdid;
196
+ if (!capability || !configuration || !trainerUdid)
197
+ return false;
198
+ const now = Date.now();
199
+ this.fallback = { capability, trainerUdid, since: now };
200
+ try {
201
+ configuration.excludeDevice(trainerUdid, NOT_AVAILABLE);
202
+ }
203
+ catch {
204
+ delete this.fallback;
205
+ return false;
206
+ }
207
+ this.closeHelperForFallback(capability);
208
+ this.safeLog(exports.FALLBACK_CHOSEN_EVENT, { capability, msSinceOpen: now - this.openedAt });
209
+ return true;
210
+ }
211
+ cancelFallback() {
212
+ const fallback = this.fallback;
213
+ const configuration = this.deps.configuration?.();
214
+ if (!this.visitOpen || !fallback || !configuration)
215
+ return false;
216
+ delete this.fallback;
217
+ this.trainerConfigured = true;
218
+ const excluded = configuration.getExclusions()
219
+ .filter(e => e.reason === NOT_AVAILABLE.reason && e.udid === fallback.trainerUdid);
220
+ const isControl = (e) => Number(e.capability === incyclist_devices_1.IncyclistCapability.Control);
221
+ excluded.sort((a, b) => isControl(b) - isControl(a));
222
+ excluded.forEach(e => { configuration.includeCapability(e.capability); });
223
+ const now = Date.now();
224
+ this.safeLog(exports.FALLBACK_CANCELLED_EVENT, { capability: fallback.capability, msSinceOpen: now - this.openedAt, msSinceFallback: now - fallback.since });
225
+ return true;
226
+ }
227
+ onFallbackReverted(udid) {
228
+ const trainerUdid = this.fallback?.trainerUdid ?? this.trainerUdid;
229
+ if (!this.visitOpen || udid !== trainerUdid)
230
+ return;
231
+ delete this.fallback;
232
+ this.fallbackReverted = true;
233
+ this.clearFallbackRevertedTimer();
234
+ this.fallbackRevertedTimer = setTimeout(() => { this.onFallbackRevertedShown(); }, exports.FALLBACK_REVERTED_DISPLAY_MS);
235
+ }
236
+ startRiderRetry({ trigger }) {
237
+ if (!this.visitOpen || this.paused || this.fallback || this.trainerRetry === 'running')
238
+ return false;
239
+ const { pairingCase, trainerPhase } = this.getState();
240
+ if (trainerPhase !== 'unavailable')
241
+ return false;
242
+ this.retryIndex++;
243
+ this.trainerRetry = 'running';
244
+ this.retryCase = pairingCase;
245
+ this.retrySawConnecting = false;
246
+ this.retryStartFields = this.getRiderRetryStartFields(trigger);
247
+ this.retryTimeoutMs = this.getAttemptTimeoutMs();
248
+ this.retryElapsedMs = 0;
249
+ this.retryResumedAt = Date.now();
250
+ this.armRetryTimer();
251
+ return true;
252
+ }
253
+ logRiderRetry(retryCapabilities) {
254
+ if (!this.retryStartFields)
255
+ return;
256
+ const fields = { retryCapabilities: retryCapabilities.join(','), ...this.retryStartFields };
257
+ delete this.retryStartFields;
258
+ this.safeLog(exports.RIDER_RETRY_EVENT, fields);
151
259
  }
152
260
  getStatus(src) {
153
261
  const hint = this.visitOpen && !this.paused ? (0, status_1.getPartialOutageHint)(src) : undefined;
154
262
  this.trackPartialOutage(hint);
263
+ const { trainerPhase, trainerRetry } = this.getState();
155
264
  const status = (0, status_1.getPairingStatusDisplay)({
156
265
  ...src,
157
- trainerPhase: this.getState().trainerPhase,
266
+ trainerPhase,
267
+ trainerRetry,
268
+ fallbackActive: this.fallback !== undefined,
269
+ fallbackCapability: this.fallback?.capability,
270
+ fallbackReverted: this.fallbackReverted,
158
271
  partialOutageSince: this.partialOutageSince,
159
272
  helperThresholdReached: this.visitOpen && this.thresholdReached,
160
273
  now: Date.now(),
@@ -175,6 +288,8 @@ class PairingGuidance {
175
288
  partialOutageShown: this.partialOutageShown !== undefined,
176
289
  switchedOffRequired: this.lastStatusId === 'S8',
177
290
  helperShown: this.helperShown,
291
+ fallbackActive: this.fallback !== undefined,
292
+ trainerRetries: this.retryIndex,
178
293
  };
179
294
  if (trainerPhase)
180
295
  fields.trainerPhase = trainerPhase;
@@ -196,6 +311,7 @@ class PairingGuidance {
196
311
  reasons,
197
312
  selectedReason,
198
313
  separateSensor: trainerPhase ? this.separateSensor : undefined,
314
+ fallbackCandidate: this.getFallbackCandidate(),
199
315
  continueTarget: this.getContinueWithoutDeviceAction().target,
200
316
  platform: this.platform,
201
317
  forRide: this.forRide,
@@ -229,6 +345,12 @@ class PairingGuidance {
229
345
  interfaceStates: this.interfaceStates,
230
346
  }));
231
347
  }
348
+ preselectReason(reason) {
349
+ if (!this.getHelper().reasons.includes(reason))
350
+ return;
351
+ this.selectedReason = reason;
352
+ this.lastReason = reason;
353
+ }
232
354
  setDeviceListOpen(open) {
233
355
  this.deviceListOpen = open;
234
356
  }
@@ -427,8 +549,134 @@ class PairingGuidance {
427
549
  catch {
428
550
  }
429
551
  }
552
+ followTrainer(state) {
553
+ const control = state?.capabilities?.find(c => c.capability === incyclist_devices_1.IncyclistCapability.Control);
554
+ this.trainerConfigured = (0, capability_1.isCapabilityActive)(control);
555
+ if (this.fallback && !this.isOnFallback(control))
556
+ delete this.fallback;
557
+ if (!this.trainerConfigured) {
558
+ if (!this.fallback)
559
+ this.endTrainer();
560
+ return;
561
+ }
562
+ if (this.phase === undefined || control.selected !== this.trainerUdid) {
563
+ this.startTrainer(control);
564
+ return;
565
+ }
566
+ this.trainerInterface = control.interface ?? this.trainerInterface;
567
+ this.onTrainerConnectState(control.connectState);
568
+ }
569
+ isOnFallback(control) {
570
+ return control?.selected === this.fallback?.trainerUdid && control?.excluded?.reason === NOT_AVAILABLE.reason && !control?.disabled;
571
+ }
572
+ closeHelperForFallback(capability) {
573
+ if (this.helperPhase !== 'open') {
574
+ this.hideHelper();
575
+ return;
576
+ }
577
+ const fields = { action: 'fallback_without_trainer' };
578
+ if (this.selectedReason)
579
+ fields.reason = this.selectedReason;
580
+ fields.fallbackCapability = capability;
581
+ this.safeLog(exports.HELPER_ACTION_EVENT, this.getHelperEventFields(fields));
582
+ this.logHelperClosed('fallback');
583
+ this.hideHelper();
584
+ }
585
+ onFallbackRevertedShown() {
586
+ delete this.fallbackRevertedTimer;
587
+ try {
588
+ this.fallbackReverted = false;
589
+ this.deps.onChange();
590
+ }
591
+ catch {
592
+ }
593
+ }
594
+ clearFallbackRevertedTimer() {
595
+ if (this.fallbackRevertedTimer)
596
+ clearTimeout(this.fallbackRevertedTimer);
597
+ delete this.fallbackRevertedTimer;
598
+ }
599
+ followRiderRetry(previousConnectState) {
600
+ if (this.trainerRetry !== 'running')
601
+ return;
602
+ if (this.phase === 'connected') {
603
+ this.endRiderRetry('connected');
604
+ return;
605
+ }
606
+ const connectState = this.lastConnectState;
607
+ if (connectState === 'connecting' && previousConnectState !== 'connecting')
608
+ this.retrySawConnecting = true;
609
+ else if (connectState === 'failed' && previousConnectState !== 'failed' && this.retrySawConnecting)
610
+ this.endRiderRetry('failed');
611
+ }
612
+ endRiderRetry(retryResult) {
613
+ const durationMs = this.getRetryElapsedMs();
614
+ const lasting = retryResult === 'failed' || retryResult === 'timeout';
615
+ this.clearRiderRetry();
616
+ if (lasting)
617
+ this.trainerRetry = 'failed';
618
+ this.safeLog(exports.RIDER_RETRY_ENDED_EVENT, {
619
+ retryResult,
620
+ durationMs,
621
+ retryIndex: this.retryIndex,
622
+ pairingCase: this.retryCase,
623
+ platform: this.platform,
624
+ });
625
+ }
626
+ clearRiderRetry() {
627
+ this.clearRetryTimer();
628
+ delete this.trainerRetry;
629
+ delete this.retryTimeoutMs;
630
+ delete this.retryResumedAt;
631
+ delete this.retryStartFields;
632
+ this.retryElapsedMs = 0;
633
+ this.retrySawConnecting = false;
634
+ }
635
+ getRiderRetryStartFields(trigger) {
636
+ const fields = { trigger, retryIndex: this.retryIndex };
637
+ if (this.unavailableSince !== undefined)
638
+ fields.msSinceUnavailable = Date.now() - this.unavailableSince;
639
+ fields.separateDeviceConnected = this.separateSensor !== undefined;
640
+ fields.helperOpen = this.helperPhase === 'open';
641
+ fields.pairingCase = this.retryCase;
642
+ fields.platform = this.platform;
643
+ return fields;
644
+ }
645
+ getRetryElapsedMs() {
646
+ const running = this.retryResumedAt === undefined ? 0 : Date.now() - this.retryResumedAt;
647
+ return this.retryElapsedMs + running;
648
+ }
649
+ stopRetryClock() {
650
+ this.retryElapsedMs = this.getRetryElapsedMs();
651
+ delete this.retryResumedAt;
652
+ }
653
+ armRetryTimer() {
654
+ this.clearRetryTimer();
655
+ if (this.paused || this.trainerRetry !== 'running' || this.retryTimeoutMs === undefined)
656
+ return;
657
+ const remaining = Math.max(0, this.retryTimeoutMs - this.getRetryElapsedMs());
658
+ this.retryTimer = setTimeout(() => { this.onRetryTimeout(); }, remaining);
659
+ }
660
+ clearRetryTimer() {
661
+ if (this.retryTimer)
662
+ clearTimeout(this.retryTimer);
663
+ delete this.retryTimer;
664
+ }
665
+ onRetryTimeout() {
666
+ delete this.retryTimer;
667
+ try {
668
+ if (this.trainerRetry !== 'running')
669
+ return;
670
+ this.endRiderRetry('timeout');
671
+ this.deps.onChange();
672
+ }
673
+ catch {
674
+ }
675
+ }
430
676
  startTrainer(control) {
431
677
  this.stopAttempt();
678
+ this.clearRiderRetry();
679
+ delete this.unavailableSince;
432
680
  this.trainerUdid = control.selected;
433
681
  this.trainerInterface = control.interface;
434
682
  this.lastConnectState = control.connectState;
@@ -441,6 +689,8 @@ class PairingGuidance {
441
689
  }
442
690
  endTrainer() {
443
691
  this.stopAttempt();
692
+ this.clearRiderRetry();
693
+ delete this.unavailableSince;
444
694
  delete this.phase;
445
695
  delete this.trainerUdid;
446
696
  delete this.trainerInterface;
@@ -466,6 +716,12 @@ class PairingGuidance {
466
716
  const fromPhase = this.phase;
467
717
  const attemptMs = this.attemptTimeoutMs === undefined ? undefined : this.getAttemptElapsedMs();
468
718
  this.phase = toPhase;
719
+ if (toPhase === 'unavailable')
720
+ this.unavailableSince = Date.now();
721
+ else
722
+ delete this.unavailableSince;
723
+ if (toPhase === 'connected' && this.trainerRetry === 'failed')
724
+ this.clearRiderRetry();
469
725
  if (toPhase === 'connected')
470
726
  this.stopAttempt();
471
727
  else if (toPhase === 'connecting')
@@ -530,6 +786,11 @@ class PairingGuidance {
530
786
  }
531
787
  reset() {
532
788
  this.endTrainer();
789
+ this.retryIndex = 0;
790
+ delete this.retryCase;
791
+ delete this.fallback;
792
+ this.fallbackReverted = false;
793
+ this.clearFallbackRevertedTimer();
533
794
  this.clearPartialOutage();
534
795
  this.resetHelper();
535
796
  this.visitOpen = false;