From 70688ede67df4cc9cd39224eec437ce89c5da2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96rjan=20Backsell?= Date: Mon, 11 Nov 2024 19:49:10 +0100 Subject: [PATCH] [ferroamp] Binding for ferroamp 20241111 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes done in: FerroampHandler.java FerroampMqttCommunication.java README.md Signed-off-by: Örjan Backsell --- .../org.openhab.binding.ferroamp/README.md | 27 ++++++------ .../ferroamp/internal/FerroampHandler.java | 42 +++++++++---------- .../internal/FerroampMqttCommunication.java | 8 ++-- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/bundles/org.openhab.binding.ferroamp/README.md b/bundles/org.openhab.binding.ferroamp/README.md index 6359f332512d5..ba51464bb0db1 100644 --- a/bundles/org.openhab.binding.ferroamp/README.md +++ b/bundles/org.openhab.binding.ferroamp/README.md @@ -4,6 +4,8 @@ The Ferroamp binding is used to get live data from Ferroamp EnergyHub The Ferroamp binding is compatible with EnergyHub Wall and EnergyHub XL, and connects to your local EnergyHub via LAN. Data and commands are received/sent using MQTT where the user connects to the MQTT broker residing on the EnergyHub. +The communication with the broker might take some minute to establish, so Please just be patient. The Thing will be +in state INITIALIZATION during this time and ONLINE once connection is established. *note* Contact Ferroamp support to enable MQTT in the EnergyHub and to get the Username and Password: @@ -209,11 +211,7 @@ Thing ferroamp:energyhub:myenergyhub [ hostName="energyhub-ip", userName="myUser ``` ```java -Thing ferroamp:energyhub:myenergyhub [ hostName="energyhub-ip", userName="myUserName", password="myPassword", hasBattery=false, ssoS0=true ] -``` - -```java -Thing ferroamp:energyhub:myenergyhub [ hostName="energyhub-ip", userName="myUserName", password="myPassword", hasBattery=true, ssoS0=true, eso=true ] +Thing ferroamp:energyhub:myenergyhub [ hostName="energyhub-ip", userName="myUserName", password="myPassword", hasBattery=true, eso=true ] ``` @@ -226,20 +224,21 @@ String Ferroamp "RequestCharge" { channel="ferroamp:energyhub:myenergyh ## Rules -Ex. Set Charging with 5000W with cron trigger: +Ex. Rule name: Set Charge Level. +Set charging level to 5000W when item RequestCharge is updated. ```yaml +configuration: {} triggers: - id: "1" + - id: "1" configuration: - cronExpression: 0 0/2 * * * ? * - type: timer.GenericCronTrigger + itemName: EnergyHub_RequestCharge + type: core.ItemStateUpdateTrigger conditions: [] actions: - inputs: {} - id: "2" + - id: "2" configuration: - type: application/vnd.openhab.dsl.rule - script: ChargingWith5000W.sendCommand("5000") - type: script.ScriptAction + itemName: EnergyHub_RequestCharge + command: "5000" + type: core.ItemCommandAction ``` diff --git a/bundles/org.openhab.binding.ferroamp/src/main/java/org/openhab/binding/ferroamp/internal/FerroampHandler.java b/bundles/org.openhab.binding.ferroamp/src/main/java/org/openhab/binding/ferroamp/internal/FerroampHandler.java index 8be70518dcb3f..9528f38eca096 100644 --- a/bundles/org.openhab.binding.ferroamp/src/main/java/org/openhab/binding/ferroamp/internal/FerroampHandler.java +++ b/bundles/org.openhab.binding.ferroamp/src/main/java/org/openhab/binding/ferroamp/internal/FerroampHandler.java @@ -68,16 +68,16 @@ public void handleCommand(ChannelUID channelUID, Command command) { if (FerroampBindingConstants.CHANNEL_REQUESTCHARGE.equals(channelUID.getId())) { String requestCmdJsonCharge = "{\"" + "transId" + "\":\"" + transId + "\",\"cmd\":{\"name\":\"charge\",\"arg\":\"" + valueConfiguration + "\"}}"; - FerroampMqttCommunication.sendMQTT(requestCmdJsonCharge, ferroampConfig); + FerroampMqttCommunication.sendPublishedTopic(requestCmdJsonCharge, ferroampConfig); } if (FerroampBindingConstants.CHANNEL_REQUESTDISCHARGE.equals(channelUID.getId())) { String requestCmdJsonDisCharge = "{\"" + "transId" + "\":\"" + transId + "\",\"cmd\":{\"name\":\"discharge\",\"arg\":\"" + valueConfiguration + "\"}}"; - FerroampMqttCommunication.sendMQTT(requestCmdJsonDisCharge, ferroampConfig); + FerroampMqttCommunication.sendPublishedTopic(requestCmdJsonDisCharge, ferroampConfig); } if (FerroampBindingConstants.CHANNEL_AUTO.equals(channelUID.getId())) { String requestCmdJsonAuto = "{\"" + "transId" + "\":\"" + transId + "\",\"cmd\":{\"name\":\"auto\"}}"; - FerroampMqttCommunication.sendMQTT(requestCmdJsonAuto, ferroampConfig); + FerroampMqttCommunication.sendPublishedTopic(requestCmdJsonAuto, ferroampConfig); } } @@ -95,26 +95,26 @@ public void initialize() { final MqttBrokerConnection ferroampConnection = new MqttBrokerConnection(ferroampConfig.hostName, FerroampBindingConstants.BROKER_PORT, false, false, ferroampConfig.userName); - scheduler.execute(() -> { - try { - startMqttConnection(); - } catch (InterruptedException e) { - logger.debug("Faulty startMqttConnection()"); - } - }); - scheduler.scheduleWithFixedDelay(this::pollTask, 60, refreshInterval, TimeUnit.SECONDS); this.setFerroampConnection(ferroampConnection); } private void pollTask() { + try { + startMqttConnection(); + } catch (InterruptedException e) { + logger.debug("Problems with startMqttConnection()"); + } if (getFerroampConnection().connectionState().toString().equals("DISCONNECTED")) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR); logger.debug("Problem connection to MqttBroker"); - } else { + // } else { + } + if (getFerroampConnection().connectionState().toString().equals("CONNECTED")) { try { channelUpdate(); updateStatus(ThingStatus.ONLINE); + } catch (RuntimeException scheduleWithFixedDelayException) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, scheduleWithFixedDelayException.getClass().getName() + ":" @@ -124,15 +124,15 @@ private void pollTask() { } private void startMqttConnection() throws InterruptedException { - try { - TimeUnit.SECONDS.sleep(10); - } catch (InterruptedException e) { - logger.debug("Connection to MqttBroker disturbed during startup of MqttConnection"); - } - ferroampMqttCommunication.getMQTT("ehubTopic", ferroampConfig); - ferroampMqttCommunication.getMQTT("ssoTopic", ferroampConfig); - ferroampMqttCommunication.getMQTT("esoTopic", ferroampConfig); - ferroampMqttCommunication.getMQTT("esmTopic", ferroampConfig); + MqttBrokerConnection localSubscribeConnection = FerroampHandler.getFerroampConnection(); + + localSubscribeConnection.start(); + localSubscribeConnection.setCredentials(ferroampConfig.userName, ferroampConfig.password); + + ferroampMqttCommunication.getSubscribedTopic("ehubTopic", ferroampConfig); + ferroampMqttCommunication.getSubscribedTopic("ssoTopic", ferroampConfig); + ferroampMqttCommunication.getSubscribedTopic("esoTopic", ferroampConfig); + ferroampMqttCommunication.getSubscribedTopic("esmTopic", ferroampConfig); } private void channelUpdate() { diff --git a/bundles/org.openhab.binding.ferroamp/src/main/java/org/openhab/binding/ferroamp/internal/FerroampMqttCommunication.java b/bundles/org.openhab.binding.ferroamp/src/main/java/org/openhab/binding/ferroamp/internal/FerroampMqttCommunication.java index 5c6acc0fb9316..c99880494f325 100644 --- a/bundles/org.openhab.binding.ferroamp/src/main/java/org/openhab/binding/ferroamp/internal/FerroampMqttCommunication.java +++ b/bundles/org.openhab.binding.ferroamp/src/main/java/org/openhab/binding/ferroamp/internal/FerroampMqttCommunication.java @@ -60,7 +60,7 @@ public FerroampMqttCommunication(Thing thing) { } // Handles request topic - static void sendMQTT(String payload, FerroampConfiguration ferroampConfig) { + static void sendPublishedTopic(String payload, FerroampConfiguration ferroampConfig) { MqttBrokerConnection localConfigurationConnection = FerroampHandler.getFerroampConnection(); @@ -70,12 +70,12 @@ static void sendMQTT(String payload, FerroampConfiguration ferroampConfig) { } // Handles respective topic type - void getMQTT(String topic, FerroampConfiguration ferroampConfig) { + void getSubscribedTopic(String topic, FerroampConfiguration ferroampConfig) { MqttBrokerConnection localSubscribeConnection = FerroampHandler.getFerroampConnection(); - localSubscribeConnection.start(); - localSubscribeConnection.setCredentials(ferroampConfig.userName, ferroampConfig.password); + // localSubscribeConnection.start(); + // localSubscribeConnection.setCredentials(ferroampConfig.userName, ferroampConfig.password); if ("ehubTopic".equals(topic)) { localSubscribeConnection.subscribe(FerroampBindingConstants.EHUB_TOPIC, this);