2023-12-01 17:27:04 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2024-04-25 17:45:22 +00:00
|
|
|
use actix::{Actor, Addr, Context, Handler, Message, Recipient};
|
2024-04-30 06:44:33 +00:00
|
|
|
use emgauwa_common::db::DbController;
|
|
|
|
use emgauwa_common::errors::EmgauwaError;
|
|
|
|
use emgauwa_common::models::{convert_db_list, Controller, Relay};
|
|
|
|
use emgauwa_common::types::{ControllerWsAction, EmgauwaUid, RelayStates};
|
2023-12-01 17:27:04 +00:00
|
|
|
use futures::executor::block_on;
|
|
|
|
use sqlx::{Pool, Sqlite};
|
|
|
|
|
2024-04-25 17:45:22 +00:00
|
|
|
use crate::handlers::v1::ws::relays::{RelaysWs, SendRelays};
|
|
|
|
|
2023-12-01 17:27:04 +00:00
|
|
|
#[derive(Message)]
|
2023-12-04 22:59:26 +00:00
|
|
|
#[rtype(result = "Result<(), EmgauwaError>")]
|
2023-12-01 17:27:04 +00:00
|
|
|
pub struct DisconnectController {
|
2024-04-28 00:29:34 +00:00
|
|
|
pub controller_uid: EmgauwaUid,
|
2023-12-01 17:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
2023-12-04 22:59:26 +00:00
|
|
|
#[rtype(result = "Result<(), EmgauwaError>")]
|
2023-12-01 17:27:04 +00:00
|
|
|
pub struct ConnectController {
|
|
|
|
pub address: Recipient<ControllerWsAction>,
|
|
|
|
pub controller: Controller,
|
|
|
|
}
|
|
|
|
|
2024-04-25 15:21:54 +00:00
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct UpdateRelayStates {
|
2024-04-28 00:29:34 +00:00
|
|
|
pub controller_uid: EmgauwaUid,
|
2024-04-25 15:21:54 +00:00
|
|
|
pub relay_states: RelayStates,
|
|
|
|
}
|
|
|
|
|
2024-04-25 17:45:22 +00:00
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "Result<Vec<Relay>, EmgauwaError>")]
|
|
|
|
pub struct GetRelays {}
|
|
|
|
|
2023-12-04 22:59:26 +00:00
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "Result<(), EmgauwaError>")]
|
|
|
|
pub struct Action {
|
2024-04-28 00:29:34 +00:00
|
|
|
pub controller_uid: EmgauwaUid,
|
2023-12-04 22:59:26 +00:00
|
|
|
pub action: ControllerWsAction,
|
|
|
|
}
|
|
|
|
|
2024-04-25 17:45:22 +00:00
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct ConnectRelayClient {
|
|
|
|
pub addr: Addr<RelaysWs>,
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:57:00 +00:00
|
|
|
pub struct AppState {
|
2023-12-01 17:27:04 +00:00
|
|
|
pub pool: Pool<Sqlite>,
|
2024-04-28 00:29:34 +00:00
|
|
|
pub connected_controllers: HashMap<EmgauwaUid, (Controller, Recipient<ControllerWsAction>)>,
|
2024-04-25 17:45:22 +00:00
|
|
|
pub connected_relay_clients: Vec<Addr<RelaysWs>>,
|
2023-12-01 17:27:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 20:57:00 +00:00
|
|
|
impl AppState {
|
|
|
|
pub fn new(pool: Pool<Sqlite>) -> AppState {
|
|
|
|
AppState {
|
2023-12-01 17:27:04 +00:00
|
|
|
pool,
|
2023-12-04 22:59:26 +00:00
|
|
|
connected_controllers: HashMap::new(),
|
2024-04-25 17:45:22 +00:00
|
|
|
connected_relay_clients: Vec::new(),
|
2023-12-01 17:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-25 17:45:22 +00:00
|
|
|
|
|
|
|
fn get_relays(&self) -> Result<Vec<Relay>, EmgauwaError> {
|
|
|
|
let mut pool_conn = block_on(self.pool.acquire())?;
|
|
|
|
let db_controllers = block_on(DbController::get_all(&mut pool_conn))?;
|
|
|
|
let mut controllers: Vec<Controller> = convert_db_list(&mut pool_conn, db_controllers)?;
|
|
|
|
|
|
|
|
self.connected_controllers
|
|
|
|
.iter()
|
|
|
|
.for_each(|(uid, (connected_controller, _))| {
|
|
|
|
if let Some(c) = controllers.iter_mut().find(|c| c.c.uid == *uid) {
|
|
|
|
c.apply_relay_states(&connected_controller.get_relay_states());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut relays: Vec<Relay> = Vec::new();
|
|
|
|
controllers.iter().for_each(|c| {
|
|
|
|
relays.extend(c.relays.clone());
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(relays)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify_relay_clients(&mut self) {
|
|
|
|
self.connected_relay_clients.retain(|addr| addr.connected());
|
|
|
|
|
|
|
|
match self.get_relays() {
|
|
|
|
Ok(relays) => match serde_json::to_string(&relays) {
|
|
|
|
Ok(json) => {
|
|
|
|
self.connected_relay_clients.iter_mut().for_each(|addr| {
|
|
|
|
let relays_json = json.clone();
|
|
|
|
addr.do_send(SendRelays { relays_json });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("Failed to serialize relays: {:?}", err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("Failed to get relays: {:?}", err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-12-01 17:27:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 20:57:00 +00:00
|
|
|
impl Actor for AppState {
|
2023-12-01 17:27:04 +00:00
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:57:00 +00:00
|
|
|
impl Handler<DisconnectController> for AppState {
|
2023-12-04 22:59:26 +00:00
|
|
|
type Result = Result<(), EmgauwaError>;
|
2023-12-01 17:27:04 +00:00
|
|
|
|
|
|
|
fn handle(&mut self, msg: DisconnectController, _ctx: &mut Self::Context) -> Self::Result {
|
2023-12-04 22:59:26 +00:00
|
|
|
let mut pool_conn = block_on(self.pool.acquire())?;
|
2023-12-01 17:27:04 +00:00
|
|
|
|
2023-12-05 15:11:40 +00:00
|
|
|
if let Some((controller, address)) = self.connected_controllers.remove(&msg.controller_uid)
|
|
|
|
{
|
2023-12-01 17:27:04 +00:00
|
|
|
if let Err(err) = block_on(controller.c.update_active(&mut pool_conn, false)) {
|
|
|
|
log::error!(
|
|
|
|
"Failed to mark controller {} as inactive: {:?}",
|
|
|
|
controller.c.uid,
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
2024-04-25 00:12:25 +00:00
|
|
|
// TODO: why does the block_on(send()) version not return? The AppState will be stuck.
|
|
|
|
//block_on(address.send(ControllerWsAction::Disconnect))??;
|
|
|
|
address.do_send(ControllerWsAction::Disconnect);
|
2023-12-01 17:27:04 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:57:00 +00:00
|
|
|
impl Handler<ConnectController> for AppState {
|
2023-12-04 22:59:26 +00:00
|
|
|
type Result = Result<(), EmgauwaError>;
|
2023-12-01 17:27:04 +00:00
|
|
|
|
|
|
|
fn handle(&mut self, msg: ConnectController, _ctx: &mut Self::Context) -> Self::Result {
|
2024-03-03 21:39:28 +00:00
|
|
|
log::debug!("Connecting controller: {}", msg.controller.c.uid);
|
2023-12-04 22:59:26 +00:00
|
|
|
self.connected_controllers
|
|
|
|
.insert(msg.controller.c.uid.clone(), (msg.controller, msg.address));
|
2023-12-01 17:27:04 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-12-04 22:59:26 +00:00
|
|
|
|
2024-04-25 15:21:54 +00:00
|
|
|
impl Handler<UpdateRelayStates> for AppState {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: UpdateRelayStates, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
if let Some((controller, _)) = self.connected_controllers.get_mut(&msg.controller_uid) {
|
|
|
|
controller.apply_relay_states(&msg.relay_states);
|
|
|
|
}
|
2024-04-25 17:45:22 +00:00
|
|
|
self.notify_relay_clients();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler<GetRelays> for AppState {
|
|
|
|
type Result = Result<Vec<Relay>, EmgauwaError>;
|
|
|
|
|
|
|
|
fn handle(&mut self, _msg: GetRelays, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
self.get_relays()
|
2024-04-25 15:21:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:57:00 +00:00
|
|
|
impl Handler<Action> for AppState {
|
2023-12-04 22:59:26 +00:00
|
|
|
type Result = Result<(), EmgauwaError>;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: Action, _ctx: &mut Self::Context) -> Self::Result {
|
2023-12-05 15:11:40 +00:00
|
|
|
log::debug!("Forwarding action: {:?}", msg.action);
|
2023-12-04 22:59:26 +00:00
|
|
|
if let Some((_, address)) = self.connected_controllers.get(&msg.controller_uid) {
|
2024-04-25 00:12:25 +00:00
|
|
|
// TODO: why does the block_on(send()) version not return? The AppState will be stuck.
|
|
|
|
//block_on(address.send(msg.action))?
|
|
|
|
address.do_send(msg.action);
|
|
|
|
Ok(())
|
2023-12-04 22:59:26 +00:00
|
|
|
} else {
|
|
|
|
Err(EmgauwaError::Connection(msg.controller_uid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-25 17:45:22 +00:00
|
|
|
|
|
|
|
impl Handler<ConnectRelayClient> for AppState {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: ConnectRelayClient, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
self.connected_relay_clients.push(msg.addr);
|
|
|
|
}
|
|
|
|
}
|