Add notifier to break relay loop
This commit is contained in:
		
							parent
							
								
									83c1f033d5
								
							
						
					
					
						commit
						8785186dfa
					
				
					 12 changed files with 124 additions and 45 deletions
				
			
		| 
						 | 
				
			
			@ -41,7 +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<AppState>>,
 | 
			
		||||
	app_state: web::Data<Addr<AppState>>,
 | 
			
		||||
	path: web::Path<(String,)>,
 | 
			
		||||
	data: web::Json<RequestUpdateController>,
 | 
			
		||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -60,7 +60,7 @@ pub async fn update(
 | 
			
		|||
 | 
			
		||||
	let return_controller = Controller::from_db_model(&mut pool_conn, controller)?;
 | 
			
		||||
 | 
			
		||||
	app_server
 | 
			
		||||
	app_state
 | 
			
		||||
		.send(app_state::Action {
 | 
			
		||||
			controller_uid: uid.clone(),
 | 
			
		||||
			action: ControllerWsAction::Controller(return_controller.clone()),
 | 
			
		||||
| 
						 | 
				
			
			@ -73,7 +73,7 @@ pub async fn update(
 | 
			
		|||
#[delete("/api/v1/controllers/{controller_id}")]
 | 
			
		||||
pub async fn delete(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	app_server: web::Data<Addr<AppState>>,
 | 
			
		||||
	app_state: web::Data<Addr<AppState>>,
 | 
			
		||||
	path: web::Path<(String,)>,
 | 
			
		||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
| 
						 | 
				
			
			@ -81,7 +81,7 @@ pub async fn delete(
 | 
			
		|||
	let (controller_uid,) = path.into_inner();
 | 
			
		||||
	let uid = ControllerUid::try_from(controller_uid.as_str())?;
 | 
			
		||||
 | 
			
		||||
	app_server
 | 
			
		||||
	app_state
 | 
			
		||||
		.send(app_state::DisconnectController {
 | 
			
		||||
			controller_uid: uid.clone(),
 | 
			
		||||
		})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -84,7 +84,7 @@ pub async fn show_for_controller(
 | 
			
		|||
#[put("/api/v1/controllers/{controller_id}/relays/{relay_num}")]
 | 
			
		||||
pub async fn update_for_controller(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	app_server: web::Data<Addr<AppState>>,
 | 
			
		||||
	app_state: web::Data<Addr<AppState>>,
 | 
			
		||||
	path: web::Path<(String, i64)>,
 | 
			
		||||
	data: web::Json<RequestUpdateRelay>,
 | 
			
		||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -140,7 +140,7 @@ pub async fn update_for_controller(
 | 
			
		|||
 | 
			
		||||
	let return_relay = Relay::from_db_model(&mut pool_conn, relay)?;
 | 
			
		||||
 | 
			
		||||
	app_server
 | 
			
		||||
	app_state
 | 
			
		||||
		.send(app_state::Action {
 | 
			
		||||
			controller_uid: uid,
 | 
			
		||||
			action: ControllerWsAction::Relays(vec![return_relay.clone()]),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -64,7 +64,7 @@ impl ControllerWs {
 | 
			
		|||
 | 
			
		||||
		let addr = ctx.address();
 | 
			
		||||
		self.controller_uid = Some(controller_uid.clone());
 | 
			
		||||
		block_on(self.app_server.send(ConnectController {
 | 
			
		||||
		block_on(self.app_state.send(ConnectController {
 | 
			
		||||
			address: addr.recipient(),
 | 
			
		||||
			controller,
 | 
			
		||||
		}))??;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,7 +19,7 @@ use crate::utils::flatten_result;
 | 
			
		|||
pub struct ControllerWs {
 | 
			
		||||
	pub pool: Pool<Sqlite>,
 | 
			
		||||
	pub controller_uid: Option<ControllerUid>,
 | 
			
		||||
	pub app_server: Addr<AppState>,
 | 
			
		||||
	pub app_state: Addr<AppState>,
 | 
			
		||||
	pub hb: Instant,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -33,7 +33,7 @@ impl Actor for ControllerWs {
 | 
			
		|||
	fn stopped(&mut self, _ctx: &mut Self::Context) {
 | 
			
		||||
		if let Some(controller_uid) = &self.controller_uid {
 | 
			
		||||
			let flat_res = flatten_result(
 | 
			
		||||
				block_on(self.app_server.send(DisconnectController {
 | 
			
		||||
				block_on(self.app_state.send(DisconnectController {
 | 
			
		||||
					controller_uid: controller_uid.clone(),
 | 
			
		||||
				}))
 | 
			
		||||
				.map_err(EmgauwaError::from),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,7 +14,7 @@ pub mod controllers;
 | 
			
		|||
#[get("/api/v1/ws/controllers")]
 | 
			
		||||
pub async fn ws_controllers(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	app_server: web::Data<Addr<AppState>>,
 | 
			
		||||
	app_state: web::Data<Addr<AppState>>,
 | 
			
		||||
	req: HttpRequest,
 | 
			
		||||
	stream: web::Payload,
 | 
			
		||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -22,7 +22,7 @@ pub async fn ws_controllers(
 | 
			
		|||
		ControllerWs {
 | 
			
		||||
			pool: pool.get_ref().clone(),
 | 
			
		||||
			controller_uid: None,
 | 
			
		||||
			app_server: app_server.get_ref().clone(),
 | 
			
		||||
			app_state: app_state.get_ref().clone(),
 | 
			
		||||
			hb: Instant::now(),
 | 
			
		||||
		},
 | 
			
		||||
		&req,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -33,7 +33,7 @@ async fn main() -> Result<(), std::io::Error> {
 | 
			
		|||
		.map_err(EmgauwaError::from)?;
 | 
			
		||||
	conn.close().await.map_err(EmgauwaError::from)?;
 | 
			
		||||
 | 
			
		||||
	let app_server = AppState::new(pool.clone()).start();
 | 
			
		||||
	let app_state = AppState::new(pool.clone()).start();
 | 
			
		||||
 | 
			
		||||
	log::info!("Starting server on {}:{}", settings.host, settings.port);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -54,7 +54,7 @@ async fn main() -> Result<(), std::io::Error> {
 | 
			
		|||
			.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
 | 
			
		||||
			.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
 | 
			
		||||
			.app_data(web::Data::new(pool.clone()))
 | 
			
		||||
			.app_data(web::Data::new(app_server.clone()))
 | 
			
		||||
			.app_data(web::Data::new(app_state.clone()))
 | 
			
		||||
			.service(handlers::v1::controllers::index)
 | 
			
		||||
			.service(handlers::v1::controllers::show)
 | 
			
		||||
			.service(handlers::v1::controllers::update)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue