From 2bf22465308407f9ca19ff076464322a862a899f Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Mon, 2 Oct 2023 21:43:07 +0200 Subject: [PATCH] [plugwise] Cleanup code (#15688) The cleanup includes: * Use lambdas * Use enhanced switches * Make abstract class constructors protected * Fix typos Signed-off-by: Wouter Born --- .../internal/PlugwiseBindingConstants.java | 1 - .../PlugwiseCommunicationContext.java | 23 ++++---- .../plugwise/internal/PlugwiseDeviceTask.java | 23 ++++---- .../PlugwiseFilteredMessageListenerList.java | 2 +- .../internal/PlugwiseMessageProcessor.java | 5 +- .../internal/PlugwiseMessageSender.java | 2 +- .../PlugwiseThingDiscoveryService.java | 4 +- .../plugwise/internal/PlugwiseUtils.java | 4 +- .../handler/AbstractPlugwiseThingHandler.java | 2 +- .../AbstractSleepingEndDeviceHandler.java | 7 ++- .../handler/PlugwiseRelayDeviceHandler.java | 11 ++-- .../handler/PlugwiseSenseHandler.java | 4 +- .../handler/PlugwiseStickHandler.java | 5 +- .../listener/PlugwiseMessageListener.java | 2 +- .../protocol/AcknowledgementMessage.java | 4 +- .../protocol/AnnounceAwakeRequestMessage.java | 2 +- .../protocol/ClockSetRequestMessage.java | 4 +- .../protocol/InformationResponseMessage.java | 28 ++++------ .../plugwise/internal/protocol/Message.java | 23 +++----- .../internal/protocol/MessageFactory.java | 54 +++++++------------ .../internal/protocol/field/Energy.java | 2 +- 21 files changed, 84 insertions(+), 128 deletions(-) diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseBindingConstants.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseBindingConstants.java index 877321264..fd1821767 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseBindingConstants.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseBindingConstants.java @@ -44,7 +44,6 @@ public class PlugwiseBindingConstants { // List of all configuration properties public static final String CONFIG_PROPERTY_MAC_ADDRESS = "macAddress"; public static final String CONFIG_PROPERTY_RECALIBRATE = "recalibrate"; - public static final String CONFIG_PROPERTY_SERIAL_PORT = "serialPort"; public static final String CONFIG_PROPERTY_UPDATE_CONFIGURATION = "updateConfiguration"; public static final String CONFIG_PROPERTY_UPDATE_INTERVAL = "updateInterval"; diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseCommunicationContext.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseCommunicationContext.java index d1ffd42ca..1c0271d17 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseCommunicationContext.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseCommunicationContext.java @@ -60,18 +60,15 @@ public class PlugwiseCommunicationContext { public static final int MAX_BUFFER_SIZE = 1024; - private static final Comparator QUEUED_MESSAGE_COMPERATOR = new Comparator<@Nullable PlugwiseQueuedMessage>() { - @Override - public int compare(@Nullable PlugwiseQueuedMessage o1, @Nullable PlugwiseQueuedMessage o2) { - if (o1 == null || o2 == null) { - return -1; - } - int result = o1.getPriority().compareTo(o2.getPriority()); - if (result == 0) { - result = o1.getDateTime().compareTo(o2.getDateTime()); - } - return result; + private static final Comparator QUEUED_MESSAGE_COMPARATOR = (o1, o2) -> { + if (o1 == null || o2 == null) { + return -1; } + int result = o1.getPriority().compareTo(o2.getPriority()); + if (result == 0) { + result = o1.getDateTime().compareTo(o2.getDateTime()); + } + return result; }; private final Logger logger = LoggerFactory.getLogger(PlugwiseCommunicationContext.class); @@ -79,7 +76,7 @@ public class PlugwiseCommunicationContext { MAX_BUFFER_SIZE, true); private final BlockingQueue<@Nullable Message> receivedQueue = new ArrayBlockingQueue<>(MAX_BUFFER_SIZE, true); private final PriorityBlockingQueue<@Nullable PlugwiseQueuedMessage> sendQueue = new PriorityBlockingQueue<>( - MAX_BUFFER_SIZE, QUEUED_MESSAGE_COMPERATOR); + MAX_BUFFER_SIZE, QUEUED_MESSAGE_COMPARATOR); private final BlockingQueue<@Nullable PlugwiseQueuedMessage> sentQueue = new ArrayBlockingQueue<>(MAX_BUFFER_SIZE, true); private final ReentrantLock sentQueueLock = new ReentrantLock(); @@ -141,7 +138,7 @@ public class PlugwiseCommunicationContext { } // Build exception message when port not found - String availablePorts = serialPortManager.getIdentifiers().map(id -> id.getName()) + String availablePorts = serialPortManager.getIdentifiers().map(SerialPortIdentifier::getName) .collect(Collectors.joining(System.lineSeparator())); throw new PlugwiseInitializationException( diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseDeviceTask.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseDeviceTask.java index f58016b1a..c103800a1 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseDeviceTask.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseDeviceTask.java @@ -45,22 +45,19 @@ public abstract class PlugwiseDeviceTask { private @Nullable ScheduledFuture future; - private Runnable scheduledRunnable = new Runnable() { - @Override - public void run() { - try { - lock.lock(); - logger.debug("Running '{}' Plugwise task for {} ({})", name, deviceType, macAddress); - runTask(); - } catch (Exception e) { - logger.warn("Error while running '{}' Plugwise task for {} ({})", name, deviceType, macAddress, e); - } finally { - lock.unlock(); - } + private final Runnable scheduledRunnable = () -> { + try { + lock.lock(); + logger.debug("Running '{}' Plugwise task for {} ({})", getName(), deviceType, macAddress); + runTask(); + } catch (Exception e) { + logger.warn("Error while running '{}' Plugwise task for {} ({})", getName(), deviceType, macAddress, e); + } finally { + lock.unlock(); } }; - public PlugwiseDeviceTask(String name, ScheduledExecutorService scheduler) { + protected PlugwiseDeviceTask(String name, ScheduledExecutorService scheduler) { this.name = name; this.scheduler = scheduler; } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseFilteredMessageListenerList.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseFilteredMessageListenerList.java index b31507752..69990d13f 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseFilteredMessageListenerList.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseFilteredMessageListenerList.java @@ -61,7 +61,7 @@ public class PlugwiseFilteredMessageListenerList { for (PlugwiseFilteredMessageListener filteredListener : filteredListeners) { if (filteredListener.matches(message)) { try { - filteredListener.getListener().handleReponseMessage(message); + filteredListener.getListener().handleResponseMessage(message); } catch (Exception e) { logger.warn("Listener failed to handle message: {}", message, e); } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageProcessor.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageProcessor.java index 93362e040..27fb280dc 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageProcessor.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageProcessor.java @@ -17,7 +17,6 @@ import static org.openhab.binding.plugwise.internal.PlugwiseCommunicationContext import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; -import java.util.Iterator; import java.util.TooManyListenersException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -159,9 +158,7 @@ public class PlugwiseMessageProcessor implements SerialPortEventListener { try { context.getSentQueueLock().lock(); - Iterator<@Nullable PlugwiseQueuedMessage> messageIterator = context.getSentQueue().iterator(); - while (messageIterator.hasNext()) { - PlugwiseQueuedMessage queuedSentMessage = messageIterator.next(); + for (PlugwiseQueuedMessage queuedSentMessage : context.getSentQueue()) { if (queuedSentMessage != null && queuedSentMessage.getMessage().getSequenceNumber() == message.getSequenceNumber()) { logger.debug("Removing from sentQueue: {}", queuedSentMessage.getMessage()); diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageSender.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageSender.java index e2651760c..8cefcb275 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageSender.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageSender.java @@ -39,7 +39,7 @@ public class PlugwiseMessageSender { private class MessageSenderThread extends Thread { - private int messageWaitTime; + private final int messageWaitTime; public MessageSenderThread(int messageWaitTime) { super("OH-binding-" + context.getBridgeUID() + "-message-sender"); diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseThingDiscoveryService.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseThingDiscoveryService.java index fddac119a..6b915adef 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseThingDiscoveryService.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseThingDiscoveryService.java @@ -133,7 +133,7 @@ public class PlugwiseThingDiscoveryService extends AbstractDiscoveryService ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, mac); thingDiscovered(DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID) - .withLabel("Plugwise " + node.deviceType.toString()) + .withLabel("Plugwise " + node.deviceType) .withProperty(PlugwiseBindingConstants.CONFIG_PROPERTY_MAC_ADDRESS, mac) .withProperties(new HashMap<>(node.properties)) .withRepresentationProperty(PlugwiseBindingConstants.PROPERTY_MAC_ADDRESS).build()); @@ -215,7 +215,7 @@ public class PlugwiseThingDiscoveryService extends AbstractDiscoveryService } @Override - public void handleReponseMessage(Message message) { + public void handleResponseMessage(Message message) { switch (message.getType()) { case ANNOUNCE_AWAKE_REQUEST: handleAnnounceAwakeRequest((AnnounceAwakeRequestMessage) message); diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseUtils.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseUtils.java index fb86148f9..ceb722888 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseUtils.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseUtils.java @@ -103,9 +103,9 @@ public final class PlugwiseUtils { final String delimiter = "_"; StringBuilder upperCamelBuilder = new StringBuilder(text.length()); for (String str : text.split(delimiter)) { - if (upperCamelBuilder.isEmpty() && str.length() > 0) { + if (upperCamelBuilder.isEmpty() && !str.isEmpty()) { upperCamelBuilder.append(str.substring(0, 1).toLowerCase()); - } else if (str.length() > 0) { + } else if (!str.isEmpty()) { upperCamelBuilder.append(str.substring(0, 1).toUpperCase()); } if (str.length() > 1) { diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/AbstractPlugwiseThingHandler.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/AbstractPlugwiseThingHandler.java index 95706a12f..c0db1ed6f 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/AbstractPlugwiseThingHandler.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/AbstractPlugwiseThingHandler.java @@ -88,7 +88,7 @@ public abstract class AbstractPlugwiseThingHandler extends BaseThingHandler impl private @Nullable LocalDateTime lastConfigurationUpdateSend; private int unansweredPings; - public AbstractPlugwiseThingHandler(Thing thing) { + protected AbstractPlugwiseThingHandler(Thing thing) { super(thing); } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/AbstractSleepingEndDeviceHandler.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/AbstractSleepingEndDeviceHandler.java index da92053ef..8026d7430 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/AbstractSleepingEndDeviceHandler.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/AbstractSleepingEndDeviceHandler.java @@ -42,7 +42,7 @@ public abstract class AbstractSleepingEndDeviceHandler extends AbstractPlugwiseT private final Logger logger = LoggerFactory.getLogger(AbstractSleepingEndDeviceHandler.class); - public AbstractSleepingEndDeviceHandler(Thing thing) { + protected AbstractSleepingEndDeviceHandler(Thing thing) { super(thing); } @@ -72,12 +72,11 @@ public abstract class AbstractSleepingEndDeviceHandler extends AbstractPlugwiseT } @Override - public void handleReponseMessage(Message message) { + public void handleResponseMessage(Message message) { updateLastSeen(); switch (message.getType()) { - case ACKNOWLEDGEMENT_V1: - case ACKNOWLEDGEMENT_V2: + case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2: handleAcknowledgement((AcknowledgementMessage) message); break; case ANNOUNCE_AWAKE_REQUEST: diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseRelayDeviceHandler.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseRelayDeviceHandler.java index 567b90600..e290c21ab 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseRelayDeviceHandler.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseRelayDeviceHandler.java @@ -205,9 +205,9 @@ public class PlugwiseRelayDeviceHandler extends AbstractPlugwiseThingHandler { if (deviceType == DeviceType.CIRCLE_PLUS) { // The Circle+ real-time clock needs to be updated first to prevent clock sync issues sendCommandMessage(new RealTimeClockSetRequestMessage(macAddress, LocalDateTime.now())); - scheduler.schedule(() -> { - sendCommandMessage(new ClockSetRequestMessage(macAddress, LocalDateTime.now())); - }, 5, TimeUnit.SECONDS); + scheduler.schedule( + () -> sendCommandMessage(new ClockSetRequestMessage(macAddress, LocalDateTime.now())), 5, + TimeUnit.SECONDS); } else { sendCommandMessage(new ClockSetRequestMessage(macAddress, LocalDateTime.now())); } @@ -467,12 +467,11 @@ public class PlugwiseRelayDeviceHandler extends AbstractPlugwiseThingHandler { } @Override - public void handleReponseMessage(Message message) { + public void handleResponseMessage(Message message) { updateLastSeen(); switch (message.getType()) { - case ACKNOWLEDGEMENT_V1: - case ACKNOWLEDGEMENT_V2: + case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2: handleAcknowledgement((AcknowledgementMessage) message); break; case CLOCK_GET_RESPONSE: diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseSenseHandler.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseSenseHandler.java index ffa7430bb..5160a41b6 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseSenseHandler.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseSenseHandler.java @@ -117,13 +117,13 @@ public class PlugwiseSenseHandler extends AbstractSleepingEndDeviceHandler { } @Override - public void handleReponseMessage(Message message) { + public void handleResponseMessage(Message message) { switch (message.getType()) { case SENSE_REPORT_REQUEST: handleSenseReportRequestMessage((SenseReportRequestMessage) message); break; default: - super.handleReponseMessage(message); + super.handleResponseMessage(message); break; } } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseStickHandler.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseStickHandler.java index b95700cfc..bb2720755 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseStickHandler.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/handler/PlugwiseStickHandler.java @@ -177,10 +177,9 @@ public class PlugwiseStickHandler extends BaseBridgeHandler implements PlugwiseM } @Override - public void handleReponseMessage(Message message) { + public void handleResponseMessage(Message message) { switch (message.getType()) { - case ACKNOWLEDGEMENT_V1: - case ACKNOWLEDGEMENT_V2: + case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2: handleAcknowledgement((AcknowledgementMessage) message); break; case DEVICE_INFORMATION_RESPONSE: diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/listener/PlugwiseMessageListener.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/listener/PlugwiseMessageListener.java index 740e12c3f..556e4a7e4 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/listener/PlugwiseMessageListener.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/listener/PlugwiseMessageListener.java @@ -23,5 +23,5 @@ import org.openhab.binding.plugwise.internal.protocol.Message; @NonNullByDefault public interface PlugwiseMessageListener { - void handleReponseMessage(Message message); + void handleResponseMessage(Message message); } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/AcknowledgementMessage.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/AcknowledgementMessage.java index 2ee5578b6..a297065aa 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/AcknowledgementMessage.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/AcknowledgementMessage.java @@ -67,9 +67,9 @@ public class AcknowledgementMessage extends Message { return TYPES_BY_VALUE.get(value); } - private int identifier; + private final int identifier; - private ExtensionCode(int value) { + ExtensionCode(int value) { identifier = value; } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/AnnounceAwakeRequestMessage.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/AnnounceAwakeRequestMessage.java index 7aa164d31..a0944c7c6 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/AnnounceAwakeRequestMessage.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/AnnounceAwakeRequestMessage.java @@ -45,7 +45,7 @@ public class AnnounceAwakeRequestMessage extends Message { WAKEUP_BUTTON(5); public static AwakeReason forValue(int value) { - return Arrays.stream(values()).filter(awakeReason -> awakeReason.id == value).findFirst().get(); + return Arrays.stream(values()).filter(awakeReason -> awakeReason.id == value).findFirst().orElse(null); } private final int id; diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/ClockSetRequestMessage.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/ClockSetRequestMessage.java index af9f964e3..d000f4c53 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/ClockSetRequestMessage.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/ClockSetRequestMessage.java @@ -44,13 +44,13 @@ public class ClockSetRequestMessage extends Message { String minutes = String.format("%04X", (utcDateTime.getDayOfMonth() - 1) * 24 * 60 + (utcDateTime.getHour() * 60) + utcDateTime.getMinute()); // If we set logaddress to FFFFFFFFF then previous buffered data will be kept by the Circle+ - String logaddress = "FFFFFFFF"; + String logAddress = "FFFFFFFF"; String hour = String.format("%02X", utcDateTime.getHour()); String minute = String.format("%02X", utcDateTime.getMinute()); String second = String.format("%02X", utcDateTime.getSecond()); // Monday = 0, ... , Sunday = 6 String dayOfWeek = String.format("%02X", utcDateTime.getDayOfWeek().getValue() - 1); - return year + month + minutes + logaddress + hour + minute + second + dayOfWeek; + return year + month + minutes + logAddress + hour + minute + second + dayOfWeek; } } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/InformationResponseMessage.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/InformationResponseMessage.java index 712a6cd34..63e706539 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/InformationResponseMessage.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/InformationResponseMessage.java @@ -84,24 +84,16 @@ public class InformationResponseMessage extends Message { } private DeviceType intToDeviceType(int i) { - switch (i) { - case 0: - return DeviceType.STICK; - case 1: - return DeviceType.CIRCLE_PLUS; - case 2: - return DeviceType.CIRCLE; - case 3: - return DeviceType.SWITCH; - case 5: - return DeviceType.SENSE; - case 6: - return DeviceType.SCAN; - case 9: - return DeviceType.STEALTH; - default: - return null; - } + return switch (i) { + case 0 -> DeviceType.STICK; + case 1 -> DeviceType.CIRCLE_PLUS; + case 2 -> DeviceType.CIRCLE; + case 3 -> DeviceType.SWITCH; + case 5 -> DeviceType.SENSE; + case 6 -> DeviceType.SCAN; + case 9 -> DeviceType.STEALTH; + default -> null; + }; } @Override diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/Message.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/Message.java index fdfa0359c..7954e8c22 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/Message.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/Message.java @@ -12,7 +12,7 @@ */ package org.openhab.binding.plugwise.internal.protocol; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.openhab.binding.plugwise.internal.protocol.field.MACAddress; import org.openhab.binding.plugwise.internal.protocol.field.MessageType; @@ -40,14 +40,7 @@ public abstract class Message { int crc = 0x0000; int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12) - byte[] bytes = new byte[0]; - try { - bytes = string.getBytes("ASCII"); - } catch (UnsupportedEncodingException e) { - return ""; - } - - for (byte b : bytes) { + for (byte b : string.getBytes(StandardCharsets.US_ASCII)) { for (int i = 0; i < 8; i++) { boolean bit = ((b >> (7 - i) & 1) == 1); boolean c15 = ((crc >> 15 & 1) == 1); @@ -69,11 +62,11 @@ public abstract class Message { protected String payload; - public Message(MessageType messageType) { + protected Message(MessageType messageType) { this(messageType, null, null, null); } - public Message(MessageType messageType, Integer sequenceNumber, MACAddress macAddress, String payload) { + protected Message(MessageType messageType, Integer sequenceNumber, MACAddress macAddress, String payload) { this.type = messageType; this.sequenceNumber = sequenceNumber; this.macAddress = macAddress; @@ -84,19 +77,19 @@ public abstract class Message { } } - public Message(MessageType messageType, Integer sequenceNumber, String payload) { + protected Message(MessageType messageType, Integer sequenceNumber, String payload) { this(messageType, sequenceNumber, null, payload); } - public Message(MessageType messageType, MACAddress macAddress) { + protected Message(MessageType messageType, MACAddress macAddress) { this(messageType, null, macAddress, null); } - public Message(MessageType messageType, MACAddress macAddress, String payload) { + protected Message(MessageType messageType, MACAddress macAddress, String payload) { this(messageType, null, macAddress, payload); } - public Message(MessageType messageType, String payload) { + protected Message(MessageType messageType, String payload) { this(messageType, null, null, payload); } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/MessageFactory.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/MessageFactory.java index 5d5d0f20c..0a9e0e94e 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/MessageFactory.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/MessageFactory.java @@ -25,40 +25,24 @@ public class MessageFactory { public Message createMessage(MessageType messageType, int sequenceNumber, String payload) throws IllegalArgumentException { - switch (messageType) { - case ACKNOWLEDGEMENT_V1: - case ACKNOWLEDGEMENT_V2: - return new AcknowledgementMessage(messageType, sequenceNumber, payload); - case ANNOUNCE_AWAKE_REQUEST: - return new AnnounceAwakeRequestMessage(sequenceNumber, payload); - case BROADCAST_GROUP_SWITCH_RESPONSE: - return new BroadcastGroupSwitchResponseMessage(sequenceNumber, payload); - case CLOCK_GET_RESPONSE: - return new ClockGetResponseMessage(sequenceNumber, payload); - case DEVICE_INFORMATION_RESPONSE: - return new InformationResponseMessage(sequenceNumber, payload); - case DEVICE_ROLE_CALL_RESPONSE: - return new RoleCallResponseMessage(sequenceNumber, payload); - case MODULE_JOINED_NETWORK_REQUEST: - return new ModuleJoinedNetworkRequestMessage(sequenceNumber, payload); - case NETWORK_STATUS_RESPONSE: - return new NetworkStatusResponseMessage(sequenceNumber, payload); - case NODE_AVAILABLE: - return new NodeAvailableMessage(sequenceNumber, payload); - case PING_RESPONSE: - return new PingResponseMessage(sequenceNumber, payload); - case POWER_BUFFER_RESPONSE: - return new PowerBufferResponseMessage(sequenceNumber, payload); - case POWER_CALIBRATION_RESPONSE: - return new PowerCalibrationResponseMessage(sequenceNumber, payload); - case POWER_INFORMATION_RESPONSE: - return new PowerInformationResponseMessage(sequenceNumber, payload); - case REAL_TIME_CLOCK_GET_RESPONSE: - return new RealTimeClockGetResponseMessage(sequenceNumber, payload); - case SENSE_REPORT_REQUEST: - return new SenseReportRequestMessage(sequenceNumber, payload); - default: - throw new IllegalArgumentException("Unsupported message type: " + messageType); - } + return switch (messageType) { + case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2 -> + new AcknowledgementMessage(messageType, sequenceNumber, payload); + case ANNOUNCE_AWAKE_REQUEST -> new AnnounceAwakeRequestMessage(sequenceNumber, payload); + case BROADCAST_GROUP_SWITCH_RESPONSE -> new BroadcastGroupSwitchResponseMessage(sequenceNumber, payload); + case CLOCK_GET_RESPONSE -> new ClockGetResponseMessage(sequenceNumber, payload); + case DEVICE_INFORMATION_RESPONSE -> new InformationResponseMessage(sequenceNumber, payload); + case DEVICE_ROLE_CALL_RESPONSE -> new RoleCallResponseMessage(sequenceNumber, payload); + case MODULE_JOINED_NETWORK_REQUEST -> new ModuleJoinedNetworkRequestMessage(sequenceNumber, payload); + case NETWORK_STATUS_RESPONSE -> new NetworkStatusResponseMessage(sequenceNumber, payload); + case NODE_AVAILABLE -> new NodeAvailableMessage(sequenceNumber, payload); + case PING_RESPONSE -> new PingResponseMessage(sequenceNumber, payload); + case POWER_BUFFER_RESPONSE -> new PowerBufferResponseMessage(sequenceNumber, payload); + case POWER_CALIBRATION_RESPONSE -> new PowerCalibrationResponseMessage(sequenceNumber, payload); + case POWER_INFORMATION_RESPONSE -> new PowerInformationResponseMessage(sequenceNumber, payload); + case REAL_TIME_CLOCK_GET_RESPONSE -> new RealTimeClockGetResponseMessage(sequenceNumber, payload); + case SENSE_REPORT_REQUEST -> new SenseReportRequestMessage(sequenceNumber, payload); + default -> throw new IllegalArgumentException("Unsupported message type: " + messageType); + }; } } diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/field/Energy.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/field/Energy.java index 51e559ea4..0a18aa4bd 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/field/Energy.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/protocol/field/Energy.java @@ -92,7 +92,7 @@ public class Energy { } double seconds = localInterval.getSeconds(); - seconds += localInterval.getNano() / ChronoUnit.SECONDS.getDuration().toNanos(); + seconds += (double) localInterval.getNano() / ChronoUnit.SECONDS.getDuration().toNanos(); return seconds; }