Skip to content

Commit

Permalink
[ferroamp] Binding for ferroamp 20241111
Browse files Browse the repository at this point in the history
Changes done in:
FerroampHandler.java
FerroampMqttCommunication.java
README.md

Signed-off-by: Örjan Backsell <[email protected]>
  • Loading branch information
basse04 committed Nov 11, 2024
1 parent 2cd2491 commit 70688ed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
27 changes: 13 additions & 14 deletions bundles/org.openhab.binding.ferroamp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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 ]
```


Expand All @@ -226,20 +224,21 @@ String Ferroamp "RequestCharge" <energy> { 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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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() + ":"
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand Down

0 comments on commit 70688ed

Please sign in to comment.