ulogger-server/js/track.js

214 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-06-29 12:54:32 +02: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/>.
*/
2019-05-15 11:32:36 +02:00
import { config } from './constants.js';
import uAjax from './ajax.js';
import uData from './data.js';
import uEvent from './event.js';
import uPosition from './position.js';
/**
* @class uTrack
* @extends {uData}
* @property {number} id
* @property {string} name
* @property {uUser} user
2019-06-29 12:54:32 +02:00
* @property {boolean} continuous
2019-05-15 11:32:36 +02:00
* @property {?uPosition[]} positions
* @property {?Array<{x: number, y: number}>} plotData
2017-04-14 17:24:09 +02:00
*/
2019-05-15 11:32:36 +02:00
export default class uTrack extends uData {
2017-04-14 17:24:09 +02:00
2019-05-15 11:32:36 +02:00
/**
* @param {number} id
* @param {string} name
* @param {uUser} user
*/
constructor(id, name, user) {
super(id, name, 'id', 'name');
this._user = user;
this._positions = null;
this._plotData = null;
this._maxId = 0;
this._onlyLatest = false;
2019-06-29 12:54:32 +02:00
this._continuous = true;
2019-05-15 11:32:36 +02:00
}
2017-04-14 17:24:09 +02:00
/**
2019-05-15 11:32:36 +02:00
* @return {?uPosition[]}
*/
2019-05-15 11:32:36 +02:00
get positions() {
return this._positions;
}
/**
* @param {uUser} user
*/
set user(user) {
this._user = user;
}
/**
* @return {uUser}
*/
get user() {
return this._user;
}
2019-06-29 12:54:32 +02:00
/**
* @param {boolean} value
*/
set continuous(value) {
this._continuous = value;
}
/**
* @return {boolean}
*/
get continuous() {
return this._continuous;
}
2019-05-15 11:32:36 +02:00
/**
* @param {boolean} value
*/
set onlyLatest(value) {
this._onlyLatest = value;
}
clear() {
this._positions = null;
this._plotData = null;
}
/**
* Get track data from xml
2019-06-29 12:54:32 +02:00
* @param {XMLDocument} xml XML with positions data
* @param {boolean} isUpdate If true append to old data
2019-05-15 11:32:36 +02:00
*/
fromXml(xml, isUpdate) {
let positions = [];
let plotData = [];
let totalDistance = 0;
let totalSeconds = 0;
if (isUpdate && this._positions) {
positions = this._positions;
plotData = this._plotData;
totalDistance = positions[positions.length - 1].totalDistance;
totalSeconds = positions[positions.length - 1].totalSeconds;
}
2019-05-15 11:32:36 +02:00
const xmlPos = xml.getElementsByTagName('position');
for (xml of xmlPos) {
const position = uPosition.fromXml(xml);
totalDistance += position.distance;
totalSeconds += position.seconds;
position.totalDistance = totalDistance;
position.totalSeconds = totalSeconds;
positions.push(position);
if (position.altitude != null) {
plotData.push({ x: position.totalDistance, y: position.altitude * config.factor_m });
}
if (position.id > this._maxId) {
this._maxId = position.id;
}
}
2019-05-15 11:32:36 +02:00
this._positions = positions;
this._plotData = plotData;
}
2017-04-14 17:24:09 +02:00
2019-05-15 11:32:36 +02:00
/**
* @return {?Array<{x: number, y: number}>}
*/
get plotData() {
return this._plotData;
}
2017-04-14 17:24:09 +02:00
/**
2019-05-15 11:32:36 +02:00
* @return {number}
*/
get length() {
return this._positions ? this._positions.length : 0;
}
/**
* @return {boolean}
*/
2019-05-15 11:32:36 +02:00
get hasPositions() {
return this._positions !== null;
2017-04-14 17:24:09 +02:00
}
/**
2019-05-15 11:32:36 +02:00
* @throws
* @return {Promise<void>}
*/
2019-05-15 11:32:36 +02:00
fetch() {
const data = {
userid: this._user.id
};
let isUpdate = this.hasPositions;
if (config.showLatest) {
data.last = 1;
isUpdate = false;
} else {
data.trackid = this.id;
2017-04-14 17:24:09 +02:00
}
2019-05-15 11:32:36 +02:00
if (this._onlyLatest !== config.showLatest) {
this._onlyLatest = config.showLatest;
isUpdate = false;
} else {
data.afterid = this._maxId;
2017-04-14 17:24:09 +02:00
}
2019-06-29 12:54:32 +02:00
return uAjax.get('utils/getpositions.php', data).then((xml) => {
2019-05-15 11:32:36 +02:00
this.fromXml(xml, isUpdate);
2019-06-29 12:54:32 +02:00
this.render();
return xml;
2019-05-15 11:32:36 +02:00
});
}
2019-05-15 11:32:36 +02:00
/**
2019-06-29 12:54:32 +02:00
* Save track data
2019-05-15 11:32:36 +02:00
* @param {string} action
* @return {Promise<void>}
*/
update(action) {
return uAjax.post('utils/handletrack.php',
{
action: action,
trackid: this.id,
trackname: this.name
});
2017-04-14 17:24:09 +02:00
}
2019-06-29 12:54:32 +02:00
/**
* Render track
*/
2019-05-15 11:32:36 +02:00
render() {
this.emit(uEvent.TRACK_READY);
}
2019-05-15 11:32:36 +02:00
/**
* Export to file
* @param {string} type File type
*/
export(type) {
const url = `utils/export.php?type=${type}&userid=${this._user.id}&trackid=${this.id}`;
this.emit(uEvent.OPEN_URL, url);
}
}