]> git.basschouten.com Git - openhab-addons.git/blob
a54c2b338095b4acdbbbb3bb729e4c979a59c0cd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.plugwise.internal.protocol;
14
15 import static java.time.ZoneOffset.UTC;
16 import static org.openhab.binding.plugwise.internal.protocol.field.MessageType.DEVICE_INFORMATION_RESPONSE;
17
18 import java.time.Instant;
19 import java.time.LocalDateTime;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 import org.openhab.binding.plugwise.internal.protocol.field.DeviceType;
24 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
25
26 /**
27  * Contains generic device information. This message is the response of an {@link InformationRequestMessage}.
28  *
29  * @author Wouter Born, Karel Goderis - Initial contribution
30  */
31 public class InformationResponseMessage extends Message {
32
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})");
35
36     private int year;
37     private int month;
38     private int minutes;
39     private int logAddress;
40     private boolean powerState;
41     private int hertz;
42     private String hardwareVersion;
43     private LocalDateTime firmwareVersion;
44     private DeviceType deviceType;
45
46     public InformationResponseMessage(int sequenceNumber, String payload) {
47         super(DEVICE_INFORMATION_RESPONSE, sequenceNumber, payload);
48     }
49
50     public DeviceType getDeviceType() {
51         return deviceType;
52     }
53
54     public LocalDateTime getFirmwareVersion() {
55         return firmwareVersion;
56     }
57
58     public String getHardwareVersion() {
59         return hardwareVersion;
60     }
61
62     public int getHertz() {
63         return (hertz == 133) ? 50 : 60;
64     }
65
66     public int getLogAddress() {
67         return logAddress;
68     }
69
70     public int getMinutes() {
71         return minutes;
72     }
73
74     public int getMonth() {
75         return month;
76     }
77
78     public boolean getPowerState() {
79         return powerState;
80     }
81
82     public int getYear() {
83         return year;
84     }
85
86     private DeviceType intToDeviceType(int i) {
87         switch (i) {
88             case 0:
89                 return DeviceType.STICK;
90             case 1:
91                 return DeviceType.CIRCLE_PLUS;
92             case 2:
93                 return DeviceType.CIRCLE;
94             case 3:
95                 return DeviceType.SWITCH;
96             case 5:
97                 return DeviceType.SENSE;
98             case 6:
99                 return DeviceType.SCAN;
100             case 9:
101                 return DeviceType.STEALTH;
102             default:
103                 return null;
104         }
105     }
106
107     @Override
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));
122         } else {
123             throw new PlugwisePayloadMismatchException(DEVICE_INFORMATION_RESPONSE, PAYLOAD_PATTERN, payload);
124         }
125     }
126 }