]> git.basschouten.com Git - openhab-addons.git/blob
4b5fd5a5eb5dc40242c508865659722f785d4f3d
[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 java.util.Objects;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.hue.internal.exceptions.DTOPresentButEmptyException;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.library.unit.Units;
22
23 import com.google.gson.annotations.SerializedName;
24
25 /**
26  * DTO for colour temperature of a light in CLIP 2.
27  *
28  * @author Andrew Fiddian-Green - Initial contribution
29  */
30 @NonNullByDefault
31 public class ColorTemperature {
32     private @Nullable Long mirek;
33     private @Nullable @SerializedName("mirek_schema") MirekSchema mirekSchema;
34
35     /**
36      * Get the color temperature as a QuantityType value.
37      *
38      * @return a QuantityType value
39      * @throws DTOPresentButEmptyException to indicate that the DTO is present but empty.
40      */
41     public @Nullable QuantityType<?> getAbsolute() throws DTOPresentButEmptyException {
42         Long mirek = this.mirek;
43         if (Objects.nonNull(mirek)) {
44             return QuantityType.valueOf(mirek, Units.MIRED).toInvertibleUnit(Units.KELVIN);
45         }
46         throw new DTOPresentButEmptyException("'color_temperature' DTO is present but empty");
47     }
48
49     public @Nullable Long getMirek() {
50         return mirek;
51     }
52
53     public @Nullable MirekSchema getMirekSchema() {
54         return mirekSchema;
55     }
56
57     /**
58      * Get the color temperature as a percentage based on the MirekSchema. Note: this method is only to be used on
59      * cached state DTOs which already have a defined mirek schema.
60      *
61      * @return the percentage of the mirekSchema range.
62      * @throws DTOPresentButEmptyException to indicate that the DTO is present but empty.
63      */
64     public @Nullable Double getPercent() throws DTOPresentButEmptyException {
65         Long mirek = this.mirek;
66         if (Objects.nonNull(mirek)) {
67             MirekSchema mirekSchema = this.mirekSchema;
68             mirekSchema = Objects.nonNull(mirekSchema) ? mirekSchema : MirekSchema.DEFAULT_SCHEMA;
69             double min = mirekSchema.getMirekMinimum();
70             double max = mirekSchema.getMirekMaximum();
71             double percent = 100f * (mirek.doubleValue() - min) / (max - min);
72             return Math.max(0, Math.min(100, percent));
73         }
74         throw new DTOPresentButEmptyException("'mirek_schema' DTO is present but empty");
75     }
76
77     public ColorTemperature setMirek(double mirek) {
78         this.mirek = Math.round(mirek);
79         return this;
80     }
81
82     public ColorTemperature setMirekSchema(@Nullable MirekSchema mirekSchema) {
83         this.mirekSchema = mirekSchema;
84         return this;
85     }
86 }