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.A5_11;
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.eep.Base._4BSMessage;
22 import org.openhab.binding.enocean.internal.eep.EEPHelper;
23 import org.openhab.binding.enocean.internal.messages.ERP1Message;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
32 import org.openhab.core.util.HexUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
38 * @author Vincent Bakker - Initial contribution
41 public class A5_11_04 extends _4BSMessage {
47 FAILURE_ON_THE_EXTERNAL_PERIPHERY
50 private enum ParameterMode {
51 EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS,
53 ENERGY_METERING_VALUE,
57 public enum EnergyUnit {
69 private Logger logger = LoggerFactory.getLogger(A5_11_04.class);
71 public A5_11_04(ERP1Message packet) {
75 protected boolean isErrorState() {
78 int state = (db0 >> 4) & 0x03;
81 // TODO: display error state on thing
82 logger.warn("Received error {}: {}", state, Error.values()[state]);
89 protected ParameterMode getParameterMode() {
90 int pm = (getDB0() >> 1) & 0x03;
91 return ParameterMode.values()[pm];
94 protected EnergyUnit getEnergyUnit() {
97 return EnergyUnit.values()[unit];
100 return EnergyUnit.NOT_SUPPORTED;
103 protected State getLightingStatus() {
105 boolean lightOn = getBit(db0, 0);
107 return OnOffType.from(lightOn);
110 protected State getDimmerStatus() {
111 if (getParameterMode() == ParameterMode.EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS) {
112 return new PercentType(getDB3Value() * 100 / 255);
114 return UnDefType.UNDEF;
117 protected State getEnergyMeasurementData() {
118 if (getParameterMode() == ParameterMode.ENERGY_METERING_VALUE) {
119 EnergyUnit unit = getEnergyUnit();
135 return UnDefType.UNDEF;
138 return new QuantityType<>(
139 Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB3(), getDB2() }), 16) * factor,
140 Units.KILOWATT_HOUR);
143 return UnDefType.UNDEF;
146 protected State getPowerMeasurementData() {
147 if (getParameterMode() == ParameterMode.ENERGY_METERING_VALUE) {
148 EnergyUnit unit = getEnergyUnit();
164 return UnDefType.UNDEF;
167 return new QuantityType<>(
168 Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB3(), getDB2() }), 16) * factor, Units.WATT);
171 return UnDefType.UNDEF;
174 protected State getOperatingHours() {
175 if (getParameterMode() == ParameterMode.EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS) {
176 return new DecimalType(getDB2Value() << 8 + getDB1Value());
179 return UnDefType.UNDEF;
183 protected State convertToStateImpl(String channelId, String channelTypeId,
184 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
185 if (isErrorState()) {
186 return UnDefType.UNDEF;
190 case CHANNEL_GENERAL_SWITCHING:
191 return getLightingStatus();
193 return getDimmerStatus();
194 case CHANNEL_INSTANTPOWER:
195 return getPowerMeasurementData();
196 case CHANNEL_TOTALUSAGE:
197 State value = getEnergyMeasurementData();
198 State currentState = getCurrentStateFunc.apply(channelId);
199 return EEPHelper.validateTotalUsage(value, currentState, config);
200 case CHANNEL_COUNTER:
201 return getOperatingHours();
204 return UnDefType.UNDEF;