mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-12 23:31:02 +00:00
7dc313c567
The route cache key followed the relay's rest, so after one failed relay request (always the case offline) every day the device held only from Valhalla missed the cache for ten minutes. The key now follows the configured provider, and a resting relay only stops the question from going out. A route now names the engine that drew it. A day OSRM drew because Valhalla could not answer is OSRM's on the rail, and its other ways are asked of OSRM, instead of being labelled Valhalla's and compared with Valhalla offers. An own Valhalla gets the relay's twelve second deadline, and a day that runs it out goes to OSRM without a second wait. A day that avoids something keeps its old cache key, so the days a device already holds stay found. The leg segment and the offer types move to modules both engines import.
53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
import { useSettingsStore } from '../../store/settingsStore';
|
|
import type { DistanceUnit, RouteSegment, Waypoint } from '../../types';
|
|
import { formatDistance } from '../../utils/units';
|
|
|
|
/** The unit a route's texts are written in, which is why it is part of the route cache key. */
|
|
export function getDistanceUnit(): DistanceUnit {
|
|
return useSettingsStore.getState().settings.distance_unit === 'imperial' ? 'imperial' : 'metric';
|
|
}
|
|
|
|
/** A routed distance as the connectors show it: metres below a kilometre, else the user's unit. */
|
|
export function formatRouteDistance(meters: number): string {
|
|
const unit = getDistanceUnit();
|
|
if (unit === 'metric' && meters < 1000) {
|
|
return `${Math.round(meters)} m`;
|
|
}
|
|
return formatDistance(meters / 1000, unit);
|
|
}
|
|
|
|
/** A routed time in hours and minutes, the minutes rounded down. */
|
|
export function formatDuration(seconds: number): string {
|
|
const h = Math.floor(seconds / 3600);
|
|
const m = Math.floor((seconds % 3600) / 60);
|
|
if (h > 0) {
|
|
return `${h} h ${m} min`;
|
|
}
|
|
return `${m} min`;
|
|
}
|
|
|
|
/**
|
|
* The connector data for leg i of a routed chain, which runs from waypoint i to waypoint
|
|
* i + 1: both ends, the midpoint the label sits on, and the walking time at 5 km/h next to
|
|
* the time the engine measured. The same for every engine, so a day reads alike whichever
|
|
* of them drew it.
|
|
*/
|
|
export function legSegment(
|
|
leg: { distance: number; duration: number },
|
|
i: number,
|
|
waypoints: Waypoint[]
|
|
): RouteSegment {
|
|
const from: [number, number] = [waypoints[i].lat, waypoints[i].lng];
|
|
const to: [number, number] = [waypoints[i + 1].lat, waypoints[i + 1].lng];
|
|
return {
|
|
mid: [(from[0] + to[0]) / 2, (from[1] + to[1]) / 2],
|
|
from,
|
|
to,
|
|
distance: leg.distance,
|
|
duration: leg.duration,
|
|
walkingText: formatDuration(leg.distance / (5000 / 3600)),
|
|
drivingText: formatDuration(leg.duration),
|
|
distanceText: formatRouteDistance(leg.distance),
|
|
};
|
|
}
|