]> git.basschouten.com Git - openhab-addons.git/blob
a9231752dfb416c6926338a7c30b1f2b10b8b818
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.clip1;
14
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.types.StateOption;
23
24 import com.google.gson.annotations.SerializedName;
25 import com.google.gson.reflect.TypeToken;
26
27 /**
28  * Basic scene information.
29  *
30  * @author Hengrui Jiang - Initial contribution
31  */
32 @NonNullByDefault
33 public class Scene {
34     public static final Type GSON_TYPE = new TypeToken<Map<String, Scene>>() {
35     }.getType();
36
37     private @NonNullByDefault({}) String id;
38     private @NonNullByDefault({}) String name;
39     @SerializedName("lights")
40     private @Nullable List<String> lightIds;
41     @SerializedName("group")
42     private @Nullable String groupId;
43     private boolean recycle;
44
45     /**
46      * Default constructor for GSon.
47      */
48     public Scene() {
49         super();
50     }
51
52     /**
53      * Test constructor
54      */
55     public Scene(String id, String name, @Nullable String groupId, List<String> lightIds, boolean recycle) {
56         this.id = id;
57         this.name = name;
58         this.groupId = groupId;
59         this.lightIds = lightIds;
60         this.recycle = recycle;
61     }
62
63     public String getId() {
64         return id;
65     }
66
67     public void setId(String id) {
68         this.id = id;
69     }
70
71     /**
72      * Returns the human readable name of the scene. If the name is omitted upon creation, this
73      * defaults to the ID.
74      *
75      * @return human readable name of the scene
76      */
77     public String getName() {
78         return name;
79     }
80
81     /**
82      * Returns the list of lights that the scene applies to. For group scenes, this list should be identical to the list
83      * of all lights that are in the group.
84      *
85      * @return list of lights that the scene applies to
86      */
87     public List<String> getLightIds() {
88         List<String> lightIds = this.lightIds;
89         return lightIds != null ? lightIds : new ArrayList<String>();
90     }
91
92     /**
93      * Returns the group that the scene belongs to. This field is optional for scenes that applies to a specific list of
94      * lights instead of a group.
95      *
96      * @return the group that the scene belongs to
97      */
98     public @Nullable String getGroupId() {
99         return groupId;
100     }
101
102     /**
103      * Indicates if the scene can be recycled by the bridge. A recyclable scene is not able to be activated.
104      *
105      * @return whether the scene can be recycled
106      */
107     public boolean isRecycle() {
108         return recycle;
109     }
110
111     /**
112      * Creates a {@link StateOption} to display this scene, including the group that it belongs to.
113      * <p>
114      * The display name is built with the following pattern:
115      * <ol>
116      * <li>Human readable name of the scene if set. Otherwise, the ID is displayed</li>
117      * <li>Group for which the scene is defined</li>
118      * </ol>
119      */
120     public StateOption toStateOption(Map<String, String> groupNames) {
121         StringBuilder stateOptionLabel = new StringBuilder(name);
122         if (groupId != null && groupNames.containsKey(groupId)) {
123             stateOptionLabel.append(" (").append(groupNames.get(groupId)).append(")");
124         }
125
126         return new StateOption(id, stateOptionLabel.toString());
127     }
128
129     /**
130      * Creates a {@link StateOption} to display this scene.
131      */
132     public StateOption toStateOption() {
133         return new StateOption(id, name);
134     }
135
136     /**
137      * Returns whether the scene is applicable to the given group.
138      * <p>
139      * According to the hue API, a scene is applicable to a group if either
140      * <ol>
141      * <li>The scene is defined for the group</li>
142      * <li>All lights of the scene also belong to the group</li>
143      * </ol>
144      */
145     public boolean isApplicableTo(FullGroup group) {
146         String groupId = this.groupId;
147         if (groupId == null) {
148             return getLightIds().stream().allMatch(id -> group.getLightIds().contains(id));
149         } else {
150             return group.getId().contentEquals(groupId);
151         }
152     }
153
154     public String extractKeyForComparator() {
155         return (groupId != null ? groupId : "") + "#" + name;
156     }
157
158     @Override
159     public String toString() {
160         return String.format("{Scene name: %s; id: %s; lightIds: %s; groupId: %s; recycle: %s}", name, id, lightIds,
161                 groupId, recycle);
162     }
163 }