Can get the full list of relays through the API

This commit is contained in:
Pierre HUBERT 2024-07-24 23:35:58 +02:00
parent 4d5ba939d1
commit 73163e6e69
5 changed files with 43 additions and 2 deletions

View File

@ -1,6 +1,6 @@
use crate::app_config::AppConfig; use crate::app_config::AppConfig;
use crate::crypto::pki; use crate::crypto::pki;
use crate::devices::device::{Device, DeviceGeneralInfo, DeviceId, DeviceInfo}; use crate::devices::device::{Device, DeviceGeneralInfo, DeviceId, DeviceInfo, DeviceRelay};
use crate::utils::time_utils::time_secs; use crate::utils::time_utils::time_secs;
use openssl::x509::{X509Req, X509}; use openssl::x509::{X509Req, X509};
use std::collections::HashMap; use std::collections::HashMap;
@ -192,4 +192,12 @@ impl DevicesList {
Ok(()) Ok(())
} }
/// Get the full list of relays
pub fn relays_list(&mut self) -> Vec<DeviceRelay> {
self.0
.iter()
.flat_map(|(_id, d)| d.relays.clone())
.collect()
}
} }

View File

@ -1,5 +1,5 @@
use crate::constants; use crate::constants;
use crate::devices::device::{Device, DeviceGeneralInfo, DeviceId, DeviceInfo}; use crate::devices::device::{Device, DeviceGeneralInfo, DeviceId, DeviceInfo, DeviceRelay};
use crate::devices::devices_list::DevicesList; use crate::devices::devices_list::DevicesList;
use crate::energy::consumption; use crate::energy::consumption;
use crate::energy::consumption::EnergyConsumption; use crate::energy::consumption::EnergyConsumption;
@ -171,3 +171,16 @@ impl Handler<GetSingleDevice> for EnergyActor {
self.devices.get_single(&msg.0) self.devices.get_single(&msg.0)
} }
} }
/// Get the full list of relays
#[derive(Message)]
#[rtype(result = "Vec<DeviceRelay>")]
pub struct GetRelaysList;
impl Handler<GetRelaysList> for EnergyActor {
type Result = Vec<DeviceRelay>;
fn handle(&mut self, _msg: GetRelaysList, _ctx: &mut Context<Self>) -> Self::Result {
self.devices.relays_list()
}
}

View File

@ -107,10 +107,12 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
})) }))
.route("/", web::get().to(server_controller::secure_home)) .route("/", web::get().to(server_controller::secure_home))
// Web API // Web API
// Server controller
.route( .route(
"/web_api/server/config", "/web_api/server/config",
web::get().to(server_controller::config), web::get().to(server_controller::config),
) )
// Auth controller
.route( .route(
"/web_api/auth/password_auth", "/web_api/auth/password_auth",
web::post().to(auth_controller::password_auth), web::post().to(auth_controller::password_auth),
@ -123,6 +125,7 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
"/web_api/auth/sign_out", "/web_api/auth/sign_out",
web::get().to(auth_controller::sign_out), web::get().to(auth_controller::sign_out),
) )
// Energy controller
.route( .route(
"/web_api/energy/curr_consumption", "/web_api/energy/curr_consumption",
web::get().to(energy_controller::curr_consumption), web::get().to(energy_controller::curr_consumption),
@ -131,6 +134,7 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
"/web_api/energy/cached_consumption", "/web_api/energy/cached_consumption",
web::get().to(energy_controller::cached_consumption), web::get().to(energy_controller::cached_consumption),
) )
// Devices controller
.route( .route(
"/web_api/devices/list_pending", "/web_api/devices/list_pending",
web::get().to(devices_controller::list_pending), web::get().to(devices_controller::list_pending),
@ -155,6 +159,11 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
"/web_api/device/{id}", "/web_api/device/{id}",
web::delete().to(devices_controller::delete_device), web::delete().to(devices_controller::delete_device),
) )
// Relays API
.route(
"/web_api/relays/list",
web::get().to(relays_controller::get_list),
)
// Devices API // Devices API
.route( .route(
"/devices_api/utils/time", "/devices_api/utils/time",

View File

@ -1,4 +1,5 @@
pub mod auth_controller; pub mod auth_controller;
pub mod devices_controller; pub mod devices_controller;
pub mod energy_controller; pub mod energy_controller;
pub mod relays_controller;
pub mod server_controller; pub mod server_controller;

View File

@ -0,0 +1,10 @@
use crate::energy::energy_actor;
use crate::server::custom_error::HttpResult;
use crate::server::WebEnergyActor;
use actix_web::HttpResponse;
/// Get the full list of relays
pub async fn get_list(actor: WebEnergyActor) -> HttpResult {
let list = actor.send(energy_actor::GetRelaysList).await?;
Ok(HttpResponse::Ok().json(list))
}