SolarEnergy/central_backend/src/server/web_api/devices_controller.rs

65 lines
1.8 KiB
Rust
Raw Normal View History

2024-07-03 19:10:15 +00:00
use crate::devices::device::DeviceId;
use crate::energy::energy_actor;
use crate::server::custom_error::HttpResult;
use crate::server::WebEnergyActor;
2024-07-03 19:10:15 +00:00
use actix_web::{web, HttpResponse};
/// Get the list of pending (not accepted yet) devices
pub async fn list_pending(actor: WebEnergyActor) -> HttpResult {
let list = actor
.send(energy_actor::GetDeviceLists)
.await?
.into_iter()
.filter(|d| !d.validated)
.collect::<Vec<_>>();
Ok(HttpResponse::Ok().json(list))
}
/// Get the list of validated (not accepted yet) devices
pub async fn list_validated(actor: WebEnergyActor) -> HttpResult {
let list = actor
.send(energy_actor::GetDeviceLists)
.await?
.into_iter()
.filter(|d| d.validated)
.collect::<Vec<_>>();
Ok(HttpResponse::Ok().json(list))
}
2024-07-03 19:10:15 +00:00
#[derive(serde::Deserialize)]
pub struct DeviceInPath {
id: DeviceId,
}
2024-07-18 18:06:46 +00:00
/// Get a single device information
pub async fn get_single(actor: WebEnergyActor, id: web::Path<DeviceInPath>) -> HttpResult {
let Some(dev) = actor
.send(energy_actor::GetSingleDevice(id.id.clone()))
.await?
else {
return Ok(HttpResponse::NotFound().json("Requested device was not found!"));
};
Ok(HttpResponse::Ok().json(dev))
}
2024-07-03 19:32:32 +00:00
/// Validate a device
pub async fn validate_device(actor: WebEnergyActor, id: web::Path<DeviceInPath>) -> HttpResult {
actor
.send(energy_actor::ValidateDevice(id.id.clone()))
.await??;
Ok(HttpResponse::Accepted().finish())
}
2024-07-03 19:10:15 +00:00
/// 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())
}