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_12;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.function.Function;
19 import org.openhab.binding.enocean.internal.config.EnOceanChannelTariffInfoConfig;
20 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
21 import org.openhab.binding.enocean.internal.eep.EEPHelper;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.openhab.core.util.HexUtils;
32 * @author Dominik Krickl-Vorreiter - Initial contribution
34 public abstract class A5_12 extends _4BSMessage {
35 public A5_12(ERP1Message packet) {
39 protected State calcCumulativeValue(float value) {
40 return new QuantityType<>(value, Units.ONE);
43 protected State calcCurrentValue(float value) {
44 return new QuantityType<>(value, Units.ONE);
47 protected State getCumulativeValue() {
49 boolean dt = getBit(db0, 2);
52 byte div = (byte) (db0 & 0x03);
70 return UnDefType.UNDEF;
73 float cumulativeValue = Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB_3(), getDB_2(), getDB_1() }),
75 return calcCumulativeValue(cumulativeValue);
78 return UnDefType.UNDEF;
81 protected State getCurrentValue() {
83 boolean dt = getBit(db0, 2);
86 byte div = (byte) (db0 & 0x03);
104 return UnDefType.UNDEF;
107 float currentValue = Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB_3(), getDB_2(), getDB_1() }), 16)
110 return calcCurrentValue(currentValue);
113 return UnDefType.UNDEF;
116 protected int getTariffInfo() {
117 return ((getDB_0() >>> 4) & 0xff);
121 protected State convertToStateImpl(String channelId, String channelTypeId,
122 Function<String, State> getCurrentStateFunc, Configuration config) {
124 EnOceanChannelTariffInfoConfig c = config.as(EnOceanChannelTariffInfoConfig.class);
125 if (c.tariff != getTariffInfo()) {
126 return UnDefType.UNDEF;
129 switch (channelTypeId) {
130 case CHANNEL_INSTANTPOWER:
131 case CHANNEL_CURRENTFLOW:
132 case CHANNEL_CURRENTNUMBER:
133 return getCurrentValue();
134 case CHANNEL_TOTALUSAGE:
135 State value = getCumulativeValue();
136 State currentState = getCurrentStateFunc.apply(channelId);
137 return EEPHelper.validateTotalUsage(value, currentState, config);
138 case CHANNEL_CUMULATIVEVALUE:
139 case CHANNEL_COUNTER:
140 return getCumulativeValue();
143 return UnDefType.UNDEF;