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.openhab.binding.enocean.internal.eep.Base._4BSMessage;
20 import org.openhab.binding.enocean.internal.eep.EEPHelper;
21 import org.openhab.binding.enocean.internal.messages.ERP1Message;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30 import org.openhab.core.util.HexUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
36 * @author Vincent Bakker - Initial contribution
39 public class A5_11_04 extends _4BSMessage {
45 FAILURE_ON_THE_EXTERNAL_PERIPHERY
48 private enum ParameterMode {
49 EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS,
51 ENERGY_METERING_VALUE,
55 public enum EnergyUnit {
67 private static Logger logger = LoggerFactory.getLogger(A5_11_04.class);
69 public A5_11_04(ERP1Message packet) {
73 protected boolean isErrorState() {
76 int state = (db0 >> 4) & 0x03;
79 // TODO: display error state on thing
80 logger.warn("Received error {}: {}", state, Error.values()[state]);
87 protected ParameterMode getParameterMode() {
88 int pm = (getDB_0() >> 1) & 0x03;
89 return ParameterMode.values()[pm];
92 protected EnergyUnit getEnergyUnit() {
95 return EnergyUnit.values()[unit];
98 return EnergyUnit.NOT_SUPPORTED;
101 protected State getLightingStatus() {
102 byte db0 = getDB_0();
103 boolean lightOn = getBit(db0, 0);
105 return lightOn ? OnOffType.ON : OnOffType.OFF;
108 protected State getDimmerStatus() {
109 if (getParameterMode() == ParameterMode.EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS) {
110 return new PercentType(getDB_3Value() * 100 / 255);
112 return UnDefType.UNDEF;
115 protected State getEnergyMeasurementData() {
116 if (getParameterMode() == ParameterMode.ENERGY_METERING_VALUE) {
117 EnergyUnit unit = getEnergyUnit();
133 return UnDefType.UNDEF;
136 return new QuantityType<>(
137 Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB_3(), getDB_2() }), 16) * factor,
138 Units.KILOWATT_HOUR);
141 return UnDefType.UNDEF;
144 protected State getPowerMeasurementData() {
145 if (getParameterMode() == ParameterMode.ENERGY_METERING_VALUE) {
146 EnergyUnit unit = getEnergyUnit();
162 return UnDefType.UNDEF;
165 return new QuantityType<>(
166 Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB_3(), getDB_2() }), 16) * factor, Units.WATT);
169 return UnDefType.UNDEF;
172 protected State getOperatingHours() {
173 if (getParameterMode() == ParameterMode.EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS) {
174 return new DecimalType(getDB_2Value() << 8 + getDB_1Value());
177 return UnDefType.UNDEF;
181 protected State convertToStateImpl(String channelId, String channelTypeId,
182 Function<String, State> getCurrentStateFunc, Configuration config) {
183 if (isErrorState()) {
184 return UnDefType.UNDEF;
188 case CHANNEL_GENERAL_SWITCHING:
189 return getLightingStatus();
191 return getDimmerStatus();
192 case CHANNEL_INSTANTPOWER:
193 return getPowerMeasurementData();
194 case CHANNEL_TOTALUSAGE:
195 State value = getEnergyMeasurementData();
196 State currentState = getCurrentStateFunc.apply(channelId);
197 return EEPHelper.validateTotalUsage(value, currentState, config);
198 case CHANNEL_COUNTER:
199 return getOperatingHours();
202 return UnDefType.UNDEF;