]> git.basschouten.com Git - openhab-addons.git/blob
82770678a82c5d16d06b90a5ceb3bf18a6eaf56b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lcn.internal.converter;
14
15 import java.math.BigDecimal;
16
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;
22
23 /**
24  * Base class for S0 counter value converters.
25  *
26  * @author Fabian Wolter - Initial Contribution
27  */
28 @NonNullByDefault
29 public class S0Converter extends ValueConverter {
30     private final Logger logger = LoggerFactory.getLogger(S0Converter.class);
31     protected double pulsesPerKwh;
32
33     public S0Converter(@Nullable Object parameter) {
34         super(Units.WATT, n -> 0d, h -> 0L);
35
36         if (parameter == null) {
37             pulsesPerKwh = 1000;
38             logger.debug("Pulses per kWh not set. Assuming 1000 imp./kWh.");
39         } else if (parameter instanceof BigDecimal) {
40             pulsesPerKwh = ((BigDecimal) parameter).doubleValue();
41         } else {
42             logger.warn("Could not parse 'pulses', unexpected type, should be float or integer: {}", parameter);
43         }
44     }
45
46     @Override
47     public long toNative(double value) {
48         return Math.round(value * pulsesPerKwh / 1000);
49     }
50
51     @Override
52     public double toHumanReadable(long value) {
53         return value / pulsesPerKwh * 1000;
54     }
55 }