Track points

This commit is contained in:
Eric van der Vlist 2022-11-08 21:09:33 +01:00
parent b0f767721a
commit 16ca39a160
6 changed files with 208 additions and 4 deletions

View File

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

View File

@ -29,5 +29,5 @@ export const putNewTrk = async (id?: IdTrk | IdGpx) => {
},
emptyTrk
);
return finalId;
return finalId as IdTrk;
};

79
src/db/trkpt.test.ts Normal file
View File

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

50
src/db/trkpt.ts Normal file
View File

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

View File

@ -22,5 +22,5 @@ export const putNewTrkseg = async (id?: IdTrk | IdGpx | IdTrkseg) => {
},
emptyTrkseg
);
return finalId;
return finalId as IdTrkseg;
};

38
src/db/types.d.ts vendored
View File

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