1
1
use crate :: config:: AppConfig ;
2
2
use crate :: config:: SharedConfig ;
3
+ use crate :: ev:: send_ev_data;
4
+ use crate :: ev:: BatteryData ;
3
5
use crate :: ev:: EV_MODEL_FILE ;
6
+ use crate :: mitm:: Packet ;
4
7
use axum:: {
5
8
body:: Body ,
6
9
extract:: { Query , RawBody , State } ,
@@ -17,6 +20,7 @@ use std::path::PathBuf;
17
20
use std:: sync:: Arc ;
18
21
use tokio:: fs;
19
22
use tokio:: io:: AsyncWriteExt ;
23
+ use tokio:: sync:: mpsc:: Sender ;
20
24
21
25
const TEMPLATE : & str = include_str ! ( "../static/index.html" ) ;
22
26
const PICO_CSS : & str = include_str ! ( "../static/pico.min.css" ) ;
@@ -28,6 +32,8 @@ const NAME: &str = "<i><bright-black> web: </>";
28
32
pub struct AppState {
29
33
pub config : SharedConfig ,
30
34
pub config_file : Arc < PathBuf > ,
35
+ pub tx : Option < Sender < Packet > > ,
36
+ pub sensor_channel : Arc < Option < u8 > > ,
31
37
}
32
38
33
39
pub fn app ( state : Arc < AppState > ) -> Router {
@@ -37,6 +43,7 @@ pub fn app(state: Arc<AppState>) -> Router {
37
43
. route ( "/download" , get ( download_handler) )
38
44
. route ( "/restart" , get ( restart_handler) )
39
45
. route ( "/upload-hex-model" , post ( upload_hex_model_handler) )
46
+ . route ( "/battery" , post ( battery_handler) )
40
47
. with_state ( state)
41
48
}
42
49
@@ -49,6 +56,33 @@ async fn index() -> impl IntoResponse {
49
56
Html ( html)
50
57
}
51
58
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
+
52
86
fn generate_filename ( ) -> String {
53
87
let now = Local :: now ( ) ;
54
88
now. format ( "%Y%m%d%H%M%S_aa-proxy-rs.log" ) . to_string ( )
0 commit comments