]> git.basschouten.com Git - openhab-addons.git/blob
7225e2ec237c8791a4fae37dbeae635e77076c02
[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_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 real-time clock value of a Circle+. The Circle+ is the only device that holds a real-time clock value.
26  *
27  * @author Wouter Born - Initial contribution
28  */
29 public class RealTimeClockSetRequestMessage extends Message {
30
31     private ZonedDateTime utcDateTime;
32
33     public RealTimeClockSetRequestMessage(MACAddress macAddress, LocalDateTime localDateTime) {
34         super(REAL_TIME_CLOCK_SET_REQUEST, macAddress);
35         this.utcDateTime = localDateTime.atZone(ZoneId.systemDefault()).withZoneSameInstant(UTC);
36     }
37
38     @Override
39     protected String payloadToHexString() {
40         // Real-time clock values in the message are decimals and not hexadecimals
41         String second = String.format("%02d", utcDateTime.getSecond());
42         String minute = String.format("%02d", utcDateTime.getMinute());
43         String hour = String.format("%02d", utcDateTime.getHour());
44         // Monday = 0, ... , Sunday = 6
45         String dayOfWeek = String.format("%02d", utcDateTime.getDayOfWeek().getValue() - 1);
46         String day = String.format("%02d", utcDateTime.getDayOfMonth());
47         String month = String.format("%02d", utcDateTime.getMonthValue());
48         String year = String.format("%02d", utcDateTime.getYear() - 2000);
49
50         return second + minute + hour + dayOfWeek + day + month + year;
51     }
52 }