mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-14 02:11:12 -06:00
Prevent cron monitor calls outside production, improve thrown error messages/API response
This commit is contained in:
122
src/location.ts
122
src/location.ts
@@ -1,79 +1,97 @@
|
|||||||
import { env } from "@/env/server.mjs";
|
import { env } from '@/env/server.mjs';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import * as life360 from "life360-node-api";
|
import * as life360 from 'life360-node-api';
|
||||||
|
|
||||||
const center = {
|
const center = {
|
||||||
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;
|
||||||
|
|
||||||
export type Position = {
|
export type Position = {
|
||||||
longitude: number;
|
longitude: number;
|
||||||
latitude: number;
|
latitude: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
// K = Kilometers, N = Nautical Miles, M = Miles (default: M)
|
// K = Kilometers, N = Nautical Miles, M = Miles (default: M)
|
||||||
export function distance(
|
export function distance(
|
||||||
a: Position,
|
a: Position,
|
||||||
b: Position,
|
b: Position,
|
||||||
unit: "K" | "N" | "M" = "M"
|
unit: 'K' | 'N' | 'M' = 'M'
|
||||||
) {
|
) {
|
||||||
if (a.latitude == b.latitude && a.longitude == b.longitude) {
|
if (a.latitude == b.latitude && a.longitude == b.longitude) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} 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;
|
||||||
const radtheta = (Math.PI * theta) / 180;
|
const radtheta = (Math.PI * theta) / 180;
|
||||||
let dist =
|
let dist =
|
||||||
Math.sin(radlat1) * Math.sin(radlat2) +
|
Math.sin(radlat1) * Math.sin(radlat2) +
|
||||||
Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
|
Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
|
||||||
|
|
||||||
if (dist > 1) dist = 1;
|
if (dist > 1) dist = 1;
|
||||||
|
|
||||||
dist = Math.acos(dist);
|
dist = Math.acos(dist);
|
||||||
dist = (dist * 180) / Math.PI;
|
dist = (dist * 180) / Math.PI;
|
||||||
dist = dist * 60 * MILES_PER_NAUTICAL_MILE; // Convert to miles
|
dist = dist * 60 * MILES_PER_NAUTICAL_MILE; // Convert to miles
|
||||||
|
|
||||||
// Convert to the specified unit
|
// Convert to the specified unit
|
||||||
switch (unit) {
|
switch (unit) {
|
||||||
case "K":
|
case 'K':
|
||||||
return dist * KILOMETERS_PER_MILE;
|
return dist * KILOMETERS_PER_MILE;
|
||||||
case "N":
|
case 'N':
|
||||||
return dist / MILES_PER_NAUTICAL_MILE;
|
return dist / MILES_PER_NAUTICAL_MILE;
|
||||||
case "M":
|
case 'M':
|
||||||
default:
|
default:
|
||||||
return dist;
|
return dist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns The distance in meters between me and the center
|
* @returns The distance in meters between me and the center
|
||||||
*/
|
*/
|
||||||
export async function getDistance(): Promise<number> {
|
export async function getDistance(): Promise<number> {
|
||||||
// Setup the Life360 API client
|
let client;
|
||||||
const client = await life360.login(
|
try {
|
||||||
env.LIFE360_USERNAME,
|
// Setup the Life360 API client
|
||||||
env.LIFE360_PASSWORD
|
client = await life360.login(
|
||||||
);
|
env.LIFE360_USERNAME,
|
||||||
|
env.LIFE360_PASSWORD + 'a'
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed while logging in to Life360: ${
|
||||||
|
e instanceof Error ? e.message : e
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Get my current location
|
let me;
|
||||||
const circles = await client.listCircles();
|
try {
|
||||||
const myCircle = circles[0];
|
// Get my current location
|
||||||
const members = await myCircle.listMembers();
|
const circles = await client.listCircles();
|
||||||
const me = members.findById(env.LIFE360_MEMBER_ID);
|
const myCircle = circles[0];
|
||||||
|
const members = await myCircle.listMembers();
|
||||||
|
me = members.findById(env.LIFE360_MEMBER_ID);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed while getting my location from Life360: ${
|
||||||
|
e instanceof Error ? e.message : e
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Parse my location into latitude and longitude
|
// Parse my location into latitude and longitude
|
||||||
const current = {
|
const current = {
|
||||||
latitude: parseFloat(me.location.latitude),
|
latitude: parseFloat(me.location.latitude),
|
||||||
longitude: parseFloat(me.location.longitude),
|
longitude: parseFloat(me.location.longitude)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Calculate the distance between my location and the center
|
// Calculate the distance between my location and the center
|
||||||
const difference = distance(current, center, "K") * 1000;
|
const difference = distance(current, center, 'K') * 1000;
|
||||||
|
|
||||||
return difference;
|
return difference;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,49 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { getDistance } from "@/location";
|
import { getDistance } from '@/location';
|
||||||
import { env } from "@/env/server.mjs";
|
import { env } from '@/env/server.mjs';
|
||||||
import monitorAsync from "@/monitor";
|
import monitorAsync from '@/monitor';
|
||||||
|
|
||||||
type ResponseData = {
|
type ResponseData = {
|
||||||
diff: number;
|
diff: number;
|
||||||
inRange: boolean;
|
inRange: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type StatusData = { status: string };
|
type StatusData = { status: string };
|
||||||
|
|
||||||
const center = {
|
const center = {
|
||||||
latitude: env.CENTER_LATITUDE,
|
latitude: env.CENTER_LATITUDE,
|
||||||
longitude: env.CENTER_LONGITUDE,
|
longitude: env.CENTER_LONGITUDE
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse<(ResponseData & StatusData) | StatusData>
|
res: NextApiResponse<(ResponseData & StatusData) | StatusData>
|
||||||
) {
|
) {
|
||||||
if (req.query.key != env.API_KEY) {
|
if (req.query.key != env.API_KEY) {
|
||||||
// auth failed
|
// auth failed
|
||||||
res.status(401).json({ status: "Unauthorized" });
|
res.status(401).json({ status: 'Unauthorized' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await monitorAsync(async function () {
|
async function innerFunction() {
|
||||||
const diff = await getDistance();
|
const diff = await getDistance();
|
||||||
// auth passed
|
// auth passed
|
||||||
res.setHeader(
|
res.setHeader(
|
||||||
"Cache-Control",
|
'Cache-Control',
|
||||||
`max-age=0, s-maxage=${env.EDGE_CACHE_TIME_SECONDS}, stale-while-revalidate`
|
`max-age=0, s-maxage=${env.EDGE_CACHE_TIME_SECONDS}, stale-while-revalidate`
|
||||||
);
|
);
|
||||||
res
|
res
|
||||||
.status(200)
|
.status(200)
|
||||||
.json({ diff, inRange: diff < env.MAX_DISTANCE, status: "Authorized" });
|
.json({ diff, inRange: diff < env.MAX_DISTANCE, status: 'Success' });
|
||||||
});
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (process.env.NODE_ENV === 'production')
|
||||||
|
await monitorAsync(innerFunction);
|
||||||
|
else await innerFunction();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
res.status(500).json({ status: 'Error' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -215,7 +215,6 @@ export async function getMatchingTime(
|
|||||||
): Promise<TimeConfig | null> {
|
): Promise<TimeConfig | null> {
|
||||||
const times = config.times.filter((time) => {
|
const times = config.times.filter((time) => {
|
||||||
// If the day doesn't match, skip.
|
// If the day doesn't match, skip.
|
||||||
console.log(dayAsNumber[now.getDay().toString()]);
|
|
||||||
if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false;
|
if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false;
|
||||||
|
|
||||||
const startTime = time.time;
|
const startTime = time.time;
|
||||||
|
|||||||
Reference in New Issue
Block a user