Improve active handling for controllers
This commit is contained in:
parent
ec461a1a14
commit
6459804e1f
7 changed files with 87 additions and 35 deletions
emgauwa-lib/src/handlers/v1/ws
|
@ -18,10 +18,18 @@ pub enum ControllerWsAction {
|
|||
|
||||
struct ControllerWs {
|
||||
pub pool: Pool<Sqlite>,
|
||||
pub controller: Option<DbController>,
|
||||
}
|
||||
|
||||
impl Actor for ControllerWs {
|
||||
type Context = ws::WebsocketContext<Self>;
|
||||
|
||||
fn stopped(&mut self, _ctx: &mut Self::Context) {
|
||||
if let Some(controller) = &self.controller {
|
||||
let mut pool_conn = futures::executor::block_on(self.pool.acquire()).unwrap();
|
||||
futures::executor::block_on(controller.update_active(&mut pool_conn, false)).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
|
||||
|
@ -32,7 +40,8 @@ impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
|
|||
Ok(Message::Ping(msg)) => ctx.pong(&msg),
|
||||
Ok(Message::Text(text)) => {
|
||||
let action: ControllerWsAction = serde_json::from_str(&text).unwrap();
|
||||
let action_res = futures::executor::block_on(handle_action(&mut pool_conn, action));
|
||||
let action_res =
|
||||
futures::executor::block_on(self.handle_action(&mut pool_conn, action));
|
||||
if let Err(e) = action_res {
|
||||
log::error!("Error handling action: {:?}", e);
|
||||
ctx.text(serde_json::to_string(&e).unwrap());
|
||||
|
@ -47,33 +56,39 @@ impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn handle_action(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
action: ControllerWsAction,
|
||||
) -> Result<(), DatabaseError> {
|
||||
match action {
|
||||
ControllerWsAction::Register(controller) => {
|
||||
log::info!("Registering controller: {:?}", controller);
|
||||
let c = &controller.controller;
|
||||
let controller_db =
|
||||
DbController::get_by_uid_or_create(conn, &c.uid, &c.name, c.relay_count, c.active)
|
||||
impl ControllerWs {
|
||||
pub async fn handle_action(
|
||||
&mut self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
action: ControllerWsAction,
|
||||
) -> Result<(), DatabaseError> {
|
||||
match action {
|
||||
ControllerWsAction::Register(controller) => {
|
||||
log::info!("Registering controller: {:?}", controller);
|
||||
let c = &controller.controller;
|
||||
let controller_db =
|
||||
DbController::get_by_uid_or_create(conn, &c.uid, &c.name, c.relay_count)
|
||||
.await?;
|
||||
controller_db.update_active(conn, true).await?;
|
||||
|
||||
println!("Controller: {:?}", controller_db);
|
||||
|
||||
for relay in &controller.relays {
|
||||
let r = &relay.relay;
|
||||
let relay_db = DbRelay::get_by_controller_and_num_or_create(
|
||||
conn,
|
||||
&controller_db,
|
||||
r.number,
|
||||
&r.name,
|
||||
)
|
||||
.await?;
|
||||
println!("Controller relay: {:?}", relay_db);
|
||||
}
|
||||
|
||||
println!("Controller: {:?}", controller_db);
|
||||
self.controller = Some(controller_db);
|
||||
|
||||
for relay in &controller.relays {
|
||||
let r = &relay.relay;
|
||||
let relay_db = DbRelay::get_by_controller_and_num_or_create(
|
||||
conn,
|
||||
&controller_db,
|
||||
r.number,
|
||||
&r.name,
|
||||
)
|
||||
.await?;
|
||||
println!("Controller relay: {:?}", relay_db);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,6 +102,7 @@ pub async fn index(
|
|||
let resp = ws::start(
|
||||
ControllerWs {
|
||||
pool: pool.get_ref().clone(),
|
||||
controller: None,
|
||||
},
|
||||
&req,
|
||||
stream,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue