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