incyclist-services 1.8.8 → 1.9.0

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 (72) hide show
  1. package/lib/cjs/devices/page/index.js +6 -0
  2. package/lib/cjs/devices/page/service.js +69 -5
  3. package/lib/cjs/devices/page/visit-log-factory.js +41 -0
  4. package/lib/cjs/devices/page/visit-log.js +143 -0
  5. package/lib/cjs/devices/page/visit-tracker-factory.js +41 -0
  6. package/lib/cjs/devices/page/visit-tracker.js +142 -0
  7. package/lib/cjs/devices/pairing/guidance.js +152 -0
  8. package/lib/cjs/devices/pairing/index.js +2 -0
  9. package/lib/cjs/devices/pairing/status.js +54 -0
  10. package/lib/cjs/ride/display/service.js +15 -0
  11. package/lib/cjs/ui/service.js +39 -0
  12. package/lib/esm/devices/page/__tests__/desktop-matrix.js +23 -0
  13. package/lib/esm/devices/page/base/logobserver.js +15 -0
  14. package/lib/esm/devices/page/base/orchestrator.js +1 -0
  15. package/lib/esm/devices/page/base/service.js +380 -0
  16. package/lib/esm/devices/page/base/types.js +1 -0
  17. package/lib/esm/devices/page/base/visit-log-factory.js +33 -0
  18. package/lib/esm/devices/page/base/visit-log.js +138 -0
  19. package/lib/esm/devices/page/desktop/matrix.js +23 -0
  20. package/lib/esm/devices/page/desktop/orchestrator.js +17 -0
  21. package/lib/esm/devices/page/desktop/service.js +250 -0
  22. package/lib/esm/devices/page/desktop/types.js +1 -0
  23. package/lib/esm/devices/page/desktop-service.js +204 -0
  24. package/lib/esm/devices/page/index.js +11 -1
  25. package/lib/esm/devices/page/mobile/orchestrator.js +2 -0
  26. package/lib/esm/devices/page/mobile/service.js +194 -0
  27. package/lib/esm/devices/page/mobile/statemachine.js +345 -0
  28. package/lib/esm/devices/page/orchestrator.js +19 -0
  29. package/lib/esm/devices/page/pairing-page-service.js +385 -0
  30. package/lib/esm/devices/page/service.js +70 -6
  31. package/lib/esm/devices/page/visit-log-factory.js +33 -0
  32. package/lib/esm/devices/page/visit-log.js +138 -0
  33. package/lib/esm/devices/page/visit-tracker-factory.js +33 -0
  34. package/lib/esm/devices/page/visit-tracker.js +137 -0
  35. package/lib/esm/devices/pairing/guidance.js +141 -0
  36. package/lib/esm/devices/pairing/index.js +2 -0
  37. package/lib/esm/devices/pairing/service.js +54 -1
  38. package/lib/esm/devices/pairing/status.js +49 -0
  39. package/lib/esm/ride/display/service.js +16 -1
  40. package/lib/esm/ui/service.js +43 -1
  41. package/lib/types/activities/ride/service.d.ts +1 -1
  42. package/lib/types/devices/page/__tests__/desktop-matrix.d.ts +8 -0
  43. package/lib/types/devices/page/base/logobserver.d.ts +8 -0
  44. package/lib/types/devices/page/base/orchestrator.d.ts +11 -0
  45. package/lib/types/devices/page/base/service.d.ts +54 -0
  46. package/lib/types/devices/page/base/types.d.ts +130 -0
  47. package/lib/types/devices/page/base/visit-log-factory.d.ts +13 -0
  48. package/lib/types/devices/page/base/visit-log.d.ts +25 -0
  49. package/lib/types/devices/page/desktop/matrix.d.ts +8 -0
  50. package/lib/types/devices/page/desktop/orchestrator.d.ts +2 -0
  51. package/lib/types/devices/page/desktop/service.d.ts +32 -0
  52. package/lib/types/devices/page/desktop/types.d.ts +25 -0
  53. package/lib/types/devices/page/desktop-service.d.ts +28 -0
  54. package/lib/types/devices/page/index.d.ts +3 -1
  55. package/lib/types/devices/page/mobile/orchestrator.d.ts +2 -0
  56. package/lib/types/devices/page/mobile/service.d.ts +24 -0
  57. package/lib/types/devices/page/mobile/statemachine.d.ts +49 -0
  58. package/lib/types/devices/page/orchestrator.d.ts +13 -0
  59. package/lib/types/devices/page/pairing-page-service.d.ts +53 -0
  60. package/lib/types/devices/page/service.d.ts +9 -1
  61. package/lib/types/devices/page/types.d.ts +55 -0
  62. package/lib/types/devices/page/visit-log-factory.d.ts +13 -0
  63. package/lib/types/devices/page/visit-log.d.ts +25 -0
  64. package/lib/types/devices/page/visit-tracker-factory.d.ts +13 -0
  65. package/lib/types/devices/page/visit-tracker.d.ts +25 -0
  66. package/lib/types/devices/pairing/guidance.d.ts +55 -0
  67. package/lib/types/devices/pairing/index.d.ts +2 -0
  68. package/lib/types/devices/pairing/service.d.ts +8 -0
  69. package/lib/types/devices/pairing/status.d.ts +29 -0
  70. package/lib/types/ride/display/service.d.ts +3 -1
  71. package/lib/types/ui/service.d.ts +4 -0
  72. package/package.json +1 -1
@@ -0,0 +1,138 @@
1
+ let visitSequence = 0;
2
+ export const PAGE_LEFT_EVENT = 'pairing page left';
3
+ export const VISIT_UNCLOSED_EVENT = 'pairing visit unclosed';
4
+ export const getPreviousVisitOutcome = (last) => {
5
+ if (!last || !last.closed || !last.via)
6
+ return 'none';
7
+ switch (last.via) {
8
+ case 'ok':
9
+ return 'ready';
10
+ case 'simulate':
11
+ return 'simulate';
12
+ case 'skip':
13
+ case 'cancel':
14
+ if (last.rideAfter === 'device')
15
+ return 'skip-then-ride';
16
+ if (last.rideAfter === 'simulate')
17
+ return 'simulate';
18
+ return 'skip-no-ride';
19
+ case 'app_exit':
20
+ return last.canStartRide ? 'app-exit-ready' : 'app-exit-not-ready';
21
+ }
22
+ };
23
+ export class PairingVisitTracker {
24
+ deps;
25
+ constructor(deps) {
26
+ this.deps = deps;
27
+ }
28
+ onAppLaunch(isNewUser) {
29
+ const record = this.deps.store.get();
30
+ if (!record) {
31
+ this.deps.store.set({ date: this.deps.today(), countToday: 0, firstEverPending: isNewUser });
32
+ return;
33
+ }
34
+ const last = record.last;
35
+ if (!last || last.closed)
36
+ return;
37
+ const now = this.deps.now();
38
+ this.deps.log(VISIT_UNCLOSED_EVENT, {
39
+ visitId: last.visitId,
40
+ visitSessionId: last.sessionId,
41
+ dwellMs: last.backgroundedAt !== undefined ? last.backgroundedAt - last.openedAt : null,
42
+ backgroundMs: last.backgroundMs,
43
+ forRide: last.forRide,
44
+ canStartRide: last.canStartRide,
45
+ visitIndexToday: last.visitIndexToday,
46
+ firstEverVisit: last.firstEverVisit,
47
+ visitAgeMs: now - last.openedAt,
48
+ platform: this.deps.platform,
49
+ });
50
+ this.deps.store.set({
51
+ ...record,
52
+ last: { ...last, closed: true, via: 'app_exit', inferred: true, closedAt: last.backgroundedAt ?? last.openedAt, backgroundedAt: undefined },
53
+ });
54
+ }
55
+ openVisit({ forRide }) {
56
+ const now = this.deps.now();
57
+ const today = this.deps.today();
58
+ const previous = this.deps.store.get();
59
+ if (previous?.last && !previous.last.closed)
60
+ this.closeVisit('app_exit', { canStartRide: previous.last.canStartRide });
61
+ const record = this.deps.store.get() ?? { date: today, countToday: 0, firstEverPending: false };
62
+ const countToday = record.date === today ? record.countToday + 1 : 1;
63
+ const visitId = `${now}-${++visitSequence}`;
64
+ const prevLast = record.last;
65
+ this.deps.store.set({
66
+ date: today,
67
+ countToday,
68
+ firstEverPending: false,
69
+ last: {
70
+ visitId,
71
+ sessionId: this.deps.sessionId,
72
+ openedAt: now,
73
+ backgroundMs: 0,
74
+ forRide,
75
+ canStartRide: false,
76
+ visitIndexToday: countToday,
77
+ firstEverVisit: record.firstEverPending,
78
+ previousVisitOutcome: getPreviousVisitOutcome(prevLast),
79
+ previousVisitAgeMs: prevLast?.closedAt !== undefined ? now - prevLast.closedAt : undefined,
80
+ closed: false,
81
+ },
82
+ });
83
+ return { visitId };
84
+ }
85
+ onBackground({ canStartRide } = {}) {
86
+ const record = this.deps.store.get();
87
+ const last = record?.last;
88
+ if (!record || !last || last.closed || last.backgroundedAt !== undefined)
89
+ return;
90
+ const refreshed = canStartRide ?? last.canStartRide;
91
+ this.deps.store.set({ ...record, last: { ...last, canStartRide: refreshed, backgroundedAt: this.deps.now() } });
92
+ }
93
+ onForeground() {
94
+ const record = this.deps.store.get();
95
+ const last = record?.last;
96
+ if (!record || !last || last.closed || last.backgroundedAt === undefined)
97
+ return;
98
+ const backgroundMs = last.backgroundMs + (this.deps.now() - last.backgroundedAt);
99
+ this.deps.store.set({ ...record, last: { ...last, backgroundMs, backgroundedAt: undefined } });
100
+ }
101
+ closeVisit(via, { canStartRide }) {
102
+ const record = this.deps.store.get();
103
+ const last = record?.last;
104
+ if (!record || !last || last.closed)
105
+ return;
106
+ const now = this.deps.now();
107
+ const backgroundMs = last.backgroundMs + (last.backgroundedAt !== undefined ? now - last.backgroundedAt : 0);
108
+ this.deps.log(PAGE_LEFT_EVENT, {
109
+ visitId: last.visitId,
110
+ via,
111
+ dwellMs: now - last.openedAt,
112
+ backgroundMs,
113
+ forRide: last.forRide,
114
+ canStartRide,
115
+ visitIndexToday: last.visitIndexToday,
116
+ firstEverVisit: last.firstEverVisit,
117
+ previousVisitOutcome: last.previousVisitOutcome,
118
+ previousVisitAgeMs: last.previousVisitAgeMs,
119
+ platform: this.deps.platform,
120
+ });
121
+ this.deps.store.set({
122
+ ...record,
123
+ last: { ...last, closed: true, via, canStartRide, backgroundMs, backgroundedAt: undefined, closedAt: now },
124
+ });
125
+ }
126
+ onAppExit(canStartRide) {
127
+ this.closeVisit('app_exit', { canStartRide });
128
+ }
129
+ onRideStarted({ simulate }) {
130
+ const record = this.deps.store.get();
131
+ const last = record?.last;
132
+ if (!record || !last || !last.closed || last.rideAfter)
133
+ return;
134
+ if (last.via !== 'skip' && last.via !== 'cancel')
135
+ return;
136
+ this.deps.store.set({ ...record, last: { ...last, rideAfter: simulate ? 'simulate' : 'device' } });
137
+ }
138
+ }
@@ -0,0 +1,33 @@
1
+ import { EventLogger } from 'gd-eventlog';
2
+ import { getBindings } from '../../api';
3
+ import { useAppState } from '../../appstate';
4
+ import { PairingVisitTracker } from './visit-tracker';
5
+ export const PAIRING_VISITS_STATE_KEY = 'pairingVisits';
6
+ let instance;
7
+ let logger;
8
+ export const createPairingVisitStore = (appState) => ({
9
+ get: () => appState.getPersistedState(PAIRING_VISITS_STATE_KEY) ?? undefined,
10
+ set: (record) => { appState.setPersistedState(PAIRING_VISITS_STATE_KEY, record); },
11
+ });
12
+ export const formatLocalDate = (date) => {
13
+ const pad = (n) => String(n).padStart(2, '0');
14
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
15
+ };
16
+ export const toVisitPlatform = (platform) => platform?.startsWith('mobile') ? 'mobile' : 'desktop';
17
+ const log = (message, fields) => {
18
+ logger = logger ?? new EventLogger('Pairing');
19
+ logger.logEvent({ message, ...fields });
20
+ };
21
+ export const initPairingVisitTracker = (platform) => {
22
+ const appInfo = getBindings()?.appInfo;
23
+ instance = new PairingVisitTracker({
24
+ store: createPairingVisitStore(useAppState()),
25
+ sessionId: appInfo?.session ?? '',
26
+ platform: toVisitPlatform(platform ?? appInfo?.getChannel()),
27
+ now: () => Date.now(),
28
+ today: () => formatLocalDate(new Date()),
29
+ log,
30
+ });
31
+ return instance;
32
+ };
33
+ export const usePairingVisitTracker = () => instance ?? initPairingVisitTracker();
@@ -0,0 +1,137 @@
1
+ export const PAGE_LEFT_EVENT = 'pairing page left';
2
+ export const VISIT_UNCLOSED_EVENT = 'pairing visit unclosed';
3
+ export const getPreviousVisitOutcome = (last) => {
4
+ if (!last || !last.closed || !last.via)
5
+ return 'none';
6
+ switch (last.via) {
7
+ case 'ok':
8
+ return 'ready';
9
+ case 'simulate':
10
+ return 'simulate';
11
+ case 'skip':
12
+ case 'cancel':
13
+ if (last.rideAfter === 'device')
14
+ return 'skip-then-ride';
15
+ if (last.rideAfter === 'simulate')
16
+ return 'simulate';
17
+ return 'skip-no-ride';
18
+ case 'app_exit':
19
+ return last.canStartRide ? 'app-exit-ready' : 'app-exit-not-ready';
20
+ }
21
+ };
22
+ export class PairingVisitTracker {
23
+ deps;
24
+ constructor(deps) {
25
+ this.deps = deps;
26
+ }
27
+ onAppLaunch(isNewUser) {
28
+ const record = this.deps.store.get();
29
+ if (!record) {
30
+ this.deps.store.set({ date: this.deps.today(), countToday: 0, firstEverPending: isNewUser });
31
+ return;
32
+ }
33
+ const last = record.last;
34
+ if (!last || last.closed)
35
+ return;
36
+ const now = this.deps.now();
37
+ this.deps.log(VISIT_UNCLOSED_EVENT, {
38
+ visitId: last.visitId,
39
+ visitSessionId: last.sessionId,
40
+ dwellMs: last.backgroundedAt !== undefined ? last.backgroundedAt - last.openedAt : null,
41
+ backgroundMs: last.backgroundMs,
42
+ forRide: last.forRide,
43
+ canStartRide: last.canStartRide,
44
+ visitIndexToday: last.visitIndexToday,
45
+ firstEverVisit: last.firstEverVisit,
46
+ visitAgeMs: now - last.openedAt,
47
+ platform: this.deps.platform,
48
+ });
49
+ this.deps.store.set({
50
+ ...record,
51
+ last: { ...last, closed: true, via: 'app_exit', inferred: true, closedAt: last.backgroundedAt ?? last.openedAt, backgroundedAt: undefined },
52
+ });
53
+ }
54
+ openVisit({ forRide }) {
55
+ const now = this.deps.now();
56
+ const today = this.deps.today();
57
+ const previous = this.deps.store.get();
58
+ if (previous?.last && !previous.last.closed)
59
+ this.closeVisit('app_exit', { canStartRide: previous.last.canStartRide });
60
+ const record = this.deps.store.get() ?? { date: today, countToday: 0, firstEverPending: false };
61
+ const countToday = record.date === today ? record.countToday + 1 : 1;
62
+ const visitId = `${now}-${Math.random().toString(36).slice(2, 10)}`;
63
+ const prevLast = record.last;
64
+ this.deps.store.set({
65
+ date: today,
66
+ countToday,
67
+ firstEverPending: false,
68
+ last: {
69
+ visitId,
70
+ sessionId: this.deps.sessionId,
71
+ openedAt: now,
72
+ backgroundMs: 0,
73
+ forRide,
74
+ canStartRide: false,
75
+ visitIndexToday: countToday,
76
+ firstEverVisit: record.firstEverPending,
77
+ previousVisitOutcome: getPreviousVisitOutcome(prevLast),
78
+ previousVisitAgeMs: prevLast?.closedAt !== undefined ? now - prevLast.closedAt : undefined,
79
+ closed: false,
80
+ },
81
+ });
82
+ return { visitId };
83
+ }
84
+ onBackground({ canStartRide } = {}) {
85
+ const record = this.deps.store.get();
86
+ const last = record?.last;
87
+ if (!record || !last || last.closed || last.backgroundedAt !== undefined)
88
+ return;
89
+ const refreshed = canStartRide ?? last.canStartRide;
90
+ this.deps.store.set({ ...record, last: { ...last, canStartRide: refreshed, backgroundedAt: this.deps.now() } });
91
+ }
92
+ onForeground() {
93
+ const record = this.deps.store.get();
94
+ const last = record?.last;
95
+ if (!record || !last || last.closed || last.backgroundedAt === undefined)
96
+ return;
97
+ const backgroundMs = last.backgroundMs + (this.deps.now() - last.backgroundedAt);
98
+ this.deps.store.set({ ...record, last: { ...last, backgroundMs, backgroundedAt: undefined } });
99
+ }
100
+ closeVisit(via, { canStartRide }) {
101
+ const record = this.deps.store.get();
102
+ const last = record?.last;
103
+ if (!record || !last || last.closed)
104
+ return;
105
+ const now = this.deps.now();
106
+ const backgroundMs = last.backgroundMs + (last.backgroundedAt !== undefined ? now - last.backgroundedAt : 0);
107
+ this.deps.log(PAGE_LEFT_EVENT, {
108
+ visitId: last.visitId,
109
+ via,
110
+ dwellMs: now - last.openedAt,
111
+ backgroundMs,
112
+ forRide: last.forRide,
113
+ canStartRide,
114
+ visitIndexToday: last.visitIndexToday,
115
+ firstEverVisit: last.firstEverVisit,
116
+ previousVisitOutcome: last.previousVisitOutcome,
117
+ previousVisitAgeMs: last.previousVisitAgeMs,
118
+ platform: this.deps.platform,
119
+ });
120
+ this.deps.store.set({
121
+ ...record,
122
+ last: { ...last, closed: true, via, canStartRide, backgroundMs, backgroundedAt: undefined, closedAt: now },
123
+ });
124
+ }
125
+ onAppExit(canStartRide) {
126
+ this.closeVisit('app_exit', { canStartRide });
127
+ }
128
+ onRideStarted({ simulate }) {
129
+ const record = this.deps.store.get();
130
+ const last = record?.last;
131
+ if (!record || !last || !last.closed || last.rideAfter)
132
+ return;
133
+ if (last.via !== 'skip' && last.via !== 'cancel')
134
+ return;
135
+ this.deps.store.set({ ...record, last: { ...last, rideAfter: simulate ? 'simulate' : 'device' } });
136
+ }
137
+ }
@@ -0,0 +1,141 @@
1
+ import { IncyclistCapability } from "incyclist-devices";
2
+ export const PAIRING_CAPABILITY_ROLES = [
3
+ { capability: IncyclistCapability.Control, role: 'required', descriptor: 'control' },
4
+ { capability: IncyclistCapability.Power, role: 'required', descriptor: 'power' },
5
+ { capability: IncyclistCapability.Speed, role: 'required', descriptor: 'speed' },
6
+ { capability: IncyclistCapability.HeartRate, role: 'optional', descriptor: 'heartrate' },
7
+ { capability: IncyclistCapability.Cadence, role: 'optional', descriptor: 'cadence' },
8
+ { capability: IncyclistCapability.AppControl, role: 'optional', descriptor: 'app_control' },
9
+ ];
10
+ const INTERFACE_LABELS = {
11
+ ant: 'ANT+',
12
+ ble: 'Bluetooth',
13
+ serial: 'Serial',
14
+ tcpip: 'TCP/IP',
15
+ wifi: 'Wi-Fi',
16
+ };
17
+ const DESKTOP_SEARCH_INTERFACES = new Set(['ble', 'ant']);
18
+ const searchInterfaces = (interfaces, platform) => platform === 'desktop'
19
+ ? interfaces.filter(i => DESKTOP_SEARCH_INTERFACES.has(i.id))
20
+ : interfaces;
21
+ export const canScanWithInterfaces = (interfaces, platform) => searchInterfaces(interfaces, platform).some(i => i.enabled && i.available);
22
+ export const getUnavailableInterfaces = (interfaces, platform) => searchInterfaces(interfaces, platform).filter(i => i.enabled && !i.available).map(i => i.id);
23
+ const isBrokenInterface = (interfaces, id) => interfaces.find(i => i.id === id && i.enabled && !i.available);
24
+ export const getPairingHint = (input) => {
25
+ if (input.anyDeviceSelected)
26
+ return undefined;
27
+ if (input.platform === 'mobile') {
28
+ const ble = isBrokenInterface(input.interfaces, 'ble');
29
+ if (ble)
30
+ return ble.permissionDenied ? 'bluetooth-permission' : 'bluetooth-off';
31
+ if (isBrokenInterface(input.interfaces, 'wifi'))
32
+ return 'wifi-off';
33
+ return undefined;
34
+ }
35
+ if (isBrokenInterface(input.interfaces, 'ant'))
36
+ return 'ant';
37
+ return undefined;
38
+ };
39
+ export const derivePairingStatus = (input) => {
40
+ if (!canScanWithInterfaces(input.interfaces, input.platform))
41
+ return 'S1';
42
+ if (input.canStartRide)
43
+ return 'S2';
44
+ if (input.connectingDeviceName)
45
+ return 'S3';
46
+ if (input.initialising)
47
+ return 'S4';
48
+ if (getPairingHint(input))
49
+ return 'S1b';
50
+ return 'S5';
51
+ };
52
+ const formatList = (names) => {
53
+ if (names.length <= 1)
54
+ return names.join('');
55
+ return `${names.slice(0, -1).join(', ')} and ${names.at(-1)}`;
56
+ };
57
+ const getNoSearchText = (params, isShort) => {
58
+ if (params.platform === 'mobile' && params.bluetoothDenied)
59
+ return isShort
60
+ ? { text: 'Bluetooth not allowed.', link: 'Settings' }
61
+ : { text: "Can't search: Bluetooth is not allowed.", link: 'Open settings' };
62
+ const names = (params.unavailable ?? []).map(id => INTERFACE_LABELS[id]);
63
+ if (params.platform === 'mobile') {
64
+ const list = formatList(names);
65
+ const verb = names.length > 1 ? 'are' : 'is';
66
+ const link = names.length === 1 && names[0] === INTERFACE_LABELS.ble ? 'Bluetooth settings' : 'Settings';
67
+ if (isShort)
68
+ return { text: `${list} ${verb} off.`, link: 'Settings' };
69
+ return { text: `Can't search: ${list} ${verb} off.`, link };
70
+ }
71
+ const list = formatList(names);
72
+ if (names.length === 2)
73
+ return { text: `Can't search: ${list} are both unavailable.`, link: 'Check connections' };
74
+ return { text: `Can't search: ${list} unavailable.`, link: 'Check connections' };
75
+ };
76
+ const getHintText = (hint, isShort) => {
77
+ switch (hint) {
78
+ case 'bluetooth-permission':
79
+ return isShort
80
+ ? { text: 'Bluetooth not allowed.', link: 'Settings' }
81
+ : { text: "Incyclist isn't allowed to use Bluetooth, so most trainers won't show up.", link: 'Open settings' };
82
+ case 'bluetooth-off':
83
+ return isShort
84
+ ? { text: 'Bluetooth is off.', link: 'Settings' }
85
+ : { text: "Bluetooth is off, so most trainers won't show up.", link: 'Bluetooth settings' };
86
+ case 'wifi-off':
87
+ return isShort
88
+ ? { text: 'Wi-Fi is off · needed for Wi-Fi trainers' }
89
+ : { text: 'Wi-Fi is off. If your trainer connects over Wi-Fi, turn Wi-Fi on.' };
90
+ case 'ant':
91
+ return { text: 'ANT+ stick not found. If you use one, plug it in, try another USB port, or close other apps that use it.' };
92
+ }
93
+ };
94
+ export const getPairingGuidanceText = (id, params = {}, variant = 'full') => {
95
+ const isShort = variant === 'short';
96
+ const device = params.deviceName ?? '';
97
+ switch (id) {
98
+ case 'S1':
99
+ return getNoSearchText(params, isShort);
100
+ case 'S1b':
101
+ return getHintText(params.hint, isShort);
102
+ case 'S2': {
103
+ if (isShort)
104
+ return { text: `Ready to ride with ${device}.` };
105
+ const tail = `You can add optional sensors, or press ${params.rideMode ? 'Start' : 'OK'}.`;
106
+ return { text: `Ready to ride with ${device}. ${tail}` };
107
+ }
108
+ case 'S3':
109
+ if (params.isTrainer)
110
+ return { text: isShort ? `Connecting to ${device}…` : `Connecting to your trainer ${device}…` };
111
+ return { text: `Connecting to ${device}…` };
112
+ case 'S4':
113
+ return { text: 'Getting ready to search…' };
114
+ case 'S5':
115
+ return isShort
116
+ ? { text: 'Searching… Pedal to wake your trainer.' }
117
+ : { text: 'Searching for devices… Tip: pedal a few strokes to wake up your trainer.' };
118
+ case 'row-required':
119
+ return { text: 'TO RIDE', subtext: 'connect any one' };
120
+ case 'row-required-trainer':
121
+ return { text: 'TO RIDE', subtext: 'connecting to your trainer' };
122
+ case 'row-ready':
123
+ return { text: '✓ READY TO RIDE' };
124
+ case 'row-optional':
125
+ return { text: 'OPTIONAL', subtext: 'extras, not needed' };
126
+ }
127
+ };
128
+ export const getPairingRowLabelId = (trainerSelected) => trainerSelected ? 'row-required-trainer' : 'row-required';
129
+ export const getCapabilityHelpText = (capability, variant = 'full') => {
130
+ const isShort = variant === 'short';
131
+ switch (capability) {
132
+ case 'control': return 'Smart trainer';
133
+ case 'power': return 'Power meter';
134
+ case 'speed': return 'Speed sensor';
135
+ case 'heartrate': return 'Heart-rate monitor';
136
+ case 'cadence': return 'Cadence sensor';
137
+ case 'app_control': return isShort ? 'Remote buttons' : 'Remote buttons, e.g. Zwift Play';
138
+ default: return undefined;
139
+ }
140
+ };
141
+ export const getEmptyTileFooterText = (role) => role === 'required' ? 'Searching…' : 'Optional';
@@ -1,2 +1,4 @@
1
1
  export * from './service';
2
2
  export * from './model';
3
+ export * from './guidance';
4
+ export * from './status';
@@ -82,6 +82,9 @@ let DevicePairingService = (() => {
82
82
  settings = {};
83
83
  state = { initialized: false, deleted: [] };
84
84
  deviceSelectState = null;
85
+ static STALLED_AFTER_MS = 30 * 1000;
86
+ stalledTimer;
87
+ stalledLogged = false;
85
88
  onPairingStartedHandler = this.onPairingStarted.bind(this);
86
89
  onPairingSuccessHandler = this.onPairingSuccess.bind(this);
87
90
  onPairingErrorHandler = this.onPairingError.bind(this);
@@ -113,6 +116,7 @@ let DevicePairingService = (() => {
113
116
  }
114
117
  async start(onStateChanged) {
115
118
  this.pairingConfirmed = false;
119
+ this.stalledLogged = false;
116
120
  if (this.state.stopped) {
117
121
  this.state.stopped = false;
118
122
  this.state.deleted = [];
@@ -171,6 +175,7 @@ let DevicePairingService = (() => {
171
175
  this.access.off('interface-changed', this.onInterfaceStateChangedHandler);
172
176
  }
173
177
  async stop(adapters = [], forExit = false) {
178
+ this.clearStalledTimer();
174
179
  const adapterFilter = adapters ?? [];
175
180
  if (!forExit) {
176
181
  this.logEvent({ message: 'Stop Pairing (Skip)' });
@@ -318,7 +323,7 @@ let DevicePairingService = (() => {
318
323
  async unselectDevices(capability) {
319
324
  try {
320
325
  const adapater = this.getDeviceConfiguration().getSelected(capability);
321
- this.getDeviceConfiguration().unselect(capability, true);
326
+ this.getDeviceConfiguration().disableCapability(capability, true);
322
327
  if (adapater?.isStarted()) {
323
328
  this.restart();
324
329
  }
@@ -327,6 +332,15 @@ let DevicePairingService = (() => {
327
332
  this.logError(err, 'deleteDevice');
328
333
  }
329
334
  }
335
+ useCapability(capability) {
336
+ try {
337
+ this.getDeviceConfiguration().disableCapability(capability, false);
338
+ this.restart().catch(err => { this.logError(err, 'useCapability'); });
339
+ }
340
+ catch (err) {
341
+ this.logError(err, 'useCapability');
342
+ }
343
+ }
330
344
  async changeInterfaceSettings(name, settings) {
331
345
  try {
332
346
  const _interface = this.state.interfaces.find(i => i.name === name);
@@ -371,6 +385,7 @@ let DevicePairingService = (() => {
371
385
  if (timeSincePrev < 3000) {
372
386
  this.state.tsPrevStart = -1;
373
387
  await sleep(3000 - timeSincePrev);
388
+ delete this.state.tsPrevStart;
374
389
  }
375
390
  }
376
391
  if (wasActive) {
@@ -462,6 +477,7 @@ let DevicePairingService = (() => {
462
477
  emitStateChange(newState) {
463
478
  const { onStateChanged, onDeviceSelectStateChanged } = this.settings || {};
464
479
  this.checkCanStart();
480
+ this.checkPairingStalled();
465
481
  if (this.state.stopRequested)
466
482
  return;
467
483
  const state = this.getExternaState(newState);
@@ -474,6 +490,43 @@ let DevicePairingService = (() => {
474
490
  if (onDeviceSelectStateChanged && typeof onDeviceSelectStateChanged === 'function')
475
491
  onDeviceSelectStateChanged(this.getDeviceSelectionState());
476
492
  }
493
+ checkPairingStalled() {
494
+ if (this.stalledLogged)
495
+ return;
496
+ if (!this.isStalled()) {
497
+ this.clearStalledTimer();
498
+ return;
499
+ }
500
+ if (!this.stalledTimer)
501
+ this.stalledTimer = setTimeout(() => { this.onPairingStalled(); }, DevicePairingService.STALLED_AFTER_MS);
502
+ }
503
+ isStalled() {
504
+ if (this.isPairing() || this.isScanning())
505
+ return false;
506
+ const selected = (this.state.capabilities ?? []).filter(c => c.selected);
507
+ return selected.length > 0 && selected.every(c => c.connectState === 'waiting');
508
+ }
509
+ onPairingStalled() {
510
+ delete this.stalledTimer;
511
+ if (this.stalledLogged || !this.isStalled())
512
+ return;
513
+ this.stalledLogged = true;
514
+ this.logEvent({
515
+ message: 'pairing stalled',
516
+ usage: this.usage,
517
+ isPairing: this.isPairing(),
518
+ isScanning: this.isScanning(),
519
+ waiting: Boolean(this.state.waiting),
520
+ sentinel: this.state.tsPrevStart === -1,
521
+ interfaces: (this.state.interfaces ?? []).map(i => ({ name: i.name, state: i.state })),
522
+ capabilities: (this.state.capabilities ?? []).filter(c => c.selected).map(c => ({ capability: c.capability, connectState: c.connectState })),
523
+ });
524
+ }
525
+ clearStalledTimer() {
526
+ if (this.stalledTimer)
527
+ clearTimeout(this.stalledTimer);
528
+ delete this.stalledTimer;
529
+ }
477
530
  emitStartStatus() {
478
531
  const { onStateChanged } = this.settings || {};
479
532
  const { canStartRide } = this.state;
@@ -0,0 +1,49 @@
1
+ import { derivePairingStatus, getPairingGuidanceText, getUnavailableInterfaces } from './guidance';
2
+ const REQUIRED_CAPABILITIES = ['control', 'power', 'speed'];
3
+ export const toPairingInterfaceStates = (interfaces) => interfaces.map(i => ({
4
+ id: i.name,
5
+ enabled: i.enabled !== false,
6
+ available: i.state !== 'disconnected' && i.state !== 'unavailable',
7
+ }));
8
+ const DOT_BY_STATUS = { S1: 'red', S2: 'green' };
9
+ const statusDot = (id) => DOT_BY_STATUS[id] ?? 'amber';
10
+ export const getPairingStatusDisplay = (src) => {
11
+ const find = (name) => src.capabilities.find(c => c.capability === name);
12
+ const readyCapability = REQUIRED_CAPABILITIES
13
+ .map(find)
14
+ .find(c => c?.selected && c.connectState === 'connected');
15
+ const connectingCapability = src.capabilities.find(c => c.selected && c.connectState === 'connecting');
16
+ const anyDeviceSelected = src.capabilities.some(c => Boolean(c.selected));
17
+ const derived = derivePairingStatus({
18
+ platform: src.platform,
19
+ interfaces: src.interfaces,
20
+ anyDeviceSelected,
21
+ initialising: src.loading,
22
+ searching: false,
23
+ canStartRide: src.canStartRide,
24
+ readyDeviceName: readyCapability?.deviceName,
25
+ connectingDeviceName: connectingCapability?.deviceName,
26
+ connectingIsTrainer: connectingCapability?.capability === 'control',
27
+ });
28
+ let id = derived === 'S1b' ? 'S5' : derived;
29
+ if (src.loading)
30
+ id = 'S4';
31
+ const deviceName = id === 'S3' ? connectingCapability?.deviceName : readyCapability?.deviceName;
32
+ const params = {
33
+ platform: src.platform,
34
+ unavailable: getUnavailableInterfaces(src.interfaces, src.platform),
35
+ deviceName,
36
+ isTrainer: connectingCapability?.capability === 'control',
37
+ rideMode: src.rideMode,
38
+ };
39
+ const full = getPairingGuidanceText(id, params, 'full');
40
+ const short = getPairingGuidanceText(id, params, 'short');
41
+ return {
42
+ id,
43
+ text: full.text,
44
+ shortText: short.text,
45
+ link: full.link,
46
+ shortLink: short.link,
47
+ dot: statusDot(id),
48
+ };
49
+ };