Fix bugs and add controller action for controller ws
This commit is contained in:
		
							parent
							
								
									8b1affd8c7
								
							
						
					
					
						commit
						6f8d63e7be
					
				
					 11 changed files with 177 additions and 56 deletions
				
			
		| 
						 | 
				
			
			@ -51,7 +51,8 @@ impl Handler<DisconnectController> for AppServer {
 | 
			
		|||
	fn handle(&mut self, msg: DisconnectController, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
		let mut pool_conn = block_on(self.pool.acquire())?;
 | 
			
		||||
 | 
			
		||||
		if let Some((controller, _)) = self.connected_controllers.remove(&msg.controller_uid) {
 | 
			
		||||
		if let Some((controller, address)) = self.connected_controllers.remove(&msg.controller_uid)
 | 
			
		||||
		{
 | 
			
		||||
			if let Err(err) = block_on(controller.c.update_active(&mut pool_conn, false)) {
 | 
			
		||||
				log::error!(
 | 
			
		||||
					"Failed to mark controller {} as inactive: {:?}",
 | 
			
		||||
| 
						 | 
				
			
			@ -59,6 +60,7 @@ impl Handler<DisconnectController> for AppServer {
 | 
			
		|||
					err
 | 
			
		||||
				);
 | 
			
		||||
			}
 | 
			
		||||
			block_on(address.send(ControllerWsAction::Disconnect))??;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -79,6 +81,7 @@ impl Handler<Action> for AppServer {
 | 
			
		|||
	type Result = Result<(), EmgauwaError>;
 | 
			
		||||
 | 
			
		||||
	fn handle(&mut self, msg: Action, _ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
		log::debug!("Forwarding action: {:?}", msg.action);
 | 
			
		||||
		if let Some((_, address)) = self.connected_controllers.get(&msg.controller_uid) {
 | 
			
		||||
			block_on(address.send(msg.action))?
 | 
			
		||||
		} else {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,10 +1,14 @@
 | 
			
		|||
use actix::Addr;
 | 
			
		||||
use actix_web::{delete, get, put, web, HttpResponse};
 | 
			
		||||
use emgauwa_lib::db::DbController;
 | 
			
		||||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
 | 
			
		||||
use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel};
 | 
			
		||||
use emgauwa_lib::types::{ControllerUid, RequestUpdateController};
 | 
			
		||||
use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestUpdateController};
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
use crate::app_state;
 | 
			
		||||
use crate::app_state::AppServer;
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/controllers")]
 | 
			
		||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
| 
						 | 
				
			
			@ -37,6 +41,7 @@ pub async fn show(
 | 
			
		|||
#[put("/api/v1/controllers/{controller_id}")]
 | 
			
		||||
pub async fn update(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	app_server: web::Data<Addr<AppServer>>,
 | 
			
		||||
	path: web::Path<(String,)>,
 | 
			
		||||
	data: web::Json<RequestUpdateController>,
 | 
			
		||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -54,12 +59,21 @@ pub async fn update(
 | 
			
		|||
		.await?;
 | 
			
		||||
 | 
			
		||||
	let return_controller = Controller::from_db_model(&mut pool_conn, controller)?;
 | 
			
		||||
 | 
			
		||||
	app_server
 | 
			
		||||
		.send(app_state::Action {
 | 
			
		||||
			controller_uid: uid.clone(),
 | 
			
		||||
			action: ControllerWsAction::Controller(return_controller.clone()),
 | 
			
		||||
		})
 | 
			
		||||
		.await??;
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_controller))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[delete("/api/v1/controllers/{controller_id}")]
 | 
			
		||||
pub async fn delete(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	app_server: web::Data<Addr<AppServer>>,
 | 
			
		||||
	path: web::Path<(String,)>,
 | 
			
		||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
| 
						 | 
				
			
			@ -67,6 +81,12 @@ pub async fn delete(
 | 
			
		|||
	let (controller_uid,) = path.into_inner();
 | 
			
		||||
	let uid = ControllerUid::try_from(controller_uid.as_str())?;
 | 
			
		||||
 | 
			
		||||
	app_server
 | 
			
		||||
		.send(app_state::DisconnectController {
 | 
			
		||||
			controller_uid: uid.clone(),
 | 
			
		||||
		})
 | 
			
		||||
		.await??;
 | 
			
		||||
 | 
			
		||||
	DbController::delete_by_uid(&mut pool_conn, uid).await?;
 | 
			
		||||
	Ok(HttpResponse::Ok().json("controller got deleted"))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,7 +16,11 @@ impl ControllerWs {
 | 
			
		|||
		ctx: &mut <ControllerWs as Actor>::Context,
 | 
			
		||||
		controller: Controller,
 | 
			
		||||
	) -> Result<(), EmgauwaError> {
 | 
			
		||||
		log::info!("Registering controller: {:?}", controller);
 | 
			
		||||
		log::info!(
 | 
			
		||||
			"Registering controller: {} ({})",
 | 
			
		||||
			controller.c.name,
 | 
			
		||||
			controller.c.uid
 | 
			
		||||
		);
 | 
			
		||||
		let c = &controller.c;
 | 
			
		||||
		let controller_db = block_on(DbController::get_by_uid_or_create(
 | 
			
		||||
			conn,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -78,8 +78,16 @@ impl Handler<ControllerWsAction> for ControllerWs {
 | 
			
		|||
	type Result = Result<(), EmgauwaError>;
 | 
			
		||||
 | 
			
		||||
	fn handle(&mut self, action: ControllerWsAction, ctx: &mut Self::Context) -> Self::Result {
 | 
			
		||||
		let action_json = serde_json::to_string(&action)?;
 | 
			
		||||
		ctx.text(action_json);
 | 
			
		||||
		match action {
 | 
			
		||||
			ControllerWsAction::Disconnect => {
 | 
			
		||||
				ctx.close(None);
 | 
			
		||||
				ctx.stop();
 | 
			
		||||
			}
 | 
			
		||||
			_ => {
 | 
			
		||||
				let action_json = serde_json::to_string(&action)?;
 | 
			
		||||
				ctx.text(action_json);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue