mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-13 14:11:08 -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,15 +17,27 @@ 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 radlat1 = (Math.PI * a.latitude) / 180;
|
||||||
const radlat2 = (Math.PI * b.latitude) / 180;
|
const radlat2 = (Math.PI * b.latitude) / 180;
|
||||||
const theta = a.longitude - b.longitude;
|
const theta = a.longitude - b.longitude;
|
||||||
@@ -50,7 +63,6 @@ export function distance(
|
|||||||
return dist;
|
return dist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns The distance in meters between me and the center
|
* @returns The distance in meters between me and the center
|
||||||
@@ -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