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