[plugwise] Cleanup code (#15688)
The cleanup includes: * Use lambdas * Use enhanced switches * Make abstract class constructors protected * Fix typos Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
parent
86f8bc991d
commit
2bf2246530
|
@ -44,7 +44,6 @@ public class PlugwiseBindingConstants {
|
||||||
// List of all configuration properties
|
// List of all configuration properties
|
||||||
public static final String CONFIG_PROPERTY_MAC_ADDRESS = "macAddress";
|
public static final String CONFIG_PROPERTY_MAC_ADDRESS = "macAddress";
|
||||||
public static final String CONFIG_PROPERTY_RECALIBRATE = "recalibrate";
|
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_CONFIGURATION = "updateConfiguration";
|
||||||
public static final String CONFIG_PROPERTY_UPDATE_INTERVAL = "updateInterval";
|
public static final String CONFIG_PROPERTY_UPDATE_INTERVAL = "updateInterval";
|
||||||
|
|
||||||
|
|
|
@ -60,9 +60,7 @@ public class PlugwiseCommunicationContext {
|
||||||
|
|
||||||
public static final int MAX_BUFFER_SIZE = 1024;
|
public static final int MAX_BUFFER_SIZE = 1024;
|
||||||
|
|
||||||
private static final Comparator<? super @Nullable PlugwiseQueuedMessage> QUEUED_MESSAGE_COMPERATOR = new Comparator<@Nullable PlugwiseQueuedMessage>() {
|
private static final Comparator<? super @Nullable PlugwiseQueuedMessage> QUEUED_MESSAGE_COMPARATOR = (o1, o2) -> {
|
||||||
@Override
|
|
||||||
public int compare(@Nullable PlugwiseQueuedMessage o1, @Nullable PlugwiseQueuedMessage o2) {
|
|
||||||
if (o1 == null || o2 == null) {
|
if (o1 == null || o2 == null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +69,6 @@ public class PlugwiseCommunicationContext {
|
||||||
result = o1.getDateTime().compareTo(o2.getDateTime());
|
result = o1.getDateTime().compareTo(o2.getDateTime());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(PlugwiseCommunicationContext.class);
|
private final Logger logger = LoggerFactory.getLogger(PlugwiseCommunicationContext.class);
|
||||||
|
@ -79,7 +76,7 @@ public class PlugwiseCommunicationContext {
|
||||||
MAX_BUFFER_SIZE, true);
|
MAX_BUFFER_SIZE, true);
|
||||||
private final BlockingQueue<@Nullable Message> receivedQueue = new ArrayBlockingQueue<>(MAX_BUFFER_SIZE, true);
|
private final BlockingQueue<@Nullable Message> receivedQueue = new ArrayBlockingQueue<>(MAX_BUFFER_SIZE, true);
|
||||||
private final PriorityBlockingQueue<@Nullable PlugwiseQueuedMessage> sendQueue = new PriorityBlockingQueue<>(
|
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,
|
private final BlockingQueue<@Nullable PlugwiseQueuedMessage> sentQueue = new ArrayBlockingQueue<>(MAX_BUFFER_SIZE,
|
||||||
true);
|
true);
|
||||||
private final ReentrantLock sentQueueLock = new ReentrantLock();
|
private final ReentrantLock sentQueueLock = new ReentrantLock();
|
||||||
|
@ -141,7 +138,7 @@ public class PlugwiseCommunicationContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build exception message when port not found
|
// 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()));
|
.collect(Collectors.joining(System.lineSeparator()));
|
||||||
|
|
||||||
throw new PlugwiseInitializationException(
|
throw new PlugwiseInitializationException(
|
||||||
|
|
|
@ -45,22 +45,19 @@ public abstract class PlugwiseDeviceTask {
|
||||||
|
|
||||||
private @Nullable ScheduledFuture<?> future;
|
private @Nullable ScheduledFuture<?> future;
|
||||||
|
|
||||||
private Runnable scheduledRunnable = new Runnable() {
|
private final Runnable scheduledRunnable = () -> {
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
try {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
logger.debug("Running '{}' Plugwise task for {} ({})", name, deviceType, macAddress);
|
logger.debug("Running '{}' Plugwise task for {} ({})", getName(), deviceType, macAddress);
|
||||||
runTask();
|
runTask();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.warn("Error while running '{}' Plugwise task for {} ({})", name, deviceType, macAddress, e);
|
logger.warn("Error while running '{}' Plugwise task for {} ({})", getName(), deviceType, macAddress, e);
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public PlugwiseDeviceTask(String name, ScheduledExecutorService scheduler) {
|
protected PlugwiseDeviceTask(String name, ScheduledExecutorService scheduler) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ public class PlugwiseFilteredMessageListenerList {
|
||||||
for (PlugwiseFilteredMessageListener filteredListener : filteredListeners) {
|
for (PlugwiseFilteredMessageListener filteredListener : filteredListeners) {
|
||||||
if (filteredListener.matches(message)) {
|
if (filteredListener.matches(message)) {
|
||||||
try {
|
try {
|
||||||
filteredListener.getListener().handleReponseMessage(message);
|
filteredListener.getListener().handleResponseMessage(message);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.warn("Listener failed to handle message: {}", message, e);
|
logger.warn("Listener failed to handle message: {}", message, e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,6 @@ import static org.openhab.binding.plugwise.internal.PlugwiseCommunicationContext
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.TooManyListenersException;
|
import java.util.TooManyListenersException;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
@ -159,9 +158,7 @@ public class PlugwiseMessageProcessor implements SerialPortEventListener {
|
||||||
try {
|
try {
|
||||||
context.getSentQueueLock().lock();
|
context.getSentQueueLock().lock();
|
||||||
|
|
||||||
Iterator<@Nullable PlugwiseQueuedMessage> messageIterator = context.getSentQueue().iterator();
|
for (PlugwiseQueuedMessage queuedSentMessage : context.getSentQueue()) {
|
||||||
while (messageIterator.hasNext()) {
|
|
||||||
PlugwiseQueuedMessage queuedSentMessage = messageIterator.next();
|
|
||||||
if (queuedSentMessage != null
|
if (queuedSentMessage != null
|
||||||
&& queuedSentMessage.getMessage().getSequenceNumber() == message.getSequenceNumber()) {
|
&& queuedSentMessage.getMessage().getSequenceNumber() == message.getSequenceNumber()) {
|
||||||
logger.debug("Removing from sentQueue: {}", queuedSentMessage.getMessage());
|
logger.debug("Removing from sentQueue: {}", queuedSentMessage.getMessage());
|
||||||
|
|
|
@ -39,7 +39,7 @@ public class PlugwiseMessageSender {
|
||||||
|
|
||||||
private class MessageSenderThread extends Thread {
|
private class MessageSenderThread extends Thread {
|
||||||
|
|
||||||
private int messageWaitTime;
|
private final int messageWaitTime;
|
||||||
|
|
||||||
public MessageSenderThread(int messageWaitTime) {
|
public MessageSenderThread(int messageWaitTime) {
|
||||||
super("OH-binding-" + context.getBridgeUID() + "-message-sender");
|
super("OH-binding-" + context.getBridgeUID() + "-message-sender");
|
||||||
|
|
|
@ -133,7 +133,7 @@ public class PlugwiseThingDiscoveryService extends AbstractDiscoveryService
|
||||||
ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, mac);
|
ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, mac);
|
||||||
|
|
||||||
thingDiscovered(DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
|
thingDiscovered(DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
|
||||||
.withLabel("Plugwise " + node.deviceType.toString())
|
.withLabel("Plugwise " + node.deviceType)
|
||||||
.withProperty(PlugwiseBindingConstants.CONFIG_PROPERTY_MAC_ADDRESS, mac)
|
.withProperty(PlugwiseBindingConstants.CONFIG_PROPERTY_MAC_ADDRESS, mac)
|
||||||
.withProperties(new HashMap<>(node.properties))
|
.withProperties(new HashMap<>(node.properties))
|
||||||
.withRepresentationProperty(PlugwiseBindingConstants.PROPERTY_MAC_ADDRESS).build());
|
.withRepresentationProperty(PlugwiseBindingConstants.PROPERTY_MAC_ADDRESS).build());
|
||||||
|
@ -215,7 +215,7 @@ public class PlugwiseThingDiscoveryService extends AbstractDiscoveryService
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleReponseMessage(Message message) {
|
public void handleResponseMessage(Message message) {
|
||||||
switch (message.getType()) {
|
switch (message.getType()) {
|
||||||
case ANNOUNCE_AWAKE_REQUEST:
|
case ANNOUNCE_AWAKE_REQUEST:
|
||||||
handleAnnounceAwakeRequest((AnnounceAwakeRequestMessage) message);
|
handleAnnounceAwakeRequest((AnnounceAwakeRequestMessage) message);
|
||||||
|
|
|
@ -103,9 +103,9 @@ public final class PlugwiseUtils {
|
||||||
final String delimiter = "_";
|
final String delimiter = "_";
|
||||||
StringBuilder upperCamelBuilder = new StringBuilder(text.length());
|
StringBuilder upperCamelBuilder = new StringBuilder(text.length());
|
||||||
for (String str : text.split(delimiter)) {
|
for (String str : text.split(delimiter)) {
|
||||||
if (upperCamelBuilder.isEmpty() && str.length() > 0) {
|
if (upperCamelBuilder.isEmpty() && !str.isEmpty()) {
|
||||||
upperCamelBuilder.append(str.substring(0, 1).toLowerCase());
|
upperCamelBuilder.append(str.substring(0, 1).toLowerCase());
|
||||||
} else if (str.length() > 0) {
|
} else if (!str.isEmpty()) {
|
||||||
upperCamelBuilder.append(str.substring(0, 1).toUpperCase());
|
upperCamelBuilder.append(str.substring(0, 1).toUpperCase());
|
||||||
}
|
}
|
||||||
if (str.length() > 1) {
|
if (str.length() > 1) {
|
||||||
|
|
|
@ -88,7 +88,7 @@ public abstract class AbstractPlugwiseThingHandler extends BaseThingHandler impl
|
||||||
private @Nullable LocalDateTime lastConfigurationUpdateSend;
|
private @Nullable LocalDateTime lastConfigurationUpdateSend;
|
||||||
private int unansweredPings;
|
private int unansweredPings;
|
||||||
|
|
||||||
public AbstractPlugwiseThingHandler(Thing thing) {
|
protected AbstractPlugwiseThingHandler(Thing thing) {
|
||||||
super(thing);
|
super(thing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ public abstract class AbstractSleepingEndDeviceHandler extends AbstractPlugwiseT
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(AbstractSleepingEndDeviceHandler.class);
|
private final Logger logger = LoggerFactory.getLogger(AbstractSleepingEndDeviceHandler.class);
|
||||||
|
|
||||||
public AbstractSleepingEndDeviceHandler(Thing thing) {
|
protected AbstractSleepingEndDeviceHandler(Thing thing) {
|
||||||
super(thing);
|
super(thing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,12 +72,11 @@ public abstract class AbstractSleepingEndDeviceHandler extends AbstractPlugwiseT
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleReponseMessage(Message message) {
|
public void handleResponseMessage(Message message) {
|
||||||
updateLastSeen();
|
updateLastSeen();
|
||||||
|
|
||||||
switch (message.getType()) {
|
switch (message.getType()) {
|
||||||
case ACKNOWLEDGEMENT_V1:
|
case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2:
|
||||||
case ACKNOWLEDGEMENT_V2:
|
|
||||||
handleAcknowledgement((AcknowledgementMessage) message);
|
handleAcknowledgement((AcknowledgementMessage) message);
|
||||||
break;
|
break;
|
||||||
case ANNOUNCE_AWAKE_REQUEST:
|
case ANNOUNCE_AWAKE_REQUEST:
|
||||||
|
|
|
@ -205,9 +205,9 @@ public class PlugwiseRelayDeviceHandler extends AbstractPlugwiseThingHandler {
|
||||||
if (deviceType == DeviceType.CIRCLE_PLUS) {
|
if (deviceType == DeviceType.CIRCLE_PLUS) {
|
||||||
// The Circle+ real-time clock needs to be updated first to prevent clock sync issues
|
// The Circle+ real-time clock needs to be updated first to prevent clock sync issues
|
||||||
sendCommandMessage(new RealTimeClockSetRequestMessage(macAddress, LocalDateTime.now()));
|
sendCommandMessage(new RealTimeClockSetRequestMessage(macAddress, LocalDateTime.now()));
|
||||||
scheduler.schedule(() -> {
|
scheduler.schedule(
|
||||||
sendCommandMessage(new ClockSetRequestMessage(macAddress, LocalDateTime.now()));
|
() -> sendCommandMessage(new ClockSetRequestMessage(macAddress, LocalDateTime.now())), 5,
|
||||||
}, 5, TimeUnit.SECONDS);
|
TimeUnit.SECONDS);
|
||||||
} else {
|
} else {
|
||||||
sendCommandMessage(new ClockSetRequestMessage(macAddress, LocalDateTime.now()));
|
sendCommandMessage(new ClockSetRequestMessage(macAddress, LocalDateTime.now()));
|
||||||
}
|
}
|
||||||
|
@ -467,12 +467,11 @@ public class PlugwiseRelayDeviceHandler extends AbstractPlugwiseThingHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleReponseMessage(Message message) {
|
public void handleResponseMessage(Message message) {
|
||||||
updateLastSeen();
|
updateLastSeen();
|
||||||
|
|
||||||
switch (message.getType()) {
|
switch (message.getType()) {
|
||||||
case ACKNOWLEDGEMENT_V1:
|
case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2:
|
||||||
case ACKNOWLEDGEMENT_V2:
|
|
||||||
handleAcknowledgement((AcknowledgementMessage) message);
|
handleAcknowledgement((AcknowledgementMessage) message);
|
||||||
break;
|
break;
|
||||||
case CLOCK_GET_RESPONSE:
|
case CLOCK_GET_RESPONSE:
|
||||||
|
|
|
@ -117,13 +117,13 @@ public class PlugwiseSenseHandler extends AbstractSleepingEndDeviceHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleReponseMessage(Message message) {
|
public void handleResponseMessage(Message message) {
|
||||||
switch (message.getType()) {
|
switch (message.getType()) {
|
||||||
case SENSE_REPORT_REQUEST:
|
case SENSE_REPORT_REQUEST:
|
||||||
handleSenseReportRequestMessage((SenseReportRequestMessage) message);
|
handleSenseReportRequestMessage((SenseReportRequestMessage) message);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
super.handleReponseMessage(message);
|
super.handleResponseMessage(message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,10 +177,9 @@ public class PlugwiseStickHandler extends BaseBridgeHandler implements PlugwiseM
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleReponseMessage(Message message) {
|
public void handleResponseMessage(Message message) {
|
||||||
switch (message.getType()) {
|
switch (message.getType()) {
|
||||||
case ACKNOWLEDGEMENT_V1:
|
case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2:
|
||||||
case ACKNOWLEDGEMENT_V2:
|
|
||||||
handleAcknowledgement((AcknowledgementMessage) message);
|
handleAcknowledgement((AcknowledgementMessage) message);
|
||||||
break;
|
break;
|
||||||
case DEVICE_INFORMATION_RESPONSE:
|
case DEVICE_INFORMATION_RESPONSE:
|
||||||
|
|
|
@ -23,5 +23,5 @@ import org.openhab.binding.plugwise.internal.protocol.Message;
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public interface PlugwiseMessageListener {
|
public interface PlugwiseMessageListener {
|
||||||
|
|
||||||
void handleReponseMessage(Message message);
|
void handleResponseMessage(Message message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,9 +67,9 @@ public class AcknowledgementMessage extends Message {
|
||||||
return TYPES_BY_VALUE.get(value);
|
return TYPES_BY_VALUE.get(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int identifier;
|
private final int identifier;
|
||||||
|
|
||||||
private ExtensionCode(int value) {
|
ExtensionCode(int value) {
|
||||||
identifier = value;
|
identifier = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class AnnounceAwakeRequestMessage extends Message {
|
||||||
WAKEUP_BUTTON(5);
|
WAKEUP_BUTTON(5);
|
||||||
|
|
||||||
public static AwakeReason forValue(int value) {
|
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;
|
private final int id;
|
||||||
|
|
|
@ -44,13 +44,13 @@ public class ClockSetRequestMessage extends Message {
|
||||||
String minutes = String.format("%04X",
|
String minutes = String.format("%04X",
|
||||||
(utcDateTime.getDayOfMonth() - 1) * 24 * 60 + (utcDateTime.getHour() * 60) + utcDateTime.getMinute());
|
(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+
|
// 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 hour = String.format("%02X", utcDateTime.getHour());
|
||||||
String minute = String.format("%02X", utcDateTime.getMinute());
|
String minute = String.format("%02X", utcDateTime.getMinute());
|
||||||
String second = String.format("%02X", utcDateTime.getSecond());
|
String second = String.format("%02X", utcDateTime.getSecond());
|
||||||
// Monday = 0, ... , Sunday = 6
|
// Monday = 0, ... , Sunday = 6
|
||||||
String dayOfWeek = String.format("%02X", utcDateTime.getDayOfWeek().getValue() - 1);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,24 +84,16 @@ public class InformationResponseMessage extends Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
private DeviceType intToDeviceType(int i) {
|
private DeviceType intToDeviceType(int i) {
|
||||||
switch (i) {
|
return switch (i) {
|
||||||
case 0:
|
case 0 -> DeviceType.STICK;
|
||||||
return DeviceType.STICK;
|
case 1 -> DeviceType.CIRCLE_PLUS;
|
||||||
case 1:
|
case 2 -> DeviceType.CIRCLE;
|
||||||
return DeviceType.CIRCLE_PLUS;
|
case 3 -> DeviceType.SWITCH;
|
||||||
case 2:
|
case 5 -> DeviceType.SENSE;
|
||||||
return DeviceType.CIRCLE;
|
case 6 -> DeviceType.SCAN;
|
||||||
case 3:
|
case 9 -> DeviceType.STEALTH;
|
||||||
return DeviceType.SWITCH;
|
default -> null;
|
||||||
case 5:
|
};
|
||||||
return DeviceType.SENSE;
|
|
||||||
case 6:
|
|
||||||
return DeviceType.SCAN;
|
|
||||||
case 9:
|
|
||||||
return DeviceType.STEALTH;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
*/
|
*/
|
||||||
package org.openhab.binding.plugwise.internal.protocol;
|
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.MACAddress;
|
||||||
import org.openhab.binding.plugwise.internal.protocol.field.MessageType;
|
import org.openhab.binding.plugwise.internal.protocol.field.MessageType;
|
||||||
|
@ -40,14 +40,7 @@ public abstract class Message {
|
||||||
int crc = 0x0000;
|
int crc = 0x0000;
|
||||||
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
|
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
|
||||||
|
|
||||||
byte[] bytes = new byte[0];
|
for (byte b : string.getBytes(StandardCharsets.US_ASCII)) {
|
||||||
try {
|
|
||||||
bytes = string.getBytes("ASCII");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
for (byte b : bytes) {
|
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
boolean bit = ((b >> (7 - i) & 1) == 1);
|
boolean bit = ((b >> (7 - i) & 1) == 1);
|
||||||
boolean c15 = ((crc >> 15 & 1) == 1);
|
boolean c15 = ((crc >> 15 & 1) == 1);
|
||||||
|
@ -69,11 +62,11 @@ public abstract class Message {
|
||||||
|
|
||||||
protected String payload;
|
protected String payload;
|
||||||
|
|
||||||
public Message(MessageType messageType) {
|
protected Message(MessageType messageType) {
|
||||||
this(messageType, null, null, null);
|
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.type = messageType;
|
||||||
this.sequenceNumber = sequenceNumber;
|
this.sequenceNumber = sequenceNumber;
|
||||||
this.macAddress = macAddress;
|
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);
|
this(messageType, sequenceNumber, null, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Message(MessageType messageType, MACAddress macAddress) {
|
protected Message(MessageType messageType, MACAddress macAddress) {
|
||||||
this(messageType, null, macAddress, null);
|
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);
|
this(messageType, null, macAddress, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Message(MessageType messageType, String payload) {
|
protected Message(MessageType messageType, String payload) {
|
||||||
this(messageType, null, null, payload);
|
this(messageType, null, null, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,40 +25,24 @@ public class MessageFactory {
|
||||||
|
|
||||||
public Message createMessage(MessageType messageType, int sequenceNumber, String payload)
|
public Message createMessage(MessageType messageType, int sequenceNumber, String payload)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
switch (messageType) {
|
return switch (messageType) {
|
||||||
case ACKNOWLEDGEMENT_V1:
|
case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2 ->
|
||||||
case ACKNOWLEDGEMENT_V2:
|
new AcknowledgementMessage(messageType, sequenceNumber, payload);
|
||||||
return new AcknowledgementMessage(messageType, sequenceNumber, payload);
|
case ANNOUNCE_AWAKE_REQUEST -> new AnnounceAwakeRequestMessage(sequenceNumber, payload);
|
||||||
case ANNOUNCE_AWAKE_REQUEST:
|
case BROADCAST_GROUP_SWITCH_RESPONSE -> new BroadcastGroupSwitchResponseMessage(sequenceNumber, payload);
|
||||||
return new AnnounceAwakeRequestMessage(sequenceNumber, payload);
|
case CLOCK_GET_RESPONSE -> new ClockGetResponseMessage(sequenceNumber, payload);
|
||||||
case BROADCAST_GROUP_SWITCH_RESPONSE:
|
case DEVICE_INFORMATION_RESPONSE -> new InformationResponseMessage(sequenceNumber, payload);
|
||||||
return new BroadcastGroupSwitchResponseMessage(sequenceNumber, payload);
|
case DEVICE_ROLE_CALL_RESPONSE -> new RoleCallResponseMessage(sequenceNumber, payload);
|
||||||
case CLOCK_GET_RESPONSE:
|
case MODULE_JOINED_NETWORK_REQUEST -> new ModuleJoinedNetworkRequestMessage(sequenceNumber, payload);
|
||||||
return new ClockGetResponseMessage(sequenceNumber, payload);
|
case NETWORK_STATUS_RESPONSE -> new NetworkStatusResponseMessage(sequenceNumber, payload);
|
||||||
case DEVICE_INFORMATION_RESPONSE:
|
case NODE_AVAILABLE -> new NodeAvailableMessage(sequenceNumber, payload);
|
||||||
return new InformationResponseMessage(sequenceNumber, payload);
|
case PING_RESPONSE -> new PingResponseMessage(sequenceNumber, payload);
|
||||||
case DEVICE_ROLE_CALL_RESPONSE:
|
case POWER_BUFFER_RESPONSE -> new PowerBufferResponseMessage(sequenceNumber, payload);
|
||||||
return new RoleCallResponseMessage(sequenceNumber, payload);
|
case POWER_CALIBRATION_RESPONSE -> new PowerCalibrationResponseMessage(sequenceNumber, payload);
|
||||||
case MODULE_JOINED_NETWORK_REQUEST:
|
case POWER_INFORMATION_RESPONSE -> new PowerInformationResponseMessage(sequenceNumber, payload);
|
||||||
return new ModuleJoinedNetworkRequestMessage(sequenceNumber, payload);
|
case REAL_TIME_CLOCK_GET_RESPONSE -> new RealTimeClockGetResponseMessage(sequenceNumber, payload);
|
||||||
case NETWORK_STATUS_RESPONSE:
|
case SENSE_REPORT_REQUEST -> new SenseReportRequestMessage(sequenceNumber, payload);
|
||||||
return new NetworkStatusResponseMessage(sequenceNumber, payload);
|
default -> throw new IllegalArgumentException("Unsupported message type: " + messageType);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ public class Energy {
|
||||||
}
|
}
|
||||||
|
|
||||||
double seconds = localInterval.getSeconds();
|
double seconds = localInterval.getSeconds();
|
||||||
seconds += localInterval.getNano() / ChronoUnit.SECONDS.getDuration().toNanos();
|
seconds += (double) localInterval.getNano() / ChronoUnit.SECONDS.getDuration().toNanos();
|
||||||
return seconds;
|
return seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue