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_01;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enocean.internal.config.EnOceanChannelDimmerConfig;
22 import org.openhab.binding.enocean.internal.eep.Base._VLDMessage;
23 import org.openhab.binding.enocean.internal.eep.EEPHelper;
24 import org.openhab.binding.enocean.internal.messages.ERP1Message;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.UnDefType;
36 import org.openhab.core.util.HexUtils;
40 * @author Daniel Weber - Initial contribution
43 public abstract class D2_01 extends _VLDMessage {
45 protected static final byte CMD_MASK = 0x0f;
46 protected static final byte OUTPUT_VALUE_MASK = 0x7f;
47 protected static final byte OUTPUT_CHANNEL_MASK = 0x1f;
49 protected static final byte CMD_ACTUATOR_SET_STATUS = 0x01;
50 protected static final byte CMD_ACTUATOR_STATUS_QUERY = 0x03;
51 protected static final byte CMD_ACTUATOR_STATUS_RESPONE = 0x04;
52 protected static final byte CMD_ACTUATOR_MEASUREMENT_QUERY = 0x06;
53 protected static final byte CMD_ACTUATOR_MEASUREMENT_RESPONE = 0x07;
55 protected static final byte ALL_CHANNELS_MASK = 0x1e;
56 protected static final byte CHANNEL_A_MASK = 0x00;
57 protected static final byte CHANNEL_B_MASK = 0x01;
59 protected static final byte STATUS_SWITCHING_ON = 0x01;
60 protected static final byte STATUS_SWITCHING_OFF = 0x00;
61 protected static final byte STATUS_DIMMING_100 = 0x64;
67 public D2_01(ERP1Message packet) {
71 protected byte getCMD() {
72 return (byte) (bytes[0] & CMD_MASK);
75 protected void setSwitchingData(OnOffType command, byte outputChannel) {
76 if (command == OnOffType.ON) {
77 setData(CMD_ACTUATOR_SET_STATUS, outputChannel, STATUS_SWITCHING_ON);
79 setData(CMD_ACTUATOR_SET_STATUS, outputChannel, STATUS_SWITCHING_OFF);
83 protected void setSwitchingQueryData(byte outputChannel) {
84 setData(CMD_ACTUATOR_STATUS_QUERY, outputChannel);
87 protected State getSwitchingData() {
88 if (getCMD() == CMD_ACTUATOR_STATUS_RESPONE) {
89 return OnOffType.from((bytes[bytes.length - 1] & OUTPUT_VALUE_MASK) != STATUS_SWITCHING_OFF);
92 return UnDefType.UNDEF;
95 protected byte getChannel() {
96 return (byte) (bytes[1] & OUTPUT_CHANNEL_MASK);
99 protected State getSwitchingData(byte channel) {
100 if (getCMD() == CMD_ACTUATOR_STATUS_RESPONE && (getChannel() == channel || getChannel() == ALL_CHANNELS_MASK)) {
101 return OnOffType.from((bytes[bytes.length - 1] & OUTPUT_VALUE_MASK) != STATUS_SWITCHING_OFF);
104 return UnDefType.UNDEF;
107 protected void setDimmingData(Command command, byte outputChannel, Configuration config) {
110 if (command instanceof DecimalType decimalCommand) {
111 if (decimalCommand.equals(DecimalType.ZERO)) {
112 outputValue = STATUS_SWITCHING_OFF;
114 outputValue = decimalCommand.byteValue();
116 } else if ((OnOffType) command == OnOffType.ON) {
117 outputValue = STATUS_DIMMING_100;
119 outputValue = STATUS_SWITCHING_OFF;
122 EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
123 byte rampingTime = Integer.valueOf(c.rampingTime).byteValue();
125 setData(CMD_ACTUATOR_SET_STATUS, (byte) ((rampingTime << 5) | outputChannel), outputValue);
128 protected State getDimmingData() {
129 if (getCMD() == CMD_ACTUATOR_STATUS_RESPONE) {
130 return new PercentType((bytes[bytes.length - 1] & OUTPUT_VALUE_MASK));
133 return UnDefType.UNDEF;
136 protected void setEnergyMeasurementQueryData(byte outputChannel) {
137 setData(CMD_ACTUATOR_MEASUREMENT_QUERY, outputChannel);
140 protected void setPowerMeasurementQueryData(byte outputChannel) {
141 setData(CMD_ACTUATOR_MEASUREMENT_QUERY, (byte) (0x20 | outputChannel));
144 protected State getEnergyMeasurementData() {
145 if (getCMD() == CMD_ACTUATOR_MEASUREMENT_RESPONE) {
148 switch (bytes[1] >>> 5) {
149 case 0: // value is given as watt seconds, so divide it by 3600 to get watt hours, and 1000 to get
151 factor /= (3600 * 1000);
153 case 1: // value is given as watt hours, so divide it by 1000 to get kilowatt hours
156 case 2: // value is given as kilowatt hours
160 return UnDefType.UNDEF;
163 float energy = Long.parseLong(HexUtils.bytesToHex(new byte[] { bytes[2], bytes[3], bytes[4], bytes[5] }),
165 return new QuantityType<>(energy, Units.KILOWATT_HOUR);
168 return UnDefType.UNDEF;
171 protected State getPowerMeasurementData() {
172 if (getCMD() == CMD_ACTUATOR_MEASUREMENT_RESPONE) {
175 switch (bytes[1] >>> 5) {
176 case 3: // value is given as watt
179 case 4: // value is given as kilowatt
183 return UnDefType.UNDEF;
186 float power = Long.parseLong(HexUtils.bytesToHex(new byte[] { bytes[2], bytes[3], bytes[4], bytes[5] }), 16)
189 return new QuantityType<>(power, Units.WATT);
192 return UnDefType.UNDEF;
196 public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
197 discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
198 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
202 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
203 Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
204 if (channelId.equals(CHANNEL_GENERAL_SWITCHING)) {
205 if (command == RefreshType.REFRESH) {
206 setSwitchingQueryData(ALL_CHANNELS_MASK);
208 setSwitchingData((OnOffType) command, ALL_CHANNELS_MASK);
210 } else if (channelId.equals(CHANNEL_GENERAL_SWITCHINGA)) {
211 if (command == RefreshType.REFRESH) {
212 setSwitchingQueryData(CHANNEL_A_MASK);
214 setSwitchingData((OnOffType) command, CHANNEL_A_MASK);
216 } else if (channelId.equals(CHANNEL_GENERAL_SWITCHINGB)) {
217 if (command == RefreshType.REFRESH) {
218 setSwitchingQueryData(CHANNEL_B_MASK);
220 setSwitchingData((OnOffType) command, CHANNEL_B_MASK);
222 } else if (channelId.equals(CHANNEL_DIMMER)) {
223 if (command == RefreshType.REFRESH) {
224 setSwitchingQueryData(ALL_CHANNELS_MASK);
226 if (config != null) {
227 setDimmingData(command, ALL_CHANNELS_MASK, config);
229 logger.error("Cannot set dimming data when config is null");
232 } else if (channelId.equals(CHANNEL_INSTANTPOWER) && command == RefreshType.REFRESH) {
233 setPowerMeasurementQueryData(ALL_CHANNELS_MASK);
234 } else if (channelId.equals(CHANNEL_TOTALUSAGE) && command == RefreshType.REFRESH) {
235 setEnergyMeasurementQueryData(ALL_CHANNELS_MASK);
240 protected State convertToStateImpl(String channelId, String channelTypeId,
241 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
243 case CHANNEL_GENERAL_SWITCHING:
244 return getSwitchingData();
245 case CHANNEL_GENERAL_SWITCHINGA:
246 return getSwitchingData(CHANNEL_A_MASK);
247 case CHANNEL_GENERAL_SWITCHINGB:
248 return getSwitchingData(CHANNEL_B_MASK);
250 return getDimmingData();
251 case CHANNEL_INSTANTPOWER:
252 return getPowerMeasurementData();
253 case CHANNEL_TOTALUSAGE:
254 State value = getEnergyMeasurementData();
255 State currentState = getCurrentStateFunc.apply(channelId);
256 return EEPHelper.validateTotalUsage(value, currentState, config);
259 return UnDefType.UNDEF;