Skip to content

Commit 6acb347

Browse files
committed
web: rewrite initial axum battery_handler
1 parent c3d4e41 commit 6acb347

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/ev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>
2828

2929
#[derive(Debug, Deserialize)]
3030
pub struct BatteryData {
31-
battery_level: f32,
31+
pub battery_level: f32,
3232
}
3333

3434
// reset server context

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ async fn tokio_main(
171171
let state = web::AppState {
172172
config: config.clone(),
173173
config_file: config_file.into(),
174+
tx: None,
175+
sensor_channel: None.into(),
174176
};
175177
let app = web::app(state.into());
176178

src/web.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::config::AppConfig;
22
use crate::config::SharedConfig;
3+
use crate::ev::send_ev_data;
4+
use crate::ev::BatteryData;
35
use crate::ev::EV_MODEL_FILE;
6+
use crate::mitm::Packet;
47
use axum::{
58
body::Body,
69
extract::{Query, RawBody, State},
@@ -17,6 +20,7 @@ use std::path::PathBuf;
1720
use std::sync::Arc;
1821
use tokio::fs;
1922
use tokio::io::AsyncWriteExt;
23+
use tokio::sync::mpsc::Sender;
2024

2125
const TEMPLATE: &str = include_str!("../static/index.html");
2226
const PICO_CSS: &str = include_str!("../static/pico.min.css");
@@ -28,6 +32,8 @@ const NAME: &str = "<i><bright-black> web: </>";
2832
pub struct AppState {
2933
pub config: SharedConfig,
3034
pub config_file: Arc<PathBuf>,
35+
pub tx: Option<Sender<Packet>>,
36+
pub sensor_channel: Arc<Option<u8>>,
3137
}
3238

3339
pub fn app(state: Arc<AppState>) -> Router {
@@ -37,6 +43,7 @@ pub fn app(state: Arc<AppState>) -> Router {
3743
.route("/download", get(download_handler))
3844
.route("/restart", get(restart_handler))
3945
.route("/upload-hex-model", post(upload_hex_model_handler))
46+
.route("/battery", post(battery_handler))
4047
.with_state(state)
4148
}
4249

@@ -49,6 +56,33 @@ async fn index() -> impl IntoResponse {
4956
Html(html)
5057
}
5158

59+
pub async fn battery_handler(
60+
State(state): State<Arc<AppState>>,
61+
Json(data): Json<BatteryData>,
62+
) -> impl IntoResponse {
63+
if data.battery_level < 0.0 || data.battery_level > 100.0 {
64+
let msg = format!(
65+
"battery_level out of range: {} (expected 0.0–100.0)",
66+
data.battery_level
67+
);
68+
return (StatusCode::BAD_REQUEST, msg).into_response();
69+
}
70+
71+
info!("{} Received battery level: {}", NAME, data.battery_level);
72+
73+
if let Some(ch) = *state.sensor_channel {
74+
if let Some(tx) = &state.tx {
75+
if let Err(e) = send_ev_data(tx.clone(), data.battery_level, ch, 0, 0.0).await {
76+
error!("{} EV model error: {}", NAME, e);
77+
}
78+
}
79+
} else {
80+
warn!("{} Not sending packet because no sensor channel yet", NAME);
81+
}
82+
83+
(StatusCode::OK, "OK").into_response()
84+
}
85+
5286
fn generate_filename() -> String {
5387
let now = Local::now();
5488
now.format("%Y%m%d%H%M%S_aa-proxy-rs.log").to_string()

0 commit comments

Comments
 (0)