ulogger-server/js/test/helpers/trackfactory.js

109 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-12-14 17:01:26 +01:00
/*
* μlogger
*
* Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
import uPosition from '../../src/position.js';
import uPositionSet from '../../src/positionset.js';
import uTrack from '../../src/track.js';
import uUser from '../../src/user.js';
export default class TrackFactory {
/**
* @template T
2019-12-19 18:31:25 +01:00
* @param {(number|uPosition[])=} p Positions array or number of positions to be autogenerated
2019-12-14 17:01:26 +01:00
* @param {T} type
* @param {Object} params
* @return {T}
*/
2019-12-19 18:31:25 +01:00
static getSet(p = 2, type, params) {
2019-12-14 17:01:26 +01:00
let track;
if (type === uTrack) {
track = new uTrack(params.id, params.name, params.user);
} else {
track = new uPositionSet();
}
2019-12-19 18:31:25 +01:00
if (Array.isArray(p)) {
track.fromJson(p, true);
} else {
const length = p;
if (length) {
const positions = [];
let lat = 21.01;
let lon = 52.23;
for (let i = 0; i < length; i++) {
positions.push(this.getPosition({ id: i + 1, latitude: lat, longitude: lon }));
lat += 0.5;
lon += 0.5;
}
track.fromJson(positions, true);
2019-12-14 17:01:26 +01:00
}
}
return track;
}
/**
2019-12-19 18:31:25 +01:00
* @param {(number|uPosition[])=} p Positions array or number of positions to be autogenerated
2019-12-14 17:01:26 +01:00
* @param {{ id: number, name: string, user: uUser }=} params
* @return {uTrack}
*/
2019-12-19 18:31:25 +01:00
static getTrack(p = 2, params) {
2019-12-14 17:01:26 +01:00
params = params || {};
params.id = params.id || 1;
params.name = params.name || 'test track';
params.user = params.user || new uUser(1, 'testUser');
2019-12-19 18:31:25 +01:00
return this.getSet(p, uTrack, params);
2019-12-14 17:01:26 +01:00
}
/**
2019-12-19 18:31:25 +01:00
* @param {(number|uPosition[])=} p Positions array or number of positions to be autogenerated
* @param {uPosition[]=} positions
2019-12-14 17:01:26 +01:00
* @return {uPositionSet}
*/
2019-12-19 18:31:25 +01:00
static getPositionSet(p = 2, positions) {
return this.getSet(p, uPositionSet, positions);
2019-12-14 17:01:26 +01:00
}
/**
2019-12-19 18:31:25 +01:00
* @param {Object=} params
2019-12-14 17:01:26 +01:00
* @return {uPosition}
*/
2019-12-19 18:31:25 +01:00
static getPosition(params) {
params = params || {};
2019-12-14 17:01:26 +01:00
const position = new uPosition();
2019-12-19 18:31:25 +01:00
position.id = params.id || 1;
position.latitude = params.latitude || 52.23;
position.longitude = params.longitude || 21.01;
position.altitude = params.altitude || null;
position.speed = params.speed || null;
position.bearing = params.bearing || null;
position.timestamp = params.timestamp || 1;
position.accuracy = params.accuracy || null;
position.provider = params.provider || null;
position.comment = params.comment || null;
position.image = params.image || null;
position.username = params.username || 'testUser';
position.trackid = params.trackid || 1;
position.trackname = params.trackname || 'test track';
position.meters = params.meters || 0;
position.seconds = params.seconds || 0;
2019-12-14 17:01:26 +01:00
return position;
}
}