]> git.basschouten.com Git - openhab-addons.git/blob
fedbfe35fdcc425aa69ef37668b827207e2c88e8
[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.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     protected static final byte MT_UNKNOWN_STATUS = (byte) 0x80; // Sent by some systems during teach in
48
49     protected static final byte rmtMask = (byte) 0x0f;
50     protected static final byte RMT_BASIC_STATUS = 0x00;
51     protected static final byte RMT_EXTENDED_STATUS = 0x01; // not yet implemented
52
53     protected static final byte DOMC_NOACTION = 0x0f;
54     protected static final byte CONTROL_NOACTION = 0;
55     protected static final byte TMOC_NOACTION = 127;
56     protected static final byte TMOC_ACTIVATE = (byte) 0xff;
57     protected static final byte THRESHOLD_NOACTION = 127;
58
59     public D2_50() {
60         super();
61     }
62
63     public D2_50(ERP1Message packet) {
64         super(packet);
65     }
66
67     protected byte getMessageType(byte b) {
68         return (byte) (b & mtMask);
69     }
70
71     @Override
72     public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
73         discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
74                 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
75     }
76
77     @Override
78     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
79             Function<String, State> getCurrentStateFunc, Configuration config) {
80
81         // we need to send just a single message to refresh all channel states, hence just send refresh for OM
82         if (command == RefreshType.REFRESH && CHANNEL_VENTILATIONOPERATIONMODE.equals(channelId)) {
83             setData((byte) (MT_REMOTE_TRANSMISSION_REQUEST + RMT_BASIC_STATUS));
84         } else {
85             switch (channelId) {
86                 case CHANNEL_VENTILATIONOPERATIONMODE:
87                     if (command instanceof StringType) {
88                         byte value = (byte) (Helper.tryParseInt(((StringType) command).toString(), 15) & 0x0f);
89                         setData((byte) (MT_CONTROL + value), CONTROL_NOACTION, TMOC_NOACTION, THRESHOLD_NOACTION,
90                                 THRESHOLD_NOACTION, CONTROL_NOACTION);
91                     }
92                     break;
93                 case CHANNEL_TIMEROPERATIONMODE:
94                     if (command instanceof OnOffType) {
95                         byte value = (OnOffType) command == OnOffType.ON ? TMOC_ACTIVATE : TMOC_NOACTION;
96                         setData((byte) (MT_CONTROL + DOMC_NOACTION), CONTROL_NOACTION, value, THRESHOLD_NOACTION,
97                                 THRESHOLD_NOACTION, CONTROL_NOACTION);
98                     }
99                     break;
100             }
101         }
102     }
103
104     @Override
105     protected State convertToStateImpl(String channelId, String channelTypeId,
106             Function<String, State> getCurrentStateFunc, Configuration config) {
107
108         if (getMessageType(bytes[0]) != MT_BASIC_STATUS) {
109             return UnDefType.UNDEF;
110         }
111
112         switch (channelId) {
113             case CHANNEL_VENTILATIONOPERATIONMODE:
114                 return new StringType(String.valueOf(bytes[0] & 0x0f));
115             case CHANNEL_FIREPLACESAFETYMODE:
116                 return OnOffType.from(getBit(bytes[1], 3));
117             case CHANNEL_HEATEXCHANGERBYPASSSTATUS:
118                 return getBit(bytes[1], 2) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
119             case CHANNEL_SUPPLYAIRFLAPSTATUS:
120                 return getBit(bytes[1], 1) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
121             case CHANNEL_EXHAUSTAIRFLAPSTATUS:
122                 return getBit(bytes[1], 0) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
123             case CHANNEL_DEFROSTMODE:
124                 return OnOffType.from(getBit(bytes[2], 7));
125             case CHANNEL_COOLINGPROTECTIONMODE:
126                 return OnOffType.from(getBit(bytes[2], 6));
127             case CHANNEL_OUTDOORAIRHEATERSTATUS:
128                 return OnOffType.from(getBit(bytes[2], 5));
129             case CHANNEL_SUPPLYAIRHEATERSTATUS:
130                 return OnOffType.from(getBit(bytes[2], 4));
131             case CHANNEL_DRAINHEATERSTATUS:
132                 return OnOffType.from(getBit(bytes[2], 3));
133             case CHANNEL_TIMEROPERATIONMODE:
134                 return OnOffType.from(getBit(bytes[2], 2));
135             case CHANNEL_MAINTENANCESTATUS:
136                 return OnOffType.from(getBit(bytes[2], 1));
137             case CHANNEL_WEEKLYTIMERPROGRAMSTATUS:
138                 return OnOffType.from(getBit(bytes[2], 0));
139             case CHANNEL_ROOMTEMPERATURECONTROLSTATUS:
140                 return OnOffType.from(getBit(bytes[3], 7));
141             case CHANNEL_AIRQUALITYVALUE1:
142                 return new QuantityType<>((bytes[3] & 0x7f), Units.PERCENT);
143             case CHANNEL_AIRQUALITYVALUE2:
144                 return new QuantityType<>((bytes[4] & 0x7f), Units.PERCENT);
145             case CHANNEL_OUTDOORAIRTEMPERATURE:
146                 return new QuantityType<>(-63 + (bytes[5] >>> 1), SIUnits.CELSIUS);
147             case CHANNEL_SUPPLYAIRTEMPERATURE:
148                 return new QuantityType<>(-63 + (bytes[6] >>> 2) + ((bytes[5] & 1) << 6), SIUnits.CELSIUS);
149             case CHANNEL_INDOORAIRTEMPERATURE:
150                 return new QuantityType<>(-63 + (bytes[7] >>> 3) + ((bytes[6] & 0b11) << 5), SIUnits.CELSIUS);
151             case CHANNEL_EXHAUSTAIRTEMPERATURE:
152                 return new QuantityType<>(-63 + (bytes[8] >>> 4) + ((bytes[7] & 0b111) << 4), SIUnits.CELSIUS);
153             case CHANNEL_SUPPLYAIRFANAIRFLOWRATE:
154                 return new QuantityType<>((bytes[9] >>> 2) + ((bytes[8] & 0b1111) << 6), Units.CUBICMETRE_PER_MINUTE);
155             case CHANNEL_EXHAUSTAIRFANAIRFLOWRATE:
156                 return new QuantityType<>((bytes[10] & 0xff) + ((bytes[9] & 0b11) << 8), Units.CUBICMETRE_PER_MINUTE);
157             case CHANNEL_SUPPLYFANSPEED:
158                 return new DecimalType((bytes[12] >>> 4) + (bytes[11] << 4));
159             case CHANNEL_EXHAUSTFANSPEED:
160                 return new DecimalType((bytes[13] & 0xff) + ((bytes[12] & 0b1111) << 8));
161         }
162
163         return UnDefType.UNDEF;
164     }
165
166     @Override
167     protected boolean validateData(byte[] bytes) {
168         if (bytes.length == 0) {
169             return false;
170         }
171
172         switch (getMessageType(bytes[0])) {
173             case MT_REMOTE_TRANSMISSION_REQUEST:
174                 return bytes.length == 1;
175             case MT_CONTROL:
176                 return bytes.length == 6;
177             case MT_BASIC_STATUS:
178                 return bytes.length == 14;
179             case MT_EXTENDED_STATUS: // MT_EXTENDED_STATUS is not yet supported, however return true to avoid Exceptions
180                 return true;
181             case MT_UNKNOWN_STATUS:
182                 return true;
183             default:
184                 logger.error("Invalid data, unknown message type: {} ({})", getMessageType(bytes[0]), bytes);
185                 return false;
186         }
187     }
188 }