Can delete a pending device

This commit is contained in:
2024-07-03 21:10:15 +02:00
parent 716af6219a
commit 2502ed6bcf
6 changed files with 120 additions and 9 deletions

View File

@ -95,4 +95,27 @@ impl DevicesList {
pub fn full_list(&self) -> Vec<Device> {
self.0.clone().into_values().collect()
}
/// Delete a device
pub fn delete(&mut self, id: &DeviceId) -> anyhow::Result<()> {
let crt_path = AppConfig::get().device_cert_path(id);
if crt_path.is_file() {
// TODO : implement
unimplemented!("Certificate revocation not implemented yet!");
}
let csr_path = AppConfig::get().device_csr_path(id);
if csr_path.is_file() {
std::fs::remove_file(&csr_path)?;
}
let conf_path = AppConfig::get().device_config_path(id);
if conf_path.is_file() {
std::fs::remove_file(&conf_path)?;
}
self.0.remove(id);
Ok(())
}
}

View File

@ -94,6 +94,22 @@ impl Handler<EnrollDevice> for EnergyActor {
}
}
/// Delete a device
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct DeleteDevice(pub DeviceId);
impl Handler<DeleteDevice> for EnergyActor {
type Result = anyhow::Result<()>;
fn handle(&mut self, msg: DeleteDevice, _ctx: &mut Context<Self>) -> Self::Result {
log::info!("Requested to delete device {:?}...", &msg.0);
self.devices.delete(&msg.0)?;
// TODO : delete energy related information
Ok(())
}
}
/// Get the list of devices
#[derive(Message)]
#[rtype(result = "Vec<Device>")]

View File

@ -139,6 +139,10 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
"/web_api/devices/list_validated",
web::get().to(devices_controller::list_validated),
)
.route(
"/web_api/device/{id}",
web::delete().to(devices_controller::delete_device),
)
// Devices API
.route(
"/devices_api/utils/time",

View File

@ -1,7 +1,8 @@
use crate::devices::device::DeviceId;
use crate::energy::energy_actor;
use crate::server::custom_error::HttpResult;
use crate::server::WebEnergyActor;
use actix_web::HttpResponse;
use actix_web::{web, HttpResponse};
/// Get the list of pending (not accepted yet) devices
pub async fn list_pending(actor: WebEnergyActor) -> HttpResult {
@ -26,3 +27,17 @@ pub async fn list_validated(actor: WebEnergyActor) -> HttpResult {
Ok(HttpResponse::Ok().json(list))
}
#[derive(serde::Deserialize)]
pub struct DeviceInPath {
id: DeviceId,
}
/// Delete a device
pub async fn delete_device(actor: WebEnergyActor, id: web::Path<DeviceInPath>) -> HttpResult {
actor
.send(energy_actor::DeleteDevice(id.id.clone()))
.await??;
Ok(HttpResponse::Accepted().finish())
}