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_SET_REQUEST;
18 import java.time.LocalDateTime;
19 import java.time.ZoneId;
20 import java.time.ZonedDateTime;
22 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
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.
28 * @author Wouter Born, Karel Goderis - Initial contribution
30 public class ClockSetRequestMessage extends Message {
32 private ZonedDateTime utcDateTime;
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);
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);
54 return year + month + minutes + logAddress + hour + minute + second + dayOfWeek;