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) {
89 return DeviceType.STICK;
91 return DeviceType.CIRCLE_PLUS;
93 return DeviceType.CIRCLE;
95 return DeviceType.SWITCH;
97 return DeviceType.SENSE;
99 return DeviceType.SCAN;
101 return DeviceType.STEALTH;
108 protected void parsePayload() {
109 Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
110 if (matcher.matches()) {
111 macAddress = new MACAddress(matcher.group(1));
112 year = Integer.parseInt(matcher.group(2), 16) + 2000;
113 month = Integer.parseInt(matcher.group(3), 16);
114 minutes = Integer.parseInt(matcher.group(4), 16);
115 logAddress = (Integer.parseInt(matcher.group(5), 16) - 278528) / 32;
116 powerState = (matcher.group(6).equals("01"));
117 hertz = Integer.parseInt(matcher.group(7), 16);
118 hardwareVersion = matcher.group(8).substring(0, 4) + "-" + matcher.group(8).substring(4, 8) + "-"
119 + matcher.group(8).substring(8, 12);
120 firmwareVersion = LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(matcher.group(9), 16)), UTC);
121 deviceType = intToDeviceType(Integer.parseInt(matcher.group(10), 16));
123 throw new PlugwisePayloadMismatchException(DEVICE_INFORMATION_RESPONSE, PAYLOAD_PATTERN, payload);