incyclist-services 1.8.7 → 1.8.9

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 (40) 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/routes/list/loaders/api.js +3 -2
  12. package/lib/cjs/ui/service.js +39 -0
  13. package/lib/cjs/utils/settleWithLimit.js +20 -0
  14. package/lib/esm/devices/page/index.js +2 -0
  15. package/lib/esm/devices/page/service.js +70 -6
  16. package/lib/esm/devices/page/visit-log-factory.js +33 -0
  17. package/lib/esm/devices/page/visit-log.js +138 -0
  18. package/lib/esm/devices/page/visit-tracker-factory.js +33 -0
  19. package/lib/esm/devices/page/visit-tracker.js +137 -0
  20. package/lib/esm/devices/pairing/guidance.js +141 -0
  21. package/lib/esm/devices/pairing/index.js +2 -0
  22. package/lib/esm/devices/pairing/status.js +49 -0
  23. package/lib/esm/ride/display/service.js +16 -1
  24. package/lib/esm/routes/list/loaders/api.js +3 -2
  25. package/lib/esm/ui/service.js +40 -1
  26. package/lib/esm/utils/settleWithLimit.js +16 -0
  27. package/lib/types/devices/page/index.d.ts +2 -0
  28. package/lib/types/devices/page/service.d.ts +9 -1
  29. package/lib/types/devices/page/types.d.ts +55 -0
  30. package/lib/types/devices/page/visit-log-factory.d.ts +13 -0
  31. package/lib/types/devices/page/visit-log.d.ts +25 -0
  32. package/lib/types/devices/page/visit-tracker-factory.d.ts +13 -0
  33. package/lib/types/devices/page/visit-tracker.d.ts +25 -0
  34. package/lib/types/devices/pairing/guidance.d.ts +55 -0
  35. package/lib/types/devices/pairing/index.d.ts +2 -0
  36. package/lib/types/devices/pairing/status.d.ts +29 -0
  37. package/lib/types/ride/display/service.d.ts +2 -0
  38. package/lib/types/ui/service.d.ts +4 -0
  39. package/lib/types/utils/settleWithLimit.d.ts +1 -0
  40. 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';
@@ -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
+ };
@@ -38,7 +38,7 @@ import { Observer, Singleton } from "../../base/types";
38
38
  import { Injectable } from "../../base/decorators";
39
39
  import { Step, useWorkoutList, useWorkoutRide, Workout } from "../../workouts";
40
40
  import { useRouteList } from "../../routes";
41
- import { isVirtualShiftingEnabled as checkVirtualShiftingEnabled, useDeviceConfiguration, useDeviceRide } from "../../devices";
41
+ import { isVirtualShiftingEnabled as checkVirtualShiftingEnabled, useDeviceConfiguration, useDeviceRide, usePairingVisitTracker } from "../../devices";
42
42
  import { useUserSettings } from "../../settings";
43
43
  import { IncyclistCapability } from "incyclist-devices";
44
44
  import { formatDateTime, getLegacyInterface, waitNextTick } from "../../utils";
@@ -65,6 +65,7 @@ let RideDisplayService = (() => {
65
65
  let _getDeviceConfiguration_decorators;
66
66
  let _getUIBinding_decorators;
67
67
  let _getCoaches_decorators;
68
+ let _getPairingVisitTracker_decorators;
68
69
  var RideDisplayService = class extends _classSuper {
69
70
  static { _classThis = this; }
70
71
  static {
@@ -78,6 +79,7 @@ let RideDisplayService = (() => {
78
79
  _getDeviceConfiguration_decorators = [Injectable];
79
80
  _getUIBinding_decorators = [Injectable];
80
81
  _getCoaches_decorators = [Injectable];
82
+ _getPairingVisitTracker_decorators = [Injectable];
81
83
  __esDecorate(this, null, _getActivityRide_decorators, { kind: "method", name: "getActivityRide", static: false, private: false, access: { has: obj => "getActivityRide" in obj, get: obj => obj.getActivityRide }, metadata: _metadata }, null, _instanceExtraInitializers);
82
84
  __esDecorate(this, null, _getWorkoutRide_decorators, { kind: "method", name: "getWorkoutRide", static: false, private: false, access: { has: obj => "getWorkoutRide" in obj, get: obj => obj.getWorkoutRide }, metadata: _metadata }, null, _instanceExtraInitializers);
83
85
  __esDecorate(this, null, _getWorkoutList_decorators, { kind: "method", name: "getWorkoutList", static: false, private: false, access: { has: obj => "getWorkoutList" in obj, get: obj => obj.getWorkoutList }, metadata: _metadata }, null, _instanceExtraInitializers);
@@ -87,6 +89,7 @@ let RideDisplayService = (() => {
87
89
  __esDecorate(this, null, _getDeviceConfiguration_decorators, { kind: "method", name: "getDeviceConfiguration", static: false, private: false, access: { has: obj => "getDeviceConfiguration" in obj, get: obj => obj.getDeviceConfiguration }, metadata: _metadata }, null, _instanceExtraInitializers);
88
90
  __esDecorate(this, null, _getUIBinding_decorators, { kind: "method", name: "getUIBinding", static: false, private: false, access: { has: obj => "getUIBinding" in obj, get: obj => obj.getUIBinding }, metadata: _metadata }, null, _instanceExtraInitializers);
89
91
  __esDecorate(this, null, _getCoaches_decorators, { kind: "method", name: "getCoaches", static: false, private: false, access: { has: obj => "getCoaches" in obj, get: obj => obj.getCoaches }, metadata: _metadata }, null, _instanceExtraInitializers);
92
+ __esDecorate(this, null, _getPairingVisitTracker_decorators, { kind: "method", name: "getPairingVisitTracker", static: false, private: false, access: { has: obj => "getPairingVisitTracker" in obj, get: obj => obj.getPairingVisitTracker }, metadata: _metadata }, null, _instanceExtraInitializers);
90
93
  __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
91
94
  RideDisplayService = _classThis = _classDescriptor.value;
92
95
  if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
@@ -132,6 +135,7 @@ let RideDisplayService = (() => {
132
135
  try {
133
136
  if (simulate)
134
137
  this.enforceSimulator();
138
+ this.notifyPairingVisitRideStarted(simulate);
135
139
  const rideProps = this.getRideModeService().getLogProps();
136
140
  this.logEvent({ message: 'Start ride', ...rideProps,
137
141
  bike: this.getBike(),
@@ -151,6 +155,14 @@ let RideDisplayService = (() => {
151
155
  this.logError(err, 'start');
152
156
  }
153
157
  }
158
+ notifyPairingVisitRideStarted(simulate) {
159
+ try {
160
+ this.getPairingVisitTracker().onRideStarted({ simulate: simulate === true });
161
+ }
162
+ catch (err) {
163
+ this.logError(err, 'notifyPairingVisitRideStarted');
164
+ }
165
+ }
154
166
  startWithMissingSensors() {
155
167
  try {
156
168
  const props = this.getStartOverlayProps();
@@ -1166,6 +1178,9 @@ let RideDisplayService = (() => {
1166
1178
  getCoaches() {
1167
1179
  return getCoachesService();
1168
1180
  }
1181
+ getPairingVisitTracker() {
1182
+ return usePairingVisitTracker();
1183
+ }
1169
1184
  };
1170
1185
  return RideDisplayService = _classThis;
1171
1186
  })();
@@ -6,6 +6,8 @@ import { addDetails } from "../../base/utils/route";
6
6
  import { RoutesDbLoader } from "./db";
7
7
  import { Loader } from "./types";
8
8
  import { sleep } from "../../../utils/sleep";
9
+ import { settleWithLimit } from "../../../utils/settleWithLimit";
10
+ const ROUTE_DETAILS_CONCURRENCY = 4;
9
11
  const valid = (v) => (v !== undefined && v !== null);
10
12
  class ApiError extends Error {
11
13
  _type;
@@ -164,8 +166,7 @@ export class RoutesApiLoader extends Loader {
164
166
  this.logger.logEvent({ message: 'error', fn: 'loadDetails#unique', error: err.message, stack: err.stack });
165
167
  }
166
168
  const failed = enforced ? items : await this.loadDetailsFromRepo(items);
167
- const promises = failed.map(i => this.getDetails(i.route));
168
- const res = await Promise.allSettled(promises);
169
+ const res = await settleWithLimit(failed, ROUTE_DETAILS_CONCURRENCY, i => this.getDetails(i.route));
169
170
  await this.processLoadDetailsResult(res, items, true);
170
171
  }
171
172
  async processLoadDetailsResult(res, items, save) {
@@ -39,7 +39,7 @@ import { IncyclistService } from "../base/service";
39
39
  import { useUserSettings } from "../settings";
40
40
  import { v4 as generateUUID } from 'uuid';
41
41
  import { SerialPortProvider } from 'incyclist-devices';
42
- import { useDeviceAccess, useDeviceConfiguration, useDevicePairing } from "../devices";
42
+ import { initPairingVisitTracker, useDeviceAccess, useDeviceConfiguration, useDevicePairing, usePairingVisitTracker } from "../devices";
43
43
  import { useFreeRideService, useRouteList } from "../routes";
44
44
  import { useWorkoutList } from "../workouts";
45
45
  import { useActiveRides, useActivityList } from "../activities";
@@ -126,6 +126,7 @@ let UserInterfaceServcie = (() => {
126
126
  this.initLogging();
127
127
  this.logEvent({ message: 'App mounted', channel: platform });
128
128
  this.createUserStats();
129
+ this.initPairingVisits();
129
130
  await this.initDeviceServices();
130
131
  this.preloadData();
131
132
  this.onSessionStart();
@@ -144,6 +145,7 @@ let UserInterfaceServcie = (() => {
144
145
  this.logEvent({ message: 'onAppExit called' });
145
146
  this.stopHeartbeatWorker();
146
147
  this.sendAppExitMessage();
148
+ this.closePairingVisitOnExit();
147
149
  IncyclistPageService.closePage();
148
150
  const cleanup = async () => {
149
151
  await waitNextTick();
@@ -170,6 +172,7 @@ let UserInterfaceServcie = (() => {
170
172
  this.appState = 'Background';
171
173
  this.logEvent({ message: 'onAppPause called' });
172
174
  this.stopHeartbeatWorker();
175
+ this.onPairingVisitBackground();
173
176
  useDeviceAccess().pauseBackgroundActivity()
174
177
  .catch((err) => { this.logError(err, 'onAppPause:pauseBackgroundActivity'); });
175
178
  this.backgroundTimer = setTimeout(() => {
@@ -196,6 +199,7 @@ let UserInterfaceServcie = (() => {
196
199
  resumeRequired = false;
197
200
  }
198
201
  this.logEvent({ message: 'onAppResume called' });
202
+ this.onPairingVisitForeground();
199
203
  this.startHeartbeatWorker();
200
204
  this.backgroundPausedByService = false;
201
205
  this.appState = 'Active';
@@ -533,6 +537,41 @@ let UserInterfaceServcie = (() => {
533
537
  }
534
538
  onNewUser() {
535
539
  }
540
+ initPairingVisits() {
541
+ try {
542
+ const isNewUser = this.getUserSettings().isNewUser() ?? false;
543
+ initPairingVisitTracker(this.platform).onAppLaunch(isNewUser);
544
+ }
545
+ catch (err) {
546
+ this.logError(err, 'initPairingVisits');
547
+ }
548
+ }
549
+ closePairingVisitOnExit() {
550
+ try {
551
+ const canStartRide = useDevicePairing().getState()?.canStartRide ?? false;
552
+ usePairingVisitTracker().onAppExit(canStartRide);
553
+ }
554
+ catch (err) {
555
+ this.logError(err, 'closePairingVisitOnExit');
556
+ }
557
+ }
558
+ onPairingVisitBackground() {
559
+ try {
560
+ const canStartRide = useDevicePairing().getState()?.canStartRide;
561
+ usePairingVisitTracker().onBackground({ canStartRide });
562
+ }
563
+ catch (err) {
564
+ this.logError(err, 'onPairingVisitBackground');
565
+ }
566
+ }
567
+ onPairingVisitForeground() {
568
+ try {
569
+ usePairingVisitTracker().onForeground();
570
+ }
571
+ catch (err) {
572
+ this.logError(err, 'onPairingVisitForeground');
573
+ }
574
+ }
536
575
  getMessageQueue() {
537
576
  return getBindings().mq;
538
577
  }