@mojro/react-native-gnss-tracking-health 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +368 -2
- package/android/build.gradle +31 -0
- package/android/src/main/AndroidManifest.xml +24 -0
- package/android/src/main/java/com/gnsstrackinghealth/GnssTrackingHealthPackage.java +31 -0
- package/android/src/main/java/com/gnsstrackinghealth/GnssTrackingHealthSettingsModule.java +431 -0
- package/android/src/main/java/com/gnsstrackinghealth/autodatetime/AutoDateTimeModule.java +87 -0
- package/android/src/main/java/com/gnsstrackinghealth/autodatetime/AutoDateTimePackage.java +27 -0
- package/package.json +55 -3
- package/src/TrackingQualityMonitor.js +600 -0
- package/src/adapters/normalizeLocation.js +65 -0
- package/src/backgroundSettings.js +257 -0
- package/src/config.js +96 -0
- package/src/constants.js +161 -0
- package/src/deviceFitness.js +135 -0
- package/src/gnssMonitor.js +245 -0
- package/src/hooks/useDeviceFitness.js +65 -0
- package/src/hooks/useSystemHealth.js +54 -0
- package/src/hooks/useTimeValidation.js +42 -0
- package/src/hooks/useTrackingMonitor.js +265 -0
- package/src/hooks/useTrackingQuality.js +118 -0
- package/src/hooks/useTrackingQualityAlerts.js +22 -0
- package/src/index.d.ts +476 -0
- package/src/index.js +125 -0
- package/src/systemHealth.js +116 -0
- package/src/timeValidation.js +174 -0
- package/src/traceAnalyzer.js +100 -0
- package/src/utils/distance.js +16 -0
- package/src/utils/logger.js +38 -0
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
import { DeviceEventEmitter, Platform } from "react-native";
|
|
2
|
+
import GnssMonitor from "./gnssMonitor";
|
|
3
|
+
import { collectSystemHealth } from "./systemHealth";
|
|
4
|
+
import { assessDeviceFitness, getAndroidApiLevel } from "./deviceFitness";
|
|
5
|
+
import { normalizeLocation } from "./adapters/normalizeLocation";
|
|
6
|
+
import {
|
|
7
|
+
detectAbnormalTrace,
|
|
8
|
+
detectLocationThrottling,
|
|
9
|
+
getAccuracyMeters,
|
|
10
|
+
isGoodAccuracySample,
|
|
11
|
+
isMockedLocation,
|
|
12
|
+
isPoorTrackingSample,
|
|
13
|
+
locationTimestampMs,
|
|
14
|
+
} from "./traceAnalyzer";
|
|
15
|
+
import {
|
|
16
|
+
ALERT_SEVERITY,
|
|
17
|
+
ALERT_TYPES,
|
|
18
|
+
EVENT_NAMES,
|
|
19
|
+
} from "./constants";
|
|
20
|
+
import { getConfig, getThresholds } from "./config";
|
|
21
|
+
import { getLogger } from "./utils/logger";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Orchestrates GNSS + system health + location-trace quality alerts.
|
|
25
|
+
* Feed traces from any tracking library via ingestLocation().
|
|
26
|
+
*/
|
|
27
|
+
class TrackingQualityMonitor {
|
|
28
|
+
#running = false;
|
|
29
|
+
#tripId = null;
|
|
30
|
+
#tripStartedAt = null;
|
|
31
|
+
#tripStartSatWindowFired = false;
|
|
32
|
+
#tripStartMaxSats = 0;
|
|
33
|
+
#tripStartWindowTimer = null;
|
|
34
|
+
#systemCheckTimer = null;
|
|
35
|
+
#lastLocation = null;
|
|
36
|
+
#lastTraceAt = null;
|
|
37
|
+
#poorTrackingStartedAt = null;
|
|
38
|
+
#lastAlertAtByType = {};
|
|
39
|
+
#latestGnss = null;
|
|
40
|
+
#latestSystem = null;
|
|
41
|
+
#latestDeviceFitness = null;
|
|
42
|
+
#alertHistory = [];
|
|
43
|
+
#unsubscribeGnss = null;
|
|
44
|
+
#notifyEnabled = true;
|
|
45
|
+
#locationTrackingActive = false;
|
|
46
|
+
#loginFitnessEvaluated = false;
|
|
47
|
+
|
|
48
|
+
get isRunning() {
|
|
49
|
+
return this.#running;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get isLocationTrackingActive() {
|
|
53
|
+
return this.#locationTrackingActive;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Host must set this when native location tracking starts/stops.
|
|
58
|
+
* Session validations (poor accuracy, throttle, teleport, mock) only run while active.
|
|
59
|
+
*/
|
|
60
|
+
setLocationTrackingActive(active) {
|
|
61
|
+
const next = !!active;
|
|
62
|
+
if (this.#locationTrackingActive === next) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.#locationTrackingActive = next;
|
|
66
|
+
if (!next) {
|
|
67
|
+
this.#poorTrackingStartedAt = null;
|
|
68
|
+
}
|
|
69
|
+
this.#emitAction("TRACKING_ACTIVE", `Location tracking active=${next}`);
|
|
70
|
+
this.#emitStatus();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* One-shot device fitness at login / session restore (independent of tracking).
|
|
75
|
+
*/
|
|
76
|
+
async evaluateDeviceFitnessOnLogin({ force = false } = {}) {
|
|
77
|
+
if (this.#loginFitnessEvaluated && !force) {
|
|
78
|
+
return this.#latestDeviceFitness;
|
|
79
|
+
}
|
|
80
|
+
this.#loginFitnessEvaluated = true;
|
|
81
|
+
this.#emitAction(
|
|
82
|
+
"LOGIN_FITNESS",
|
|
83
|
+
"Running device fitness once on login (independent of tracking)"
|
|
84
|
+
);
|
|
85
|
+
return this.evaluateDeviceFitness({ logActions: true });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Snapshot used by tracking-quality screens / hooks.
|
|
90
|
+
*/
|
|
91
|
+
getStatus() {
|
|
92
|
+
return {
|
|
93
|
+
running: this.#running,
|
|
94
|
+
platform: Platform.OS,
|
|
95
|
+
apiLevel: getAndroidApiLevel(),
|
|
96
|
+
gnssSupported: GnssMonitor.isSupported,
|
|
97
|
+
gnssListening: GnssMonitor.isListening,
|
|
98
|
+
locationTrackingActive: this.#locationTrackingActive,
|
|
99
|
+
tripId: this.#tripId,
|
|
100
|
+
tripStartedAt: this.#tripStartedAt,
|
|
101
|
+
tripStartMaxSats: this.#tripStartMaxSats,
|
|
102
|
+
tripStartSatWindowFired: this.#tripStartSatWindowFired,
|
|
103
|
+
lastTraceAt: this.#lastTraceAt,
|
|
104
|
+
lastLocation: this.#lastLocation,
|
|
105
|
+
poorTrackingStartedAt: this.#poorTrackingStartedAt,
|
|
106
|
+
latestGnss: this.#latestGnss,
|
|
107
|
+
latestSystem: this.#latestSystem,
|
|
108
|
+
deviceFitness: this.#latestDeviceFitness,
|
|
109
|
+
recentAlerts: this.#alertHistory.slice(0, 20),
|
|
110
|
+
thresholds: getThresholds(),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async evaluateDeviceFitness({ logActions = true } = {}) {
|
|
115
|
+
if (logActions) {
|
|
116
|
+
this.#emitAction(
|
|
117
|
+
"EVALUATE_FITNESS",
|
|
118
|
+
"Re-evaluating device fitness (API + capability + settings)"
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const systemHealth =
|
|
122
|
+
this.#latestSystem || (await collectSystemHealth());
|
|
123
|
+
this.#latestSystem = systemHealth;
|
|
124
|
+
|
|
125
|
+
let gnssCapabilityFlags = null;
|
|
126
|
+
try {
|
|
127
|
+
const snapshot = await GnssMonitor.getSnapshot();
|
|
128
|
+
// Same as driver-app: pass capability flags from snapshot as-is.
|
|
129
|
+
// Live satellite counts / accuracy are intentionally omitted.
|
|
130
|
+
gnssCapabilityFlags = {
|
|
131
|
+
isGNSSSupported: snapshot?.isGNSSSupported,
|
|
132
|
+
supportsCn0: snapshot?.supportsCn0,
|
|
133
|
+
supportsCarrierFreq: snapshot?.supportsCarrierFreq,
|
|
134
|
+
isDualFrequencySupported: snapshot?.isDualFrequencySupported,
|
|
135
|
+
};
|
|
136
|
+
} catch (e) {}
|
|
137
|
+
|
|
138
|
+
this.#latestDeviceFitness = assessDeviceFitness({
|
|
139
|
+
apiLevel: getAndroidApiLevel(),
|
|
140
|
+
systemHealth,
|
|
141
|
+
gnssCapabilityFlags,
|
|
142
|
+
});
|
|
143
|
+
this.#emitStatus();
|
|
144
|
+
const fit = this.#latestDeviceFitness;
|
|
145
|
+
if (logActions) {
|
|
146
|
+
this.#emitAction(
|
|
147
|
+
"FITNESS_RESULT",
|
|
148
|
+
`fitness=${fit?.fitness || "?"} capable=${!!fit?.capableForTracking} tier=${fit?.gnssCapability?.tier || "?"} issues=${(fit?.settingsIssues || []).join(",") || "none"}`,
|
|
149
|
+
{ fitness: fit?.fitness }
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return this.#latestDeviceFitness;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @param {object} [options]
|
|
157
|
+
* @param {string|null} [options.tripId]
|
|
158
|
+
* @param {boolean} [options.notify] — enable showNotification callback (default true)
|
|
159
|
+
* @param {boolean} [options.notifyDriver] — alias of `notify` (driver-app parity)
|
|
160
|
+
*/
|
|
161
|
+
async start({ tripId = null, notify = true, notifyDriver } = {}) {
|
|
162
|
+
const shouldNotify =
|
|
163
|
+
notifyDriver !== undefined ? notifyDriver !== false : notify !== false;
|
|
164
|
+
this.#notifyEnabled = shouldNotify;
|
|
165
|
+
|
|
166
|
+
const isNewTrip = tripId && tripId !== this.#tripId;
|
|
167
|
+
this.#tripId = tripId || this.#tripId;
|
|
168
|
+
|
|
169
|
+
this.#emitAction(
|
|
170
|
+
"START_REQUESTED",
|
|
171
|
+
`Start monitor requested tripId=${this.#tripId || "—"} alreadyRunning=${this.#running}`
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
if (isNewTrip || (tripId && !this.#tripStartedAt)) {
|
|
175
|
+
this.#beginTripStartSatelliteWindow(tripId);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (this.#running) {
|
|
179
|
+
this.#emitAction(
|
|
180
|
+
"START_SKIPPED",
|
|
181
|
+
"Monitor already running — refreshed trip window / status only"
|
|
182
|
+
);
|
|
183
|
+
this.#emitStatus();
|
|
184
|
+
return this.getStatus();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
this.#running = true;
|
|
188
|
+
getLogger().log("starting");
|
|
189
|
+
this.#emitAction("MONITOR_STARTING", "Starting GNSS + system health loops");
|
|
190
|
+
|
|
191
|
+
this.#unsubscribeGnss = GnssMonitor.onStatus((status) => {
|
|
192
|
+
this.#latestGnss = status;
|
|
193
|
+
this.#onGnssUpdate(status);
|
|
194
|
+
DeviceEventEmitter.emit(EVENT_NAMES.GNSS_UPDATE, status);
|
|
195
|
+
this.#emitStatus();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const gnssStarted = await GnssMonitor.start();
|
|
199
|
+
this.#latestGnss = GnssMonitor.getLatestStatus();
|
|
200
|
+
|
|
201
|
+
await this.#runSystemChecks();
|
|
202
|
+
const { systemCheckIntervalMs } = getThresholds();
|
|
203
|
+
this.#systemCheckTimer = setInterval(() => {
|
|
204
|
+
this.#runSystemChecks().catch(() => {});
|
|
205
|
+
}, systemCheckIntervalMs);
|
|
206
|
+
|
|
207
|
+
this.#emitStatus();
|
|
208
|
+
this.#emitAction(
|
|
209
|
+
"MONITOR_STARTED",
|
|
210
|
+
`Monitor started gnssListening=${!!gnssStarted} tripId=${this.#tripId || "—"} systemCheckEvery=${systemCheckIntervalMs / 1000}s`
|
|
211
|
+
);
|
|
212
|
+
return this.getStatus();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
onTripStarted(tripId) {
|
|
216
|
+
this.#tripId = tripId || this.#tripId;
|
|
217
|
+
const { tripStartSatWindowMs } = getThresholds();
|
|
218
|
+
this.#emitAction(
|
|
219
|
+
"TRIP_STARTED",
|
|
220
|
+
`Trip start tripId=${this.#tripId || "—"} opening ${tripStartSatWindowMs / 1000}s sat window`
|
|
221
|
+
);
|
|
222
|
+
this.#beginTripStartSatelliteWindow(this.#tripId);
|
|
223
|
+
if (!this.#running) {
|
|
224
|
+
this.start({ tripId: this.#tripId }).catch(() => {});
|
|
225
|
+
} else {
|
|
226
|
+
this.#emitStatus();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Feed a location fix from any tracking library.
|
|
232
|
+
* Session quality validations run only while location tracking is active.
|
|
233
|
+
*/
|
|
234
|
+
ingestLocation(location) {
|
|
235
|
+
const normalized = normalizeLocation(location);
|
|
236
|
+
if (!normalized) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const now = Date.now();
|
|
241
|
+
const prev = this.#lastLocation;
|
|
242
|
+
this.#lastLocation = normalized;
|
|
243
|
+
this.#lastTraceAt = now;
|
|
244
|
+
|
|
245
|
+
if (!this.#locationTrackingActive) {
|
|
246
|
+
this.#poorTrackingStartedAt = null;
|
|
247
|
+
this.#emitStatus();
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!this.#running) {
|
|
252
|
+
this.start({ tripId: this.#tripId }).catch(() => {});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (isMockedLocation(normalized)) {
|
|
256
|
+
this.#raiseAlert({
|
|
257
|
+
type: ALERT_TYPES.LOCATION_MOCKED,
|
|
258
|
+
severity: ALERT_SEVERITY.BLOCKER,
|
|
259
|
+
title: "Mock location detected",
|
|
260
|
+
message:
|
|
261
|
+
"Fake/mock GPS is enabled. Disable mock location to continue the trip.",
|
|
262
|
+
meta: {
|
|
263
|
+
latitude: normalized.coords.latitude,
|
|
264
|
+
longitude: normalized.coords.longitude,
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const accuracyM = getAccuracyMeters(normalized);
|
|
270
|
+
const sats =
|
|
271
|
+
this.#latestGnss?.satellitesUsedInFix ||
|
|
272
|
+
this.#latestGnss?.satellitesVisible ||
|
|
273
|
+
null;
|
|
274
|
+
const { poorTrackingDurationMs, poorAccuracyThresholdM } = getThresholds();
|
|
275
|
+
|
|
276
|
+
// Good accuracy (≤ threshold meters) immediately resets the timer.
|
|
277
|
+
if (isGoodAccuracySample(accuracyM)) {
|
|
278
|
+
if (this.#poorTrackingStartedAt) {
|
|
279
|
+
this.#emitAction(
|
|
280
|
+
"POOR_TRACKING_RESET",
|
|
281
|
+
`Poor-tracking timer reset — good accuracy ${accuracyM}m (≤${poorAccuracyThresholdM}m)`
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
this.#poorTrackingStartedAt = null;
|
|
285
|
+
} else if (isPoorTrackingSample({ accuracyM, satellitesVisible: sats })) {
|
|
286
|
+
if (!this.#poorTrackingStartedAt) {
|
|
287
|
+
this.#poorTrackingStartedAt = now;
|
|
288
|
+
this.#emitAction(
|
|
289
|
+
"POOR_TRACKING_TIMER_START",
|
|
290
|
+
`Poor-tracking timer started accuracy=${accuracyM ?? "n/a"}m sats=${sats ?? "n/a"}`
|
|
291
|
+
);
|
|
292
|
+
} else if (now - this.#poorTrackingStartedAt >= poorTrackingDurationMs) {
|
|
293
|
+
this.#raiseAlert({
|
|
294
|
+
type: ALERT_TYPES.POOR_TRACKING_SUSTAINED,
|
|
295
|
+
severity: ALERT_SEVERITY.WARNING,
|
|
296
|
+
title: "Poor GPS tracking",
|
|
297
|
+
message:
|
|
298
|
+
"Location accuracy or satellite visibility has been poor for over 10 minutes.",
|
|
299
|
+
meta: {
|
|
300
|
+
accuracyM,
|
|
301
|
+
satellitesVisible: sats,
|
|
302
|
+
durationMs: now - this.#poorTrackingStartedAt,
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
this.#poorTrackingStartedAt = null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const abnormal = detectAbnormalTrace(prev, normalized);
|
|
311
|
+
if (abnormal) {
|
|
312
|
+
this.#raiseAlert({
|
|
313
|
+
type: ALERT_TYPES.ABNORMAL_TRACE,
|
|
314
|
+
severity: ALERT_SEVERITY.WARNING,
|
|
315
|
+
title: "Abnormal GPS jump detected",
|
|
316
|
+
message:
|
|
317
|
+
"A sudden GNSS teleport / drift was detected in tracking traces.",
|
|
318
|
+
meta: {
|
|
319
|
+
...abnormal,
|
|
320
|
+
from: prev?.coords,
|
|
321
|
+
to: normalized.coords,
|
|
322
|
+
locationTime: locationTimestampMs(normalized),
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
this.#emitStatus();
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Throttle: gap since last fix ≥ max(throttleMinGapMs, expectedTraceIntervalMs * multiplier).
|
|
332
|
+
* Only while location tracking is active.
|
|
333
|
+
*/
|
|
334
|
+
heartbeat() {
|
|
335
|
+
if (!this.#running || !this.#locationTrackingActive) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const throttle = detectLocationThrottling(this.#lastTraceAt);
|
|
339
|
+
if (throttle) {
|
|
340
|
+
this.#raiseAlert({
|
|
341
|
+
type: ALERT_TYPES.LOCATION_THROTTLING,
|
|
342
|
+
severity: ALERT_SEVERITY.WARNING,
|
|
343
|
+
title: "Location updates throttled",
|
|
344
|
+
message:
|
|
345
|
+
"GPS traces are arriving much slower than expected. Check battery / background restrictions.",
|
|
346
|
+
meta: throttle,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async stop({ clearTrip = true } = {}) {
|
|
352
|
+
this.#emitAction(
|
|
353
|
+
"STOP_REQUESTED",
|
|
354
|
+
`Stop monitor requested clearTrip=${!!clearTrip} wasRunning=${this.#running}`
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
if (this.#tripStartWindowTimer) {
|
|
358
|
+
clearTimeout(this.#tripStartWindowTimer);
|
|
359
|
+
this.#tripStartWindowTimer = null;
|
|
360
|
+
}
|
|
361
|
+
if (this.#systemCheckTimer) {
|
|
362
|
+
clearInterval(this.#systemCheckTimer);
|
|
363
|
+
this.#systemCheckTimer = null;
|
|
364
|
+
}
|
|
365
|
+
if (this.#unsubscribeGnss) {
|
|
366
|
+
this.#unsubscribeGnss();
|
|
367
|
+
this.#unsubscribeGnss = null;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
await GnssMonitor.stop();
|
|
371
|
+
this.#running = false;
|
|
372
|
+
|
|
373
|
+
if (clearTrip) {
|
|
374
|
+
this.#tripId = null;
|
|
375
|
+
this.#tripStartedAt = null;
|
|
376
|
+
this.#tripStartSatWindowFired = false;
|
|
377
|
+
this.#tripStartMaxSats = 0;
|
|
378
|
+
this.#poorTrackingStartedAt = null;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
this.#emitStatus();
|
|
382
|
+
this.#emitAction(
|
|
383
|
+
"MONITOR_STOPPED",
|
|
384
|
+
`Monitor stopped clearTrip=${!!clearTrip}`
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
onAlert(callback) {
|
|
389
|
+
const sub = DeviceEventEmitter.addListener(EVENT_NAMES.ALERT, callback);
|
|
390
|
+
return () => sub.remove();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
onStatus(callback) {
|
|
394
|
+
const sub = DeviceEventEmitter.addListener(EVENT_NAMES.STATUS, callback);
|
|
395
|
+
return () => sub.remove();
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
onGnssUpdate(callback) {
|
|
399
|
+
const sub = DeviceEventEmitter.addListener(
|
|
400
|
+
EVENT_NAMES.GNSS_UPDATE,
|
|
401
|
+
callback
|
|
402
|
+
);
|
|
403
|
+
return () => sub.remove();
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
onAction(callback) {
|
|
407
|
+
const sub = DeviceEventEmitter.addListener(EVENT_NAMES.ACTION, callback);
|
|
408
|
+
return () => sub.remove();
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
#emitAction(type, message, meta = null) {
|
|
412
|
+
const payload = {
|
|
413
|
+
type,
|
|
414
|
+
message,
|
|
415
|
+
meta,
|
|
416
|
+
tripId: this.#tripId,
|
|
417
|
+
at: Date.now(),
|
|
418
|
+
};
|
|
419
|
+
getLogger().log(`ACTION ${type}: ${message}`);
|
|
420
|
+
DeviceEventEmitter.emit(EVENT_NAMES.ACTION, payload);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
#beginTripStartSatelliteWindow(tripId) {
|
|
424
|
+
this.#tripStartedAt = Date.now();
|
|
425
|
+
this.#tripStartSatWindowFired = false;
|
|
426
|
+
this.#tripStartMaxSats = this.#latestGnss?.satellitesVisible || 0;
|
|
427
|
+
|
|
428
|
+
if (this.#tripStartWindowTimer) {
|
|
429
|
+
clearTimeout(this.#tripStartWindowTimer);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const { tripStartSatWindowMs } = getThresholds();
|
|
433
|
+
this.#tripStartWindowTimer = setTimeout(() => {
|
|
434
|
+
this.#evaluateTripStartSatelliteWindow();
|
|
435
|
+
}, tripStartSatWindowMs);
|
|
436
|
+
|
|
437
|
+
this.#emitAction(
|
|
438
|
+
"TRIP_SAT_WINDOW_OPEN",
|
|
439
|
+
`${tripStartSatWindowMs / 1000}s sat window opened tripId=${tripId || "—"} seedMaxSats=${this.#tripStartMaxSats}`
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
#onGnssUpdate(status) {
|
|
444
|
+
if (!status) return;
|
|
445
|
+
const visible = Number(status.satellitesVisible) || 0;
|
|
446
|
+
if (visible > this.#tripStartMaxSats) {
|
|
447
|
+
this.#tripStartMaxSats = visible;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
#evaluateTripStartSatelliteWindow() {
|
|
452
|
+
if (this.#tripStartSatWindowFired || !this.#tripStartedAt) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
this.#tripStartSatWindowFired = true;
|
|
456
|
+
|
|
457
|
+
const maxSats = this.#tripStartMaxSats;
|
|
458
|
+
const { minSatellitesForGoodFix, tripStartSatWindowMs } = getThresholds();
|
|
459
|
+
this.#emitAction(
|
|
460
|
+
"TRIP_SAT_WINDOW_CLOSED",
|
|
461
|
+
`${tripStartSatWindowMs / 1000}s sat window closed maxSats=${maxSats} threshold=${minSatellitesForGoodFix} ok=${
|
|
462
|
+
maxSats >= minSatellitesForGoodFix
|
|
463
|
+
}`
|
|
464
|
+
);
|
|
465
|
+
if (maxSats < minSatellitesForGoodFix) {
|
|
466
|
+
this.#raiseAlert({
|
|
467
|
+
type: ALERT_TYPES.LOW_SATELLITE_AFTER_TRIP_START,
|
|
468
|
+
severity: ALERT_SEVERITY.WARNING,
|
|
469
|
+
title: "Low satellite visibility",
|
|
470
|
+
message: `Fewer than ${minSatellitesForGoodFix} satellites were visible in the first ${tripStartSatWindowMs / 1000} seconds after trip start. You may be in a closed / covered area.`,
|
|
471
|
+
meta: {
|
|
472
|
+
maxSatellitesVisible: maxSats,
|
|
473
|
+
threshold: minSatellitesForGoodFix,
|
|
474
|
+
windowMs: tripStartSatWindowMs,
|
|
475
|
+
tripId: this.#tripId,
|
|
476
|
+
},
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
this.#emitStatus();
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
async #runSystemChecks() {
|
|
483
|
+
this.#emitAction("SYSTEM_CHECK", "Running system health + fitness pass");
|
|
484
|
+
const health = await collectSystemHealth();
|
|
485
|
+
this.#latestSystem = health;
|
|
486
|
+
await this.evaluateDeviceFitness({ logActions: false });
|
|
487
|
+
const fit = this.#latestDeviceFitness;
|
|
488
|
+
this.#emitAction(
|
|
489
|
+
"SYSTEM_CHECK_DONE",
|
|
490
|
+
`batt=${health?.batteryLevel ?? "?"}% LPM=${!!health?.lowPowerMode} battOpt=${!!health?.batteryOptimizationEnabled} fitness=${fit?.fitness || "?"}`
|
|
491
|
+
);
|
|
492
|
+
|
|
493
|
+
if (health.batteryOptimizationEnabled) {
|
|
494
|
+
this.#raiseAlert({
|
|
495
|
+
type: ALERT_TYPES.BATTERY_OPTIMIZATION_ENABLED,
|
|
496
|
+
severity: ALERT_SEVERITY.WARNING,
|
|
497
|
+
title: "Battery optimization enabled",
|
|
498
|
+
message:
|
|
499
|
+
"Disable battery optimization for this app to keep background tracking reliable.",
|
|
500
|
+
meta: health,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if (health.powerSaveMode || health.lowPowerMode) {
|
|
505
|
+
this.#raiseAlert({
|
|
506
|
+
type: ALERT_TYPES.LOW_POWER_MODE,
|
|
507
|
+
severity: ALERT_SEVERITY.WARNING,
|
|
508
|
+
title: "Low power / battery saver on",
|
|
509
|
+
message:
|
|
510
|
+
"Power save mode can throttle GPS. Turn it off for accurate trip tracking.",
|
|
511
|
+
meta: health,
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (health.backgroundTrackingRestricted) {
|
|
516
|
+
this.#raiseAlert({
|
|
517
|
+
type: ALERT_TYPES.BACKGROUND_TRACKING_RESTRICTED,
|
|
518
|
+
severity: ALERT_SEVERITY.WARNING,
|
|
519
|
+
title: "Background tracking may be restricted",
|
|
520
|
+
message:
|
|
521
|
+
"System battery restrictions may limit background location. Allow unrestricted battery use for this app.",
|
|
522
|
+
meta: {
|
|
523
|
+
note:
|
|
524
|
+
"Uses isIgnoringBatteryOptimizations as a proxy. Future work may also query AppOps / standby bucket.",
|
|
525
|
+
...health,
|
|
526
|
+
},
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (health.badTimeOrTimezone) {
|
|
531
|
+
this.#raiseAlert({
|
|
532
|
+
type: ALERT_TYPES.BAD_TIME_TIMEZONE,
|
|
533
|
+
severity: ALERT_SEVERITY.WARNING,
|
|
534
|
+
title: "Device time / timezone issue",
|
|
535
|
+
message:
|
|
536
|
+
"Automatic date & time appears off, or the device clock is skewed. Enable automatic time settings.",
|
|
537
|
+
meta: health,
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (this.#locationTrackingActive) {
|
|
542
|
+
this.heartbeat();
|
|
543
|
+
}
|
|
544
|
+
this.#emitStatus();
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
#raiseAlert(alert) {
|
|
548
|
+
const now = Date.now();
|
|
549
|
+
const lastAt = this.#lastAlertAtByType[alert.type] || 0;
|
|
550
|
+
if (now - lastAt < getThresholds().alertCooldownMs) {
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
this.#lastAlertAtByType[alert.type] = now;
|
|
554
|
+
|
|
555
|
+
const payload = {
|
|
556
|
+
...alert,
|
|
557
|
+
tripId: this.#tripId,
|
|
558
|
+
at: now,
|
|
559
|
+
gnss: this.#latestGnss
|
|
560
|
+
? {
|
|
561
|
+
satellitesVisible: this.#latestGnss.satellitesVisible,
|
|
562
|
+
satellitesUsedInFix: this.#latestGnss.satellitesUsedInFix,
|
|
563
|
+
}
|
|
564
|
+
: null,
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
this.#alertHistory.unshift(payload);
|
|
568
|
+
if (this.#alertHistory.length > 50) {
|
|
569
|
+
this.#alertHistory.length = 50;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
getLogger().warn(`ALERT ${payload.type}: ${payload.message}`);
|
|
573
|
+
|
|
574
|
+
const cfg = getConfig();
|
|
575
|
+
if (typeof cfg.persistAlert === "function") {
|
|
576
|
+
try {
|
|
577
|
+
cfg.persistAlert(payload);
|
|
578
|
+
} catch (e) {}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
DeviceEventEmitter.emit(EVENT_NAMES.ALERT, payload);
|
|
582
|
+
|
|
583
|
+
if (this.#notifyEnabled && typeof cfg.showNotification === "function") {
|
|
584
|
+
try {
|
|
585
|
+
cfg.showNotification(payload.title, payload.message, {
|
|
586
|
+
alert: payload,
|
|
587
|
+
debounce: true,
|
|
588
|
+
});
|
|
589
|
+
} catch (e) {}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
#emitStatus() {
|
|
594
|
+
DeviceEventEmitter.emit(EVENT_NAMES.STATUS, this.getStatus());
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const trackingQualityMonitor = new TrackingQualityMonitor();
|
|
599
|
+
export default trackingQualityMonitor;
|
|
600
|
+
export { TrackingQualityMonitor };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize location traces from any tracking library into a common shape.
|
|
3
|
+
*
|
|
4
|
+
* Accepts (examples):
|
|
5
|
+
* - react-native-background-geolocation Location
|
|
6
|
+
* - { coords: { latitude, longitude, accuracy }, timestamp, mock }
|
|
7
|
+
* - { latitude, longitude, accuracy, mocked, time }
|
|
8
|
+
* - Geolocation Position-like objects
|
|
9
|
+
*/
|
|
10
|
+
export function normalizeLocation(raw) {
|
|
11
|
+
if (!raw || typeof raw !== "object") {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const coordsSource = raw.coords || raw.coordinate || raw;
|
|
16
|
+
const latitude = Number(
|
|
17
|
+
coordsSource.latitude ?? coordsSource.lat ?? raw.latitude ?? raw.lat
|
|
18
|
+
);
|
|
19
|
+
const longitude = Number(
|
|
20
|
+
coordsSource.longitude ?? coordsSource.lng ?? coordsSource.lon ?? raw.longitude
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const accuracy = Number(
|
|
28
|
+
coordsSource.accuracy ?? raw.accuracy ?? raw.horizontalAccuracy
|
|
29
|
+
);
|
|
30
|
+
const altitude = Number(coordsSource.altitude ?? raw.altitude);
|
|
31
|
+
const speed = Number(coordsSource.speed ?? raw.speed);
|
|
32
|
+
const heading = Number(
|
|
33
|
+
coordsSource.heading ?? coordsSource.course ?? raw.heading ?? raw.course
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const timestamp =
|
|
37
|
+
raw.timestamp ??
|
|
38
|
+
raw.time ??
|
|
39
|
+
raw.age ??
|
|
40
|
+
coordsSource.timestamp ??
|
|
41
|
+
Date.now();
|
|
42
|
+
|
|
43
|
+
const mock = !!(
|
|
44
|
+
raw.mock ||
|
|
45
|
+
raw.mocked ||
|
|
46
|
+
raw.is_mock ||
|
|
47
|
+
coordsSource.mocked ||
|
|
48
|
+
coordsSource.mock
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
coords: {
|
|
53
|
+
latitude,
|
|
54
|
+
longitude,
|
|
55
|
+
accuracy: Number.isFinite(accuracy) ? accuracy : null,
|
|
56
|
+
altitude: Number.isFinite(altitude) ? altitude : null,
|
|
57
|
+
speed: Number.isFinite(speed) ? speed : null,
|
|
58
|
+
heading: Number.isFinite(heading) ? heading : null,
|
|
59
|
+
mocked: mock,
|
|
60
|
+
},
|
|
61
|
+
timestamp,
|
|
62
|
+
mock,
|
|
63
|
+
raw,
|
|
64
|
+
};
|
|
65
|
+
}
|