]> git.basschouten.com Git - openhab-addons.git/blob
d000f4c533555c0ee12e4a9c57e983f7928dec4b
[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_SET_REQUEST;
17
18 import java.time.LocalDateTime;
19 import java.time.ZoneId;
20 import java.time.ZonedDateTime;
21
22 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
23
24 /**
25  * Sets the clock of the Circle+. Based on what is known about the Plugwise protocol, only the clock of the Circle+ has
26  * to be set. The Circle+ sets the clock of all other network nodes.
27  *
28  * @author Wouter Born, Karel Goderis - Initial contribution
29  */
30 public class ClockSetRequestMessage extends Message {
31
32     private ZonedDateTime utcDateTime;
33
34     public ClockSetRequestMessage(MACAddress macAddress, LocalDateTime localDateTime) {
35         super(CLOCK_SET_REQUEST, macAddress);
36         // Nodes expect clock info to be in the UTC timezone
37         this.utcDateTime = localDateTime.atZone(ZoneId.systemDefault()).withZoneSameInstant(UTC);
38     }
39
40     @Override
41     protected String payloadToHexString() {
42         String year = String.format("%02X", utcDateTime.getYear() - 2000);
43         String month = String.format("%02X", utcDateTime.getMonthValue());
44         String minutes = String.format("%04X",
45                 (utcDateTime.getDayOfMonth() - 1) * 24 * 60 + (utcDateTime.getHour() * 60) + utcDateTime.getMinute());
46         // If we set logaddress to FFFFFFFFF then previous buffered data will be kept by the Circle+
47         String logAddress = "FFFFFFFF";
48         String hour = String.format("%02X", utcDateTime.getHour());
49         String minute = String.format("%02X", utcDateTime.getMinute());
50         String second = String.format("%02X", utcDateTime.getSecond());
51         // Monday = 0, ... , Sunday = 6
52         String dayOfWeek = String.format("%02X", utcDateTime.getDayOfWeek().getValue() - 1);
53
54         return year + month + minutes + logAddress + hour + minute + second + dayOfWeek;
55     }
56 }