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.rfxcom.internal.messages;
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_DATE_TIME;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
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;
29 * RFXCOM data class for Date and Time message.
31 * @author Damien Servant - Initial contribution
33 public class RFXComDateTimeMessage extends RFXComBatteryDeviceMessage<RFXComDateTimeMessage.SubType> {
35 public enum SubType implements ByteEnumWrapper {
38 private final int subType;
40 SubType(int subType) {
41 this.subType = subType;
45 public byte toByte() {
46 return (byte) subType;
50 public SubType subType;
56 private int dayOfWeek;
61 public RFXComDateTimeMessage() {
62 super(PacketType.DATE_TIME);
65 public RFXComDateTimeMessage(byte[] data) throws RFXComException {
70 public String toString() {
71 String str = super.toString();
73 str += ", Sub type = " + subType;
74 str += ", Id = " + sensorId;
75 str += ", Date Time = " + dateTime;
76 str += ", Signal level = " + signalLevel;
77 str += ", Battery level = " + batteryLevel;
83 public void encodeMessage(byte[] data) throws RFXComException {
84 super.encodeMessage(data);
86 subType = fromByte(SubType.class, super.subType);
88 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
90 year = data[6] & 0xFF;
91 month = data[7] & 0xFF;
93 dayOfWeek = data[9] & 0xFF;
94 hour = data[10] & 0xFF;
95 minute = data[11] & 0xFF;
96 second = data[12] & 0xFF;
98 dateTime = String.format("20%02d-%02d-%02dT%02d:%02d:%02d", year, month, day, hour, minute, second);
100 signalLevel = (byte) ((data[13] & 0xF0) >> 4);
101 batteryLevel = (byte) (data[13] & 0x0F);
105 public byte[] decodeMessage() {
106 byte[] data = new byte[14];
108 data[0] = (byte) (data.length - 1);
109 data[1] = RFXComBaseMessage.PacketType.DATE_TIME.toByte();
110 data[2] = subType.toByte();
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));
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);
132 return super.convertToState(channelId, config, deviceState);
137 public void convertFromState(String channelId, Type type) {
138 throw new UnsupportedOperationException();
142 public String getDeviceId() {
143 return String.valueOf(sensorId);
147 public void setDeviceId(String deviceId) {
148 throw new UnsupportedOperationException();
152 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
153 return ByteEnumUtil.convertSubType(SubType.class, subType);
157 public void setSubType(SubType subType) {
158 this.subType = subType;