diff --git a/src/repositories/ApiBasedRepository.ts b/src/repositories/ApiBasedRepository.ts index 17af7d3..774404b 100644 --- a/src/repositories/ApiBasedRepository.ts +++ b/src/repositories/ApiBasedRepository.ts @@ -1,6 +1,8 @@ import { GetterRepository } from "./GetterRepository"; import { IEta, IOrderedStop, IRoute, IShuttle, IStop, ISystem } from "../entities/entities"; +const baseUrl = "https://passiogo.com/mapGetData.php" + export interface ApiBasedRepositoryCache { etasForShuttleId?: { [shuttleId: string]: IEta[], @@ -30,6 +32,8 @@ export interface ApiBasedRepositoryMillisecondTTLs { const emptyCache: ApiBasedRepositoryCache = { etasForShuttleId: {}, etasForStopId: {}, + shuttleByShuttleId: {}, + shuttlesBySystemId: {}, } const defaultTtls: ApiBasedRepositoryMillisecondTTLs = { @@ -67,6 +71,7 @@ export class ApiBasedRepository implements GetterRepository { if (!systemId) { return null; } + await this.updateEtasForSystemIfTTL(systemId); if (this.cache?.etasForStopId !== undefined) { const etas = this.cache.etasForStopId[systemId]; @@ -88,7 +93,24 @@ export class ApiBasedRepository implements GetterRepository { } public async updateEtasForSystemIfTTL(systemId: string) { + try { + const params = { + eta: "3", + stopIds: "177666", + }; + const query = new URLSearchParams(params).toString(); + const response = await fetch(`${baseUrl}?${query}`, { + method: "GET", + }); + const json = await response.json(); + + if (json.ETAs && !json.ETAs["0000"]) { + + } + } catch (e) { + console.error(e); + } } // TODO: migrate rest of logic over to this class @@ -145,8 +167,63 @@ export class ApiBasedRepository implements GetterRepository { } public async updateShuttlesForSystemIfTTL(systemId: string) { - // Update shuttleByShuttleId, shuttlesBySystemId, shuttlesByRouteId (future) + try { + // Update shuttleByShuttleId, shuttlesBySystemId, shuttlesByRouteId (future) + const params = { + getBuses: "2" + }; + const formDataJsonObject = { + "s0": systemId, + "sA": "1" + }; + + const formData = new FormData(); + formData.set("json", JSON.stringify(formDataJsonObject)); + + const query = new URLSearchParams(params).toString(); + const response = await fetch(`${baseUrl}?${query}`, { + method: "POST", + body: formData, + }); + const json = await response.json(); + + if (json.buses && json.buses["-1"] === undefined) { + const jsonBuses = Object.values(json.buses).map((busesArr: any) => { + return busesArr[0]; + }); + + // Store shuttles by system, with the additional side effect that + // shuttleByShuttleId is updated + const shuttles = await Promise.all(jsonBuses.map(async (jsonBus: any) => { + const constructedShuttle: IShuttle = { + name: jsonBus.bus, + coordinates: { + latitude: parseFloat(jsonBus.latitude), + longitude: parseFloat(jsonBus.longitude), + }, + routeId: jsonBus.routeId, + systemId: systemId, + id: `${jsonBus.busId}` + } + + if (this.cache.shuttleByShuttleId) { + this.cache.shuttleByShuttleId[jsonBus.busId] = constructedShuttle; + } + + return constructedShuttle; + })); + + if (this.cache.shuttlesBySystemId) { + this.cache.shuttlesBySystemId[systemId] = shuttles; + } + } else { + console.warn(`No shuttle data available for system ID ${systemId} and JSON output +${json}`); + } + } catch (e) { + console.error(e); + } } public async getStopById(stopId: string): Promise<| null> {