(Re)Adding background geolocation.

This commit is contained in:
vdv committed 2022-12-13 20:59:39 +01:00
1 parent 4dbdcecadd
commit acc1d7cc1e
9 files changed
+249 -8

No files matched your search

+24 -2
View File
@@ -1,3 +1,4 @@
import { slice } from 'lodash';
import { getWpt } from '../db/wpt';
import { returnAgain } from '../workers/dispatcher-worker';
import { getAllGpxes, getGpx } from './gpx';
@@ -31,14 +32,24 @@ const sendUpdate = async (params: any) => {
};
const changeHandler = async (change: any) => {
console.log({ caller: 'ChangeHandler', change });
console.log({ caller: 'ChangeHandler', change, watches: globalThis.watches });
const { id } = change;
if (!globalThis.watches) {
globalThis.watches = new Map();
}
sendUpdate(globalThis.watches.get(id));
const parentId = id.substring(0, id.lastIndexOf('/'));
const tokens = id.split('/').slice(0, -1);
if (['1wpt', '2rte', '3trk'].includes(tokens.at(-1))) {
tokens.splice(-1);
}
const parentId = tokens.join('/');
console.log({
caller: 'ChangeHandler',
change,
watches: globalThis.watches,
parentId,
});
if (parentId === 'gpx') {
const gpxes = await getAllGpxes();
@@ -64,12 +75,23 @@ const changeHandler = async (change: any) => {
export default changeHandler;
export const getAndWatch = async (params: any) => {
console.log({
caller: 'ChangeHandler / getAndWatch',
params,
watches: globalThis.watches,
});
const { method, _dispatchId, id, ...otherParams } = params;
if (!globalThis.watches) {
globalThis.watches = new Map();
}
globalThis.watches.set(id, params);
const returnValue = await methods[<keyof typeof methods>method](params);
console.log({
caller: 'ChangeHandler / getAndWatch',
params,
returnValue,
watches: globalThis.watches,
});
return returnValue;
};
+25
View File
@@ -392,6 +392,31 @@ export const getGpx = async (params: any) => {
return gpx;
};
export const getNextGpxTrkId = async (gpxId: string) => {
const docs = await getFamily(gpxId, { include_docs: true });
const gpxIdObj = getUri('gpx', gpxId);
console.log({ caller: 'getNextGpxTrkId', gpxId, gpxIdObj, docs });
let trkId = '';
docs.rows.forEach((row: any) => {
if (row.doc.type === 'trk') {
trkId = row.doc._id;
}
});
if (trkId !== '') {
const trkIdObj = getUri('trk', trkId);
return getUri('trk', { gpx: gpxIdObj.gpx, trk: trkIdObj.trk + 1 });
}
return getUri('trk', { gpx: gpxIdObj.gpx, trk: 0 });
};
export const appendTrk = async (params: any) => {
const { gpxId, trk } = params;
const trkId = await getNextGpxTrkId(gpxId);
console.log({ caller: 'appendTrk', gpxId, trk, trkId });
await put(trkId, 'trk', (doc) => trk, {});
return trkId;
};
export const putGpx = async (params: any) => {
let { id, gpx } = params;
+1 -1
View File
@@ -10,7 +10,7 @@ export const put = async (
update: (doc: any) => any,
defaultDoc: any
) => {
var current;
let current;
try {
current = await db.get(_id);
} catch {
+29 -1
View File
@@ -1,7 +1,8 @@
import getUri from '../lib/ids';
import getUri, { intToTrkptId } from '../lib/ids';
import { appendToArray } from './gpx';
import { getFamily, put } from './lib';
import { putNewTrk } from './trk';
import { emptyWpt } from './wpt';
const emptyTrkseg: Trkseg = {
trkpt: undefined,
@@ -46,3 +47,30 @@ export const getTrkseg = async (params: any) => {
});
return trkseg;
};
export const putTrkseg = async (trkSegId: string, trkSeg = emptyTrkseg) => {
await put(
trkSegId,
'trkseg',
(doc) => {
return trkSeg;
},
emptyTrkseg
);
return trkSegId;
};
export const appendTrkpt = async (params: any) => {
const { trksegIdObj, trkpt } = params;
const idTrkseg = getUri('trkseg', trksegIdObj);
try {
await db.get(idTrkseg);
} catch {
await put(idTrkseg, 'trkseg', (doc) => doc, emptyTrkseg);
}
const trkptIdPart = intToTrkptId(new Date(trkpt.time as string).valueOf());
const trkptIdObj = { ...trksegIdObj, trkpt: trkptIdPart };
const trkptId = getUri('trkpt', trkptIdObj);
await put(trkptId, 'trkpt', (doc) => trkpt, emptyWpt);
return trkptId;
};