Skip to content

Commit ba0971f

Browse files
committed
feat: add /api/thermal endpoint
1 parent 2da096f commit ba0971f

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

rust/src/thermal_regulation/shared_state.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
1010
use anyhow::Result;
1111
use rocket::serde::{Deserialize, Serialize};
12+
use rocket_okapi::JsonSchema;
1213
use std::collections::{HashMap, VecDeque};
1314
use std::time::{SystemTime, UNIX_EPOCH};
1415
use tokio::sync::RwLock;
15-
1616
/// Maximum number of historical data points per regulator (1 day at 1Hz)
1717
pub const MAX_HISTORY_SIZE: usize = 86400;
1818

1919
/// Single data point in thermal regulation history
20-
#[derive(Debug, Clone, Serialize, Deserialize)]
20+
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
2121
pub struct ThermalDataPoint {
2222
/// Timestamp in Unix seconds
2323
pub timestamp: u64,
@@ -32,7 +32,7 @@ pub struct ThermalDataPoint {
3232
}
3333

3434
/// PID controller components for analysis
35-
#[derive(Debug, Clone, Serialize, Deserialize)]
35+
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
3636
pub struct PidComponents {
3737
/// Proportional term value
3838
pub proportional: f64,
@@ -45,7 +45,7 @@ pub struct PidComponents {
4545
}
4646

4747
/// Historical data for a single thermal regulator
48-
#[derive(Debug, Clone)]
48+
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
4949
pub struct ThermalRegulatorHistory {
5050
/// Regulator unique identifier
5151
pub id: String,
@@ -64,7 +64,7 @@ pub struct ThermalRegulatorHistory {
6464
}
6565

6666
/// Current status of a thermal regulator
67-
#[derive(Debug, Clone, Serialize, Deserialize)]
67+
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
6868
pub enum RegulatorStatus {
6969
/// Regulator is not initialized
7070
Uninitialized,
@@ -79,7 +79,7 @@ pub enum RegulatorStatus {
7979
}
8080

8181
/// Current PID parameters for a regulator
82-
#[derive(Debug, Clone, Serialize, Deserialize)]
82+
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
8383
pub struct CurrentPidParams {
8484
/// Proportional gain
8585
pub kp: f64,
@@ -95,7 +95,7 @@ pub struct CurrentPidParams {
9595
}
9696

9797
/// Shared thermal regulation state across the entire system
98-
#[derive(Debug)]
98+
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
9999
pub struct SharedThermalRegulationState {
100100
/// Map of regulator ID to its historical data
101101
regulators: HashMap<String, ThermalRegulatorHistory>,
@@ -106,7 +106,7 @@ pub struct SharedThermalRegulationState {
106106
}
107107

108108
/// Global thermal regulation system status
109-
#[derive(Debug, Clone, Serialize, Deserialize)]
109+
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
110110
pub struct ThermalSystemStatus {
111111
/// Total number of configured regulators
112112
pub total_regulators: usize,

rust/src/visualization/api/get/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
pub mod config;
66
pub mod test;
7+
pub mod thermal;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025 Ronan LE MEILLAT, SCTG Development
2+
// This file is part of the rust-photoacoustic project and is licensed under the
3+
// SCTG Development Non-Commercial License v1.0 (see LICENSE.md for details).
4+
5+
//! Thermal data retrieval API for photoacoustic applications
6+
//! This module provides an API for retrieving thermal data from the SharedThermalRegulationState
7+
8+
use crate::thermal_regulation::shared_state::{SharedThermalRegulationState, SharedThermalState};
9+
use auth_macros::openapi_protect_get;
10+
use rocket::get;
11+
use rocket_okapi::okapi::openapi3::OpenApi;
12+
use rocket_okapi::openapi_get_routes_spec;
13+
14+
#[openapi_protect_get("/api/thermal", "read:api", tag = "Thermal Regulation")]
15+
pub async fn get_thermal_data(
16+
state: &rocket::State<SharedThermalState>,
17+
) -> Result<rocket::serde::json::Json<SharedThermalRegulationState>, rocket::http::Status> {
18+
// Retrieve the current thermal regulation state
19+
let thermal_state = state.read().await.clone();
20+
21+
// Return the thermal state as JSON
22+
Ok(rocket::serde::json::Json(thermal_state))
23+
}
24+
25+
/// Centralized function to get all thermal routes with OpenAPI documentation
26+
pub fn get_thermal_routes() -> (Vec<rocket::Route>, OpenApi) {
27+
openapi_get_routes_spec![get_thermal_data]
28+
}

rust/src/visualization/api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ pub mod graph;
66
pub mod post;
77
pub use get::config::*;
88
pub use get::test::*;
9+
pub use get::thermal::*;
910
pub use post::test::*;

rust/src/visualization/server/builder.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,12 @@ pub async fn build_rocket(
206206
.manage(config.clone()); // Add config as managed state for future dynamic configuration
207207

208208
// Add thermal regulation state if available
209+
let (openapi_routes_thermal, openapi_spec_thermal) = get_thermal_routes();
209210
let rocket_builder = if let Some(thermal_state) = thermal_state {
210211
debug!("Adding SharedThermalState to Rocket state management");
211-
rocket_builder.manage(thermal_state)
212+
rocket_builder
213+
.manage(thermal_state)
214+
.mount("/", openapi_routes_thermal)
212215
} else {
213216
debug!("No thermal state provided, thermal regulation API will return 404");
214217
rocket_builder
@@ -248,7 +251,11 @@ pub async fn build_rocket(
248251
(openapi_routes_audio, openapi_spec_audio) = get_audio_streaming_routes();
249252

250253
// Merge the audio OpenAPI spec with the base spec
251-
let merged_spec = marge_spec_list(&[("/".to_string(), openapi_spec_base), ("/".to_string(),openapi_spec_audio), ("/".to_string(),openapi_spec_graph), ("/".to_string(),openapi_spec_config)]).unwrap();
254+
let merged_spec = marge_spec_list(&[("/".to_string(), openapi_spec_base),
255+
("/".to_string(),openapi_spec_audio),
256+
("/".to_string(),openapi_spec_graph),
257+
("/".to_string(),openapi_spec_config),
258+
( "/".to_string(), openapi_spec_thermal)]).unwrap();
252259
let openapi_settings = OpenApiSettings::default();
253260
rocket_builder
254261
.mount(

0 commit comments

Comments
 (0)