custom topics & payloads

This commit is contained in:
LastExceed 2025-05-23 17:46:25 +02:00
parent e01734d4bd
commit 078158a93e
7 changed files with 37 additions and 53 deletions

43
Cargo.lock generated
View File

@ -129,9 +129,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.40"
version = "0.4.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
dependencies = [
"android-tzdata",
"iana-time-zone",
@ -279,12 +279,6 @@ dependencies = [
"const-random",
]
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "encoding_rs"
version = "0.8.35"
@ -490,15 +484,6 @@ dependencies = [
"hashbrown 0.15.2",
]
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.15"
@ -952,7 +937,6 @@ dependencies = [
"config",
"csv",
"futures",
"itertools",
"json",
"rumqttc",
"serde",
@ -1082,9 +1066,9 @@ dependencies = [
[[package]]
name = "toml"
version = "0.8.20"
version = "0.8.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148"
checksum = "05ae329d1f08c4d17a59bed7ff5b5a769d062e64a62d34a3261b219e62cd5aae"
dependencies = [
"serde",
"serde_spanned",
@ -1094,26 +1078,33 @@ dependencies = [
[[package]]
name = "toml_datetime"
version = "0.6.8"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
checksum = "3da5db5a963e24bc68be8b17b6fa82814bb22ee8660f192bb182771d498f09a3"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.24"
version = "0.22.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474"
checksum = "310068873db2c5b3e7659d2cc35d21855dbafa50d1ce336397c666e3cb08137e"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"toml_write",
"winnow",
]
[[package]]
name = "toml_write"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfb942dfe1d8e29a7ee7fcbde5bd2b9a25fb89aa70caea2eba3bee836ff41076"
[[package]]
name = "trim-in-place"
version = "0.1.7"
@ -1341,9 +1332,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.7.4"
version = "0.7.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e97b544156e9bebe1a0ffbc03484fc1ffe3100cbce3ffb17eac35f7cdd7ab36"
checksum = "c06928c8748d81b05c9be96aad92e1b6ff01833332f281e8cfca3be4b35fc9ec"
dependencies = [
"memchr",
]

View File

@ -5,10 +5,14 @@ use std::collections::{HashMap, HashSet};
use config::Config;
use tap::Pipe;
use self::slave::Slave;
pub mod slave;
#[derive(Debug)]
pub struct MyConfig {
pub slaves_by_master: HashMap<String, HashSet<String>>,
pub slave_properties: HashMap<String, [bool; 3]>,
pub slave_properties: HashMap<String, Slave>,
pub machine_ids : HashMap<String, String>,
pub data_user : HashMap<String, UserData>,
pub data_machines : HashMap<String, MachineData>,

8
src/my_config/slave.rs Normal file
View File

@ -0,0 +1,8 @@
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Deserialize)]
pub struct Slave {
pub runs_continuously: bool,
pub needs_trailing_time: bool,
pub topic: String,
pub payload_on: String,
pub payload_off: String,
}

View File

@ -7,7 +7,6 @@ use rumqttc::{AsyncClient, QoS};
use tokio::sync::RwLock;
use crate::my_config::MyConfig;
use crate::utils::index;
use crate::utils::booking::Booking;
mod announcer;
@ -50,27 +49,14 @@ impl<Kind> State<Kind> {
//probably doesn't belong here, dunno where else to put it
async fn set_power_state(&self, machine: &str, new_state: bool) {
dark_grey_ln!("set power state - {machine} {new_state}");
let is_tasmota = self.config.slave_properties[machine][index::IS_TASMOTA];
let topic =
if is_tasmota {
format!("cmnd/{machine}/Power")
} else {
format!("shellies/{machine}/relay/0/command")
};
let props = &self.config.slave_properties[machine];
let payload = if new_state { &props.payload_on } else { &props.payload_off };
#[allow(clippy::collapsible_else_if)]
let payload =
if is_tasmota {
if new_state { b"ON".as_slice() } else { b"OFF".as_slice() }
} else {
if new_state { b"on".as_slice() } else { b"off".as_slice() }
};
dark_grey_ln!("publishing\n topic: {topic}\n payload: {payload:?}");
dark_grey_ln!("publishing\n topic: {}\n payload: {payload:?}", payload);
self.client
.read()
.await
.publish(topic, QoS::AtMostOnce, false, payload)
.publish(&props.topic, QoS::AtMostOnce, false, payload.as_bytes())
.await
.expect("failed to publish");
}

View File

@ -8,7 +8,6 @@ use rumqttc::Event::Incoming;
use rumqttc::Packet::Publish;
use crate::{State, Listener, BOOKING_TOPIC};
use crate::utils::index;
use crate::utils::get_power_state;
use crate::utils::logs::{log_debug, machinelog};
use crate::utils::booking::Booking;
@ -148,7 +147,7 @@ impl State<Listener> {
.get(machine)
.unwrap_or(&fallback) // machine being unknown already got logged when it got turned on, so we can ignore it here
.iter()
.filter(|slave| booking.is_running() || self.config.slave_properties[*slave][index::RUNS_CONTINUOUSLY])
.filter(|slave| booking.is_running() || self.config.slave_properties[*slave].runs_continuously)
)
.cloned()
.collect();
@ -160,10 +159,10 @@ impl State<Listener> {
.ok_or("unknown master")?
.sub(&slaves_used_by_others)
.into_iter()
.filter(|slave| if self.config.slave_properties[slave][index::RUNS_CONTINUOUSLY] { long_slaves } else { short_slaves });
.filter(|slave| if self.config.slave_properties[slave].runs_continuously { long_slaves } else { short_slaves });
for slave in slaves_to_update {
if self.config.slave_properties[&slave][index::NEEDS_TRAILING_TIME] {
if self.config.slave_properties[&slave].needs_trailing_time {
if power {
self.cancel_scheduled_shutdown(&slave).await;
} else {

View File

@ -2,7 +2,6 @@ use std::time::Duration;
pub mod logs;
pub mod booking;
pub mod index;
pub fn get_power_state(payload: &str) -> Result<String, &'static str> {
//todo: there gotta be an easier way to do this

View File

@ -1,3 +0,0 @@
pub const RUNS_CONTINUOUSLY: usize = 0;
pub const NEEDS_TRAILING_TIME: usize = 1;
pub const IS_TASMOTA: usize = 2;