mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-13 12:11:12 -06:00
Edge case handling, reduce nesting, general refactor of location funcs
This commit is contained in:
@@ -4,10 +4,11 @@ import * as life360 from 'life360-node-api';
|
|||||||
import logger from '@/logger';
|
import logger from '@/logger';
|
||||||
import Result, { err, ok } from 'true-myth/result';
|
import Result, { err, ok } from 'true-myth/result';
|
||||||
|
|
||||||
const center = {
|
const targetCenter = {
|
||||||
longitude: env.CENTER_LONGITUDE,
|
longitude: env.CENTER_LONGITUDE,
|
||||||
latitude: env.CENTER_LATITUDE
|
latitude: env.CENTER_LATITUDE
|
||||||
};
|
};
|
||||||
|
|
||||||
const MILES_PER_NAUTICAL_MILE = 1.15078;
|
const MILES_PER_NAUTICAL_MILE = 1.15078;
|
||||||
const KILOMETERS_PER_MILE = 1.60934;
|
const KILOMETERS_PER_MILE = 1.60934;
|
||||||
|
|
||||||
@@ -16,39 +17,50 @@ export type Position = {
|
|||||||
latitude: number;
|
latitude: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
// K = Kilometers, N = Nautical Miles, M = Miles (default: M)
|
/**
|
||||||
|
*
|
||||||
|
* Calculate the distance between two points using the Haversine formula
|
||||||
|
*
|
||||||
|
* K = Kilometers, N = Nautical Miles, M = Miles (default: M)
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
* @param b
|
||||||
|
* @param unit
|
||||||
|
*/
|
||||||
export function distance(
|
export function distance(
|
||||||
a: Position,
|
a: Position,
|
||||||
b: Position,
|
b: Position,
|
||||||
unit: 'K' | 'N' | 'M' = 'M'
|
unit: 'K' | 'N' | 'M' = 'M'
|
||||||
) {
|
) {
|
||||||
|
// TODO: Handle floating point precision errors
|
||||||
|
// Edge case: same point
|
||||||
if (a.latitude == b.latitude && a.longitude == b.longitude) {
|
if (a.latitude == b.latitude && a.longitude == b.longitude) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
}
|
||||||
const radlat1 = (Math.PI * a.latitude) / 180;
|
|
||||||
const radlat2 = (Math.PI * b.latitude) / 180;
|
|
||||||
const theta = a.longitude - b.longitude;
|
|
||||||
const radtheta = (Math.PI * theta) / 180;
|
|
||||||
let dist =
|
|
||||||
Math.sin(radlat1) * Math.sin(radlat2) +
|
|
||||||
Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
|
|
||||||
|
|
||||||
if (dist > 1) dist = 1;
|
const radlat1 = (Math.PI * a.latitude) / 180;
|
||||||
|
const radlat2 = (Math.PI * b.latitude) / 180;
|
||||||
|
const theta = a.longitude - b.longitude;
|
||||||
|
const radtheta = (Math.PI * theta) / 180;
|
||||||
|
let dist =
|
||||||
|
Math.sin(radlat1) * Math.sin(radlat2) +
|
||||||
|
Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
|
||||||
|
|
||||||
dist = Math.acos(dist);
|
if (dist > 1) dist = 1;
|
||||||
dist = (dist * 180) / Math.PI;
|
|
||||||
dist = dist * 60 * MILES_PER_NAUTICAL_MILE; // Convert to miles
|
|
||||||
|
|
||||||
// Convert to the specified unit
|
dist = Math.acos(dist);
|
||||||
switch (unit) {
|
dist = (dist * 180) / Math.PI;
|
||||||
case 'K':
|
dist = dist * 60 * MILES_PER_NAUTICAL_MILE; // Convert to miles
|
||||||
return dist * KILOMETERS_PER_MILE;
|
|
||||||
case 'N':
|
// Convert to the specified unit
|
||||||
return dist / MILES_PER_NAUTICAL_MILE;
|
switch (unit) {
|
||||||
case 'M':
|
case 'K':
|
||||||
default:
|
return dist * KILOMETERS_PER_MILE;
|
||||||
return dist;
|
case 'N':
|
||||||
}
|
return dist / MILES_PER_NAUTICAL_MILE;
|
||||||
|
case 'M':
|
||||||
|
default:
|
||||||
|
return dist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +79,7 @@ export async function getDistance(): Promise<Result<number, string>> {
|
|||||||
})`
|
})`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
logger.debug(`Logged in to Life360 (${env.LIFE360_USERNAME}).`);
|
||||||
|
|
||||||
// Get my current location
|
// Get my current location
|
||||||
let me;
|
let me;
|
||||||
@@ -88,6 +101,11 @@ export async function getDistance(): Promise<Result<number, string>> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (me == undefined)
|
||||||
|
return err(
|
||||||
|
`Failed to find member with ID ${env.LIFE360_MEMBER_ID} in Life360`
|
||||||
|
);
|
||||||
|
|
||||||
// Parse my latitude and longitude into floats
|
// Parse my latitude and longitude into floats
|
||||||
const current = {
|
const current = {
|
||||||
latitude: parseFloat(me.location.latitude),
|
latitude: parseFloat(me.location.latitude),
|
||||||
@@ -95,5 +113,5 @@ export async function getDistance(): Promise<Result<number, string>> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Calculate the distance between my location and the center, multiply x1000 for meters
|
// Calculate the distance between my location and the center, multiply x1000 for meters
|
||||||
return ok(distance(current, center, 'K') * 1000);
|
return ok(distance(current, targetCenter, 'K') * 1000);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user