From 774d4b083060a0394cd0a2664a863c7ca59f1cdb Mon Sep 17 00:00:00 2001 From: Karsten Richter Date: Mon, 5 Feb 2024 11:48:25 +0100 Subject: [PATCH 1/5] Use serial number suffix instead of prefix for topic When trying to use more than one inverter on the same mqtt host the previous implementation causes a topic clash as the serial numbers share a common prefix. This change uses the last 8 digits instead of the first for the topic (and whenever the shortened SN is used) --- hms2mqtt/src/home_assistant.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hms2mqtt/src/home_assistant.rs b/hms2mqtt/src/home_assistant.rs index de11b07a..3d5bd82b 100644 --- a/hms2mqtt/src/home_assistant.rs +++ b/hms2mqtt/src/home_assistant.rs @@ -71,7 +71,12 @@ impl HMSStateResponse { } fn short_dtu_sn(&self) -> String { - self.dtu_sn[..8].to_string() + let suffix = { + let split_pos = self.dtu_sn.char_indices().nth_back(8).unwrap().0; + &self.dtu_sn[split_pos..] + }; + + suffix.to_string } fn get_total_efficiency(&self) -> f32 { From dcc4353a93f54ff79ca87fa2379da39253bcaa84 Mon Sep 17 00:00:00 2001 From: Karsten Richter Date: Tue, 6 Feb 2024 14:21:48 +0100 Subject: [PATCH 2/5] fix typo in function call --- hms2mqtt/src/home_assistant.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hms2mqtt/src/home_assistant.rs b/hms2mqtt/src/home_assistant.rs index 3d5bd82b..22ff761e 100644 --- a/hms2mqtt/src/home_assistant.rs +++ b/hms2mqtt/src/home_assistant.rs @@ -76,7 +76,7 @@ impl HMSStateResponse { &self.dtu_sn[split_pos..] }; - suffix.to_string + suffix.to_string() } fn get_total_efficiency(&self) -> f32 { From 2b6d4c8e5501fb253536ec5ff26ee0e8ae7b4210 Mon Sep 17 00:00:00 2001 From: Karsten Richter Date: Tue, 6 Feb 2024 14:29:02 +0100 Subject: [PATCH 3/5] cargo fmt formatting --- hms2mqtt/src/home_assistant.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hms2mqtt/src/home_assistant.rs b/hms2mqtt/src/home_assistant.rs index 22ff761e..74e5d16b 100644 --- a/hms2mqtt/src/home_assistant.rs +++ b/hms2mqtt/src/home_assistant.rs @@ -72,8 +72,8 @@ impl HMSStateResponse { fn short_dtu_sn(&self) -> String { let suffix = { - let split_pos = self.dtu_sn.char_indices().nth_back(8).unwrap().0; - &self.dtu_sn[split_pos..] + let split_pos = self.dtu_sn.char_indices().nth_back(8).unwrap().0; + &self.dtu_sn[split_pos..] }; suffix.to_string() From a622b284ca7adca0efcc8cd01d39c55b4b82436a Mon Sep 17 00:00:00 2001 From: Karsten Richter Date: Sun, 11 Feb 2024 11:31:31 +0100 Subject: [PATCH 4/5] introduce configurable client_id running two instances of hms-mqtt-publish with the hardcoded client_id causes conflicts and high cpu load. This makes the client_id optionally configurable --- hms2mqtt/src/mqtt_config.rs | 1 + src/bin/hms-mqtt-publish/rumqttc_wrapper.rs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/hms2mqtt/src/mqtt_config.rs b/hms2mqtt/src/mqtt_config.rs index 47f61ee6..b9e38057 100644 --- a/hms2mqtt/src/mqtt_config.rs +++ b/hms2mqtt/src/mqtt_config.rs @@ -6,4 +6,5 @@ pub struct MqttConfig { pub port: Option, pub username: Option, pub password: Option, + pub client_id: Option, } diff --git a/src/bin/hms-mqtt-publish/rumqttc_wrapper.rs b/src/bin/hms-mqtt-publish/rumqttc_wrapper.rs index ea0df3f4..952ad947 100644 --- a/src/bin/hms-mqtt-publish/rumqttc_wrapper.rs +++ b/src/bin/hms-mqtt-publish/rumqttc_wrapper.rs @@ -58,8 +58,12 @@ impl mqtt_wrapper::MqttWrapper for RumqttcWrapper { } fn new(config: &MqttConfig, suffix: &str) -> Self { + + let mut client_id = config.client_id.as_deref().unwrap_or("hms-mqtt-publish").to_string(); + client_id.push_str(suffix); + let mut mqttoptions = MqttOptions::new( - "hms800wt2-mqtt-publisher".to_string() + suffix, + &client_id, &config.host, config.port.unwrap_or(1883), ); From fcc82adb6977ed1a201a35b2ee8f7a8302ef2391 Mon Sep 17 00:00:00 2001 From: Karsten Richter Date: Fri, 1 Mar 2024 14:06:26 +0100 Subject: [PATCH 5/5] fix test for new client_id --- tests/integration_tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 49983938..448ac83f 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -51,6 +51,7 @@ fn publish_one_message() { username: None, password: None, tls: None, + client_id: Some("myclient".to_string()), }, "-test", );