]> git.basschouten.com Git - openhab-addons.git/blob
7a655da1dd95673e77676c6ca6e50b612df2a516
[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.CLOCK_GET_RESPONSE;
17
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;
23
24 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
25
26 /**
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.
29  *
30  * @author Wouter Born, Karel Goderis - Initial contribution
31  */
32 public class ClockGetResponseMessage 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 hour;
38     private int minutes;
39     private int seconds;
40     private int weekday;
41
42     public ClockGetResponseMessage(int sequenceNumber, String payload) {
43         super(CLOCK_GET_RESPONSE, sequenceNumber, payload);
44     }
45
46     public int getHour() {
47         return hour;
48     }
49
50     public int getMinutes() {
51         return minutes;
52     }
53
54     public int getSeconds() {
55         return seconds;
56     }
57
58     public int getWeekday() {
59         return weekday;
60     }
61
62     @Override
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);
71         } else {
72             throw new PlugwisePayloadMismatchException(CLOCK_GET_RESPONSE, PAYLOAD_PATTERN, payload);
73         }
74     }
75
76     public String getTime() {
77         ZonedDateTime utcDateTime = ZonedDateTime.now(UTC).withHour(hour).withMinute(minutes).withSecond(seconds)
78                 .withNano(0);
79         ZonedDateTime localDateTime = utcDateTime.withZoneSameInstant(ZoneId.systemDefault());
80         return DateTimeFormatter.ISO_LOCAL_TIME.format(localDateTime);
81     }
82 }