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.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enocean.internal.config.EnOceanChannelTariffInfoConfig;
22 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
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.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;
34 * @author Dominik Krickl-Vorreiter - Initial contribution
37 public abstract class A5_12 extends _4BSMessage {
38 public A5_12(ERP1Message packet) {
42 protected State calcCumulativeValue(float value) {
43 return new QuantityType<>(value, Units.ONE);
46 protected State calcCurrentValue(float value) {
47 return new QuantityType<>(value, Units.ONE);
50 protected State getCumulativeValue() {
52 boolean dt = getBit(db0, 2);
55 byte div = (byte) (db0 & 0x03);
73 return UnDefType.UNDEF;
76 float cumulativeValue = Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB3(), getDB2(), getDB1() }), 16)
78 return calcCumulativeValue(cumulativeValue);
81 return UnDefType.UNDEF;
84 protected State getCurrentValue() {
86 boolean dt = getBit(db0, 2);
89 byte div = (byte) (db0 & 0x03);
107 return UnDefType.UNDEF;
110 float currentValue = Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB3(), getDB2(), getDB1() }), 16)
113 return calcCurrentValue(currentValue);
116 return UnDefType.UNDEF;
119 protected int getTariffInfo() {
120 return ((getDB0() >>> 4) & 0xff);
124 protected State convertToStateImpl(String channelId, String channelTypeId,
125 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
126 EnOceanChannelTariffInfoConfig c = config.as(EnOceanChannelTariffInfoConfig.class);
127 if (c.tariff != getTariffInfo()) {
128 return UnDefType.UNDEF;
131 switch (channelTypeId) {
132 case CHANNEL_INSTANTPOWER:
133 case CHANNEL_CURRENTFLOW:
134 case CHANNEL_CURRENTNUMBER:
135 return getCurrentValue();
136 case CHANNEL_TOTALUSAGE:
137 State value = getCumulativeValue();
138 State currentState = getCurrentStateFunc.apply(channelId);
139 return EEPHelper.validateTotalUsage(value, currentState, config);
140 case CHANNEL_CUMULATIVEVALUE:
141 case CHANNEL_COUNTER:
142 return getCumulativeValue();
145 return UnDefType.UNDEF;