2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.hueemulation.internal.dto;
15 import java.lang.reflect.Type;
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;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
31 * Hue API scene object
33 * @author David Graeff - Initial contribution
36 public class HueSensorEntry {
37 // A unique, editable name given to the group.
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;
46 public final @NonNullByDefault({}) transient GenericItem item;
48 private HueSensorEntry() {
56 public HueSensorEntry(GenericItem item) throws IllegalArgumentException {
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"
66 case CoreItemFactory.ROLLERSHUTTER:
67 case CoreItemFactory.DIMMER:
68 this.type = "CLIPGenericStatus"; // "status" (int)
70 case CoreItemFactory.NUMBER:
71 if (item.hasTag("temperature")) {
72 this.type = "CLIPTemperature"; // "temperature"
74 this.type = "CLIPLightLevel"; // "lightlevel" (int), "dark" (bool), "daylight" (bool)
77 case CoreItemFactory.COLOR:
78 this.type = "CLIPLightLevel"; // "lightlevel" (int), "dark" (bool), "daylight" (bool)
80 case CoreItemFactory.SWITCH:
81 this.type = "CLIPGenericFlag"; // "flag" (bool)
84 throw new IllegalArgumentException("Item type not supported as sensor");
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.
93 public static class Serializer implements JsonSerializer<HueSensorEntry> {
95 static class HueHelper extends HueSensorEntry {
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);
109 state.addProperty("open", false);
112 case "CLIPGenericStatus":
113 if (itemState instanceof DecimalType) {
114 state.addProperty("status", ((DecimalType) product.item.getState()).intValue());
116 state.addProperty("status", 0);
119 case "CLIPTemperature":
120 if (itemState instanceof DecimalType) {
121 state.addProperty("temperature", ((DecimalType) product.item.getState()).intValue());
123 state.addProperty("status", 0);
126 case "CLIPLightLevel":
127 if (itemState instanceof DecimalType) {
128 state.addProperty("lightlevel", ((DecimalType) product.item.getState()).intValue());
130 state.addProperty("status", 0);
132 state.addProperty("dark", false);
133 state.addProperty("daylight", false);
135 case "CLIPGenericFlag":
136 if (itemState instanceof OnOffType) {
137 state.addProperty("flag", ((OnOffType) product.item.getState()) == OnOffType.ON);
139 state.addProperty("open", false);
143 json.getAsJsonObject().add("state", state);