2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.plugwise.internal.protocol;
15 import static java.time.ZoneOffset.UTC;
16 import static org.openhab.binding.plugwise.internal.protocol.field.MessageType.DEVICE_INFORMATION_RESPONSE;
18 import java.time.Instant;
19 import java.time.LocalDateTime;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
23 import org.openhab.binding.plugwise.internal.protocol.field.DeviceType;
24 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
27 * Contains generic device information. This message is the response of an {@link InformationRequestMessage}.
29 * @author Wouter Born, Karel Goderis - Initial contribution
31 public class InformationResponseMessage extends Message {
33 private static final Pattern PAYLOAD_PATTERN = Pattern
34 .compile("(\\w{16})(\\w{2})(\\w{2})(\\w{4})(\\w{8})(\\w{2})(\\w{2})(\\w{12})(\\w{8})(\\w{2})");
39 private int logAddress;
40 private boolean powerState;
42 private String hardwareVersion;
43 private LocalDateTime firmwareVersion;
44 private DeviceType deviceType;
46 public InformationResponseMessage(int sequenceNumber, String payload) {
47 super(DEVICE_INFORMATION_RESPONSE, sequenceNumber, payload);
50 public DeviceType getDeviceType() {
54 public LocalDateTime getFirmwareVersion() {
55 return firmwareVersion;
58 public String getHardwareVersion() {
59 return hardwareVersion;
62 public int getHertz() {
63 return (hertz == 133) ? 50 : 60;
66 public int getLogAddress() {
70 public int getMinutes() {
74 public int getMonth() {
78 public boolean getPowerState() {
82 public int getYear() {
86 private DeviceType intToDeviceType(int i) {
88 case 0 -> DeviceType.STICK;
89 case 1 -> DeviceType.CIRCLE_PLUS;
90 case 2 -> DeviceType.CIRCLE;
91 case 3 -> DeviceType.SWITCH;
92 case 5 -> DeviceType.SENSE;
93 case 6 -> DeviceType.SCAN;
94 case 9 -> DeviceType.STEALTH;
100 protected void parsePayload() {
101 Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
102 if (matcher.matches()) {
103 macAddress = new MACAddress(matcher.group(1));
104 year = Integer.parseInt(matcher.group(2), 16) + 2000;
105 month = Integer.parseInt(matcher.group(3), 16);
106 minutes = Integer.parseInt(matcher.group(4), 16);
107 logAddress = (Integer.parseInt(matcher.group(5), 16) - 278528) / 32;
108 powerState = ("01".equals(matcher.group(6)));
109 hertz = Integer.parseInt(matcher.group(7), 16);
110 hardwareVersion = matcher.group(8).substring(0, 4) + "-" + matcher.group(8).substring(4, 8) + "-"
111 + matcher.group(8).substring(8, 12);
112 firmwareVersion = LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(matcher.group(9), 16)), UTC);
113 deviceType = intToDeviceType(Integer.parseInt(matcher.group(10), 16));
115 throw new PlugwisePayloadMismatchException(DEVICE_INFORMATION_RESPONSE, PAYLOAD_PATTERN, payload);