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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.unit.SIUnits;
17 import org.openhab.core.library.unit.Units;
20 * Holds all Converter objects.
22 * @author Fabian Wolter - Initial Contribution
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;
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);
46 private static long lightToNative(double value) {
47 return Math.round(Math.log(value) * 100);
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
53 if (value > lightToNative(100e3)) {
56 return Math.exp(value / 100d);
59 private static long angleToNative(double h) {
60 return (Math.round(h * 10) + 1000);