Skip to content

Commit

Permalink
backend: remove charger from bridge state when the charger is removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreddow committed Oct 15, 2024
1 parent 965b6b5 commit df4df17
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
18 changes: 17 additions & 1 deletion backend/src/routes/charger/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -81,6 +81,20 @@ pub async fn delete_charger(charger: i32, state: &web::Data<AppState>) -> actix_
Ok(())
}

pub fn remove_charger_from_state(charger: i32, state: &web::Data<BridgeState>) {
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,
Expand All @@ -97,6 +111,7 @@ pub async fn remove(
state: web::Data<AppState>,
uid: crate::models::uuid::Uuid,
data: web::Json<DeleteChargerSchema>,
bridge_state: web::Data<BridgeState>,
) -> Result<impl Responder, actix_web::Error> {

if !charger_belongs_to_user(&state, uid.clone().into(), data.charger.clone()).await? {
Expand All @@ -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())
}
Expand Down
5 changes: 3 additions & 2 deletions backend/src/routes/user/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -47,7 +47,7 @@ async fn get_all_chargers_for_user(user_id: uuid::Uuid, state: &web::Data<AppSta
)
)]
#[delete("/delete")]
pub async fn delete_user(state: web::Data<AppState>, user_id: crate::models::uuid::Uuid, payload: web::Json<DeleteUserSchema>) -> actix_web::Result<impl Responder> {
pub async fn delete_user(state: web::Data<AppState>, bridge_state: web::Data<BridgeState>, user_id: crate::models::uuid::Uuid, payload: web::Json<DeleteUserSchema>) -> actix_web::Result<impl Responder> {
let user_id = user_id.into();

let conn = get_connection(&state)?;
Expand All @@ -59,6 +59,7 @@ pub async fn delete_user(state: web::Data<AppState>, 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?;
Expand Down
4 changes: 4 additions & 0 deletions backend/src/udp_server/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,8 @@ impl ManagementSocket {
pub fn id(&self) -> i32 {
self.charger_id
}

pub fn get_remote_address(&self) -> SocketAddr {
self.remote_addr
}
}

0 comments on commit df4df17

Please sign in to comment.