]> git.basschouten.com Git - openhab-addons.git/blob
fb1753f2415cef1f75a256a35beec7480ec25ccf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.REAL_TIME_CLOCK_GET_RESPONSE;
17
18 import java.time.LocalDateTime;
19 import java.time.ZoneId;
20 import java.time.ZonedDateTime;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
25
26 /**
27  * Contains the real-time clock value of a Circle+. This message is the response of a
28  * {@link RealTimeClockGetRequestMessage}. The Circle+ is the only device that holds a real-time clock value.
29  *
30  * @author Wouter Born, Karel Goderis - Initial contribution
31  */
32 public class RealTimeClockGetResponseMessage extends Message {
33
34     private static final Pattern PAYLOAD_PATTERN = Pattern
35             .compile("(\\w{16})(\\w{2})(\\w{2})(\\w{2})(\\w{2})(\\w{2})(\\w{2})(\\w{2})");
36
37     private int seconds;
38     private int minutes;
39     private int hour;
40     private int weekday;
41     private int day;
42     private int month;
43     private int year;
44
45     public RealTimeClockGetResponseMessage(int sequenceNumber, String payload) {
46         super(REAL_TIME_CLOCK_GET_RESPONSE, sequenceNumber, payload);
47     }
48
49     public int getDay() {
50         return day;
51     }
52
53     public int getHour() {
54         return hour;
55     }
56
57     public int getMinutes() {
58         return minutes;
59     }
60
61     public int getMonth() {
62         return month;
63     }
64
65     public int getSeconds() {
66         return seconds;
67     }
68
69     public LocalDateTime getDateTime() {
70         ZonedDateTime utcDateTime = ZonedDateTime.of(year, month, day, hour, minutes, seconds, 0, UTC);
71         return utcDateTime.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
72     }
73
74     public int getWeekday() {
75         return weekday;
76     }
77
78     public int getYear() {
79         return year;
80     }
81
82     @Override
83     protected void parsePayload() {
84         Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
85         if (matcher.matches()) {
86             macAddress = new MACAddress(matcher.group(1));
87             // Real-time clock values in the message are decimals and not hexadecimals
88             seconds = Integer.parseInt(matcher.group(2));
89             minutes = Integer.parseInt(matcher.group(3));
90             hour = Integer.parseInt(matcher.group(4));
91             weekday = Integer.parseInt(matcher.group(5));
92             day = Integer.parseInt(matcher.group(6));
93             month = Integer.parseInt(matcher.group(7));
94             year = Integer.parseInt(matcher.group(8)) + 2000;
95         } else {
96             throw new PlugwisePayloadMismatchException(REAL_TIME_CLOCK_GET_RESPONSE, PAYLOAD_PATTERN, payload);
97         }
98     }
99 }