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.lcn.internal.converter;
15 import java.math.BigDecimal;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.unit.Units;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Base class for S0 counter value converters.
26 * @author Fabian Wolter - Initial Contribution
29 public class S0Converter extends ValueConverter {
30 private final Logger logger = LoggerFactory.getLogger(S0Converter.class);
31 protected double pulsesPerKwh;
33 public S0Converter(@Nullable Object parameter) {
34 super(Units.WATT, n -> 0d, h -> 0L);
36 if (parameter == null) {
38 logger.debug("Pulses per kWh not set. Assuming 1000 imp./kWh.");
39 } else if (parameter instanceof BigDecimal decimalValue) {
40 pulsesPerKwh = decimalValue.doubleValue();
42 logger.warn("Could not parse 'pulses', unexpected type, should be float or integer: {}", parameter);
47 public long toNative(double value) {
48 return Math.round(value * pulsesPerKwh / 1000);
52 public double toHumanReadable(long value) {
53 return value / pulsesPerKwh * 1000;