2 * Copyright (c) 2010-2020 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.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.QuantityType;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27 import org.openhab.core.util.HexUtils;
31 * @author Dominik Krickl-Vorreiter - Initial contribution
33 public abstract class A5_12 extends _4BSMessage {
34 public A5_12(ERP1Message packet) {
38 protected State calcCumulativeValue(float value) {
39 return new QuantityType<>(value, Units.ONE);
42 protected State calcCurrentValue(float value) {
43 return new QuantityType<>(value, Units.ONE);
46 protected State getCumulativeValue() {
48 boolean dt = getBit(db0, 2);
51 byte div = (byte) (db0 & 0x03);
69 return UnDefType.UNDEF;
72 float cumulativeValue = Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB_3(), getDB_2(), getDB_1() }),
74 return calcCumulativeValue(cumulativeValue);
77 return UnDefType.UNDEF;
80 protected State getCurrentValue() {
82 boolean dt = getBit(db0, 2);
85 byte div = (byte) (db0 & 0x03);
103 return UnDefType.UNDEF;
106 float currentValue = Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB_3(), getDB_2(), getDB_1() }), 16)
109 return calcCurrentValue(currentValue);
112 return UnDefType.UNDEF;
116 protected State convertToStateImpl(String channelId, String channelTypeId,
117 Function<String, State> getCurrentStateFunc, Configuration config) {
119 case CHANNEL_INSTANTPOWER:
120 case CHANNEL_CURRENTFLOW:
121 case CHANNEL_CURRENTNUMBER:
122 return getCurrentValue();
123 case CHANNEL_TOTALUSAGE:
124 State value = getCumulativeValue();
125 State currentState = getCurrentStateFunc.apply(channelId);
126 return EEPHelper.validateTotalUsage(value, currentState, config);
127 case CHANNEL_CUMULATIVEVALUE:
128 case CHANNEL_COUNTER:
129 return getCumulativeValue();
132 return UnDefType.UNDEF;