From 71dfbcf12c800b5588395e31f7a83a620ae2b223 Mon Sep 17 00:00:00 2001 From: Robert D Date: Mon, 10 Apr 2023 10:35:56 +0200 Subject: [PATCH] [eKey] Add sourceIp in cases of NAT (Kubernetes deployments) (#14616) * add natIp Signed-off-by: querdenker2k --------- Signed-off-by: querdenker2k --- bundles/org.openhab.binding.ekey/README.md | 19 ++++++++++--------- .../ekey/internal/EkeyConfiguration.java | 3 +++ .../internal/api/EkeyUdpPacketReceiver.java | 3 ++- .../ekey/internal/handler/EkeyHandler.java | 6 ++++-- .../resources/OH-INF/i18n/ekey.properties | 2 ++ .../resources/OH-INF/thing/thing-types.xml | 5 +++++ 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/bundles/org.openhab.binding.ekey/README.md b/bundles/org.openhab.binding.ekey/README.md index f21f1516f..fb728c13e 100644 --- a/bundles/org.openhab.binding.ekey/README.md +++ b/bundles/org.openhab.binding.ekey/README.md @@ -6,20 +6,21 @@ This binding connects to [ekey](https://ekey.net/) converter UDP (CV-LAN) using This binding only supports one thing type: -| Thing | Thing Type | Description | -|-------------|------------|---------------------------------------------| +| Thing | Thing Type | Description | +|-------|------------|----------------------------------------| | cvlan | Thing | Represents a single ekey converter UDP | ## Thing Configuration The binding uses the following configuration parameters. -| Parameter | Description | -|-----------|----------------------------------------------------------------| -| ipAddress | IPv4 address of the eKey udp converter. A static IP address is recommended.| -| port | The port as configured during the UDP Converter configuration. e.g. 56000 (Binding default) | -| protocol | Can be RARE, MULTI or HOME depending on what the system supports. Binding defaults to RARE | -| delimiter | The delimiter is also defined on the ekey UDP converter - use the ekey configuration software to determine which delimiter is used or to change it. Binding default is `_` (underscore) | +| Parameter | Description | +|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| ipAddress | IPv4 address of the eKey udp converter. A static IP address is recommended. | +| port | The port as configured during the UDP Converter configuration. e.g. 56000 (Binding default) | +| protocol | Can be RARE, MULTI or HOME depending on what the system supports. Binding defaults to RARE | +| delimiter | The delimiter is also defined on the ekey UDP converter - use the ekey configuration software to determine which delimiter is used or to change it. Binding default is `_` (underscore) | +| natIp | [Optional] IPv4 address of a received eKey udp packet. Can be different from the ipAddress when using NAT. (e.g. in Kubernetes) | ## Channels @@ -28,7 +29,7 @@ The binding uses the following configuration parameters. | action | Number | R/M/H | This indicates whether access was granted (value=0) or denied (value=-1). | 0,-1 (136 and 137 with RARE protocol | | fingerId | Number | R/M/H | This indicates the finger that was used by a person. | 0-9,-1 | | fsName | String | M | This returns the 4-character-long name that was specified on the controller for the specific terminals. | | -| fsSerial | Number | R/M/H | This returns the serial number for the specific terminal. | +| fsSerial | Number | R/M/H | This returns the serial number for the specific terminal. | | | inputId | Number | M | This indicates which of the four digital inputs was triggered. Value is number of Input. "-1" indicates that no input was triggered. | 0-4,-1 | | keyId | Number | M | This indicates which of the four keys was used. See ekey documentation on "keys". | 0-4,-1 | | relayId | Number | R/H | This indicates which relay has been switched. | 0-3,-1 | diff --git a/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/EkeyConfiguration.java b/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/EkeyConfiguration.java index 48fae0e3b..823269c49 100644 --- a/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/EkeyConfiguration.java +++ b/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/EkeyConfiguration.java @@ -13,11 +13,13 @@ package org.openhab.binding.ekey.internal; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; /** * The {@link EkeyConfiguration} class contains fields mapping thing configuration parameters. * * @author Hans-Jörg Merk - Initial contribution + * @author Robert Delbrück - Add natIp */ @NonNullByDefault public class EkeyConfiguration { @@ -26,4 +28,5 @@ public class EkeyConfiguration { public int port; public String protocol = ""; public String delimiter = ""; + public @Nullable String natIp; } diff --git a/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/api/EkeyUdpPacketReceiver.java b/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/api/EkeyUdpPacketReceiver.java index 38cf98c55..6e1fe8862 100644 --- a/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/api/EkeyUdpPacketReceiver.java +++ b/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/api/EkeyUdpPacketReceiver.java @@ -119,7 +119,8 @@ public class EkeyUdpPacketReceiver { lastPacket = packet.getData(); readMessage(lastPacket); } else { - logger.warn("Packet received from unknown source- {}", packet.getData()); + logger.warn("Packet received from unknown source (ip={}) - {}", + packet.getAddress().getHostAddress(), packet.getData()); } } catch (UnknownHostException e) { logger.debug("Exception during address conversion - {}", e.getMessage()); diff --git a/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/handler/EkeyHandler.java b/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/handler/EkeyHandler.java index 42a1030b2..eb59497ea 100644 --- a/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/handler/EkeyHandler.java +++ b/bundles/org.openhab.binding.ekey/src/main/java/org/openhab/binding/ekey/internal/handler/EkeyHandler.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -49,6 +50,7 @@ import org.slf4j.LoggerFactory; * sent to one of the channels. * * @author Hans-Jörg Merk - Initial contribution + * @author Robert Delbrück - Add natIp */ @NonNullByDefault public class EkeyHandler extends BaseThingHandler implements EkeyPacketListener { @@ -82,8 +84,8 @@ public class EkeyHandler extends BaseThingHandler implements EkeyPacketListener populateChannels(config.protocol); String readerThreadName = "OH-binding-" + getThing().getUID().getAsString(); - EkeyUdpPacketReceiver localReceiver = receiver = new EkeyUdpPacketReceiver(config.ipAddress, - config.port, readerThreadName); + EkeyUdpPacketReceiver localReceiver = receiver = new EkeyUdpPacketReceiver( + Optional.ofNullable(config.natIp).orElse(config.ipAddress), config.port, readerThreadName); localReceiver.addEkeyPacketListener(this); try { localReceiver.openConnection(); diff --git a/bundles/org.openhab.binding.ekey/src/main/resources/OH-INF/i18n/ekey.properties b/bundles/org.openhab.binding.ekey/src/main/resources/OH-INF/i18n/ekey.properties index 6e042dfc3..0da0edd53 100644 --- a/bundles/org.openhab.binding.ekey/src/main/resources/OH-INF/i18n/ekey.properties +++ b/bundles/org.openhab.binding.ekey/src/main/resources/OH-INF/i18n/ekey.properties @@ -15,6 +15,8 @@ thing-type.config.ekey.protocol.label = Protocol thing-type.config.ekey.protocol.description = Can be RARE, MULTI or HOME depending on what the system supports thing-type.config.ekey.delimiter.label = Delimiter thing-type.config.ekey.delimiter.description = The delimiter is also defined on the ekey UDP converter - use the ekey configuration software to determine which delimiter is used or to change it. Another option is _ (underscore) +thing-type.config.ekey.natIp.label = NAT IP +thing-type.config.ekey.natIp.description = NAT rewritten source IPv4 address of a received eKey udp packet. Can be different from the ipAddress when using NAT. (e.g. in Kubernetes) # channel types channel-type.ekey.userId.label = User ID diff --git a/bundles/org.openhab.binding.ekey/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.ekey/src/main/resources/OH-INF/thing/thing-types.xml index 04a217258..e4d6a882c 100644 --- a/bundles/org.openhab.binding.ekey/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.ekey/src/main/resources/OH-INF/thing/thing-types.xml @@ -41,6 +41,11 @@ @text/thing-type.config.ekey.delimiter.description _ + + network-address + + @text/thing-type.config.ekey.natIp.description +