diff --git a/app/components/Alert.tsx b/app/components/Alert.tsx deleted file mode 100644 index 2a33736..0000000 --- a/app/components/Alert.tsx +++ /dev/null @@ -1,54 +0,0 @@ -import React from "react"; -import { Bullet } from "./Bullet"; -import { Title } from "./Title"; - -export function Alert(props: { - name: string; - headers: Array; - routes: { [key: string]: { [key: string]: string } }; - index: number; -}) { - let name = props.name; - let headers = props.headers; - let routes = props.routes; - let index = props.index; - - return ( - -
- {headers.length > 0 ? ( - - ) : ( - - )} -
- - {headers.length > 0 ? ( -
-

- {headers[index].split(/(\[.*?\])/).map((text) => { - if (text.length == 0) return; - if (text.substring(0, 1) == "[" && text.substring(text.length - 1) == "]") { - return ( -
- -
- ); - } - return text; - })} -

-
- ) : ( -
-

No active alerts

-
- )} -
- ); -} diff --git a/app/components/Bullet.tsx b/app/components/Bullet.tsx index 0de3763..6e95fcf 100644 --- a/app/components/Bullet.tsx +++ b/app/components/Bullet.tsx @@ -1,67 +1,64 @@ export function Bullet(props: { short_name: string; color: string; text_color: string; size: number }) { - let short_name = props.short_name; - let color = props.color; - let text_color = props.text_color; - let size = props.size; + if (props.short_name.length <= 0) { + return; + } - if (short_name.length <= 1) { + if (props.short_name.length <= 1) { return ( -

- {short_name} -

+

+ {props.short_name} +

+
); } - if (short_name.length <= 2 && short_name.substring(1) == "X") { + if (props.short_name.length <= 2 && props.short_name.substring(1) == "X") { return (

- {short_name.substring(0, 1)} + {props.short_name.substring(0, 1)}

@@ -72,21 +69,20 @@ export function Bullet(props: { short_name: string; color: string; text_color: s

- {short_name} + {props.short_name}

); diff --git a/app/components/List.tsx b/app/components/List.tsx deleted file mode 100644 index c86ee39..0000000 --- a/app/components/List.tsx +++ /dev/null @@ -1,68 +0,0 @@ -import React, { ReactNode } from "react"; -import { Vehicle } from "../types"; -import { Bullet } from "./Bullet"; -import { Title } from "./Title"; - -export function List(props: { - name: string; - vehicles: { [key: string]: { [key: string]: Array } }; - routes: { [key: string]: { [key: string]: string } }; -}) { - let name = props.name; - let trips = props.vehicles; - let routes = props.routes; - let displays: Array = []; - - Object.values(trips).forEach((destinations) => { - Object.values(destinations).forEach((vehicles) => { - displays.push( -
-
-
- -

{vehicles[0].destination_name}

-
-
-

{vehicles[0].minutes_until_arrival}

-

min

-
-
- -
- {vehicles[1] ? ( - -

{vehicles[1].minutes_until_arrival}

-

min

-
- ) : undefined} -
-
, - ); - }); - }); - - if (displays.length == 0) { - return ( - -
{}
-
-
-

No vehicles scheduled

-
-
-
- ); - } - - return ( - -
{}
- {displays} -
- ); -} diff --git a/app/config.ts b/app/config.ts index 7e1cb0f..40bdd02 100644 --- a/app/config.ts +++ b/app/config.ts @@ -1,25 +1,23 @@ -module.exports = { +import { Config } from "./types"; + +export const config: Config = { subway: [ { stop_ids: ["405S"], - display: "Countdown", walk_time: 10, }, { stop_ids: ["D03S"], - display: "Countdown", walk_time: 14, }, ], bus: [ { stop_ids: ["100017", "103400"], - display: "List", walk_time: 3, }, { stop_ids: ["100723"], - display: "List", walk_time: 3, }, ], diff --git a/app/page.tsx b/app/page.tsx index 76564c0..a4f7cb0 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -2,124 +2,63 @@ import Image from "next/image"; import { useEffect, useState } from "react"; -import { Alert } from "./components/Alert"; +import { Bulletin } from "./components/Bulletin"; import { Countdown } from "./components/Countdown"; -import { List } from "./components/List"; -import { Vehicle } from "./types"; +import { Message } from "./components/Message"; +import { config } from "./config"; +import { Alert, Info } from "./types"; export default function Home() { const [time, setTime] = useState(""); - const [jeromeNames, setJeromeNames] = useState(""); - const [jeromeTimes, setJeromeTimes] = useState>([]); - const [concourseNames, setConcourseNames] = useState(""); - const [concourseTimes, setConcourseTimes] = useState>([]); + const [status, setStatus] = useState(false); + const [info, setInfo] = useState({ stops_realtime: {}, service_alerts_realtime: [], routes_static: {} }); const [serviceAlerts, setServiceAlerts] = useState>([]); - const [routes, setRoutes] = useState<{ [key: string]: { [key: string]: string } }>({}); - const [paulNames, setPaulNames] = useState(""); - const [paulTimes, setPaulTimes] = useState<{ [key: string]: { [key: string]: Array } }>({}); - const [w205Names, setW205Names] = useState(""); - const [w205Times, setW205Times] = useState<{ [key: string]: { [key: string]: Array } }>({}); const [index, setIndex] = useState(0); - const [wsStatus, setWsStatus] = useState(false); useEffect(() => { const ws = new WebSocket("ws://127.0.0.1:9001"); - const config = { - subway: [ - { - stop_ids: ["405S"], - display: "Countdown", - walk_time: 10, - }, - { - stop_ids: ["D03S"], - display: "Countdown", - walk_time: 14, - }, - ], - bus: [ - { - stop_ids: ["100017", "103400"], - display: "List", - walk_time: 3, - }, - { - stop_ids: ["100723"], - display: "List", - walk_time: 3, - }, - ], - }; - ws.onopen = () => { + setStatus(true); console.log("Websocket opened."); + ws.send(JSON.stringify(config)); }; ws.onmessage = (event) => { - setWsStatus(true); + setStatus(true); console.log("Message recieved."); const message = JSON.parse(event.data); + setInfo(message); - const jeromeName: string = message["stops_realtime"]["405S"]["name"]; - setJeromeNames(jeromeName); - - const jeromeData: Vehicle[] = message["stops_realtime"]["405S"]["trips"]; - setJeromeTimes(jeromeData); - - const concouseName: string = message["stops_realtime"]["D03S"]["name"]; - setConcourseNames(concouseName); - - const concourseData: Vehicle[] = message["stops_realtime"]["D03S"]["trips"]; - setConcourseTimes(concourseData); - - const delayData: Array = []; - - const serviceData: Array<{ route_id: string; sort_order: number; header_text: string }> = - message["service_alerts_realtime"]; + const headers: Array = []; + const serviceData: Array = message.service_alerts_realtime; serviceData .slice() .reverse() .forEach((alert) => { - if (delayData.indexOf(alert.header_text) !== -1) return; + if (headers.indexOf(alert.header_text) !== -1) return; if (alert.sort_order < 22) return; - delayData.push(alert.header_text); + headers.push(alert.header_text); }); - setServiceAlerts(delayData); - - if (delayData.length > 0) { - setIndex((i) => ((i % delayData.length) + delayData.length) % delayData.length); + setServiceAlerts(headers); + if (headers.length > 0) { + setIndex((i) => ((i % headers.length) + headers.length) % headers.length); } else { setIndex(0); } - - const routeData: { [key: string]: { [key: string]: string } } = message["routes_static"]; - setRoutes(routeData); - - const paulName: string = message["stops_realtime"]["100017"]["name"]; - setPaulNames(paulName); - - const paulData: { [key: string]: { [key: string]: Array } } = - message["stops_realtime"]["100017"]["routes"]; - setPaulTimes(paulData); - - const w205Name: string = message["stops_realtime"]["100723"]["name"]; - setW205Names(w205Name); - - const w205stData: { [key: string]: { [key: string]: Array } } = - message["stops_realtime"]["100723"]["routes"]; - setW205Times(w205stData); }; ws.onclose = () => { - setWsStatus(false); + setStatus(false); console.log("Websocket closed."); }; return () => { + setStatus(false); console.log("Closing old websocket..."); + ws.close(); }; }, []); @@ -149,28 +88,54 @@ export default function Home() { dateStyle: "long", }), ); - }, 1000); - }, []); + + if (serviceAlerts.length > 0) { + setIndex((i) => (((i + 1) % serviceAlerts.length) + serviceAlerts.length) % serviceAlerts.length); + } else { + setIndex(0); + } + }, 5000); + + return () => { + clearInterval(loop); + }; + }, [serviceAlerts.length]); return (
- - + +
- +
- - + +

{"Status: "} - {wsStatus ? ( + {status ? ( OK ) : ( ERROR @@ -200,7 +165,6 @@ export default function Home() { David Wang

-

{time}

diff --git a/app/types.d.ts b/app/types.d.ts index d109dda..1dd8253 100644 --- a/app/types.d.ts +++ b/app/types.d.ts @@ -5,3 +5,31 @@ export interface Vehicle { destination_name: string; minutes_until_arrival: number; } + +export interface Stop { + name: string; + trips: Array; + routes: Record>>; +} + +export interface Alert { + route_id: string; + sort_order: number; + header_text: string; +} + +export interface Info { + stops_realtime: Record; + service_alerts_realtime: Array; + routes_static: Record; +} + +export interface Config { + subway: Array; + bus: Array; +} + +export interface StopConfig { + stop_ids: Array; + walk_time: number; +} diff --git a/src/config.rs b/src/config.rs index 40a4bfd..cc32f67 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,7 +16,6 @@ pub struct Config { #[derive(Debug, Serialize, Deserialize, Default)] pub struct StopConfig { pub stop_ids: Vec, - pub display: String, pub walk_time: i32, } diff --git a/src/main.rs b/src/main.rs index bad0bab..b0cf075 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,7 +106,6 @@ fn main() { } let data = InfoJson { - online: true, stops_realtime: stops_map.to_owned(), service_alerts_realtime: service_alerts.subway.to_owned(), routes_static: routes_static.to_owned(), @@ -135,7 +134,6 @@ fn main() { #[derive(Debug, Clone, Serialize, Deserialize, Default)] struct InfoJson { - online: bool, stops_realtime: BTreeMap, service_alerts_realtime: Vec, routes_static: HashMap,