]> git.basschouten.com Git - openhab-addons.git/blob
f34d37afbb9bbd54f3f47286267cf2ccedc2097d
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.unit.SIUnits;
17 import org.openhab.core.library.unit.Units;
18
19 /**
20  * Holds all Converter objects.
21  *
22  * @author Fabian Wolter - Initial Contribution
23  */
24 @NonNullByDefault
25 public class Converters {
26     public static final Converter TEMPERATURE;
27     public static final Converter LIGHT;
28     public static final Converter CO2;
29     public static final Converter CURRENT;
30     public static final Converter VOLTAGE;
31     public static final Converter ANGLE;
32     public static final Converter WINDSPEED;
33     public static final Converter IDENTITY;
34
35     static {
36         TEMPERATURE = new ValueConverter(SIUnits.CELSIUS, n -> (n - 1000) / 10d, h -> Math.round(h * 10) + 1000);
37         LIGHT = new ValueConverter(Units.LUX, Converters::lightToHumanReadable, Converters::lightToNative);
38         CO2 = new ValueConverter(Units.PARTS_PER_MILLION, n -> (double) n, Math::round);
39         CURRENT = new ValueConverter(Units.AMPERE, n -> n / 100d, h -> Math.round(h * 100));
40         VOLTAGE = new ValueConverter(Units.VOLT, n -> n / 400d, h -> Math.round(h * 400));
41         ANGLE = new ValueConverter(Units.DEGREE_ANGLE, n -> (n - 1000) / 10d, Converters::angleToNative);
42         WINDSPEED = new ValueConverter(Units.METRE_PER_SECOND, n -> n / 10d, h -> Math.round(h * 10));
43         IDENTITY = new ValueConverter(null, n -> (double) n, Math::round);
44     }
45
46     private static long lightToNative(double value) {
47         return Math.round(Math.log(value) * 100);
48     }
49
50     private static double lightToHumanReadable(long value) {
51         // Max. value hardware can deliver is 100klx. Apply hard limit, because higher native values lead to very big
52         // lux values.
53         if (value > lightToNative(100e3)) {
54             return Double.NaN;
55         }
56         return Math.exp(value / 100d);
57     }
58
59     private static long angleToNative(double h) {
60         return (Math.round(h * 10) + 1000);
61     }
62 }