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