Skip to content

Commit

Permalink
Fix wmbus and mbus serial bridge initialization.
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Dywicki <luke@code-house.org>
  • Loading branch information
splatch committed May 12, 2024
1 parent 659084b commit dc34263
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
|===
|Bridge | Label ^|Description

| co7io-mbus:openhab-serial
| co7io-mbus:serial
| Serial Bridge
| The M-Bus Serial bridge allows to poll data using compatible serial interfaces.

<<co7io-mbus:openhab-serial>>
<<co7io-mbus:serial>>

|===


[[co7io-mbus:openhab-serial]]
== Bridge `co7io-mbus:openhab-serial`
[[co7io-mbus:serial]]
== Bridge `co7io-mbus:serial`

Below table contains complete configuration of `co7io-mbus:openhab-serial`.
Below table contains complete configuration of `co7io-mbus:serial`.



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,52 @@

|===

[[thing-type:co7io-mbus:openhab-serial]]
== Config `thing-type:co7io-mbus:openhab-serial`
[width="100%",caption="thing-type:co7io-mbus:openhab-serial configuration",cols="1,1,1,2"]
[[thing-type:co7io-mbus:device]]
== Config `thing-type:co7io-mbus:device`
[width="100%",caption="thing-type:co7io-mbus:device configuration",cols="1,1,1,2"]
|===
|Name | Type | Label ^|Description

| address
| INTEGER
| Primary address
|

| discoverChannels
| BOOLEAN
| Discover channels
| Enable automatic creation of channels based on polled data.

| serialNumber
| INTEGER
| Serial number
|

| manufacturerId
| TEXT
| Manufacturer identifier
| Three character identifier of manufacturer, for example 'KAM' for Kamstrup.

| version
| INTEGER
| Version
|

| deviceType
| TEXT
| Device type
| Type of device according to standard.

| refreshInterval
| INTEGER
| Refresh interval
| Time between poll cycles.

|===

[[thing-type:co7io-mbus:serial]]
== Config `thing-type:co7io-mbus:serial`
[width="100%",caption="thing-type:co7io-mbus:serial configuration",cols="1,1,1,2"]
|===
|Name | Type | Label ^|Description

Expand Down Expand Up @@ -78,47 +121,4 @@

|===

[[thing-type:co7io-mbus:device]]
== Config `thing-type:co7io-mbus:device`
[width="100%",caption="thing-type:co7io-mbus:device configuration",cols="1,1,1,2"]
|===
|Name | Type | Label ^|Description

| address
| INTEGER
| Primary address
|

| discoverChannels
| BOOLEAN
| Discover channels
| Enable automatic creation of channels based on polled data.

| serialNumber
| INTEGER
| Serial number
|

| manufacturerId
| TEXT
| Manufacturer identifier
| Three character identifier of manufacturer, for example 'KAM' for Kamstrup.

| version
| INTEGER
| Version
|

| deviceType
| TEXT
| Device type
| Type of device according to standard.

| refreshInterval
| INTEGER
| Refresh interval
| Time between poll cycles.

|===


Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
*/
package org.connectorio.addons.binding.mbus.internal.handler;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import org.connectorio.addons.binding.handler.GenericBridgeHandlerBase;
import org.connectorio.addons.binding.mbus.config.BridgeConfig;
import org.connectorio.addons.binding.mbus.internal.discovery.DiscoveryCoordinator;
import org.connectorio.addons.binding.mbus.internal.discovery.MBusDiscoveryService;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.types.Command;
import org.openmuc.jmbus.MBusConnection;
import org.slf4j.Logger;
Expand Down Expand Up @@ -57,6 +61,17 @@ public void initialize() {
});
}

@Override
public void dispose() {
if (connection.isDone()) {
try {
connection.get().close();
} catch (Exception e) {
logger.error("Failed to close serial connection", e);
}
}
}

protected abstract CompletableFuture<MBusConnection> initializeConnection();

@Override
Expand All @@ -73,4 +88,11 @@ public CompletableFuture<DiscoveryCoordinator> getDiscoveryCoordinator() {
return connection.thenApply(connection -> this.discoveryCoordinator);
}

@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return getBridgeConfig().filter(cfg -> cfg.discoveryMethod != null)
.map(cfg -> Collections.<Class<? extends ThingHandlerService>>singleton(MBusDiscoveryService.class))
.orElse(Collections.emptySet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">

<bridge-type id="openhab-serial">
<bridge-type id="serial">
<label>Serial Bridge</label>
<description>The M-Bus Serial bridge allows to poll data using compatible serial interfaces.</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,9 @@ public void dispose() {

@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return getBridgeConfig().map(cfg -> {
if (cfg.discoverDevices) {
return Collections.<Class<? extends ThingHandlerService>>singleton(WMBusDiscoveryService.class);
}
return Collections.<Class<? extends ThingHandlerService>>emptyList();
}).orElse(Collections.emptyList());
return getBridgeConfig().filter(cfg -> cfg.discoverDevices)
.map(cfg -> Collections.<Class<? extends ThingHandlerService>>singleton(WMBusDiscoveryService.class))
.orElse(Collections.emptySet());
}

@Override
Expand All @@ -110,20 +107,5 @@ public CompletableFuture<KeyStore> getKeyStore() {
public CompletableFuture<DiscoveryCoordinator> getDiscoveryCoordinator() {
return connection.thenApply(connection -> this.discoveryCoordinator);
}
@Override
public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) {
if (childHandler instanceof WMBusDeviceThingHandler) {
WMBusDeviceThingHandler handler = (WMBusDeviceThingHandler) childHandler;
dispatcher.thenAccept(dsp -> dsp.detach(handler));
}
}

@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
if (childHandler instanceof WMBusDeviceThingHandler) {
WMBusDeviceThingHandler handler = (WMBusDeviceThingHandler) childHandler;
dispatcher.thenAccept(dsp -> dsp.attach(handler));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public void initialize() {
getBridgeHandler().map(WMBusBridgeHandler::getKeyStore).orElse(CompletableFuture.failedFuture(new IllegalArgumentException()))
.thenAccept(keyStore -> keyStore.addKey(address, HexUtils.hexToBytes(encryptionKey)));
}
getBridgeHandler().map(WMBusBridgeHandler::getDispatcher).orElse(CompletableFuture.failedFuture(new IllegalArgumentException()))
.thenAccept(dispatcher -> dispatcher.attach(this));

for (Channel channel : getThing().getChannels()) {
if (WMBusBindingConstants.CHANNEL_TYPE_RSSI.equals(channel.getChannelTypeUID())) {
Expand All @@ -128,6 +130,9 @@ public void dispose() {
getBridgeHandler().map(WMBusBridgeHandler::getKeyStore).orElse(CompletableFuture.failedFuture(new IllegalArgumentException()))
.thenAccept(keyStore -> keyStore.removeKey(address));

getBridgeHandler().map(WMBusBridgeHandler::getDispatcher).orElse(CompletableFuture.failedFuture(new IllegalArgumentException()))
.thenAccept(dispatcher -> dispatcher.detach(this));

super.dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.function.Consumer;
import org.connectorio.addons.binding.wmbus.dispatch.WMBusMessageDispatcher;
import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openmuc.jmbus.HexUtils;
import org.openmuc.jmbus.wireless.WMBusListener;
import org.openmuc.jmbus.wireless.WMBusMessage;
import org.slf4j.Logger;
Expand All @@ -39,7 +40,7 @@ public WMBusMessageListenerAdapter(WMBusMessageDispatcher dispatcher, Consumer<S

@Override
public void newMessage(WMBusMessage message) {
logger.debug("Received message {}", message);
logger.debug("Received message from {}: {}", message.getSecondaryAddress(), HexUtils.bytesToHex(message.asBlob()));
dispatcher.dispatch(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
<Export-Package>
org.openmuc.jrxtx.*
</Export-Package>
<Import-Package>
gnu.io.*;version=!,
*
</Import-Package>
<Private-Package>
!gnu.io.factory,
!gnu.io.rfc2217,
Expand Down

0 comments on commit dc34263

Please sign in to comment.