Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into sensors_to_gui
Browse files Browse the repository at this point in the history
vrushang1234 committed Jun 1, 2024
2 parents 64189e5 + c22567b commit 84cfdb5
Showing 4 changed files with 52 additions and 13 deletions.
26 changes: 22 additions & 4 deletions control-station/src/components/SensorBoxes/Console.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { useContext, useEffect, useRef } from "react";
import "./SensorData.css";
import PodContext from "@/services/PodContext";

function Console() {
const { podData } = useContext(PodContext);
const listEndRef = useRef<HTMLLIElement | null>(null);

useEffect(() => {
if (listEndRef.current) {
listEndRef.current.scrollIntoView({ behavior: "smooth" });
}
console.log(podData.messages);
}, [podData.messages]);

return (
<div className="console">
<h2>Console</h2>
<ul className="console-list">
<li className="console-list-item">Start Sent</li>
<li className="console-list-item">Stop Sent</li>
<li className="console-list-item">Load Sent</li>
<li className="console-list-item">Force Stop Sent</li>
{podData.messages.map((prop, index) => (
<li
key={index}
className="console-list-item"
ref={index === podData.messages.length - 1 ? listEndRef : null}
>
{prop.timestamp.toLocaleTimeString("en-US", { hour12: false })} &nbsp;
{prop.message.toUpperCase()}
</li>
))}
</ul>
</div>
);
28 changes: 24 additions & 4 deletions control-station/src/services/PodSocketClient.ts
Original file line number Diff line number Diff line change
@@ -18,6 +18,11 @@ interface ServerToClientEvents {
serverResponse: (data: PodData) => void;
}

interface Message {
timestamp: Date;
message: string;
}

interface ClientToServerEvents {
load: (ack: (data: string) => void) => void;
run: (ack: (data: string) => void) => void;
@@ -44,6 +49,7 @@ export interface PodData {
upstream_pressure_transducer: number;
lim_temperature_port: number;
lim_temperature_starboard: number;
messages: Message[];
}

type SetPodData = Dispatch<SetStateAction<PodData>>;
@@ -90,28 +96,28 @@ class PodSocketClient {
sendLoad(): void {
this.socket.emit("load", (response: string) => {
console.log("Server acknowledged:", response);
this.setPodData((d) => ({ ...d, state: State.Load }));
this.addMessage(response, State.Load);
});
}

sendRun(): void {
this.socket.emit("run", (response: string) => {
console.log("Server acknowledged:", response);
this.setPodData((d) => ({ ...d, state: State.Running }));
this.addMessage(response, State.Running);
});
}

sendStop(): void {
this.socket.emit("stop", (response: string) => {
console.log("Server acknowledged:", response);
this.setPodData((d) => ({ ...d, state: State.Stopped }));
this.addMessage(response, State.Stopped);
});
}

sendHalt(): void {
this.socket.emit("halt", (response: string) => {
console.log("Server acknowledged:", response);
this.setPodData((d) => ({ ...d, state: State.Halted }));
this.addMessage(response, State.Halted);
});
}

@@ -131,6 +137,20 @@ class PodSocketClient {
console.log("server says", data);
this.setPodData((d) => ({ ...d, ...data }));
}

private addMessage(response: string, newState: State): void {
const timestamp = new Date();
const newMessage = {
timestamp,
message: response,
};

this.setPodData((d) => ({
...d,
state: newState,
messages: [...d.messages, newMessage],
}));
}
}

export default PodSocketClient;
1 change: 1 addition & 0 deletions control-station/src/services/usePodData.tsx
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ function usePodData() {
upstream_pressure_transducer: 0,
lim_temperature_port: 0,
lim_temperature_starboard: 0,
messages: [],
});

const podSocketClient = useMemo(() => new PodSocketClient(setPodData), []);
10 changes: 5 additions & 5 deletions pod-operation/src/main.rs
Original file line number Diff line number Diff line change
@@ -54,17 +54,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let lidar = Lidar::new();
tokio::spawn(demo::read_lidar(lidar));

let mut state_machine = StateMachine::new(io);
tokio::spawn(async move {
state_machine.run().await;
});

let limcurrent = LimCurrent::new(ads1x1x::SlaveAddr::Default);
tokio::spawn(demo::read_lim_current(limcurrent));

let inverter_board = InverterBoard::new();
tokio::spawn(demo::inverter_control(inverter_board));

let mut state_machine = StateMachine::new(io);
tokio::spawn(async move {
state_machine.run().await;
});

let app = axum::Router::new().layer(layer);

info!("Starting server on port 5000");

0 comments on commit 84cfdb5

Please sign in to comment.