]> git.basschouten.com Git - openhab-addons.git/blob
34ad8d7ef72acf2dfb2d0bcd51ad8dbb0e969086
[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.io.hueemulation.internal.dto;
14
15 import java.lang.reflect.Type;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.stream.Collectors;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.items.GroupItem;
23 import org.openhab.io.hueemulation.internal.ConfigStore;
24 import org.openhab.io.hueemulation.internal.DeviceType;
25
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
29 import com.google.gson.annotations.SerializedName;
30
31 /**
32  * Hue API group object
33  *
34  * @author David Graeff - Initial contribution
35  */
36 @NonNullByDefault
37 public class HueGroupEntry {
38     public enum TypeEnum {
39         LightGroup, // 1.4
40         Luminaire, // 1.4
41         LightSource, // 1.4
42         Room, // 1.11
43         Entertainment, // 1.22
44         Zone // 1.30
45     }
46
47     public AbstractHueState action = new HueStatePlug();
48
49     // The group type
50     public String type = TypeEnum.LightGroup.name();
51
52     // A unique, editable name given to the group.
53     public String name;
54
55     @SerializedName("class")
56     public String roomclass = "Other";
57
58     // The IDs of the lights that are in the group.
59     public List<String> lights = Collections.emptyList();
60     public List<String> sensors = Collections.emptyList();
61
62     public transient @NonNullByDefault({}) GroupItem groupItem;
63     public transient @Nullable DeviceType deviceType;
64
65     // For deserialisation
66     HueGroupEntry() {
67         name = "";
68     }
69
70     public HueGroupEntry(String name, @Nullable GroupItem groupItem, @Nullable DeviceType deviceType) {
71         this.name = name;
72         this.groupItem = groupItem;
73         this.deviceType = deviceType;
74     }
75
76     public void updateItem(GroupItem element) {
77         groupItem = element;
78     }
79
80     /**
81      * This custom serializer computes the {@link HueGroupEntry#lights} list, before serializing.
82      * It does so, by looking up all item members of the references groupItem.
83      */
84     @NonNullByDefault({})
85     public static class Serializer implements JsonSerializer<HueGroupEntry> {
86
87         private ConfigStore cs;
88
89         public Serializer(ConfigStore cs) {
90             this.cs = cs;
91         }
92
93         static class HueGroupHelper extends HueGroupEntry {
94
95         }
96
97         @Override
98         public JsonElement serialize(HueGroupEntry product, Type type, JsonSerializationContext context) {
99             GroupItem item = product.groupItem;
100             if (item != null) {
101                 product.lights = item.getMembers().stream().map(gitem -> cs.mapItemUIDtoHueID(gitem))
102                         .collect(Collectors.toList());
103             }
104
105             return context.serialize(product, HueGroupHelper.class);
106         }
107     }
108 }