mirror of
https://codeberg.org/icewind/taspromto.git
synced 2026-06-03 16:44:11 +02:00
fix output
This commit is contained in:
parent
f7bd166df2
commit
f263ce5a1c
1 changed files with 38 additions and 5 deletions
43
src/main.rs
43
src/main.rs
|
|
@ -37,8 +37,9 @@ async fn main() {
|
||||||
for device in state.iter() {
|
for device in state.iter() {
|
||||||
writeln!(
|
writeln!(
|
||||||
&mut response,
|
&mut response,
|
||||||
"switch_state[name=\"{}\"] {}",
|
"switch_state{{tasmota_id=\"{}\", name=\"{}\"}} {}",
|
||||||
device.key().hostname,
|
device.key().hostname,
|
||||||
|
device.name,
|
||||||
if device.state { 1 } else { 0 }
|
if device.state { 1 } else { 0 }
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -46,8 +47,9 @@ async fn main() {
|
||||||
if let Some(power_watts) = device.power_watts {
|
if let Some(power_watts) = device.power_watts {
|
||||||
writeln!(
|
writeln!(
|
||||||
&mut response,
|
&mut response,
|
||||||
"power_watts[name=\"{}\"] {}",
|
"power_watts{{tasmota_id=\"{}\", name=\"{}\"}} {}",
|
||||||
device.key().hostname,
|
device.key().hostname,
|
||||||
|
device.name,
|
||||||
power_watts
|
power_watts
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -56,8 +58,9 @@ async fn main() {
|
||||||
if let Some(power_yesterday) = device.power_yesterday {
|
if let Some(power_yesterday) = device.power_yesterday {
|
||||||
writeln!(
|
writeln!(
|
||||||
&mut response,
|
&mut response,
|
||||||
"power_yesterday_kwh[name=\"{}\"] {}",
|
"power_yesterday_kwh{{tasmota_id=\"{}\", name=\"{}\"}} {}",
|
||||||
device.key().hostname,
|
device.key().hostname,
|
||||||
|
device.name,
|
||||||
power_yesterday
|
power_yesterday
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -66,8 +69,9 @@ async fn main() {
|
||||||
if let Some(power_today) = device.power_today {
|
if let Some(power_today) = device.power_today {
|
||||||
writeln!(
|
writeln!(
|
||||||
&mut response,
|
&mut response,
|
||||||
"power_today_kwh[name=\"{}\"] {}",
|
"power_today_kwh{{tasmota_id=\"{}\", name=\"{}\"}} {}",
|
||||||
device.key().hostname,
|
device.key().hostname,
|
||||||
|
device.name,
|
||||||
power_today
|
power_today
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -96,6 +100,10 @@ async fn mqtt_client(host: String, port: u16, device_states: DeviceStates) {
|
||||||
.subscribe("tele/+/+/SENSOR", QoS::AtMostOnce)
|
.subscribe("tele/+/+/SENSOR", QoS::AtMostOnce)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
client
|
||||||
|
.subscribe("stat/+/+/RESULT", QoS::AtMostOnce)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let notification = event_loop.poll().await.unwrap();
|
let notification = event_loop.poll().await.unwrap();
|
||||||
|
|
@ -105,7 +113,7 @@ async fn mqtt_client(host: String, port: u16, device_states: DeviceStates) {
|
||||||
|
|
||||||
match topic {
|
match topic {
|
||||||
Topic::LWT(device) => {
|
Topic::LWT(device) => {
|
||||||
// on discovery, ask the device for it's power state
|
// on discovery, ask the device for it's power state and name
|
||||||
client
|
client
|
||||||
.publish(
|
.publish(
|
||||||
device.get_topic("cmnd", "POWER"),
|
device.get_topic("cmnd", "POWER"),
|
||||||
|
|
@ -115,11 +123,29 @@ async fn mqtt_client(host: String, port: u16, device_states: DeviceStates) {
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
client
|
||||||
|
.publish(
|
||||||
|
device.get_topic("cmnd", "DeviceName"),
|
||||||
|
QoS::AtMostOnce,
|
||||||
|
false,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
Topic::Power(device) => {
|
Topic::Power(device) => {
|
||||||
let state = message.payload.as_ref() == b"ON";
|
let state = message.payload.as_ref() == b"ON";
|
||||||
device_states.entry(device).or_default().state = state;
|
device_states.entry(device).or_default().state = state;
|
||||||
}
|
}
|
||||||
|
Topic::Result(device) => {
|
||||||
|
let payload = std::str::from_utf8(message.payload.as_ref()).unwrap_or_default();
|
||||||
|
if let Ok(json) = json::parse(payload) {
|
||||||
|
let mut device_state = device_states.entry(device).or_default();
|
||||||
|
if json["DeviceName"].is_string() {
|
||||||
|
device_state.name = json["DeviceName"].to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Topic::Sensor(device) => {
|
Topic::Sensor(device) => {
|
||||||
let payload = std::str::from_utf8(message.payload.as_ref()).unwrap_or_default();
|
let payload = std::str::from_utf8(message.payload.as_ref()).unwrap_or_default();
|
||||||
if let Ok(json) = json::parse(payload) {
|
if let Ok(json) = json::parse(payload) {
|
||||||
|
|
@ -156,6 +182,7 @@ impl Device {
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct DeviceState {
|
struct DeviceState {
|
||||||
state: bool,
|
state: bool,
|
||||||
|
name: String,
|
||||||
power_watts: Option<f32>,
|
power_watts: Option<f32>,
|
||||||
power_yesterday: Option<f32>,
|
power_yesterday: Option<f32>,
|
||||||
power_today: Option<f32>,
|
power_today: Option<f32>,
|
||||||
|
|
@ -167,6 +194,7 @@ enum Topic {
|
||||||
Power(Device),
|
Power(Device),
|
||||||
State(Device),
|
State(Device),
|
||||||
Sensor(Device),
|
Sensor(Device),
|
||||||
|
Result(Device),
|
||||||
Other(String),
|
Other(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -185,6 +213,7 @@ impl From<&str> for Topic {
|
||||||
("tele", "STATE") => Topic::State(device),
|
("tele", "STATE") => Topic::State(device),
|
||||||
("stat", "POWER") => Topic::Power(device),
|
("stat", "POWER") => Topic::Power(device),
|
||||||
("tele", "SENSOR") => Topic::Sensor(device),
|
("tele", "SENSOR") => Topic::Sensor(device),
|
||||||
|
("stat", "RESULT") => Topic::Result(device),
|
||||||
_ => Topic::Other(raw.to_string()),
|
_ => Topic::Other(raw.to_string()),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -215,4 +244,8 @@ fn parse_topic() {
|
||||||
Topic::Sensor(device.clone()),
|
Topic::Sensor(device.clone()),
|
||||||
Topic::from("tele/foo/hostname/SENSOR")
|
Topic::from("tele/foo/hostname/SENSOR")
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Topic::Result(device.clone()),
|
||||||
|
Topic::from("stat/foo/hostname/RESULT")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue