]> git.basschouten.com Git - openhab-addons.git/blob
b7ca31b13cdd1c75f0365aebe9fc6f47581ab93a
[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
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.items.GenericItem;
19 import org.openhab.core.library.CoreItemFactory;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.OpenClosedType;
23 import org.openhab.core.types.State;
24
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
29
30 /**
31  * Hue API scene object
32  *
33  * @author David Graeff - Initial contribution
34  */
35 @NonNullByDefault
36 public class HueSensorEntry {
37     // A unique, editable name given to the group.
38     public String name;
39     public String type;
40     public String modelid;
41     public String manufacturername = "openHab";
42     public String swversion = "1.0";
43     public Object config = new Object();
44     public String uniqueid;
45
46     public final @NonNullByDefault({}) transient GenericItem item;
47
48     private HueSensorEntry() {
49         item = null;
50         name = "";
51         type = "";
52         modelid = "";
53         uniqueid = "";
54     }
55
56     public HueSensorEntry(GenericItem item) throws IllegalArgumentException {
57         this.item = item;
58         String label = item.getLabel();
59         this.name = label != null ? label : item.getName();
60         this.modelid = "openHAB_" + item.getType();
61         this.uniqueid = item.getUID();
62         switch (item.getType()) {
63             case CoreItemFactory.CONTACT:
64                 this.type = "CLIPOpenClose"; // "open"
65                 break;
66             case CoreItemFactory.ROLLERSHUTTER:
67             case CoreItemFactory.DIMMER:
68                 this.type = "CLIPGenericStatus"; // "status" (int)
69                 break;
70             case CoreItemFactory.NUMBER:
71                 if (item.hasTag("temperature")) {
72                     this.type = "CLIPTemperature"; // "temperature"
73                 } else {
74                     this.type = "CLIPLightLevel"; // "lightlevel" (int), "dark" (bool), "daylight" (bool)
75                 }
76                 break;
77             case CoreItemFactory.COLOR:
78                 this.type = "CLIPLightLevel"; // "lightlevel" (int), "dark" (bool), "daylight" (bool)
79                 break;
80             case CoreItemFactory.SWITCH:
81                 this.type = "CLIPGenericFlag"; // "flag" (bool)
82                 break;
83             default:
84                 throw new IllegalArgumentException("Item type not supported as sensor");
85         }
86     }
87
88     /**
89      * This custom serializer computes the {@link HueGroupEntry#lights} list, before serializing.
90      * It does so, by looking up all item members of the references groupItem.
91      */
92     @NonNullByDefault({})
93     public static class Serializer implements JsonSerializer<HueSensorEntry> {
94
95         static class HueHelper extends HueSensorEntry {
96
97         }
98
99         @Override
100         public JsonElement serialize(HueSensorEntry product, Type type, JsonSerializationContext context) {
101             JsonElement json = context.serialize(product, HueHelper.class);
102             JsonObject state = new JsonObject();
103             State itemState = product.item.getState();
104             switch (product.type) {
105                 case "CLIPOpenClose":
106                     if (itemState instanceof OpenClosedType) {
107                         state.addProperty("open", ((OpenClosedType) product.item.getState()) == OpenClosedType.OPEN);
108                     } else {
109                         state.addProperty("open", false);
110                     }
111                     break;
112                 case "CLIPGenericStatus":
113                     if (itemState instanceof DecimalType) {
114                         state.addProperty("status", ((DecimalType) product.item.getState()).intValue());
115                     } else {
116                         state.addProperty("status", 0);
117                     }
118                     break;
119                 case "CLIPTemperature":
120                     if (itemState instanceof DecimalType) {
121                         state.addProperty("temperature", ((DecimalType) product.item.getState()).intValue());
122                     } else {
123                         state.addProperty("status", 0);
124                     }
125                     break;
126                 case "CLIPLightLevel":
127                     if (itemState instanceof DecimalType) {
128                         state.addProperty("lightlevel", ((DecimalType) product.item.getState()).intValue());
129                     } else {
130                         state.addProperty("status", 0);
131                     }
132                     state.addProperty("dark", false);
133                     state.addProperty("daylight", false);
134                     break;
135                 case "CLIPGenericFlag":
136                     if (itemState instanceof OnOffType) {
137                         state.addProperty("flag", ((OnOffType) product.item.getState()) == OnOffType.ON);
138                     } else {
139                         state.addProperty("open", false);
140                     }
141                     break;
142             }
143             json.getAsJsonObject().add("state", state);
144             return json;
145         }
146     }
147 }