2024-07-22 20:19:48 +00:00
|
|
|
use crate::devices::device::{DeviceGeneralInfo, DeviceId};
|
2024-07-03 17:17:47 +00:00
|
|
|
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};
|
2024-07-03 17:17:47 +00:00
|
|
|
|
|
|
|
/// 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-22 20:19:48 +00:00
|
|
|
/// Update a device information
|
|
|
|
pub async fn update_device(
|
|
|
|
actor: WebEnergyActor,
|
|
|
|
id: web::Path<DeviceInPath>,
|
|
|
|
update: web::Json<DeviceGeneralInfo>,
|
|
|
|
) -> HttpResult {
|
|
|
|
if let Some(e) = update.error() {
|
|
|
|
return Ok(HttpResponse::BadRequest().json(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
actor
|
|
|
|
.send(energy_actor::UpdateDeviceGeneralInfo(
|
|
|
|
id.id.clone(),
|
|
|
|
update.0.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())
|
|
|
|
}
|