Storing the current track in pouchdb.
This commit is contained in:
parent
82997cd53f
commit
20ae635d3f
|
@ -24,7 +24,7 @@ const GpxRecord: React.FC<{}> = () => {
|
|||
if (isRecording) {
|
||||
stopBackgroundGeolocation(watcher_id);
|
||||
} else {
|
||||
startBackgroundGeolocation().then((result) => {
|
||||
startBackgroundGeolocation(db).then((result) => {
|
||||
setWatcher_id(result);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import {
|
|||
string_to_bytes,
|
||||
bytes_to_base64,
|
||||
} from '@openpgp/asmcrypto.js';
|
||||
import { DBWrapper } from 'workbox-core/_private';
|
||||
|
||||
export const pushTrack = (db: any, payload: any) => {
|
||||
const sha = new Sha256();
|
||||
|
@ -23,3 +24,57 @@ export const pushTrack = (db: any, payload: any) => {
|
|||
db.put(doc);
|
||||
};
|
||||
|
||||
const CURRENT_TRACK = '---current track---';
|
||||
|
||||
const initialTrack = {
|
||||
_id: CURRENT_TRACK,
|
||||
type: 'Track',
|
||||
track: {
|
||||
$: {
|
||||
version: '1.1',
|
||||
creator: 'dyomedea version 0.000001',
|
||||
xmlns: 'http://www.topografix.com/GPX/1/1',
|
||||
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
|
||||
'xsi:schemaLocation':
|
||||
'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd',
|
||||
'xmlns:gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3',
|
||||
'xmlns:wptx1': 'http://www.garmin.com/xmlschemas/WaypointExtension/v1',
|
||||
'xmlns:gpxtpx': 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
|
||||
},
|
||||
metadata: {
|
||||
name: 'Tbd',
|
||||
desc: 'Tbd',
|
||||
time: '2022-08-27T21:35:01.000Z',
|
||||
},
|
||||
trk: [
|
||||
{
|
||||
name: 'Tbd',
|
||||
trkseg: [
|
||||
{
|
||||
trkpt: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
metadata: {
|
||||
lastModified: '2022-08-27T21:35:02.000Z',
|
||||
},
|
||||
};
|
||||
|
||||
export const appendTrkpt = async (db: any, trkpt: any) => {
|
||||
var track: any;
|
||||
await db.get(CURRENT_TRACK)
|
||||
.then((result: any) => {
|
||||
track = result;
|
||||
})
|
||||
.catch(() => {
|
||||
track = initialTrack;
|
||||
});
|
||||
track.metadata.lastModified = trkpt.time;
|
||||
const currentTrkseg = track.track.trk.at(-1).trkseg.at(-1);
|
||||
currentTrkseg.trkpt.push(trkpt);
|
||||
db.put(track).then((response: any) => {
|
||||
console.log(`db.put(track), response: ${JSON.stringify(response)}`);
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { BackgroundGeolocationPlugin } from '@capacitor-community/background-geolocation';
|
||||
import { registerPlugin } from '@capacitor/core';
|
||||
import { appendTrkpt } from '../db/tracks';
|
||||
|
||||
const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>(
|
||||
'BackgroundGeolocation'
|
||||
|
@ -34,7 +35,7 @@ const backgroundGeolocationConfig = {
|
|||
distanceFilter: 1,
|
||||
};
|
||||
|
||||
export const startBackgroundGeolocation = async () => {
|
||||
export const startBackgroundGeolocation = async (db: any) => {
|
||||
const locationHandler = (location: any, error: any) => {
|
||||
console.log('com.dyomedea.dyomedea LOG', ' - Callback');
|
||||
if (error) {
|
||||
|
@ -57,6 +58,19 @@ export const startBackgroundGeolocation = async () => {
|
|||
}
|
||||
console.log(location);
|
||||
if (location !== undefined) {
|
||||
appendTrkpt(db, {
|
||||
$: {
|
||||
lat: location.latitude,
|
||||
lon: location.longitude,
|
||||
},
|
||||
ele: location.altitude,
|
||||
time: new Date(location.time).toISOString(),
|
||||
extensions: {
|
||||
speed: location.speed,
|
||||
accuracy: location.accuracy,
|
||||
},
|
||||
});
|
||||
|
||||
//setCenter([location.latitude, location.longitude]);
|
||||
//setPosition([location.latitude, location.longitude]);
|
||||
// dispatch(
|
||||
|
|
Loading…
Reference in New Issue