v1.9.9-pre-5: primary interface endpoint
All checks were successful
Build and release .deb / Release (push) Successful in 50s
All checks were successful
Build and release .deb / Release (push) Successful in 50s
This commit is contained in:
parent
0a4e7055fe
commit
34a735deec
4 changed files with 35 additions and 9 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -239,7 +239,7 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
|
|||
|
||||
[[package]]
|
||||
name = "charge-controller-supervisor"
|
||||
version = "1.9.9-pre-4"
|
||||
version = "1.9.9-pre-5"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
|
@ -2201,7 +2201,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "tesla-charge-controller"
|
||||
version = "1.9.9-pre-4"
|
||||
version = "1.9.9-pre-5"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
|
|
|
@ -4,7 +4,7 @@ default-members = ["charge-controller-supervisor"]
|
|||
resolver = "2"
|
||||
|
||||
[workspace.package]
|
||||
version = "1.9.9-pre-4"
|
||||
version = "1.9.9-pre-5"
|
||||
|
||||
[workspace.lints.clippy]
|
||||
pedantic = "warn"
|
||||
|
|
|
@ -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());
|
||||
|
||||
tokio::select! {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use rocket::{get, routes, serde::json::Json, State};
|
||||
|
||||
pub struct ServerState {
|
||||
primary_name: String,
|
||||
map: std::collections::HashMap<
|
||||
String,
|
||||
std::sync::Arc<tokio::sync::RwLock<crate::controller::CommonData>>,
|
||||
|
@ -9,19 +10,27 @@ pub struct ServerState {
|
|||
|
||||
impl ServerState {
|
||||
pub fn new(
|
||||
primary_name: String,
|
||||
map: std::collections::HashMap<
|
||||
String,
|
||||
std::sync::Arc<tokio::sync::RwLock<crate::controller::CommonData>>,
|
||||
>,
|
||||
) -> Self {
|
||||
Self { map }
|
||||
Self { primary_name, map }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
|
||||
rocket::build()
|
||||
.manage(state)
|
||||
.mount("/", routes![metrics, interfaces, all_interfaces, interface])
|
||||
rocket::build().manage(state).mount(
|
||||
"/",
|
||||
routes![
|
||||
metrics,
|
||||
interfaces,
|
||||
all_interfaces,
|
||||
primary_interface,
|
||||
interface
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/interfaces")]
|
||||
|
@ -29,6 +38,21 @@ fn interfaces(state: &State<ServerState>) -> Json<Vec<String>> {
|
|||
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")]
|
||||
async fn all_interfaces(
|
||||
state: &State<ServerState>,
|
||||
|
@ -68,6 +92,7 @@ fn metrics() -> Result<String, ServerError> {
|
|||
enum ServerError {
|
||||
Prometheus,
|
||||
NotFound,
|
||||
InvalidPrimaryName,
|
||||
}
|
||||
|
||||
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 {
|
||||
fn respond_to(self, _: &'a rocket::Request<'_>) -> rocket::response::Result<'a> {
|
||||
Err(match self {
|
||||
Self::Prometheus => rocket::http::Status::InternalServerError,
|
||||
Self::NotFound => rocket::http::Status::NotFound,
|
||||
ServerError::Prometheus => rocket::http::Status::InternalServerError,
|
||||
Self::InvalidPrimaryName => rocket::http::Status::ServiceUnavailable,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue