more json

This commit is contained in:
Empire Phoenix 2024-06-10 21:37:58 +02:00
parent 3f98a321fc
commit b57eb2513c

View File

@ -98,7 +98,7 @@ enum SensorError {
OpenCircuit { hz: f32, min: f32 }, OpenCircuit { hz: f32, min: f32 },
} }
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Default)] #[derive(Copy, Clone, Debug, PartialEq, Default)]
struct TankState { struct TankState {
enough_water: bool, enough_water: bool,
warn_level: bool, warn_level: bool,
@ -106,6 +106,16 @@ struct TankState {
sensor_error: bool, sensor_error: bool,
raw: u16, raw: u16,
} }
#[derive(Serialize)]
struct TankStateMQTT {
enough_water: bool,
warn_level: bool,
left_ml: u32,
sensor_error: bool,
raw: u16,
water_frozen: String
}
#[derive(Serialize)] #[derive(Serialize)]
struct PlantStateMQTT<'a> { struct PlantStateMQTT<'a> {
a: &'a str, a: &'a str,
@ -355,18 +365,16 @@ fn safe_main() -> anyhow::Result<()> {
} }
let tank_state = determine_tank_state(&mut board, &config); let tank_state = determine_tank_state(&mut board, &config);
if online_mode == OnlineMode::Online { let mut tank_state_mqtt = TankStateMQTT{
match serde_json::to_string(&tank_state) { enough_water : tank_state.enough_water,
Ok(state) => { left_ml : tank_state.left_ml,
let _ = board.mqtt_publish(&config, "/water", state.as_bytes()); warn_level : tank_state.warn_level,
} sensor_error: tank_state.sensor_error,
Err(err) => { raw: tank_state.raw,
println!("Error publishing lightstate {}", err); water_frozen: "".to_owned()
}
}; };
}
let mut water_frozen = false; let mut water_frozen = false;
let mut temp: Option<f32> = None; let mut temp: Option<f32> = None;
for _attempt in 0..5 { for _attempt in 0..5 {
let water_temperature = board.water_temperature_c(); let water_temperature = board.water_temperature_c();
@ -386,21 +394,22 @@ fn safe_main() -> anyhow::Result<()> {
if res < 4_f32 { if res < 4_f32 {
water_frozen = true; water_frozen = true;
} }
if online_mode == OnlineMode::Online { tank_state_mqtt.water_frozen = water_frozen.to_string();
let _ =
board.mqtt_publish(&config, "/water/temperature", res.to_string().as_bytes());
let _ = board.mqtt_publish(
&config,
"/water/frozen",
water_frozen.to_string().as_bytes(),
);
}
} }
None => { None => {
tank_state_mqtt.water_frozen = "tank sensor error".to_owned()
}
}
if online_mode == OnlineMode::Online { if online_mode == OnlineMode::Online {
let _ = board.mqtt_publish(&config, "/water/temperature", "Error".as_bytes()); match serde_json::to_string(&tank_state_mqtt) {
Ok(state) => {
let _ = board.mqtt_publish(&config, "/water", state.as_bytes());
} }
Err(err) => {
println!("Error publishing tankstate {}", err);
} }
};
} }
let mut plantstate = [PlantState { let mut plantstate = [PlantState {
@ -622,7 +631,7 @@ fn publish_battery_state(
}; };
match serde_json::to_string(&bat) { match serde_json::to_string(&bat) {
Ok(state) => { Ok(state) => {
let _ = board.mqtt_publish(&config, "/plant/battery", state.as_bytes()); let _ = board.mqtt_publish(&config, "/battery", state.as_bytes());
} }
Err(err) => { Err(err) => {
println!("Error publishing battery_state {}", err); println!("Error publishing battery_state {}", err);