Storing the current track in pouchdb.

This commit is contained in:
Eric van der Vlist 2022-09-25 08:46:05 +02:00
parent 82997cd53f
commit 20ae635d3f
3 changed files with 77 additions and 8 deletions

View File

@ -24,7 +24,7 @@ const GpxRecord: React.FC<{}> = () => {
if (isRecording) {
stopBackgroundGeolocation(watcher_id);
} else {
startBackgroundGeolocation().then((result) => {
startBackgroundGeolocation(db).then((result) => {
setWatcher_id(result);
});
}

View File

@ -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)}`);
});
};

View File

@ -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,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,
// },
// },
// })
// );