Skip to content

Commit

Permalink
some refactor to allow for satellite selection
Browse files Browse the repository at this point in the history
  • Loading branch information
kiesenverseist committed Aug 29, 2023
1 parent 68b030b commit 5c2dde3
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 93 deletions.
6 changes: 3 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const App: React.FC = () => {

return (
<div className="App">
{/* <Home/> */}
<p>hello</p>
{/* <p>hello</p> */}
{state === undefined && <p>yo</p>}
{state && <p>{`${JSON.stringify(state)}`}</p>}
{/* <Home/> */}
</div>
);
}
export default App;
export default App;
127 changes: 69 additions & 58 deletions frontend/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,76 @@ export interface State {
stations: Map<string, GroundStation>;
current_satellite: Satellite;
backend_status: BackendStatus;
}
export interface Satellite {
tle: string,
name: string,
norad_id: number,
ind_designator: string,
}
export interface BackendStatus {
lib_state: string,
cpu: number,
mem: number,
client_list: Array<string>,
}
export enum GroundStationStatus {
OFFLINE,
IDLE,
TRACKING,
OVERHEAT,
FAULT,
}
}

export enum AntennaType {
HELICAL,
DIPOLE,
YAGI,
PARABOLIC,
PATCH,
}
export interface Satellite {
tle: string,
name: string,
norad_id: number,
ind_designator: string,
}

export enum GSKinematics {
STATIC,
AZ,
AZEL,
}
export interface BackendStatus {
lib_state: string,
cpu: number,
mem: number,
client_list: Array<string>,
}

export interface GPSPosition {
latitude: String,
longitude: String,
altitude: String,
valid: boolean,
}
export interface GroundStation {
name: String,
location: GPSPosition,
orientation: PolarPoint,
signal_strength: number,
status: GroundStationStatus,
freq_response: [number, number],
antenna_type: AntennaType,
kinematics: GSKinematics,
}
export interface PolarPoint {
az: number,
el: number,
}

export interface UpdateStation {
name: String,
status: GroundStation,
}
export enum GroundStationStatus {
OFFLINE,
IDLE,
TRACKING,
OVERHEAT,
FAULT,
}

export type StateAction = UpdateStation
export enum AntennaType {
HELICAL,
DIPOLE,
YAGI,
PARABOLIC,
PATCH,
}

export enum GSKinematics {
STATIC,
AZ,
AZEL,
}

export interface GPSPosition {
latitude: String,
longitude: String,
altitude: String,
valid: boolean,
}

export interface GroundStation {
name: String,
location: GPSPosition,
orientation: PolarPoint,
signal_strength: number,
status: GroundStationStatus,
freq_response: [number, number],
antenna_type: AntennaType,
kinematics: GSKinematics,
}

export interface PolarPoint {
az: number,
el: number,
}

/// Actions

export interface UpdateStation {
name: String,
status: GroundStation,
}

export interface SelectSatellite {
satellite: Satellite,
}

export type StateAction = UpdateStation | SelectSatellite;
17 changes: 10 additions & 7 deletions orch-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ mod websocket;
use groundstation::GroundStation;
use websocket::{handle_socket, WsState};

use crate::{groundstation::MockGroundStation, state::Action};
use groundstation::MockGroundStation;
// use state::{action::Action, Satellite};

#[tokio::main]
async fn main() {
// println!("{}",
// serde_json::to_string(
// &Action::SelectSatellite{satellite: Satellite::default()}
// ).unwrap()
// );

println!("Starting server");

// TODO: setup logging
Expand Down Expand Up @@ -45,7 +52,6 @@ async fn main() {
.await
.unwrap();


}

async fn ws_handler(
Expand All @@ -68,13 +74,10 @@ async fn groundstation_handler(mut gs: impl GroundStation, ws_state: Arc<WsState
let name = status.name.clone();
println!("{:?}", status.clone());
ws_state
.apply(Action::UpdateStation {
name: name.clone(),
status,
})
.update()
.await
.unwrap_or_else(|err| {
println!("could not apply state from {}: {err}", name);
println!("could not update state from {}: {err}", name);
});
}
}
36 changes: 26 additions & 10 deletions orch-rs/src/state/action.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use super::GroundStation;
// use super::GroundStation;
use super::{State, Satellite};

#[derive(Deserialize)]
pub trait Action {
// UpdateStation{name : str, status : str}
// SelectSatellite{satellite: Satellite},
fn apply(self, state: &mut State);
}

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Action {
// some operations on the state
UpdateStation {
name: String,
status: GroundStation,
},
}
pub struct FrontendAction {
SelectSatellite{satellite: Satellite},
}

impl Action for FrontendAction {
fn apply(self, state: &mut State) {
match self {
FrontendAction::SelectSatellite { satellite } => {
state.current_satellite = satellite;
},
// Action::UpdateStation{name, status} => {
// state.stations.insert(name, status);
// }
}
}
}
11 changes: 1 addition & 10 deletions orch-rs/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use serde::{Deserialize, Serialize};

mod action;
pub mod action;
mod point;

pub use point::{CartesianPoint, PolarPoint};
Expand All @@ -15,15 +15,6 @@ pub struct State {
backend_status: BackendStatus,
}

impl State {
pub fn apply(&mut self, action: Action) {
match action {
Action::UpdateStation { name, status } => {
self.stations.insert(name, status);
}
}
}
}

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down
37 changes: 32 additions & 5 deletions orch-rs/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl WsState {
let mut state = self.state.lock().await;
let old_json = serde_json::to_value(&*state)?;

state.apply(action);
// state.apply(action);
action.apply(&mut state);

let new_json = serde_json::to_value(&*state)?;

Expand All @@ -83,6 +84,22 @@ impl WsState {

Ok(())
}

pub async fn update(&self) -> Result<(), Error> {
let state = self.state.lock().await;

let old_json = serde_json::to_value(&*state)?;
let new_json = serde_json::to_value(&*state)?;

let ops = json_patch::diff(&old_json, &new_json).0;

if !ops.is_empty() {
let msg = serde_json::to_string(&ServerMessage::Patch { ops })?;
self.broadcast_all(msg).await;
}

Ok(())
}
}

pub async fn handle_socket(socket: WebSocket, state: Arc<WsState>) {
Expand All @@ -94,10 +111,20 @@ pub async fn handle_socket(socket: WebSocket, state: Arc<WsState>) {
// process incoming messages
while let Some(Ok(msg)) = rx.next().await {
if let Message::Text(text) = msg {
println!("{text}");
// TODO: Action decoding
state.broadcast_all(text.clone()).await;
// add the message to the current state

if let Ok(action) = serde_json::from_str::<Action>(&text) {
if let Err(err) = state.apply(action).await {
println!("could not apply '{text}' to state due to {err}");
}
} else {
println!("Recieved invalid message {text}");
}

// println!("{text}");
// // TODO: Action decoding
// state.broadcast_all(text.clone()).await;
// // add the message to the current state
}

}
}

0 comments on commit 5c2dde3

Please sign in to comment.