From df4df17511cee3aab1fba1cd0acf6e04e88398ba Mon Sep 17 00:00:00 2001 From: Frederic Henrichs Date: Tue, 15 Oct 2024 12:06:31 +0200 Subject: [PATCH] backend: remove charger from bridge state when the charger is removed. --- backend/src/routes/charger/remove.rs | 18 +++++++++++++++++- backend/src/routes/user/delete.rs | 5 +++-- backend/src/udp_server/socket.rs | 4 ++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/backend/src/routes/charger/remove.rs b/backend/src/routes/charger/remove.rs index fe9fdd0..562bd05 100644 --- a/backend/src/routes/charger/remove.rs +++ b/backend/src/routes/charger/remove.rs @@ -26,7 +26,7 @@ use crate::{ error::Error, routes::charger::charger_belongs_to_user, utils::{get_connection, web_block_unpacked}, - AppState, + AppState, BridgeState, }; #[derive(Debug, Deserialize, Serialize, ToSchema)] @@ -81,6 +81,20 @@ pub async fn delete_charger(charger: i32, state: &web::Data) -> actix_ Ok(()) } +pub fn remove_charger_from_state(charger: i32, state: &web::Data) { + let socket = { + let mut map = state.charger_management_map_with_id.lock().unwrap(); + map.remove(&charger) + }; + + if let Some(socket) = socket { + let socket = socket.lock().unwrap(); + let remote_address = socket.get_remote_address(); + let mut map = state.charger_management_map.lock().unwrap(); + let _ = map.remove(&remote_address); + } +} + #[utoipa::path( context_path = "/charger", request_body = DeleteChargerSchema, @@ -97,6 +111,7 @@ pub async fn remove( state: web::Data, uid: crate::models::uuid::Uuid, data: web::Json, + bridge_state: web::Data, ) -> Result { if !charger_belongs_to_user(&state, uid.clone().into(), data.charger.clone()).await? { @@ -106,6 +121,7 @@ pub async fn remove( delete_all_keys(data.charger.clone(), &state).await?; delete_all_allowed_users(data.charger.clone(), &state).await?; delete_charger(data.charger, &state).await?; + remove_charger_from_state(data.charger, &bridge_state); Ok(HttpResponse::Ok()) } diff --git a/backend/src/routes/user/delete.rs b/backend/src/routes/user/delete.rs index e963bf3..09f864e 100644 --- a/backend/src/routes/user/delete.rs +++ b/backend/src/routes/user/delete.rs @@ -4,7 +4,7 @@ use diesel::{prelude::*, result::Error::NotFound}; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; -use crate::{error::Error, routes::{auth::login::{validate_password, FindBy}, charger::remove::{delete_all_allowed_users, delete_all_keys, delete_charger}, user::logout::delete_all_refresh_tokens}, utils::{get_connection, web_block_unpacked}, AppState}; +use crate::{error::Error, routes::{auth::login::{validate_password, FindBy}, charger::remove::{delete_all_allowed_users, delete_all_keys, delete_charger, remove_charger_from_state}, user::logout::delete_all_refresh_tokens}, utils::{get_connection, web_block_unpacked}, AppState, BridgeState}; #[derive(ToSchema, Serialize, Deserialize)] pub struct DeleteUserSchema { @@ -47,7 +47,7 @@ async fn get_all_chargers_for_user(user_id: uuid::Uuid, state: &web::Data, user_id: crate::models::uuid::Uuid, payload: web::Json) -> actix_web::Result { +pub async fn delete_user(state: web::Data, bridge_state: web::Data, user_id: crate::models::uuid::Uuid, payload: web::Json) -> actix_web::Result { let user_id = user_id.into(); let conn = get_connection(&state)?; @@ -59,6 +59,7 @@ pub async fn delete_user(state: web::Data, user_id: crate::models::uui delete_all_keys(id, &state).await?; delete_all_allowed_users(id, &state).await?; delete_charger(id, &state).await?; + remove_charger_from_state(id, &bridge_state); } delete_all_refresh_tokens(user_id, &state).await?; diff --git a/backend/src/udp_server/socket.rs b/backend/src/udp_server/socket.rs index e8a3c8a..e833c16 100644 --- a/backend/src/udp_server/socket.rs +++ b/backend/src/udp_server/socket.rs @@ -214,4 +214,8 @@ impl ManagementSocket { pub fn id(&self) -> i32 { self.charger_id } + + pub fn get_remote_address(&self) -> SocketAddr { + self.remote_addr + } }