2018-08-22 19:34:20 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
MOCK_LOCATION_GPX_PLAYBACK_SPEED,
|
|
|
|
MOCK_LOCATION_UPDATE_INTERVAL,
|
|
|
|
MOCK_LOCATION_USE_GPX,
|
|
|
|
MOCK_LOCATION_LAT_MIN, MOCK_LOCATION_LAT_MAX,
|
|
|
|
MOCK_LOCATION_LNG_MIN, MOCK_LOCATION_LNG_MAX,
|
|
|
|
} from '@/constants';
|
|
|
|
|
2018-08-14 15:33:23 +02:00
|
|
|
let mockGPX = [];
|
2018-08-22 19:34:20 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2018-08-14 15:33:23 +02:00
|
|
|
// Use a node_modules require here, this is handled by Webpack to fetch either
|
|
|
|
// a custom mock_gpx.json or a default empty one.
|
|
|
|
mockGPX = require('mock_gpx.json'); // eslint-disable-line global-require
|
2018-08-22 19:34:20 +02:00
|
|
|
}
|
2018-06-27 11:17:38 +02:00
|
|
|
|
2018-08-22 19:34:20 +02:00
|
|
|
export function mockLocationRandom() {
|
2018-08-13 22:07:34 +02:00
|
|
|
let heading = null;
|
|
|
|
if (Math.random() > 0.25) {
|
|
|
|
heading = Math.random() * 360;
|
|
|
|
}
|
2018-09-04 13:39:25 +02:00
|
|
|
let speed = null;
|
|
|
|
if (Math.random() > 0.25) {
|
2018-09-17 11:51:35 +02:00
|
|
|
speed = Math.random() * 9; // in meters/s
|
2018-09-04 13:39:25 +02:00
|
|
|
}
|
2018-07-04 17:37:28 +02:00
|
|
|
const newLocation = {
|
2018-06-26 11:04:23 +02:00
|
|
|
coords: {
|
2018-08-14 15:33:23 +02:00
|
|
|
accuracy: Math.random() * 100, // In meters
|
2018-08-22 19:34:20 +02:00
|
|
|
latitude: (
|
|
|
|
(Math.random() * (MOCK_LOCATION_LAT_MAX - MOCK_LOCATION_LAT_MIN))
|
|
|
|
+ MOCK_LOCATION_LAT_MIN
|
|
|
|
),
|
|
|
|
longitude: (
|
|
|
|
(Math.random() * (MOCK_LOCATION_LNG_MAX - MOCK_LOCATION_LNG_MIN))
|
|
|
|
+ MOCK_LOCATION_LNG_MIN
|
|
|
|
),
|
2018-08-13 22:07:34 +02:00
|
|
|
heading,
|
2018-09-04 13:39:25 +02:00
|
|
|
speed,
|
2018-06-26 11:04:23 +02:00
|
|
|
},
|
2018-08-02 16:58:08 +02:00
|
|
|
timestamp: new Date().getTime(),
|
2018-06-26 11:04:23 +02:00
|
|
|
};
|
2018-07-04 17:37:28 +02:00
|
|
|
console.log('New mock location: ', newLocation);
|
|
|
|
return newLocation;
|
2018-06-26 11:04:23 +02:00
|
|
|
}
|
2018-06-28 14:40:56 +02:00
|
|
|
|
2018-08-22 19:34:20 +02:00
|
|
|
export function mockLocationWithGPX(index, setPosition) {
|
2018-08-14 15:33:23 +02:00
|
|
|
if (mockGPX[index]) {
|
|
|
|
setPosition(mockGPX[index]);
|
|
|
|
if (mockGPX[index + 1]) {
|
|
|
|
const delay = (
|
2018-08-29 18:57:55 +02:00
|
|
|
Date.parse(mockGPX[index + 1].time)
|
|
|
|
- Date.parse(mockGPX[index].time)
|
2018-08-14 15:33:23 +02:00
|
|
|
);
|
|
|
|
setTimeout(
|
|
|
|
() => mockLocationWithGPX(index + 1, setPosition),
|
|
|
|
delay / MOCK_LOCATION_GPX_PLAYBACK_SPEED,
|
|
|
|
);
|
|
|
|
}
|
2018-08-22 19:34:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mockLocation(setPosition) {
|
|
|
|
if (MOCK_LOCATION_USE_GPX) {
|
|
|
|
mockLocationWithGPX(0, setPosition);
|
2018-08-14 15:33:23 +02:00
|
|
|
return -1; // Return a fake setInterval id
|
2018-08-22 19:34:20 +02:00
|
|
|
}
|
|
|
|
setPosition(mockLocationRandom());
|
|
|
|
return setInterval(
|
|
|
|
() => setPosition(mockLocationRandom()),
|
|
|
|
MOCK_LOCATION_UPDATE_INTERVAL,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-07 20:04:00 +01:00
|
|
|
export function capitalize(string) {
|
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
|
|
}
|