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 877321264d0c6..fd182176729da 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 d1ffd42ca8cd0..1c0271d17fd63 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 int compare(@Nullable PlugwiseQueuedMessage o1, @Nullable PlugwiseQueuedM 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 @@ private SerialPortIdentifier findSerialPortIdentifier() throws PlugwiseInitializ } // 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 f58016b1abb2b..c103800a168ab 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 b315077525364..69990d13f0d40 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 void notifyListeners(Message message) { 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 93362e040dcf5..27fb280dc19f9 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 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 @@ private void processMessage(Message message) { 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 e2651760c3b55..8cefcb2753550 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 fddac119ac374..6b915adef7ce2 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 @@ private void createDiscoveryResult(DiscoveredNode node) { 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 @@ private void handleInformationResponse(InformationResponseMessage message) { } @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 fb86148f9e5d4..ceb722888a138 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 static String upperUnderscoreToLowerCamel(String text) { 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 95706a12f57da..c0db1ed6f5ac1 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 void start() { 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 da92053ef82a3..8026d743076f1 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 @@ protected void handleInformationResponse(InformationResponseMessage message) { } @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 567b906006413..e290c21abbff9 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 void runTask() { 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 @@ private void handleRealTimeClockGetResponse(RealTimeClockGetResponseMessage mess } @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 ffa7430bb3d90..5160a41b6de48 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 @@ protected void handleAcknowledgement(AcknowledgementMessage message) { } @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 b95700cfcb7ed..bb2720755ab44 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 @@ private void handleNetworkStatusResponse(NetworkStatusResponseMessage message) { } @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 740e12c3f8271..556e4a7e4a2f1 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 @@ @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 2ee5578b64842..a297065aadc3c 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 static ExtensionCode forValue(int value) { 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 7aa164d31fc23..a0944c7c6d62b 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 enum AwakeReason { 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 af9f964e30be6..d000f4c533555 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 @@ protected String payloadToHexString() { 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 712a6cd341f05..63e7065394a54 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 int getYear() { } 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 fdfa0359cbf9e..7954e8c220c8c 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 static String getCRC(String string) { 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 static String getCRC(String string) { 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 Message(MessageType messageType, Integer sequenceNumber, MACAddress macAd } } - 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 5d5d0f20cf8be..0a9e0e94e69e8 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 51e559ea420a5..0a18aa4bd6996 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 @@ private double intervalSeconds() { } double seconds = localInterval.getSeconds(); - seconds += localInterval.getNano() / ChronoUnit.SECONDS.getDuration().toNanos(); + seconds += (double) localInterval.getNano() / ChronoUnit.SECONDS.getDuration().toNanos(); return seconds; }