2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.enocean.internal.eep.D2_50;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.function.Function;
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;
38 * @author Daniel Weber - Initial contribution
40 public class D2_50 extends _VLDMessage {
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
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
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;
63 public D2_50(ERP1Message packet) {
67 protected byte getMessageType(byte b) {
68 return (byte) (b & mtMask);
72 public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
73 discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
74 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
78 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
79 Function<String, State> getCurrentStateFunc, Configuration config) {
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));
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);
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);
105 protected State convertToStateImpl(String channelId, String channelTypeId,
106 Function<String, State> getCurrentStateFunc, Configuration config) {
108 if (getMessageType(bytes[0]) != MT_BASIC_STATUS) {
109 return UnDefType.UNDEF;
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));
163 return UnDefType.UNDEF;
167 protected boolean validateData(byte[] bytes) {
168 if (bytes.length == 0) {
172 switch (getMessageType(bytes[0])) {
173 case MT_REMOTE_TRANSMISSION_REQUEST:
174 return bytes.length == 1;
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
181 case MT_UNKNOWN_STATUS:
184 logger.error("Invalid data, unknown message type: {} ({})", getMessageType(bytes[0]), bytes);