]> git.basschouten.com Git - openhab-addons.git/blob
75875c2433bf06957ee6a300b73235f24b284ca3
[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.*;
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.OpenClosedType;
25 import org.openhab.core.library.types.StopMoveType;
26 import org.openhab.core.library.types.UpDownType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.Type;
29
30 /**
31  * RFXCOM data class for curtain1 message. See Harrison.
32  *
33  * @author Evert van Es - Initial contribution
34  * @author Pauli Anttila - Migrated to OH2
35  */
36 public class RFXComCurtain1Message extends RFXComBatteryDeviceMessage<RFXComCurtain1Message.SubType> {
37
38     public enum SubType implements ByteEnumWrapper {
39         HARRISON(0);
40
41         private final int subType;
42
43         SubType(int subType) {
44             this.subType = subType;
45         }
46
47         @Override
48         public byte toByte() {
49             return (byte) subType;
50         }
51     }
52
53     public enum Commands implements ByteEnumWrapper {
54         OPEN(0),
55         CLOSE(1),
56         STOP(2),
57         PROGRAM(3);
58
59         private final int command;
60
61         Commands(int command) {
62             this.command = command;
63         }
64
65         @Override
66         public byte toByte() {
67             return (byte) command;
68         }
69     }
70
71     public SubType subType;
72     public char sensorId;
73     public byte unitCode;
74     public Commands command;
75
76     public RFXComCurtain1Message() {
77         super(PacketType.CURTAIN1);
78     }
79
80     public RFXComCurtain1Message(byte[] data) throws RFXComException {
81         encodeMessage(data);
82     }
83
84     @Override
85     public String toString() {
86         String str = "";
87
88         str += super.toString();
89         str += ", Sub type = " + subType;
90         str += ", Device Id = " + getDeviceId();
91         str += ", Command = " + command;
92         str += ", Signal level = " + signalLevel;
93         str += ", Battery level = " + batteryLevel;
94
95         return str;
96     }
97
98     @Override
99     public void encodeMessage(byte[] data) throws RFXComException {
100         super.encodeMessage(data);
101
102         subType = fromByte(SubType.class, super.subType);
103         sensorId = (char) data[4];
104         unitCode = data[5];
105         command = fromByte(Commands.class, data[6]);
106
107         signalLevel = (byte) ((data[7] & 0xF0) >> 4);
108         batteryLevel = (byte) ((data[7] & 0x0F));
109     }
110
111     @Override
112     public byte[] decodeMessage() {
113         // Example data 07 18 00 00 65 01 00 00
114         // 07 18 00 00 65 02 00 00
115
116         byte[] data = new byte[8];
117
118         data[0] = 0x07;
119         data[1] = 0x18;
120         data[2] = subType.toByte();
121         data[3] = seqNbr;
122         data[4] = (byte) sensorId;
123         data[5] = unitCode;
124         data[6] = command.toByte();
125         data[7] = (byte) (((signalLevel & 0x0F) << 4) + batteryLevel);
126
127         return data;
128     }
129
130     @Override
131     public String getDeviceId() {
132         return sensorId + ID_DELIMITER + unitCode;
133     }
134
135     @Override
136     public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
137             throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
138         if (channelId.equals(CHANNEL_COMMAND)) {
139             return (command == Commands.CLOSE ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
140         } else {
141             return super.convertToState(channelId, config, deviceState);
142         }
143     }
144
145     @Override
146     public void setSubType(SubType subType) {
147         this.subType = subType;
148     }
149
150     @Override
151     public void setDeviceId(String deviceId) throws RFXComException {
152         String[] ids = deviceId.split("\\" + ID_DELIMITER);
153         if (ids.length != 2) {
154             throw new RFXComException("Invalid device id '" + deviceId + "'");
155         }
156
157         sensorId = ids[0].charAt(0);
158         unitCode = Byte.parseByte(ids[1]);
159     }
160
161     @Override
162     public void convertFromState(String channelId, Type type) throws RFXComUnsupportedChannelException {
163         if (channelId.equals(CHANNEL_SHUTTER)) {
164             if (type instanceof OpenClosedType) {
165                 command = (type == OpenClosedType.CLOSED ? Commands.CLOSE : Commands.OPEN);
166             } else if (type instanceof UpDownType) {
167                 command = (type == UpDownType.UP ? Commands.CLOSE : Commands.OPEN);
168             } else if (type instanceof StopMoveType) {
169                 command = Commands.STOP;
170             } else {
171                 throw new RFXComUnsupportedChannelException("Channel " + channelId + " does not accept " + type);
172             }
173         } else {
174             throw new RFXComUnsupportedChannelException("Channel " + channelId + " is not relevant here");
175         }
176     }
177
178     @Override
179     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
180         return ByteEnumUtil.convertSubType(SubType.class, subType);
181     }
182 }