]> git.basschouten.com Git - openhab-addons.git/blob
d197a4e00b1f050d5dbe4827b9a47a5d79671899
[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.rfxcom.internal.messages;
14
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_DATE_TIME;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
17
18 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
23 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
24 import org.openhab.core.library.types.DateTimeType;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.Type;
27
28 /**
29  * RFXCOM data class for Date and Time message.
30  *
31  * @author Damien Servant - Initial contribution
32  */
33 public class RFXComDateTimeMessage extends RFXComBatteryDeviceMessage<RFXComDateTimeMessage.SubType> {
34
35     public enum SubType implements ByteEnumWrapper {
36         RTGR328N(1);
37
38         private final int subType;
39
40         SubType(int subType) {
41             this.subType = subType;
42         }
43
44         @Override
45         public byte toByte() {
46             return (byte) subType;
47         }
48     }
49
50     public SubType subType;
51     public int sensorId;
52     String dateTime;
53     private int year;
54     private int month;
55     private int day;
56     private int dayOfWeek;
57     private int hour;
58     private int minute;
59     private int second;
60
61     public RFXComDateTimeMessage() {
62         super(PacketType.DATE_TIME);
63     }
64
65     public RFXComDateTimeMessage(byte[] data) throws RFXComException {
66         encodeMessage(data);
67     }
68
69     @Override
70     public String toString() {
71         String str = super.toString();
72
73         str += ", Sub type = " + subType;
74         str += ", Id = " + sensorId;
75         str += ", Date Time = " + dateTime;
76         str += ", Signal level = " + signalLevel;
77         str += ", Battery level = " + batteryLevel;
78
79         return str;
80     }
81
82     @Override
83     public void encodeMessage(byte[] data) throws RFXComException {
84         super.encodeMessage(data);
85
86         subType = fromByte(SubType.class, super.subType);
87
88         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
89
90         year = data[6] & 0xFF;
91         month = data[7] & 0xFF;
92         day = data[8] & 0xFF;
93         dayOfWeek = data[9] & 0xFF;
94         hour = data[10] & 0xFF;
95         minute = data[11] & 0xFF;
96         second = data[12] & 0xFF;
97
98         dateTime = String.format("20%02d-%02d-%02dT%02d:%02d:%02d", year, month, day, hour, minute, second);
99
100         signalLevel = (byte) ((data[13] & 0xF0) >> 4);
101         batteryLevel = (byte) (data[13] & 0x0F);
102     }
103
104     @Override
105     public byte[] decodeMessage() {
106         byte[] data = new byte[14];
107
108         data[0] = (byte) (data.length - 1);
109         data[1] = RFXComBaseMessage.PacketType.DATE_TIME.toByte();
110         data[2] = subType.toByte();
111         data[3] = seqNbr;
112         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
113         data[5] = (byte) (sensorId & 0x00FF);
114         data[6] = (byte) (year & 0x00FF);
115         data[7] = (byte) (month & 0x00FF);
116         data[8] = (byte) (day & 0x00FF);
117         data[9] = (byte) (dayOfWeek & 0x00FF);
118         data[10] = (byte) (hour & 0x00FF);
119         data[11] = (byte) (minute & 0x00FF);
120         data[12] = (byte) (second & 0x00FF);
121         data[13] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
122
123         return data;
124     }
125
126     @Override
127     public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
128             throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
129         if (channelId.equals(CHANNEL_DATE_TIME)) {
130             return new DateTimeType(dateTime);
131         } else {
132             return super.convertToState(channelId, config, deviceState);
133         }
134     }
135
136     @Override
137     public void convertFromState(String channelId, Type type) {
138         throw new UnsupportedOperationException();
139     }
140
141     @Override
142     public String getDeviceId() {
143         return String.valueOf(sensorId);
144     }
145
146     @Override
147     public void setDeviceId(String deviceId) {
148         throw new UnsupportedOperationException();
149     }
150
151     @Override
152     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
153         return ByteEnumUtil.convertSubType(SubType.class, subType);
154     }
155
156     @Override
157     public void setSubType(SubType subType) {
158         this.subType = subType;
159     }
160 }