]> git.basschouten.com Git - openhab-addons.git/blob
b9aab5c886009013afbd009175c71a061e7a4b33
[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.velbus.internal.packets;
14
15 import static org.openhab.binding.velbus.internal.VelbusBindingConstants.COMMAND_SET_REALTIME_CLOCK;
16
17 import java.time.DayOfWeek;
18 import java.time.ZonedDateTime;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * The {@link VelbusSetRealtimeClockPacket} represents a Velbus packet that can be used to
24  * set the clock of the given Velbus module.
25  *
26  * @author Cedric Boon - Initial contribution
27  */
28 @NonNullByDefault
29 public class VelbusSetRealtimeClockPacket extends VelbusPacket {
30     private ZonedDateTime zonedDateTime;
31
32     public VelbusSetRealtimeClockPacket(byte address, ZonedDateTime zonedDateTime) {
33         super(address, PRIO_LOW);
34
35         this.zonedDateTime = zonedDateTime;
36     }
37
38     public byte getHour() {
39         return (byte) this.zonedDateTime.getHour();
40     }
41
42     public byte getMinute() {
43         return (byte) this.zonedDateTime.getMinute();
44     }
45
46     public byte getWeekDay() {
47         DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
48         switch (dayOfWeek) {
49             case MONDAY:
50                 return 0x00;
51             case TUESDAY:
52                 return 0x01;
53             case WEDNESDAY:
54                 return 0x02;
55             case THURSDAY:
56                 return 0x03;
57             case FRIDAY:
58                 return 0x04;
59             case SATURDAY:
60                 return 0x05;
61             case SUNDAY:
62                 return 0x06;
63             default:
64                 throw new IllegalArgumentException("Day " + dayOfWeek + " is not a valid weekday.");
65         }
66     }
67
68     @Override
69     protected byte[] getDataBytes() {
70         return new byte[] { COMMAND_SET_REALTIME_CLOCK, this.getWeekDay(), getHour(), getMinute() };
71     }
72 }