[lcn] Fix deprecation warnings, refactor, add test cases (#15545)
Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
This commit is contained in:
parent
5e8e097ad4
commit
a0dc5c05f2
|
@ -14,6 +14,8 @@ package org.openhab.binding.lcn.internal.pchkdiscovery;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used for deserializing the XML response of the LCN-PCHK discovery protocol.
|
* Used for deserializing the XML response of the LCN-PCHK discovery protocol.
|
||||||
*
|
*
|
||||||
|
@ -21,13 +23,14 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class ExtServices {
|
public class ExtServices {
|
||||||
private final ExtService ExtService;
|
@XStreamAlias("ExtService")
|
||||||
|
private final ExtService extService;
|
||||||
|
|
||||||
public ExtServices(ExtService extService) {
|
public ExtServices(ExtService extService) {
|
||||||
ExtService = extService;
|
this.extService = extService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExtService getExtService() {
|
public ExtService getExtService() {
|
||||||
return ExtService;
|
return extService;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ package org.openhab.binding.lcn.internal.pchkdiscovery;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.MulticastSocket;
|
import java.net.MulticastSocket;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
@ -69,14 +70,14 @@ public class LcnPchkDiscoveryService extends AbstractDiscoveryService {
|
||||||
super(SUPPORTED_THING_TYPES_UIDS, 0, false);
|
super(SUPPORTED_THING_TYPES_UIDS, 0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<InetAddress> getLocalAddresses() {
|
private List<NetworkInterface> getLocalNetworkInterfaces() {
|
||||||
List<InetAddress> result = new LinkedList<>();
|
List<NetworkInterface> result = new LinkedList<>();
|
||||||
try {
|
try {
|
||||||
for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
|
for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
|
||||||
try {
|
try {
|
||||||
if (networkInterface.isUp() && !networkInterface.isLoopback()
|
if (networkInterface.isUp() && !networkInterface.isLoopback()
|
||||||
&& !networkInterface.isPointToPoint()) {
|
&& !networkInterface.isPointToPoint()) {
|
||||||
result.addAll(Collections.list(networkInterface.getInetAddresses()));
|
result.add(networkInterface);
|
||||||
}
|
}
|
||||||
} catch (SocketException exception) {
|
} catch (SocketException exception) {
|
||||||
// ignore
|
// ignore
|
||||||
|
@ -93,13 +94,13 @@ public class LcnPchkDiscoveryService extends AbstractDiscoveryService {
|
||||||
try {
|
try {
|
||||||
InetAddress multicastAddress = InetAddress.getByName(PCHK_DISCOVERY_MULTICAST_ADDRESS);
|
InetAddress multicastAddress = InetAddress.getByName(PCHK_DISCOVERY_MULTICAST_ADDRESS);
|
||||||
|
|
||||||
getLocalAddresses().forEach(localInterfaceAddress -> {
|
getLocalNetworkInterfaces().forEach(localNetworkInterface -> {
|
||||||
logger.debug("Searching on {} ...", localInterfaceAddress.getHostAddress());
|
logger.debug("Searching on {} ...", localNetworkInterface);
|
||||||
try (MulticastSocket socket = new MulticastSocket(PCHK_DISCOVERY_PORT)) {
|
try (MulticastSocket socket = new MulticastSocket(PCHK_DISCOVERY_PORT)) {
|
||||||
socket.setInterface(localInterfaceAddress);
|
|
||||||
socket.setReuseAddress(true);
|
socket.setReuseAddress(true);
|
||||||
socket.setSoTimeout(INTERFACE_TIMEOUT_SEC * 1000);
|
socket.setSoTimeout(INTERFACE_TIMEOUT_SEC * 1000);
|
||||||
socket.joinGroup(multicastAddress);
|
socket.joinGroup(new InetSocketAddress(multicastAddress, PCHK_DISCOVERY_PORT),
|
||||||
|
localNetworkInterface);
|
||||||
|
|
||||||
byte[] requestData = DISCOVER_REQUEST.getBytes(LcnDefs.LCN_ENCODING);
|
byte[] requestData = DISCOVER_REQUEST.getBytes(LcnDefs.LCN_ENCODING);
|
||||||
DatagramPacket request = new DatagramPacket(requestData, requestData.length, multicastAddress,
|
DatagramPacket request = new DatagramPacket(requestData, requestData.length, multicastAddress,
|
||||||
|
@ -136,7 +137,7 @@ public class LcnPchkDiscoveryService extends AbstractDiscoveryService {
|
||||||
thingDiscovered(discoveryResult.build());
|
thingDiscovered(discoveryResult.build());
|
||||||
} while (true); // left by SocketTimeoutException
|
} while (true); // left by SocketTimeoutException
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.debug("Discovery failed for {}: {}", localInterfaceAddress, e.getMessage());
|
logger.debug("Discovery failed for {}: {}", localNetworkInterface, e.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
|
|
|
@ -14,6 +14,8 @@ package org.openhab.binding.lcn.internal.pchkdiscovery;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used for deserializing the XML response of the LCN-PCHK discovery protocol.
|
* Used for deserializing the XML response of the LCN-PCHK discovery protocol.
|
||||||
*
|
*
|
||||||
|
@ -21,27 +23,30 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class ServicesResponse {
|
public class ServicesResponse {
|
||||||
private final Version Version;
|
@XStreamAlias("Version")
|
||||||
private final Server Server;
|
private final Version version;
|
||||||
private final ExtServices ExtServices;
|
@XStreamAlias("Server")
|
||||||
@SuppressWarnings("unused")
|
private final Server server;
|
||||||
private final Object Services = new Object();
|
@XStreamAlias("ExtServices")
|
||||||
|
private final ExtServices extServices;
|
||||||
|
@XStreamAlias("Services")
|
||||||
|
private final Object services = new Object();
|
||||||
|
|
||||||
public ServicesResponse(Version version, Server server, ExtServices extServices) {
|
public ServicesResponse(Version version, Server server, ExtServices extServices) {
|
||||||
this.Version = version;
|
this.version = version;
|
||||||
this.Server = server;
|
this.server = server;
|
||||||
this.ExtServices = extServices;
|
this.extServices = extServices;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Server getServer() {
|
public Server getServer() {
|
||||||
return Server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Version getVersion() {
|
public Version getVersion() {
|
||||||
return Version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExtServices getExtServices() {
|
public ExtServices getExtServices() {
|
||||||
return ExtServices;
|
return extServices;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,14 +53,14 @@ public class LcnModuleCodeSubHandler extends AbstractLcnModuleSubHandler {
|
||||||
public void handleStatusMessage(Matcher matcher) {
|
public void handleStatusMessage(Matcher matcher) {
|
||||||
String code;
|
String code;
|
||||||
|
|
||||||
|
int base = 10;
|
||||||
if (matcher.pattern() == FINGERPRINT_PATTERN_HEX) {
|
if (matcher.pattern() == FINGERPRINT_PATTERN_HEX) {
|
||||||
code = String.format("%02X%02X%02X", Integer.parseInt(matcher.group("byte0"), 16),
|
base = 16;
|
||||||
Integer.parseInt(matcher.group("byte1"), 16), Integer.parseInt(matcher.group("byte2"), 16));
|
|
||||||
} else {
|
|
||||||
code = String.format("%02X%02X%02X", Integer.parseInt(matcher.group("byte0")),
|
|
||||||
Integer.parseInt(matcher.group("byte1")), Integer.parseInt(matcher.group("byte2")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
code = String.format("%02X%02X%02X", Integer.parseInt(matcher.group("byte0"), base),
|
||||||
|
Integer.parseInt(matcher.group("byte1"), base), Integer.parseInt(matcher.group("byte2"), base));
|
||||||
|
|
||||||
if (matcher.pattern() == TRANSPONDER_PATTERN) {
|
if (matcher.pattern() == TRANSPONDER_PATTERN) {
|
||||||
handler.triggerChannel(LcnChannelGroup.CODE, "transponder", code);
|
handler.triggerChannel(LcnChannelGroup.CODE, "transponder", code);
|
||||||
} else if (matcher.pattern() == FINGERPRINT_PATTERN_HEX || matcher.pattern() == FINGERPRINT_PATTERN_DEC) {
|
} else if (matcher.pattern() == FINGERPRINT_PATTERN_HEX || matcher.pattern() == FINGERPRINT_PATTERN_DEC) {
|
||||||
|
|
|
@ -31,6 +31,7 @@ import org.openhab.core.library.types.OnOffType;
|
||||||
import org.openhab.core.library.types.PercentType;
|
import org.openhab.core.library.types.PercentType;
|
||||||
import org.openhab.core.library.types.StringType;
|
import org.openhab.core.library.types.StringType;
|
||||||
import org.openhab.core.library.types.UpDownType;
|
import org.openhab.core.library.types.UpDownType;
|
||||||
|
import org.openhab.core.util.ColorUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -108,14 +109,15 @@ public class LcnModuleOutputSubHandler extends AbstractLcnModuleSubHandler {
|
||||||
currentColor = hsbType;
|
currentColor = hsbType;
|
||||||
handler.updateChannel(LcnChannelGroup.OUTPUT, OUTPUT_COLOR, currentColor);
|
handler.updateChannel(LcnChannelGroup.OUTPUT, OUTPUT_COLOR, currentColor);
|
||||||
|
|
||||||
|
PercentType[] rgb = ColorUtil.hsbToRgbPercent(currentColor);
|
||||||
|
|
||||||
if (info.getFirmwareVersion().map(v -> v >= LcnBindingConstants.FIRMWARE_2014).orElse(true)) {
|
if (info.getFirmwareVersion().map(v -> v >= LcnBindingConstants.FIRMWARE_2014).orElse(true)) {
|
||||||
handler.sendPck(PckGenerator.dimAllOutputs(currentColor.getRed().doubleValue(),
|
handler.sendPck(PckGenerator.dimAllOutputs(rgb[0].doubleValue(), rgb[1].doubleValue(), rgb[2].doubleValue(),
|
||||||
currentColor.getGreen().doubleValue(), currentColor.getBlue().doubleValue(), output4.doubleValue(),
|
output4.doubleValue(), COLOR_RAMP_MS));
|
||||||
COLOR_RAMP_MS));
|
|
||||||
} else {
|
} else {
|
||||||
handler.sendPck(PckGenerator.dimOutput(0, currentColor.getRed().doubleValue(), COLOR_RAMP_MS));
|
handler.sendPck(PckGenerator.dimOutput(0, rgb[0].doubleValue(), COLOR_RAMP_MS));
|
||||||
handler.sendPck(PckGenerator.dimOutput(1, currentColor.getGreen().doubleValue(), COLOR_RAMP_MS));
|
handler.sendPck(PckGenerator.dimOutput(1, rgb[1].doubleValue(), COLOR_RAMP_MS));
|
||||||
handler.sendPck(PckGenerator.dimOutput(2, currentColor.getBlue().doubleValue(), COLOR_RAMP_MS));
|
handler.sendPck(PckGenerator.dimOutput(2, rgb[2].doubleValue(), COLOR_RAMP_MS));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
*/
|
*/
|
||||||
package org.openhab.binding.lcn.internal.subhandler;
|
package org.openhab.binding.lcn.internal.subhandler;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
|
@ -42,8 +43,32 @@ public class LcnModuleCodeSubHandlerTest extends AbstractTestLcnModuleSubHandler
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecFingerprint() {
|
public void testDecFingerprint() {
|
||||||
tryParseAllHandlers("=M000005.ZF255255255");
|
tryParseAllHandlers("=M000005.ZF255001002");
|
||||||
verify(handler).triggerChannel(LcnChannelGroup.CODE, "fingerprint", "FFFFFF");
|
verify(handler).triggerChannel(LcnChannelGroup.CODE, "fingerprint", "FF0102");
|
||||||
verify(handler).triggerChannel(any(), any(), any());
|
verify(handler).triggerChannel(any(), any(), any());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTransponder() {
|
||||||
|
tryParseAllHandlers("=M000005.ZT255001002");
|
||||||
|
verify(handler).triggerChannel(LcnChannelGroup.CODE, "transponder", "FF0102");
|
||||||
|
verify(handler).triggerChannel(any(), any(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemote() {
|
||||||
|
tryParseAllHandlers("=M000005.ZI255001002013001");
|
||||||
|
verify(handler).triggerChannel(LcnChannelGroup.CODE, "remotecontrolkey", "B3:HIT");
|
||||||
|
verify(handler).triggerChannel(LcnChannelGroup.CODE, "remotecontrolcode", "FF0102:B3:HIT");
|
||||||
|
verify(handler, times(2)).triggerChannel(any(), any(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemoteBatteryLow() {
|
||||||
|
tryParseAllHandlers("=M000005.ZI255001002008012");
|
||||||
|
verify(handler).triggerChannel(LcnChannelGroup.CODE, "remotecontrolkey", "A8:MAKE");
|
||||||
|
verify(handler).triggerChannel(LcnChannelGroup.CODE, "remotecontrolcode", "FF0102:A8:MAKE");
|
||||||
|
verify(handler).triggerChannel(LcnChannelGroup.CODE, "remotecontrolbatterylow", "FF0102");
|
||||||
|
verify(handler, times(3)).triggerChannel(any(), any(), any());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue