From 20ae635d3f2cdf215e04a6ec41946678cc3411d1 Mon Sep 17 00:00:00 2001 From: evlist Date: Sun, 25 Sep 2022 08:46:05 +0200 Subject: [PATCH] Storing the current track in pouchdb. --- src/components/map/gpx-record.tsx | 2 +- src/db/tracks.ts | 57 ++++++++++++++++++++++++++++++- src/lib/background-geolocation.ts | 26 ++++++++++---- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/src/components/map/gpx-record.tsx b/src/components/map/gpx-record.tsx index f13f558..14e5fae 100644 --- a/src/components/map/gpx-record.tsx +++ b/src/components/map/gpx-record.tsx @@ -24,7 +24,7 @@ const GpxRecord: React.FC<{}> = () => { if (isRecording) { stopBackgroundGeolocation(watcher_id); } else { - startBackgroundGeolocation().then((result) => { + startBackgroundGeolocation(db).then((result) => { setWatcher_id(result); }); } diff --git a/src/db/tracks.ts b/src/db/tracks.ts index 44e1ced..4734b6d 100644 --- a/src/db/tracks.ts +++ b/src/db/tracks.ts @@ -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(); @@ -19,7 +20,61 @@ export const pushTrack = (db: any, payload: any) => { console.log(`Digest: ${_id}`); } const doc = { _id, type: 'Track', ...payload }; -// console.log(JSON.stringify(doc)); + // console.log(JSON.stringify(doc)); 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)}`); + }); +}; diff --git a/src/lib/background-geolocation.ts b/src/lib/background-geolocation.ts index a455969..99791df 100644 --- a/src/lib/background-geolocation.ts +++ b/src/lib/background-geolocation.ts @@ -1,5 +1,6 @@ import { BackgroundGeolocationPlugin } from '@capacitor-community/background-geolocation'; import { registerPlugin } from '@capacitor/core'; +import { appendTrkpt } from '../db/tracks'; const BackgroundGeolocation = registerPlugin( '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,16 +58,29 @@ 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( // gpxActions.appendTrkpt({ // trkKey: 'current', - // trkpt: { - // $: { - // lat: location.latitude, - // lon: location.longitude, - // }, + // trkpt: { + // $: { + // lat: location.latitude, + // lon: location.longitude, + // }, // }, // }) // );