Add basic exchange of relay states
This commit is contained in:
parent
0460e838bc
commit
8a83602d6a
10 changed files with 153 additions and 41 deletions
emgauwa-controller/src/ws
|
@ -4,7 +4,7 @@ use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule
|
|||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
|
||||
use emgauwa_lib::models::{Controller, Relay};
|
||||
use emgauwa_lib::types::{ControllerWsAction, ScheduleUid};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use futures::{future, pin_mut, SinkExt, StreamExt};
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use tokio::time;
|
||||
|
@ -12,8 +12,8 @@ use tokio_tungstenite::tungstenite::Message;
|
|||
use tokio_tungstenite::{connect_async, tungstenite};
|
||||
|
||||
use crate::app_state::AppState;
|
||||
use crate::utils::app_state_get_this;
|
||||
use crate::{app_state, utils};
|
||||
use crate::utils;
|
||||
use crate::utils::{app_state_get_relay_notifier, app_state_get_this};
|
||||
|
||||
pub async fn run_ws_loop(pool: Pool<Sqlite>, app_state: Addr<AppState>, url: String) {
|
||||
log::debug!("Spawned ws loop");
|
||||
|
@ -52,9 +52,14 @@ async fn run_websocket(
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let (app_state_tx, app_state_rx) = futures_channel::mpsc::unbounded::<Message>();
|
||||
tokio::spawn(read_app_state(app_state.clone(), app_state_tx));
|
||||
let app_state_to_ws = app_state_rx.map(Ok).forward(write);
|
||||
|
||||
let read_handler = read.for_each(|msg| handle_message(pool.clone(), app_state, msg));
|
||||
|
||||
read_handler.await;
|
||||
pin_mut!(app_state_to_ws, read_handler);
|
||||
future::select(app_state_to_ws, read_handler).await;
|
||||
|
||||
log::warn!("Lost connection to websocket");
|
||||
}
|
||||
|
@ -65,6 +70,26 @@ async fn run_websocket(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn read_app_state(
|
||||
app_state: Addr<AppState>,
|
||||
tx: futures_channel::mpsc::UnboundedSender<Message>,
|
||||
) -> Result<(), EmgauwaError> {
|
||||
let notifier = &*app_state_get_relay_notifier(&app_state).await?;
|
||||
loop {
|
||||
notifier.notified().await;
|
||||
log::debug!("Relay change detected");
|
||||
let ws_action = ControllerWsAction::Register(app_state_get_this(&app_state).await?);
|
||||
|
||||
let ws_action_json = serde_json::to_string(&ws_action)?;
|
||||
tx.unbounded_send(Message::text(ws_action_json))
|
||||
.map_err(|_| {
|
||||
EmgauwaError::Other(String::from(
|
||||
"Failed to forward message from app state to websocket",
|
||||
))
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_message(
|
||||
pool: Pool<Sqlite>,
|
||||
app_state: &Addr<AppState>,
|
||||
|
@ -77,8 +102,8 @@ async fn handle_message(
|
|||
return;
|
||||
}
|
||||
};
|
||||
if let Message::Text(text) = msg {
|
||||
match serde_json::from_str(&text) {
|
||||
match msg {
|
||||
Message::Text(text) => match serde_json::from_str(&text) {
|
||||
Ok(action) => {
|
||||
log::debug!("Received action: {:?}", action);
|
||||
let mut pool_conn = match pool.acquire().await {
|
||||
|
@ -96,7 +121,11 @@ async fn handle_message(
|
|||
Err(e) => {
|
||||
log::error!("Error deserializing action: {:?}", e);
|
||||
}
|
||||
},
|
||||
Message::Ping(_) => {
|
||||
log::debug!("Received ping");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue