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.CLOCK_GET_RESPONSE;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.time.format.DateTimeFormatter;
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 current clock value of a device. This message is the response of a {@link ClockGetRequestMessage}. Not
28 * all response fields have been reverse engineered.
30 * @author Wouter Born, Karel Goderis - Initial contribution
32 public class ClockGetResponseMessage 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})");
42 public ClockGetResponseMessage(int sequenceNumber, String payload) {
43 super(CLOCK_GET_RESPONSE, sequenceNumber, payload);
46 public int getHour() {
50 public int getMinutes() {
54 public int getSeconds() {
58 public int getWeekday() {
63 protected void parsePayload() {
64 Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
65 if (matcher.matches()) {
66 macAddress = new MACAddress(matcher.group(1));
67 hour = Integer.parseInt(matcher.group(2), 16);
68 minutes = Integer.parseInt(matcher.group(3), 16);
69 seconds = Integer.parseInt(matcher.group(4), 16);
70 weekday = Integer.parseInt(matcher.group(5), 16);
72 throw new PlugwisePayloadMismatchException(CLOCK_GET_RESPONSE, PAYLOAD_PATTERN, payload);
76 public String getTime() {
77 ZonedDateTime utcDateTime = ZonedDateTime.now(UTC).withHour(hour).withMinute(minutes).withSecond(seconds)
79 ZonedDateTime localDateTime = utcDateTime.withZoneSameInstant(ZoneId.systemDefault());
80 return DateTimeFormatter.ISO_LOCAL_TIME.format(localDateTime);