]> git.basschouten.com Git - openhab-addons.git/blob
ee12d418e843d4d6e32b62a8d00ca60286fadc2f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.enocean.internal.eep.D2_50;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.function.Function;
18
19 import org.openhab.binding.enocean.internal.Helper;
20 import org.openhab.binding.enocean.internal.eep.Base._VLDMessage;
21 import org.openhab.binding.enocean.internal.messages.ERP1Message;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.RefreshType;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
35
36 /**
37  *
38  * @author Daniel Weber - Initial contribution
39  */
40 public class D2_50 extends _VLDMessage {
41
42     protected static final byte mtMask = (byte) 0xf0;
43     protected static final byte MT_REMOTE_TRANSMISSION_REQUEST = 0x00;
44     protected static final byte MT_CONTROL = 0x20;
45     protected static final byte MT_BASIC_STATUS = 0x40;
46     protected static final byte MT_EXTENDED_STATUS = 0x60; // not yet implemented
47
48     protected static final byte rmtMask = (byte) 0x0f;
49     protected static final byte RMT_BASIC_STATUS = 0x00;
50     protected static final byte RMT_EXTENDED_STATUS = 0x01; // not yet implemented
51
52     protected static final byte DOMC_NOACTION = 0x0f;
53     protected static final byte CONTROL_NOACTION = 0;
54     protected static final byte TMOC_NOACTION = 127;
55     protected static final byte TMOC_ACTIVATE = (byte) 0xff;
56     protected static final byte THRESHOLD_NOACTION = 127;
57
58     public D2_50() {
59         super();
60     }
61
62     public D2_50(ERP1Message packet) {
63         super(packet);
64     }
65
66     protected byte getMessageType(byte b) {
67         return (byte) (b & mtMask);
68     }
69
70     @Override
71     public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
72         discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
73                 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
74     }
75
76     @Override
77     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
78             Function<String, State> getCurrentStateFunc, Configuration config) {
79
80         // we need to send just a single message to refresh all channel states, hence just send refresh for OM
81         if (command == RefreshType.REFRESH && CHANNEL_VENTILATIONOPERATIONMODE.equals(channelId)) {
82             setData((byte) (MT_REMOTE_TRANSMISSION_REQUEST + RMT_BASIC_STATUS));
83         } else {
84             switch (channelId) {
85                 case CHANNEL_VENTILATIONOPERATIONMODE:
86                     if (command instanceof StringType) {
87                         byte value = (byte) (Helper.tryParseInt(((StringType) command).toString(), 15) & 0x0f);
88                         setData((byte) (MT_CONTROL + value), CONTROL_NOACTION, TMOC_NOACTION, THRESHOLD_NOACTION,
89                                 THRESHOLD_NOACTION, CONTROL_NOACTION);
90                     }
91                     break;
92                 case CHANNEL_TIMEROPERATIONMODE:
93                     if (command instanceof OnOffType) {
94                         byte value = (OnOffType) command == OnOffType.ON ? TMOC_ACTIVATE : TMOC_NOACTION;
95                         setData((byte) (MT_CONTROL + DOMC_NOACTION), CONTROL_NOACTION, value, THRESHOLD_NOACTION,
96                                 THRESHOLD_NOACTION, CONTROL_NOACTION);
97                     }
98                     break;
99             }
100         }
101     }
102
103     @Override
104     protected State convertToStateImpl(String channelId, String channelTypeId,
105             Function<String, State> getCurrentStateFunc, Configuration config) {
106
107         if (getMessageType(bytes[0]) != MT_BASIC_STATUS) {
108             return UnDefType.UNDEF;
109         }
110
111         switch (channelId) {
112             case CHANNEL_VENTILATIONOPERATIONMODE:
113                 return new StringType(String.valueOf(bytes[0] & 0x0f));
114             case CHANNEL_FIREPLACESAFETYMODE:
115                 return OnOffType.from(getBit(bytes[1], 3));
116             case CHANNEL_HEATEXCHANGERBYPASSSTATUS:
117                 return getBit(bytes[1], 2) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
118             case CHANNEL_SUPPLYAIRFLAPSTATUS:
119                 return getBit(bytes[1], 1) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
120             case CHANNEL_EXHAUSTAIRFLAPSTATUS:
121                 return getBit(bytes[1], 0) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
122             case CHANNEL_DEFROSTMODE:
123                 return OnOffType.from(getBit(bytes[2], 7));
124             case CHANNEL_COOLINGPROTECTIONMODE:
125                 return OnOffType.from(getBit(bytes[2], 6));
126             case CHANNEL_OUTDOORAIRHEATERSTATUS:
127                 return OnOffType.from(getBit(bytes[2], 5));
128             case CHANNEL_SUPPLYAIRHEATERSTATUS:
129                 return OnOffType.from(getBit(bytes[2], 4));
130             case CHANNEL_DRAINHEATERSTATUS:
131                 return OnOffType.from(getBit(bytes[2], 3));
132             case CHANNEL_TIMEROPERATIONMODE:
133                 return OnOffType.from(getBit(bytes[2], 2));
134             case CHANNEL_MAINTENANCESTATUS:
135                 return OnOffType.from(getBit(bytes[2], 1));
136             case CHANNEL_WEEKLYTIMERPROGRAMSTATUS:
137                 return OnOffType.from(getBit(bytes[2], 0));
138             case CHANNEL_ROOMTEMPERATURECONTROLSTATUS:
139                 return OnOffType.from(getBit(bytes[3], 7));
140             case CHANNEL_AIRQUALITYVALUE1:
141                 return new QuantityType<>((bytes[3] & 0x7f), Units.PERCENT);
142             case CHANNEL_AIRQUALITYVALUE2:
143                 return new QuantityType<>((bytes[4] & 0x7f), Units.PERCENT);
144             case CHANNEL_OUTDOORAIRTEMPERATURE:
145                 return new QuantityType<>(-63 + (bytes[5] >>> 1), SIUnits.CELSIUS);
146             case CHANNEL_SUPPLYAIRTEMPERATURE:
147                 return new QuantityType<>(-63 + (bytes[6] >>> 2) + ((bytes[5] & 1) << 6), SIUnits.CELSIUS);
148             case CHANNEL_INDOORAIRTEMPERATURE:
149                 return new QuantityType<>(-63 + (bytes[7] >>> 3) + ((bytes[6] & 0b11) << 5), SIUnits.CELSIUS);
150             case CHANNEL_EXHAUSTAIRTEMPERATURE:
151                 return new QuantityType<>(-63 + (bytes[8] >>> 4) + ((bytes[7] & 0b111) << 4), SIUnits.CELSIUS);
152             case CHANNEL_SUPPLYAIRFANAIRFLOWRATE:
153                 return new QuantityType<>((bytes[9] >>> 2) + ((bytes[8] & 0b1111) << 6), Units.CUBICMETRE_PER_MINUTE);
154             case CHANNEL_EXHAUSTAIRFANAIRFLOWRATE:
155                 return new QuantityType<>((bytes[10] & 0xff) + ((bytes[9] & 0b11) << 8), Units.CUBICMETRE_PER_MINUTE);
156             case CHANNEL_SUPPLYFANSPEED:
157                 return new DecimalType((bytes[12] >>> 4) + (bytes[11] << 4));
158             case CHANNEL_EXHAUSTFANSPEED:
159                 return new DecimalType((bytes[13] & 0xff) + ((bytes[12] & 0b1111) << 8));
160         }
161
162         return UnDefType.UNDEF;
163     }
164
165     @Override
166     protected boolean validateData(byte[] bytes) {
167         if (bytes.length == 0) {
168             return false;
169         }
170
171         switch (getMessageType(bytes[0])) {
172             case MT_REMOTE_TRANSMISSION_REQUEST:
173                 return bytes.length == 1;
174             case MT_CONTROL:
175                 return bytes.length == 6;
176             case MT_BASIC_STATUS:
177                 return bytes.length == 14;
178             case MT_EXTENDED_STATUS: // MT_EXTENDED_STATUS is not yet supported, however return true to avoid Exceptions
179                 return true;
180             default:
181                 return false;
182         }
183     }
184 }