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