From 16ca39a1604a911accfd8eae169f7fd80cf45dc5 Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 8 Nov 2022 21:09:33 +0100 Subject: [PATCH] Track points --- src/db/lib.test.ts | 41 +++++++++++++++++++++++ src/db/trk.ts | 2 +- src/db/trkpt.test.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++ src/db/trkpt.ts | 50 ++++++++++++++++++++++++++++ src/db/trkseg.ts | 2 +- src/db/types.d.ts | 38 +++++++++++++++++++-- 6 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 src/db/trkpt.test.ts create mode 100644 src/db/trkpt.ts diff --git a/src/db/lib.test.ts b/src/db/lib.test.ts index 630fe48..230dc96 100644 --- a/src/db/lib.test.ts +++ b/src/db/lib.test.ts @@ -2,6 +2,7 @@ import { initDb } from '.'; import uri from '../lib/ids'; import { getFamily } from './lib'; import { putNewTrk } from './trk'; +import { putNewTrkpt } from './trkpt'; import { putNewTrkseg } from './trkseg'; declare global { @@ -134,6 +135,46 @@ Object { ], "total_rows": 6, } +`); + }); + test('returns four rows after a gpx and a track point have been inserted.', async () => { + await putNewTrkpt(); + const allDocs: any = await getFamily(uri('gpx', { gpx: '0' })); + expect(allDocs).toMatchInlineSnapshot(` +Object { + "offset": 0, + "rows": Array [ + Object { + "id": "gpx/0", + "key": "gpx/0", + "value": Object { + "rev": "1-98d42c99fcf471a500b0506a60e77559", + }, + }, + Object { + "id": "gpx/0/2trk/00000", + "key": "gpx/0/2trk/00000", + "value": Object { + "rev": "1-1e197ef8965a436730f80cabaaaa6f70", + }, + }, + Object { + "id": "gpx/0/2trk/00000/00000", + "key": "gpx/0/2trk/00000/00000", + "value": Object { + "rev": "1-33cb714b4d5553cc20fc7a753c63cf51", + }, + }, + Object { + "id": "gpx/0/2trk/00000/00000/00000", + "key": "gpx/0/2trk/00000/00000/00000", + "value": Object { + "rev": "1-c88eea2fc5a5637322a3f3357b49618f", + }, + }, + ], + "total_rows": 7, +} `); }); }); diff --git a/src/db/trk.ts b/src/db/trk.ts index 0997d6d..3a65646 100644 --- a/src/db/trk.ts +++ b/src/db/trk.ts @@ -29,5 +29,5 @@ export const putNewTrk = async (id?: IdTrk | IdGpx) => { }, emptyTrk ); - return finalId; + return finalId as IdTrk; }; diff --git a/src/db/trkpt.test.ts b/src/db/trkpt.test.ts new file mode 100644 index 0000000..ab7db77 --- /dev/null +++ b/src/db/trkpt.test.ts @@ -0,0 +1,79 @@ +import { putNewTrkpt } from './trkpt'; + +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; + +describe('The trkpt module', () => { + beforeEach(() => { + globalThis.db = { put: jest.fn() }; + globalThis.Date.now = () => 0; + }); + afterEach(() => { + globalThis.db = originalDb; + globalThis.Date.now = originalDateNow; + }); + test('db.put() a new trkpt when required', async () => { + putNewTrkpt({ + gpx: 'gpxId', + trk: 'trkid', + trkseg: 'trksegid', + trkpt: 'trkptid', + }); + await expect(globalThis.db.put).toBeCalledWith({ + _id: 'gpx/gpxId/2trk/trkid/trksegid/trkptid', + _rev: undefined, + doc: { + $: { lat: 0, lon: 0 }, + ageofdgpsdata: undefined, + cmt: undefined, + desc: undefined, + dgpsid: undefined, + ele: undefined, + extensions: { + 'dyo:accuracy': undefined, + 'dyo:batterylevel': undefined, + 'dyo:course': undefined, + 'dyo:speed': undefined, + 'dyo:useragent': undefined, + }, + fix: undefined, + geoidheight: undefined, + hdop: undefined, + link: undefined, + magvar: undefined, + name: undefined, + pdop: undefined, + sat: undefined, + src: undefined, + sym: undefined, + time: undefined, + type: undefined, + vdop: undefined, + }, + type: 'trkpt', + }); + }); + test('db.put() generates an id for the trk if needed', async () => { + const id = await putNewTrkpt({ gpx: 'gpxId' }); + expect(id).toEqual({ + gpx: 'gpxId', + trk: '00000', + trkseg: '00000', + trkpt: '00000', + }); + }); + test('db.put() generates ids for both gpx and trk if needed', async () => { + const id = await putNewTrkpt(); + expect(id).toEqual({ + gpx: '0', + trk: '00000', + trkseg: '00000', + trkpt: '00000', + }); + }); +}); diff --git a/src/db/trkpt.ts b/src/db/trkpt.ts new file mode 100644 index 0000000..473ef65 --- /dev/null +++ b/src/db/trkpt.ts @@ -0,0 +1,50 @@ +import getUri from '../lib/ids'; +import { put } from './lib'; +import { putNewTrkseg } from './trkseg'; + +const emptyTrkpt: Wpt = { + $: { lat: 0, lon: 0 }, + ele: undefined, + time: undefined, + magvar: undefined, + geoidheight: undefined, + name: undefined, + cmt: undefined, + desc: undefined, + src: undefined, + link: undefined, + sym: undefined, + type: undefined, + fix: undefined, + sat: undefined, + hdop: undefined, + vdop: undefined, + pdop: undefined, + ageofdgpsdata: undefined, + dgpsid: undefined, + extensions: { + 'dyo:speed': undefined, + 'dyo:course': undefined, + 'dyo:accuracy': undefined, + 'dyo:batterylevel': undefined, + 'dyo:useragent': undefined, + }, +}; + +export const putNewTrkpt = async (id?: IdTrk | IdGpx | IdTrkseg | IdTrkpt) => { + let finalId = { ...id }; + if (!('trkpt' in finalId)) { + const trksegId = await putNewTrkseg(id); + finalId = { ...trksegId, trkpt: '00000' }; + } + const uri = getUri('trkpt', finalId); + await put( + uri, + 'trkpt', + (trkpt) => { + return trkpt; + }, + emptyTrkpt + ); + return finalId; +}; diff --git a/src/db/trkseg.ts b/src/db/trkseg.ts index e202b32..9a27cea 100644 --- a/src/db/trkseg.ts +++ b/src/db/trkseg.ts @@ -22,5 +22,5 @@ export const putNewTrkseg = async (id?: IdTrk | IdGpx | IdTrkseg) => { }, emptyTrkseg ); - return finalId; + return finalId as IdTrkseg; }; diff --git a/src/db/types.d.ts b/src/db/types.d.ts index 445413f..6d5cea3 100644 --- a/src/db/types.d.ts +++ b/src/db/types.d.ts @@ -31,7 +31,13 @@ interface Metadata { extensions?: Extensions; } -interface Extensions {} +interface Extensions { + 'dyo:speed'?: number; + 'dyo:course'?: number; + 'dyo:accuracy'?: number; + 'dyo:batterylevel'?: number; + 'dyo:useragent'?: string; +} interface Trk { name?: string; @@ -56,6 +62,34 @@ interface Link_ { } interface Trkseg { - trkpt: any[]; + trkpt?: Wpt[]; extensions?: Extensions; } + +interface Wpt { + $: Trkpt_; + ele?: number; + time?: string; + magvar?: number; + geoidheight?: number; + name?: string; + cmt?: string; + desc?: string; + src?: string; + link?: Link; + sym?: string; + type?: string; + fix?: string; + sat?: number; + hdop?: number; + vdop?: number; + pdop?: number; + ageofdgpsdata?: number; + dgpsid?: number; + extensions: Extensions; +} + +interface Trkpt_ { + lat: number; + lon: number; +}