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.REAL_TIME_CLOCK_GET_RESPONSE;
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;
24 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
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.
30 * @author Wouter Born, Karel Goderis - Initial contribution
32 public class RealTimeClockGetResponseMessage extends Message {
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})");
45 public RealTimeClockGetResponseMessage(int sequenceNumber, String payload) {
46 super(REAL_TIME_CLOCK_GET_RESPONSE, sequenceNumber, payload);
53 public int getHour() {
57 public int getMinutes() {
61 public int getMonth() {
65 public int getSeconds() {
69 public LocalDateTime getDateTime() {
70 ZonedDateTime utcDateTime = ZonedDateTime.of(year, month, day, hour, minutes, seconds, 0, UTC);
71 return utcDateTime.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
74 public int getWeekday() {
78 public int getYear() {
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;
96 throw new PlugwisePayloadMismatchException(REAL_TIME_CLOCK_GET_RESPONSE, PAYLOAD_PATTERN, payload);