]> git.basschouten.com Git - openhab-addons.git/blob
5bc4477b16712df8e556dcb96adb970acf51be3a
[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.DecimalType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
26 import org.openhab.core.types.UnDefType;
27
28 /**
29  * RFXCOM data class for thermostat1 message.
30  * Digimax 210 Thermostat RF sensor operational
31  *
32  * @author Les Ashworth - Initial contribution
33  * @author Pauli Anttila - Migrated for OH2
34  */
35 public class RFXComThermostat1Message extends RFXComDeviceMessageImpl<RFXComThermostat1Message.SubType> {
36
37     public enum SubType implements ByteEnumWrapper {
38         DIGIMAX(0),
39         DIGIMAX_SHORT(1);
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     /* Added item for ContactTypes */
54     public enum Status implements ByteEnumWrapper {
55         NO_STATUS(0),
56         DEMAND(1),
57         NO_DEMAND(2),
58         INITIALIZING(3);
59
60         private final int status;
61
62         Status(int status) {
63             this.status = status;
64         }
65
66         @Override
67         public byte toByte() {
68             return (byte) status;
69         }
70     }
71
72     /* Operating mode */
73     public enum Mode implements ByteEnumWrapper {
74         HEATING(0),
75         COOLING(1);
76
77         private final int mode;
78
79         Mode(int mode) {
80             this.mode = mode;
81         }
82
83         @Override
84         public byte toByte() {
85             return (byte) mode;
86         }
87     }
88
89     public SubType subType;
90     public int sensorId;
91     public byte temperature;
92     public byte set;
93     public Mode mode;
94     public Status status;
95
96     public RFXComThermostat1Message() {
97         super(PacketType.THERMOSTAT1);
98     }
99
100     public RFXComThermostat1Message(byte[] data) throws RFXComException {
101         encodeMessage(data);
102     }
103
104     @Override
105     public String toString() {
106         String str = "";
107
108         str += super.toString();
109         str += ", Sub type = " + subType;
110         str += ", Device Id = " + getDeviceId();
111         str += ", Temperature = " + temperature;
112         str += ", Set = " + set;
113         str += ", Mode = " + mode;
114         str += ", Status = " + status;
115         str += ", Signal level = " + signalLevel;
116
117         return str;
118     }
119
120     @Override
121     public void encodeMessage(byte[] data) throws RFXComException {
122         super.encodeMessage(data);
123
124         subType = fromByte(SubType.class, super.subType);
125         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
126         temperature = data[6];
127         set = data[7];
128         mode = fromByte(Mode.class, (data[8] & 0xF0) >> 7);
129
130         status = fromByte(Status.class, data[8] & 0x03);
131         signalLevel = (byte) ((data[9] & 0xF0) >> 4);
132     }
133
134     @Override
135     public byte[] decodeMessage() {
136         byte[] data = new byte[10];
137
138         data[0] = 0x09;
139         data[1] = RFXComBaseMessage.PacketType.THERMOSTAT1.toByte();
140         data[2] = subType.toByte();
141         data[3] = seqNbr;
142         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
143         data[5] = (byte) (sensorId & 0x00FF);
144         data[6] = (temperature);
145         data[7] = (set);
146         data[8] = (byte) ((mode.toByte() << 7) | (status.toByte() & 0xFF));
147         data[9] = (byte) (signalLevel << 4);
148
149         return data;
150     }
151
152     @Override
153     public String getDeviceId() {
154         return String.valueOf(sensorId);
155     }
156
157     @Override
158     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
159         switch (channelId) {
160             case CHANNEL_TEMPERATURE:
161                 return new DecimalType(temperature);
162
163             case CHANNEL_SET_POINT:
164                 return new DecimalType(set);
165
166             case CHANNEL_CONTACT:
167                 switch (status) {
168                     case DEMAND:
169                         return OpenClosedType.CLOSED;
170                     case NO_DEMAND:
171                         return OpenClosedType.OPEN;
172                     default:
173                         return UnDefType.UNDEF;
174                 }
175
176             default:
177                 return super.convertToState(channelId, deviceState);
178         }
179     }
180
181     @Override
182     public void setSubType(SubType subType) {
183         throw new UnsupportedOperationException();
184     }
185
186     @Override
187     public void setDeviceId(String deviceId) {
188         throw new UnsupportedOperationException();
189     }
190
191     @Override
192     public void convertFromState(String channelId, Type type) {
193         throw new UnsupportedOperationException();
194     }
195
196     @Override
197     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
198         return ByteEnumUtil.convertSubType(SubType.class, subType);
199     }
200 }