v1.9.9-pre-5: primary interface endpoint
All checks were successful
Build and release .deb / Release (push) Successful in 50s

This commit is contained in:
Alex Janka 2024-12-28 22:45:31 +11:00
parent 0a4e7055fe
commit 34a735deec
4 changed files with 35 additions and 9 deletions

4
Cargo.lock generated
View file

@ -239,7 +239,7 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]] [[package]]
name = "charge-controller-supervisor" name = "charge-controller-supervisor"
version = "1.9.9-pre-4" version = "1.9.9-pre-5"
dependencies = [ dependencies = [
"chrono", "chrono",
"clap", "clap",
@ -2201,7 +2201,7 @@ dependencies = [
[[package]] [[package]]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "1.9.9-pre-4" version = "1.9.9-pre-5"
dependencies = [ dependencies = [
"chrono", "chrono",
"clap", "clap",

View file

@ -4,7 +4,7 @@ default-members = ["charge-controller-supervisor"]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]
version = "1.9.9-pre-4" version = "1.9.9-pre-5"
[workspace.lints.clippy] [workspace.lints.clippy]
pedantic = "warn" pedantic = "warn"

View file

@ -82,7 +82,7 @@ async fn watch(args: Args) -> eyre::Result<()> {
} }
} }
let server = web::rocket(web::ServerState::new(map)); let server = web::rocket(web::ServerState::new(config.primary_charge_controller, map));
let server_task = tokio::task::spawn(server.launch()); let server_task = tokio::task::spawn(server.launch());
tokio::select! { tokio::select! {

View file

@ -1,6 +1,7 @@
use rocket::{get, routes, serde::json::Json, State}; use rocket::{get, routes, serde::json::Json, State};
pub struct ServerState { pub struct ServerState {
primary_name: String,
map: std::collections::HashMap< map: std::collections::HashMap<
String, String,
std::sync::Arc<tokio::sync::RwLock<crate::controller::CommonData>>, std::sync::Arc<tokio::sync::RwLock<crate::controller::CommonData>>,
@ -9,19 +10,27 @@ pub struct ServerState {
impl ServerState { impl ServerState {
pub fn new( pub fn new(
primary_name: String,
map: std::collections::HashMap< map: std::collections::HashMap<
String, String,
std::sync::Arc<tokio::sync::RwLock<crate::controller::CommonData>>, std::sync::Arc<tokio::sync::RwLock<crate::controller::CommonData>>,
>, >,
) -> Self { ) -> Self {
Self { map } Self { primary_name, map }
} }
} }
pub fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> { pub fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
rocket::build() rocket::build().manage(state).mount(
.manage(state) "/",
.mount("/", routes![metrics, interfaces, all_interfaces, interface]) routes![
metrics,
interfaces,
all_interfaces,
primary_interface,
interface
],
)
} }
#[get("/interfaces")] #[get("/interfaces")]
@ -29,6 +38,21 @@ fn interfaces(state: &State<ServerState>) -> Json<Vec<String>> {
Json(state.map.keys().cloned().collect()) Json(state.map.keys().cloned().collect())
} }
#[get("/interfaces/primary")]
async fn primary_interface(
state: &State<ServerState>,
) -> Result<Json<crate::controller::CommonData>, ServerError> {
let s = state
.map
.get(&state.primary_name)
.ok_or(ServerError::InvalidPrimaryName)?
.read()
.await
.clone();
Ok(Json(s))
}
#[get("/interfaces/data")] #[get("/interfaces/data")]
async fn all_interfaces( async fn all_interfaces(
state: &State<ServerState>, state: &State<ServerState>,
@ -68,6 +92,7 @@ fn metrics() -> Result<String, ServerError> {
enum ServerError { enum ServerError {
Prometheus, Prometheus,
NotFound, NotFound,
InvalidPrimaryName,
} }
impl From<prometheus::Error> for ServerError { impl From<prometheus::Error> for ServerError {
@ -79,8 +104,9 @@ impl From<prometheus::Error> for ServerError {
impl<'a> rocket::response::Responder<'a, 'a> for ServerError { impl<'a> rocket::response::Responder<'a, 'a> for ServerError {
fn respond_to(self, _: &'a rocket::Request<'_>) -> rocket::response::Result<'a> { fn respond_to(self, _: &'a rocket::Request<'_>) -> rocket::response::Result<'a> {
Err(match self { Err(match self {
Self::Prometheus => rocket::http::Status::InternalServerError,
Self::NotFound => rocket::http::Status::NotFound, Self::NotFound => rocket::http::Status::NotFound,
ServerError::Prometheus => rocket::http::Status::InternalServerError, Self::InvalidPrimaryName => rocket::http::Status::ServiceUnavailable,
}) })
} }
} }