]> git.basschouten.com Git - openhab-addons.git/blob
1c975bedb7eaa864e44c5e983d8c14b7ffa2e218
[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.hue.internal.dto.clip2;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.OnOffType;
17 import org.openhab.core.library.types.QuantityType;
18 import org.openhab.core.library.unit.Units;
19 import org.openhab.core.types.State;
20 import org.openhab.core.types.UnDefType;
21
22 import com.google.gson.annotations.SerializedName;
23
24 /**
25  * DTO for CLIP 2 light level sensor.
26  *
27  * @author Andrew Fiddian-Green - Initial contribution
28  */
29 @NonNullByDefault
30 public class LightLevel {
31     private @SerializedName("light_level") int lightLevel;
32     private @SerializedName("light_level_valid") boolean lightLevelValid;
33
34     public int getLightLevel() {
35         return lightLevel;
36     }
37
38     public boolean isLightLevelValid() {
39         return lightLevelValid;
40     }
41
42     /**
43      * Raw sensor light level formula is '10000 * log10(lux + 1)' so apply the inverse formula to convert back to Lux.
44      * NOTE: the Philips/Signify API documentation quotes the formula as '10000 * log10(lux) + 1', however this code
45      * author thinks that that formula is wrong since zero Lux would cause a log10(0) negative infinity overflow!
46      *
47      * @return a QuantityType with light level in Lux, or UNDEF.
48      */
49     public State getLightLevelState() {
50         if (lightLevelValid) {
51             return new QuantityType<>(Math.pow(10f, (double) lightLevel / 10000f) - 1f, Units.LUX);
52         }
53         return UnDefType.UNDEF;
54     }
55
56     public State isLightLevelValidState() {
57         return OnOffType.from(lightLevelValid);
58     }
59 }