@nobody-os/react-native-background-geolocation 0.1.1 → 0.1.2
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/README.md +10 -8
- package/lib/module/geo/recorder.js +158 -1
- package/lib/module/geo/recorder.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/geo/recorder.d.ts +67 -0
- package/lib/typescript/src/geo/recorder.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/geo/recorder.ts +226 -1
- package/src/index.tsx +2 -0
package/README.md
CHANGED
|
@@ -91,14 +91,16 @@ Plus the pure layer, exported from the same package: `haversineMeters`,
|
|
|
91
91
|
|
|
92
92
|
## Roadmap
|
|
93
93
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
- **v0.
|
|
98
|
-
|
|
99
|
-
- **v0.3:**
|
|
100
|
-
|
|
101
|
-
|
|
94
|
+
The full plan lives in [ROADMAP.md](ROADMAP.md) and in the issue
|
|
95
|
+
tracker. The short version:
|
|
96
|
+
|
|
97
|
+
- **v0.2:** Android foreground service and iOS state restoration, so
|
|
98
|
+
tracking survives the app being swiped away or terminated.
|
|
99
|
+
- **v0.3:** Activity-aware sampling and an offline fix queue with batch
|
|
100
|
+
sync, for battery life and reliability.
|
|
101
|
+
- **v0.4:** Geofencing events, free like everything else here.
|
|
102
|
+
- Geofencing aside: GPX import/export helpers and a MapLibre-based
|
|
103
|
+
example with live route drawing are also on the list.
|
|
102
104
|
|
|
103
105
|
## Development
|
|
104
106
|
|
|
@@ -11,20 +11,42 @@ import { shouldAcceptFix, DEFAULT_FILTER_OPTIONS } from "./filter.js";
|
|
|
11
11
|
* built on the native engine.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* One continuous stretch of a track, bounded by pauses. When the recorder
|
|
16
|
+
* stays stopped for at least `pauseThresholdMs`, the current segment is
|
|
17
|
+
* closed at the last moving fix and the next moving fix opens a new one.
|
|
18
|
+
* The pause itself belongs to no segment.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Everything an app needs to render a finished (or live) activity. */
|
|
22
|
+
|
|
14
23
|
const DEFAULT_SPLIT_LENGTH = 1000;
|
|
15
24
|
const DEFAULT_STOP_SPEED = 0.5;
|
|
25
|
+
const DEFAULT_PAUSE_THRESHOLD_MS = 180_000;
|
|
16
26
|
export class TrackRecorder {
|
|
17
27
|
fixes = [];
|
|
18
28
|
_distanceMeters = 0;
|
|
19
29
|
_movingMs = 0;
|
|
20
30
|
_elapsedMs = 0;
|
|
31
|
+
_elevationGainMeters = 0;
|
|
21
32
|
_splits = [];
|
|
22
33
|
distanceAtSplitStart = 0;
|
|
23
34
|
timeAtSplitStart = 0;
|
|
35
|
+
completedSegments = [];
|
|
36
|
+
segment = null;
|
|
37
|
+
/** Stopped time accumulated in the current, not-yet-a-pause stop. */
|
|
38
|
+
stopEpisodeMs = 0;
|
|
39
|
+
/**
|
|
40
|
+
* Segment state captured when the current stop began. If the stop turns
|
|
41
|
+
* into a pause, the segment is closed from this snapshot, so the stopped
|
|
42
|
+
* fixes never join the segment they interrupted.
|
|
43
|
+
*/
|
|
44
|
+
stopSnapshot = null;
|
|
24
45
|
constructor(options = {}) {
|
|
25
46
|
this.filterOptions = options.filter ?? DEFAULT_FILTER_OPTIONS;
|
|
26
47
|
this.splitLengthMeters = options.splitLengthMeters ?? DEFAULT_SPLIT_LENGTH;
|
|
27
48
|
this.stopSpeedThresholdMps = options.stopSpeedThresholdMps ?? DEFAULT_STOP_SPEED;
|
|
49
|
+
this.pauseThresholdMs = options.pauseThresholdMs ?? DEFAULT_PAUSE_THRESHOLD_MS;
|
|
28
50
|
}
|
|
29
51
|
|
|
30
52
|
/**
|
|
@@ -41,11 +63,23 @@ export class TrackRecorder {
|
|
|
41
63
|
const segmentMs = fix.timestamp - last.timestamp;
|
|
42
64
|
this._distanceMeters += segmentMeters;
|
|
43
65
|
this._elapsedMs += segmentMs;
|
|
66
|
+
const altitudeDelta = fix.altitude - last.altitude;
|
|
67
|
+
if (altitudeDelta > 0) {
|
|
68
|
+
this._elevationGainMeters += altitudeDelta;
|
|
69
|
+
}
|
|
44
70
|
const impliedSpeed = segmentMs > 0 ? segmentMeters / (segmentMs / 1000) : 0;
|
|
45
71
|
const reportedSpeed = fix.speed >= 0 ? fix.speed : impliedSpeed;
|
|
46
|
-
|
|
72
|
+
const pairMoving = reportedSpeed >= this.stopSpeedThresholdMps;
|
|
73
|
+
// Sparse-fix pause: the engine emitted nothing while the user was
|
|
74
|
+
// stopped (stationary fixes are filtered out), so the pause shows up
|
|
75
|
+
// as one long, barely-moving gap before a MOVING fix. (A stopped
|
|
76
|
+
// pair, however long, belongs to the stop-episode accounting below,
|
|
77
|
+
// never to this rule.)
|
|
78
|
+
const isLongPauseGap = pairMoving && segmentMs >= this.pauseThresholdMs && impliedSpeed < this.stopSpeedThresholdMps;
|
|
79
|
+
if (pairMoving && !isLongPauseGap) {
|
|
47
80
|
this._movingMs += segmentMs;
|
|
48
81
|
}
|
|
82
|
+
this.recordSegmentPair(fix, segmentMeters, segmentMs, pairMoving, isLongPauseGap);
|
|
49
83
|
if (this._distanceMeters - this.distanceAtSplitStart >= this.splitLengthMeters) {
|
|
50
84
|
const splitDuration = fix.timestamp - this.timeAtSplitStart;
|
|
51
85
|
const splitDistance = this._distanceMeters - this.distanceAtSplitStart;
|
|
@@ -59,11 +93,82 @@ export class TrackRecorder {
|
|
|
59
93
|
}
|
|
60
94
|
} else {
|
|
61
95
|
this.timeAtSplitStart = fix.timestamp;
|
|
96
|
+
this.segment = {
|
|
97
|
+
fixCount: 1,
|
|
98
|
+
distanceMeters: 0,
|
|
99
|
+
movingMs: 0,
|
|
100
|
+
startTimestamp: fix.timestamp,
|
|
101
|
+
lastTimestamp: fix.timestamp
|
|
102
|
+
};
|
|
62
103
|
}
|
|
63
104
|
this.fixes.push(fix);
|
|
64
105
|
return true;
|
|
65
106
|
}
|
|
66
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Fold one accepted pair (previous fix -> this fix) into the segment
|
|
110
|
+
* bookkeeping, closing and reopening segments around pauses.
|
|
111
|
+
*/
|
|
112
|
+
recordSegmentPair(fix, segmentMeters, segmentMs, pairMoving, isLongPauseGap) {
|
|
113
|
+
if (!this.segment) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (isLongPauseGap) {
|
|
117
|
+
// Close the segment at the last fix before the gap (the snapshot if
|
|
118
|
+
// a stop episode was in progress, else the live accumulators) and
|
|
119
|
+
// open a new segment at this fix.
|
|
120
|
+
const closing = this.stopSnapshot ?? this.segment;
|
|
121
|
+
this.completedSegments.push(toTrackSegment(closing));
|
|
122
|
+
this.segment = {
|
|
123
|
+
fixCount: 1,
|
|
124
|
+
distanceMeters: 0,
|
|
125
|
+
movingMs: 0,
|
|
126
|
+
startTimestamp: fix.timestamp,
|
|
127
|
+
lastTimestamp: fix.timestamp
|
|
128
|
+
};
|
|
129
|
+
this.stopEpisodeMs = 0;
|
|
130
|
+
this.stopSnapshot = null;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (pairMoving) {
|
|
134
|
+
if (this.stopSnapshot !== null && this.stopEpisodeMs >= this.pauseThresholdMs) {
|
|
135
|
+
// The stop became a pause: close the segment where movement last
|
|
136
|
+
// happened, and let this fix open a fresh segment. The bridging
|
|
137
|
+
// distance (GPS drift at a standstill) counts toward the track
|
|
138
|
+
// totals but belongs to neither segment.
|
|
139
|
+
this.completedSegments.push(toTrackSegment(this.stopSnapshot));
|
|
140
|
+
this.segment = {
|
|
141
|
+
fixCount: 1,
|
|
142
|
+
distanceMeters: 0,
|
|
143
|
+
movingMs: 0,
|
|
144
|
+
startTimestamp: fix.timestamp,
|
|
145
|
+
lastTimestamp: fix.timestamp
|
|
146
|
+
};
|
|
147
|
+
} else {
|
|
148
|
+
this.segment.fixCount += 1;
|
|
149
|
+
this.segment.distanceMeters += segmentMeters;
|
|
150
|
+
this.segment.movingMs += segmentMs;
|
|
151
|
+
this.segment.lastTimestamp = fix.timestamp;
|
|
152
|
+
}
|
|
153
|
+
this.stopEpisodeMs = 0;
|
|
154
|
+
this.stopSnapshot = null;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Stopped pair: tentatively part of the current segment, unless the
|
|
159
|
+
// stop grows into a pause, in which case the snapshot taken here is
|
|
160
|
+
// what gets closed.
|
|
161
|
+
if (this.stopSnapshot === null) {
|
|
162
|
+
this.stopSnapshot = {
|
|
163
|
+
...this.segment
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
this.stopEpisodeMs += segmentMs;
|
|
167
|
+
this.segment.fixCount += 1;
|
|
168
|
+
this.segment.distanceMeters += segmentMeters;
|
|
169
|
+
this.segment.lastTimestamp = fix.timestamp;
|
|
170
|
+
}
|
|
171
|
+
|
|
67
172
|
/** Accepted fixes, in order. */
|
|
68
173
|
get points() {
|
|
69
174
|
return [...this.fixes];
|
|
@@ -101,6 +206,43 @@ export class TrackRecorder {
|
|
|
101
206
|
return [...this._splits];
|
|
102
207
|
}
|
|
103
208
|
|
|
209
|
+
/** Total climbing, in meters: positive altitude changes only. */
|
|
210
|
+
get elevationGainMeters() {
|
|
211
|
+
return this._elevationGainMeters;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* All segments, finished ones plus the one still in progress. When the
|
|
216
|
+
* recorder is sitting in a pause that has passed the threshold, the
|
|
217
|
+
* interrupted segment is already shown closed at its last moving fix.
|
|
218
|
+
*/
|
|
219
|
+
get segments() {
|
|
220
|
+
const visible = [...this.completedSegments];
|
|
221
|
+
if (this.stopSnapshot !== null && this.stopEpisodeMs >= this.pauseThresholdMs) {
|
|
222
|
+
visible.push(toTrackSegment(this.stopSnapshot));
|
|
223
|
+
} else if (this.segment !== null && this.segment.fixCount > 0) {
|
|
224
|
+
visible.push(toTrackSegment(this.segment));
|
|
225
|
+
}
|
|
226
|
+
return visible;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** One-call snapshot of the whole track, for rendering an activity. */
|
|
230
|
+
summary() {
|
|
231
|
+
const segments = this.segments;
|
|
232
|
+
return {
|
|
233
|
+
distanceMeters: this._distanceMeters,
|
|
234
|
+
elapsedMs: this._elapsedMs,
|
|
235
|
+
movingMs: this._movingMs,
|
|
236
|
+
stoppedMs: this.stoppedMs,
|
|
237
|
+
elevationGainMeters: this._elevationGainMeters,
|
|
238
|
+
averagePaceSecondsPerKm: this.averagePaceSecondsPerKm,
|
|
239
|
+
fixCount: this.fixes.length,
|
|
240
|
+
segmentCount: segments.length,
|
|
241
|
+
segments,
|
|
242
|
+
splits: this.splits
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
104
246
|
/** Average pace over the whole track, seconds per kilometer. */
|
|
105
247
|
get averagePaceSecondsPerKm() {
|
|
106
248
|
if (this._distanceMeters === 0 || this._movingMs === 0) {
|
|
@@ -113,9 +255,24 @@ export class TrackRecorder {
|
|
|
113
255
|
this._distanceMeters = 0;
|
|
114
256
|
this._movingMs = 0;
|
|
115
257
|
this._elapsedMs = 0;
|
|
258
|
+
this._elevationGainMeters = 0;
|
|
116
259
|
this._splits = [];
|
|
117
260
|
this.distanceAtSplitStart = 0;
|
|
118
261
|
this.timeAtSplitStart = 0;
|
|
262
|
+
this.completedSegments = [];
|
|
263
|
+
this.segment = null;
|
|
264
|
+
this.stopEpisodeMs = 0;
|
|
265
|
+
this.stopSnapshot = null;
|
|
119
266
|
}
|
|
120
267
|
}
|
|
268
|
+
function toTrackSegment(acc) {
|
|
269
|
+
return {
|
|
270
|
+
startTimestamp: acc.startTimestamp,
|
|
271
|
+
endTimestamp: acc.lastTimestamp,
|
|
272
|
+
durationMs: acc.lastTimestamp - acc.startTimestamp,
|
|
273
|
+
distanceMeters: acc.distanceMeters,
|
|
274
|
+
movingMs: acc.movingMs,
|
|
275
|
+
fixCount: acc.fixCount
|
|
276
|
+
};
|
|
277
|
+
}
|
|
121
278
|
//# sourceMappingURL=recorder.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["haversineMeters","shouldAcceptFix","DEFAULT_FILTER_OPTIONS","DEFAULT_SPLIT_LENGTH","DEFAULT_STOP_SPEED","TrackRecorder","fixes","_distanceMeters","_movingMs","_elapsedMs","_splits","distanceAtSplitStart","timeAtSplitStart","constructor","options","filterOptions","filter","splitLengthMeters","stopSpeedThresholdMps","addFix","fix","last","length","segmentMeters","segmentMs","timestamp","impliedSpeed","reportedSpeed","speed","splitDuration","splitDistance","push","distanceMeters","durationMs","paceSecondsPerKm","
|
|
1
|
+
{"version":3,"names":["haversineMeters","shouldAcceptFix","DEFAULT_FILTER_OPTIONS","DEFAULT_SPLIT_LENGTH","DEFAULT_STOP_SPEED","DEFAULT_PAUSE_THRESHOLD_MS","TrackRecorder","fixes","_distanceMeters","_movingMs","_elapsedMs","_elevationGainMeters","_splits","distanceAtSplitStart","timeAtSplitStart","completedSegments","segment","stopEpisodeMs","stopSnapshot","constructor","options","filterOptions","filter","splitLengthMeters","stopSpeedThresholdMps","pauseThresholdMs","addFix","fix","last","length","segmentMeters","segmentMs","timestamp","altitudeDelta","altitude","impliedSpeed","reportedSpeed","speed","pairMoving","isLongPauseGap","recordSegmentPair","splitDuration","splitDistance","push","distanceMeters","durationMs","paceSecondsPerKm","fixCount","movingMs","startTimestamp","lastTimestamp","closing","toTrackSegment","points","elapsedMs","stoppedMs","isStopped","undefined","splits","elevationGainMeters","segments","visible","summary","averagePaceSecondsPerKm","segmentCount","reset","acc","endTimestamp"],"sourceRoot":"../../../src","sources":["geo/recorder.ts"],"mappings":";;AAAA,SAASA,eAAe,QAAQ,gBAAa;AAC7C,SACEC,eAAe,EACfC,sBAAsB,QAGjB,aAAU;;AAEjB;AACA;AACA;AACA;AACA;AACA;AACA;;AAgBA;AACA;AACA;AAC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I;IACnB,IAAI,CAACC,aAAa,GAAG,CAAC;IACtB,IAAI,CAACC,YAAY,GAAG,IAAI;EAC1B;AACF;AAEA,SAASkC,cAAcA,CAACc,GAAwB,EAAgB;EAC9D,OAAO;IACLjB,cAAc,EAAEiB,GAAG,CAACjB,cAAc;IAClCkB,YAAY,EAAED,GAAG,CAAChB,aAAa;IAC/BL,UAAU,EAAEqB,GAAG,CAAChB,aAAa,GAAGgB,GAAG,CAACjB,cAAc;IAClDL,cAAc,EAAEsB,GAAG,CAACtB,cAAc;IAClCI,QAAQ,EAAEkB,GAAG,CAAClB,QAAQ;IACtBD,QAAQ,EAAEmB,GAAG,CAACnB;EAChB,CAAC;AACH","ignoreList":[]}
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NativeBackgroundGeolocation","haversineMeters","totalDistanceMeters","filterByAccuracy","durationSeconds","averagePaceMinPerKm","shouldAcceptFix","hasAcceptableAccuracy","isChronological","isFarEnough","DEFAULT_FILTER_OPTIONS","TrackRecorder","start","config","stop","getCurrentPosition","isTracking","addLocationListener","listener","onLocation","addErrorListener","onError"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,OAAOA,2BAA2B,MAI3B,6CAA0C;AAIjD,SACEC,eAAe,EACfC,mBAAmB,EACnBC,gBAAgB,EAChBC,eAAe,EACfC,mBAAmB,QAGd,eAAY;AACnB,SACEC,eAAe,EACfC,qBAAqB,EACrBC,eAAe,EACfC,WAAW,EACXC,sBAAsB,QAGjB,iBAAc;AACrB,SACEC,aAAa,
|
|
1
|
+
{"version":3,"names":["NativeBackgroundGeolocation","haversineMeters","totalDistanceMeters","filterByAccuracy","durationSeconds","averagePaceMinPerKm","shouldAcceptFix","hasAcceptableAccuracy","isChronological","isFarEnough","DEFAULT_FILTER_OPTIONS","TrackRecorder","start","config","stop","getCurrentPosition","isTracking","addLocationListener","listener","onLocation","addErrorListener","onError"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,OAAOA,2BAA2B,MAI3B,6CAA0C;AAIjD,SACEC,eAAe,EACfC,mBAAmB,EACnBC,gBAAgB,EAChBC,eAAe,EACfC,mBAAmB,QAGd,eAAY;AACnB,SACEC,eAAe,EACfC,qBAAqB,EACrBC,eAAe,EACfC,WAAW,EACXC,sBAAsB,QAGjB,iBAAc;AACrB,SACEC,aAAa,QAMR,mBAAgB;AAMvB;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,KAAKA,CAACC,MAAyB,GAAG,CAAC,CAAC,EAAiB;EACnE,OAAOb,2BAA2B,CAACY,KAAK,CAACC,MAAM,CAAC;AAClD;;AAEA;AACA,OAAO,SAASC,IAAIA,CAAA,EAAkB;EACpC,OAAOd,2BAA2B,CAACc,IAAI,CAAC,CAAC;AAC3C;;AAEA;AACA;AACA;AACA;AACA,OAAO,SAASC,kBAAkBA,CAAA,EAAyB;EACzD,OAAOf,2BAA2B,CAACe,kBAAkB,CAAC,CAAC;AACzD;;AAEA;AACA,OAAO,SAASC,UAAUA,CAAA,EAAqB;EAC7C,OAAOhB,2BAA2B,CAACgB,UAAU,CAAC,CAAC;AACjD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,mBAAmBA,CACjCC,QAAoC,EACd;EACtB,OAAOlB,2BAA2B,CAACmB,UAAU,CAACD,QAAQ,CAAC;AACzD;;AAEA;AACA,OAAO,SAASE,gBAAgBA,CAC9BF,QAA2C,EACrB;EACtB,OAAOlB,2BAA2B,CAACqB,OAAO,CAACH,QAAQ,CAAC;AACtD;AAEA,eAAe;EACbN,KAAK;EACLE,IAAI;EACJC,kBAAkB;EAClBC,UAAU;EACVC,mBAAmB;EACnBG;AACF,CAAC","ignoreList":[]}
|
|
@@ -18,6 +18,41 @@ export type Split = {
|
|
|
18
18
|
/** Average pace for the split, seconds per kilometer. */
|
|
19
19
|
paceSecondsPerKm: number;
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* One continuous stretch of a track, bounded by pauses. When the recorder
|
|
23
|
+
* stays stopped for at least `pauseThresholdMs`, the current segment is
|
|
24
|
+
* closed at the last moving fix and the next moving fix opens a new one.
|
|
25
|
+
* The pause itself belongs to no segment.
|
|
26
|
+
*/
|
|
27
|
+
export type TrackSegment = {
|
|
28
|
+
/** Timestamp of the segment's first fix. */
|
|
29
|
+
startTimestamp: number;
|
|
30
|
+
/** Timestamp of the segment's last fix. */
|
|
31
|
+
endTimestamp: number;
|
|
32
|
+
/** endTimestamp - startTimestamp, in milliseconds. */
|
|
33
|
+
durationMs: number;
|
|
34
|
+
/** Distance covered inside the segment, in meters. */
|
|
35
|
+
distanceMeters: number;
|
|
36
|
+
/** Time spent moving inside the segment, in milliseconds. */
|
|
37
|
+
movingMs: number;
|
|
38
|
+
/** Accepted fixes in the segment. */
|
|
39
|
+
fixCount: number;
|
|
40
|
+
};
|
|
41
|
+
/** Everything an app needs to render a finished (or live) activity. */
|
|
42
|
+
export type TripSummary = {
|
|
43
|
+
distanceMeters: number;
|
|
44
|
+
elapsedMs: number;
|
|
45
|
+
movingMs: number;
|
|
46
|
+
stoppedMs: number;
|
|
47
|
+
/** Sum of positive altitude changes, in meters. Descents are ignored. */
|
|
48
|
+
elevationGainMeters: number;
|
|
49
|
+
/** Moving-time pace, seconds per kilometer. 0 when undefined. */
|
|
50
|
+
averagePaceSecondsPerKm: number;
|
|
51
|
+
fixCount: number;
|
|
52
|
+
segmentCount: number;
|
|
53
|
+
segments: TrackSegment[];
|
|
54
|
+
splits: Split[];
|
|
55
|
+
};
|
|
21
56
|
export type RecorderOptions = {
|
|
22
57
|
filter?: FilterOptions;
|
|
23
58
|
/** Length of each split in meters. Default 1000 (per-kilometer splits). */
|
|
@@ -27,24 +62,46 @@ export type RecorderOptions = {
|
|
|
27
62
|
* previous fix) stays below this, in meters per second. Default 0.5.
|
|
28
63
|
*/
|
|
29
64
|
stopSpeedThresholdMps?: number;
|
|
65
|
+
/**
|
|
66
|
+
* A stop lasting at least this long ends the current segment; the next
|
|
67
|
+
* moving fix starts a new one. Default 180000 (3 minutes).
|
|
68
|
+
*/
|
|
69
|
+
pauseThresholdMs?: number;
|
|
30
70
|
};
|
|
31
71
|
export declare class TrackRecorder {
|
|
32
72
|
private readonly filterOptions;
|
|
33
73
|
private readonly splitLengthMeters;
|
|
34
74
|
private readonly stopSpeedThresholdMps;
|
|
75
|
+
private readonly pauseThresholdMs;
|
|
35
76
|
private fixes;
|
|
36
77
|
private _distanceMeters;
|
|
37
78
|
private _movingMs;
|
|
38
79
|
private _elapsedMs;
|
|
80
|
+
private _elevationGainMeters;
|
|
39
81
|
private _splits;
|
|
40
82
|
private distanceAtSplitStart;
|
|
41
83
|
private timeAtSplitStart;
|
|
84
|
+
private completedSegments;
|
|
85
|
+
private segment;
|
|
86
|
+
/** Stopped time accumulated in the current, not-yet-a-pause stop. */
|
|
87
|
+
private stopEpisodeMs;
|
|
88
|
+
/**
|
|
89
|
+
* Segment state captured when the current stop began. If the stop turns
|
|
90
|
+
* into a pause, the segment is closed from this snapshot, so the stopped
|
|
91
|
+
* fixes never join the segment they interrupted.
|
|
92
|
+
*/
|
|
93
|
+
private stopSnapshot;
|
|
42
94
|
constructor(options?: RecorderOptions);
|
|
43
95
|
/**
|
|
44
96
|
* Offer a raw fix to the track. Returns true when the fix was accepted
|
|
45
97
|
* and recorded, false when a filter rule dropped it.
|
|
46
98
|
*/
|
|
47
99
|
addFix(fix: TrackFix): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Fold one accepted pair (previous fix -> this fix) into the segment
|
|
102
|
+
* bookkeeping, closing and reopening segments around pauses.
|
|
103
|
+
*/
|
|
104
|
+
private recordSegmentPair;
|
|
48
105
|
/** Accepted fixes, in order. */
|
|
49
106
|
get points(): TrackFix[];
|
|
50
107
|
get distanceMeters(): number;
|
|
@@ -58,6 +115,16 @@ export declare class TrackRecorder {
|
|
|
58
115
|
get isStopped(): boolean;
|
|
59
116
|
/** Completed splits so far. */
|
|
60
117
|
get splits(): Split[];
|
|
118
|
+
/** Total climbing, in meters: positive altitude changes only. */
|
|
119
|
+
get elevationGainMeters(): number;
|
|
120
|
+
/**
|
|
121
|
+
* All segments, finished ones plus the one still in progress. When the
|
|
122
|
+
* recorder is sitting in a pause that has passed the threshold, the
|
|
123
|
+
* interrupted segment is already shown closed at its last moving fix.
|
|
124
|
+
*/
|
|
125
|
+
get segments(): TrackSegment[];
|
|
126
|
+
/** One-call snapshot of the whole track, for rendering an activity. */
|
|
127
|
+
summary(): TripSummary;
|
|
61
128
|
/** Average pace over the whole track, seconds per kilometer. */
|
|
62
129
|
get averagePaceSecondsPerKm(): number;
|
|
63
130
|
reset(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recorder.d.ts","sourceRoot":"","sources":["../../../../src/geo/recorder.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,OAAO,EACb,MAAM,aAAU,CAAC;AAElB;;;;;;GAMG;AAEH,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,KAAK,GAAG;IAClB,2EAA2E;IAC3E,cAAc,EAAE,MAAM,CAAC;IACvB,8CAA8C;IAC9C,UAAU,EAAE,MAAM,CAAC;IACnB,yDAAyD;IACzD,gBAAgB,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,2EAA2E;IAC3E,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"recorder.d.ts","sourceRoot":"","sources":["../../../../src/geo/recorder.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,O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,IAAI,MAAM,CAEhC;IAED;;;;OAIG;IACH,IAAI,QAAQ,IAAI,YAAY,EAAE,CAW7B;IAED,uEAAuE;IACvE,OAAO,IAAI,WAAW;IAgBtB,gEAAgE;IAChE,IAAI,uBAAuB,IAAI,MAAM,CAKpC;IAED,KAAK,IAAI,IAAI;CAcd"}
|
|
@@ -2,7 +2,7 @@ import NativeBackgroundGeolocation, { type GeolocationConfig, type GeolocationEr
|
|
|
2
2
|
export type { GeolocationConfig, GeolocationError, LocationFix };
|
|
3
3
|
export { haversineMeters, totalDistanceMeters, filterByAccuracy, durationSeconds, averagePaceMinPerKm, type Coordinate, type GeoFix, } from './location.js';
|
|
4
4
|
export { shouldAcceptFix, hasAcceptableAccuracy, isChronological, isFarEnough, DEFAULT_FILTER_OPTIONS, type FilterOptions, type FixLike, } from './geo/filter.js';
|
|
5
|
-
export { TrackRecorder, type TrackFix, type Split, type RecorderOptions, } from './geo/recorder.js';
|
|
5
|
+
export { TrackRecorder, type TrackFix, type Split, type TrackSegment, type TripSummary, type RecorderOptions, } from './geo/recorder.js';
|
|
6
6
|
type ListenerSubscription = ReturnType<typeof NativeBackgroundGeolocation.onLocation>;
|
|
7
7
|
/**
|
|
8
8
|
* Start background location tracking.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,EAAE,EAClC,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EACjB,MAAM,6CAA0C,CAAC;AAElD,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,WAAW,EAAE,CAAC;AAEjE,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EACnB,KAAK,UAAU,EACf,KAAK,MAAM,GACZ,MAAM,eAAY,CAAC;AACpB,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,eAAe,EACf,WA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EACL,eAAe,EACf,qBAAqB,EACrB,eAAe,EACf,WAAW,EACX,sBAAsB,EACtB,KAAK,aAAa,EAClB,KAAK,OAAO,GACb,MAAM,iBAAc,CAAC;AACtB,OAAO,EACL,aAAa,EACb,KAAK,QAAQ,EACb,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,eAAe,GACrB,MAAM,mBAAgB,CAAC;AAExB,KAAK,oBAAoB,GAAG,UAAU,CACpC,OAAO,2BAA2B,CAAC,UAAU,CAC9C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,MAAM,GAAE,iBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAEnE;AAED,mDAAmD;AACnD,wBAAgB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAEpC;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,OAAO,CAAC,WAAW,CAAC,CAEzD;AAED,oDAAoD;AACpD,wBAAgB,UAAU,IAAI,OAAO,CAAC,OAAO,CAAC,CAE7C;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,IAAI,GACnC,oBAAoB,CAEtB;AAED,2EAA2E;AAC3E,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAC1C,oBAAoB,CAEtB;;;;;;;;;AAED,wBAOE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nobody-os/react-native-background-geolocation",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Open-source background geolocation for React Native: continuous background location tracking for iOS and Android, MIT licensed.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
package/src/geo/recorder.ts
CHANGED
|
@@ -28,6 +28,43 @@ export type Split = {
|
|
|
28
28
|
paceSecondsPerKm: number;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* One continuous stretch of a track, bounded by pauses. When the recorder
|
|
33
|
+
* stays stopped for at least `pauseThresholdMs`, the current segment is
|
|
34
|
+
* closed at the last moving fix and the next moving fix opens a new one.
|
|
35
|
+
* The pause itself belongs to no segment.
|
|
36
|
+
*/
|
|
37
|
+
export type TrackSegment = {
|
|
38
|
+
/** Timestamp of the segment's first fix. */
|
|
39
|
+
startTimestamp: number;
|
|
40
|
+
/** Timestamp of the segment's last fix. */
|
|
41
|
+
endTimestamp: number;
|
|
42
|
+
/** endTimestamp - startTimestamp, in milliseconds. */
|
|
43
|
+
durationMs: number;
|
|
44
|
+
/** Distance covered inside the segment, in meters. */
|
|
45
|
+
distanceMeters: number;
|
|
46
|
+
/** Time spent moving inside the segment, in milliseconds. */
|
|
47
|
+
movingMs: number;
|
|
48
|
+
/** Accepted fixes in the segment. */
|
|
49
|
+
fixCount: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Everything an app needs to render a finished (or live) activity. */
|
|
53
|
+
export type TripSummary = {
|
|
54
|
+
distanceMeters: number;
|
|
55
|
+
elapsedMs: number;
|
|
56
|
+
movingMs: number;
|
|
57
|
+
stoppedMs: number;
|
|
58
|
+
/** Sum of positive altitude changes, in meters. Descents are ignored. */
|
|
59
|
+
elevationGainMeters: number;
|
|
60
|
+
/** Moving-time pace, seconds per kilometer. 0 when undefined. */
|
|
61
|
+
averagePaceSecondsPerKm: number;
|
|
62
|
+
fixCount: number;
|
|
63
|
+
segmentCount: number;
|
|
64
|
+
segments: TrackSegment[];
|
|
65
|
+
splits: Split[];
|
|
66
|
+
};
|
|
67
|
+
|
|
31
68
|
export type RecorderOptions = {
|
|
32
69
|
filter?: FilterOptions;
|
|
33
70
|
/** Length of each split in meters. Default 1000 (per-kilometer splits). */
|
|
@@ -37,29 +74,58 @@ export type RecorderOptions = {
|
|
|
37
74
|
* previous fix) stays below this, in meters per second. Default 0.5.
|
|
38
75
|
*/
|
|
39
76
|
stopSpeedThresholdMps?: number;
|
|
77
|
+
/**
|
|
78
|
+
* A stop lasting at least this long ends the current segment; the next
|
|
79
|
+
* moving fix starts a new one. Default 180000 (3 minutes).
|
|
80
|
+
*/
|
|
81
|
+
pauseThresholdMs?: number;
|
|
40
82
|
};
|
|
41
83
|
|
|
42
84
|
const DEFAULT_SPLIT_LENGTH = 1000;
|
|
43
85
|
const DEFAULT_STOP_SPEED = 0.5;
|
|
86
|
+
const DEFAULT_PAUSE_THRESHOLD_MS = 180_000;
|
|
87
|
+
|
|
88
|
+
type SegmentAccumulators = {
|
|
89
|
+
fixCount: number;
|
|
90
|
+
distanceMeters: number;
|
|
91
|
+
movingMs: number;
|
|
92
|
+
startTimestamp: number;
|
|
93
|
+
lastTimestamp: number;
|
|
94
|
+
};
|
|
44
95
|
|
|
45
96
|
export class TrackRecorder {
|
|
46
97
|
private readonly filterOptions: FilterOptions;
|
|
47
98
|
private readonly splitLengthMeters: number;
|
|
48
99
|
private readonly stopSpeedThresholdMps: number;
|
|
100
|
+
private readonly pauseThresholdMs: number;
|
|
49
101
|
|
|
50
102
|
private fixes: TrackFix[] = [];
|
|
51
103
|
private _distanceMeters = 0;
|
|
52
104
|
private _movingMs = 0;
|
|
53
105
|
private _elapsedMs = 0;
|
|
106
|
+
private _elevationGainMeters = 0;
|
|
54
107
|
private _splits: Split[] = [];
|
|
55
108
|
private distanceAtSplitStart = 0;
|
|
56
109
|
private timeAtSplitStart = 0;
|
|
57
110
|
|
|
111
|
+
private completedSegments: TrackSegment[] = [];
|
|
112
|
+
private segment: SegmentAccumulators | null = null;
|
|
113
|
+
/** Stopped time accumulated in the current, not-yet-a-pause stop. */
|
|
114
|
+
private stopEpisodeMs = 0;
|
|
115
|
+
/**
|
|
116
|
+
* Segment state captured when the current stop began. If the stop turns
|
|
117
|
+
* into a pause, the segment is closed from this snapshot, so the stopped
|
|
118
|
+
* fixes never join the segment they interrupted.
|
|
119
|
+
*/
|
|
120
|
+
private stopSnapshot: SegmentAccumulators | null = null;
|
|
121
|
+
|
|
58
122
|
constructor(options: RecorderOptions = {}) {
|
|
59
123
|
this.filterOptions = options.filter ?? DEFAULT_FILTER_OPTIONS;
|
|
60
124
|
this.splitLengthMeters = options.splitLengthMeters ?? DEFAULT_SPLIT_LENGTH;
|
|
61
125
|
this.stopSpeedThresholdMps =
|
|
62
126
|
options.stopSpeedThresholdMps ?? DEFAULT_STOP_SPEED;
|
|
127
|
+
this.pauseThresholdMs =
|
|
128
|
+
options.pauseThresholdMs ?? DEFAULT_PAUSE_THRESHOLD_MS;
|
|
63
129
|
}
|
|
64
130
|
|
|
65
131
|
/**
|
|
@@ -78,13 +144,36 @@ export class TrackRecorder {
|
|
|
78
144
|
this._distanceMeters += segmentMeters;
|
|
79
145
|
this._elapsedMs += segmentMs;
|
|
80
146
|
|
|
147
|
+
const altitudeDelta = fix.altitude - last.altitude;
|
|
148
|
+
if (altitudeDelta > 0) {
|
|
149
|
+
this._elevationGainMeters += altitudeDelta;
|
|
150
|
+
}
|
|
151
|
+
|
|
81
152
|
const impliedSpeed =
|
|
82
153
|
segmentMs > 0 ? segmentMeters / (segmentMs / 1000) : 0;
|
|
83
154
|
const reportedSpeed = fix.speed >= 0 ? fix.speed : impliedSpeed;
|
|
84
|
-
|
|
155
|
+
const pairMoving = reportedSpeed >= this.stopSpeedThresholdMps;
|
|
156
|
+
// Sparse-fix pause: the engine emitted nothing while the user was
|
|
157
|
+
// stopped (stationary fixes are filtered out), so the pause shows up
|
|
158
|
+
// as one long, barely-moving gap before a MOVING fix. (A stopped
|
|
159
|
+
// pair, however long, belongs to the stop-episode accounting below,
|
|
160
|
+
// never to this rule.)
|
|
161
|
+
const isLongPauseGap =
|
|
162
|
+
pairMoving &&
|
|
163
|
+
segmentMs >= this.pauseThresholdMs &&
|
|
164
|
+
impliedSpeed < this.stopSpeedThresholdMps;
|
|
165
|
+
if (pairMoving && !isLongPauseGap) {
|
|
85
166
|
this._movingMs += segmentMs;
|
|
86
167
|
}
|
|
87
168
|
|
|
169
|
+
this.recordSegmentPair(
|
|
170
|
+
fix,
|
|
171
|
+
segmentMeters,
|
|
172
|
+
segmentMs,
|
|
173
|
+
pairMoving,
|
|
174
|
+
isLongPauseGap
|
|
175
|
+
);
|
|
176
|
+
|
|
88
177
|
if (
|
|
89
178
|
this._distanceMeters - this.distanceAtSplitStart >=
|
|
90
179
|
this.splitLengthMeters
|
|
@@ -104,12 +193,92 @@ export class TrackRecorder {
|
|
|
104
193
|
}
|
|
105
194
|
} else {
|
|
106
195
|
this.timeAtSplitStart = fix.timestamp;
|
|
196
|
+
this.segment = {
|
|
197
|
+
fixCount: 1,
|
|
198
|
+
distanceMeters: 0,
|
|
199
|
+
movingMs: 0,
|
|
200
|
+
startTimestamp: fix.timestamp,
|
|
201
|
+
lastTimestamp: fix.timestamp,
|
|
202
|
+
};
|
|
107
203
|
}
|
|
108
204
|
|
|
109
205
|
this.fixes.push(fix);
|
|
110
206
|
return true;
|
|
111
207
|
}
|
|
112
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Fold one accepted pair (previous fix -> this fix) into the segment
|
|
211
|
+
* bookkeeping, closing and reopening segments around pauses.
|
|
212
|
+
*/
|
|
213
|
+
private recordSegmentPair(
|
|
214
|
+
fix: TrackFix,
|
|
215
|
+
segmentMeters: number,
|
|
216
|
+
segmentMs: number,
|
|
217
|
+
pairMoving: boolean,
|
|
218
|
+
isLongPauseGap: boolean
|
|
219
|
+
): void {
|
|
220
|
+
if (!this.segment) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (isLongPauseGap) {
|
|
225
|
+
// Close the segment at the last fix before the gap (the snapshot if
|
|
226
|
+
// a stop episode was in progress, else the live accumulators) and
|
|
227
|
+
// open a new segment at this fix.
|
|
228
|
+
const closing = this.stopSnapshot ?? this.segment;
|
|
229
|
+
this.completedSegments.push(toTrackSegment(closing));
|
|
230
|
+
this.segment = {
|
|
231
|
+
fixCount: 1,
|
|
232
|
+
distanceMeters: 0,
|
|
233
|
+
movingMs: 0,
|
|
234
|
+
startTimestamp: fix.timestamp,
|
|
235
|
+
lastTimestamp: fix.timestamp,
|
|
236
|
+
};
|
|
237
|
+
this.stopEpisodeMs = 0;
|
|
238
|
+
this.stopSnapshot = null;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (pairMoving) {
|
|
243
|
+
if (
|
|
244
|
+
this.stopSnapshot !== null &&
|
|
245
|
+
this.stopEpisodeMs >= this.pauseThresholdMs
|
|
246
|
+
) {
|
|
247
|
+
// The stop became a pause: close the segment where movement last
|
|
248
|
+
// happened, and let this fix open a fresh segment. The bridging
|
|
249
|
+
// distance (GPS drift at a standstill) counts toward the track
|
|
250
|
+
// totals but belongs to neither segment.
|
|
251
|
+
this.completedSegments.push(toTrackSegment(this.stopSnapshot));
|
|
252
|
+
this.segment = {
|
|
253
|
+
fixCount: 1,
|
|
254
|
+
distanceMeters: 0,
|
|
255
|
+
movingMs: 0,
|
|
256
|
+
startTimestamp: fix.timestamp,
|
|
257
|
+
lastTimestamp: fix.timestamp,
|
|
258
|
+
};
|
|
259
|
+
} else {
|
|
260
|
+
this.segment.fixCount += 1;
|
|
261
|
+
this.segment.distanceMeters += segmentMeters;
|
|
262
|
+
this.segment.movingMs += segmentMs;
|
|
263
|
+
this.segment.lastTimestamp = fix.timestamp;
|
|
264
|
+
}
|
|
265
|
+
this.stopEpisodeMs = 0;
|
|
266
|
+
this.stopSnapshot = null;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Stopped pair: tentatively part of the current segment, unless the
|
|
271
|
+
// stop grows into a pause, in which case the snapshot taken here is
|
|
272
|
+
// what gets closed.
|
|
273
|
+
if (this.stopSnapshot === null) {
|
|
274
|
+
this.stopSnapshot = { ...this.segment };
|
|
275
|
+
}
|
|
276
|
+
this.stopEpisodeMs += segmentMs;
|
|
277
|
+
this.segment.fixCount += 1;
|
|
278
|
+
this.segment.distanceMeters += segmentMeters;
|
|
279
|
+
this.segment.lastTimestamp = fix.timestamp;
|
|
280
|
+
}
|
|
281
|
+
|
|
113
282
|
/** Accepted fixes, in order. */
|
|
114
283
|
get points(): TrackFix[] {
|
|
115
284
|
return [...this.fixes];
|
|
@@ -150,6 +319,46 @@ export class TrackRecorder {
|
|
|
150
319
|
return [...this._splits];
|
|
151
320
|
}
|
|
152
321
|
|
|
322
|
+
/** Total climbing, in meters: positive altitude changes only. */
|
|
323
|
+
get elevationGainMeters(): number {
|
|
324
|
+
return this._elevationGainMeters;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* All segments, finished ones plus the one still in progress. When the
|
|
329
|
+
* recorder is sitting in a pause that has passed the threshold, the
|
|
330
|
+
* interrupted segment is already shown closed at its last moving fix.
|
|
331
|
+
*/
|
|
332
|
+
get segments(): TrackSegment[] {
|
|
333
|
+
const visible = [...this.completedSegments];
|
|
334
|
+
if (
|
|
335
|
+
this.stopSnapshot !== null &&
|
|
336
|
+
this.stopEpisodeMs >= this.pauseThresholdMs
|
|
337
|
+
) {
|
|
338
|
+
visible.push(toTrackSegment(this.stopSnapshot));
|
|
339
|
+
} else if (this.segment !== null && this.segment.fixCount > 0) {
|
|
340
|
+
visible.push(toTrackSegment(this.segment));
|
|
341
|
+
}
|
|
342
|
+
return visible;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** One-call snapshot of the whole track, for rendering an activity. */
|
|
346
|
+
summary(): TripSummary {
|
|
347
|
+
const segments = this.segments;
|
|
348
|
+
return {
|
|
349
|
+
distanceMeters: this._distanceMeters,
|
|
350
|
+
elapsedMs: this._elapsedMs,
|
|
351
|
+
movingMs: this._movingMs,
|
|
352
|
+
stoppedMs: this.stoppedMs,
|
|
353
|
+
elevationGainMeters: this._elevationGainMeters,
|
|
354
|
+
averagePaceSecondsPerKm: this.averagePaceSecondsPerKm,
|
|
355
|
+
fixCount: this.fixes.length,
|
|
356
|
+
segmentCount: segments.length,
|
|
357
|
+
segments,
|
|
358
|
+
splits: this.splits,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
153
362
|
/** Average pace over the whole track, seconds per kilometer. */
|
|
154
363
|
get averagePaceSecondsPerKm(): number {
|
|
155
364
|
if (this._distanceMeters === 0 || this._movingMs === 0) {
|
|
@@ -163,8 +372,24 @@ export class TrackRecorder {
|
|
|
163
372
|
this._distanceMeters = 0;
|
|
164
373
|
this._movingMs = 0;
|
|
165
374
|
this._elapsedMs = 0;
|
|
375
|
+
this._elevationGainMeters = 0;
|
|
166
376
|
this._splits = [];
|
|
167
377
|
this.distanceAtSplitStart = 0;
|
|
168
378
|
this.timeAtSplitStart = 0;
|
|
379
|
+
this.completedSegments = [];
|
|
380
|
+
this.segment = null;
|
|
381
|
+
this.stopEpisodeMs = 0;
|
|
382
|
+
this.stopSnapshot = null;
|
|
169
383
|
}
|
|
170
384
|
}
|
|
385
|
+
|
|
386
|
+
function toTrackSegment(acc: SegmentAccumulators): TrackSegment {
|
|
387
|
+
return {
|
|
388
|
+
startTimestamp: acc.startTimestamp,
|
|
389
|
+
endTimestamp: acc.lastTimestamp,
|
|
390
|
+
durationMs: acc.lastTimestamp - acc.startTimestamp,
|
|
391
|
+
distanceMeters: acc.distanceMeters,
|
|
392
|
+
movingMs: acc.movingMs,
|
|
393
|
+
fixCount: acc.fixCount,
|
|
394
|
+
};
|
|
395
|
+
}
|