2 * Copyright (c) 2010-2020 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.neeo.internal.serialization;
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19 import java.util.Objects;
22 import org.apache.commons.lang.StringUtils;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.type.ThingType;
28 import org.openhab.io.neeo.NeeoService;
29 import org.openhab.io.neeo.internal.NeeoBrainServlet;
30 import org.openhab.io.neeo.internal.NeeoConstants;
31 import org.openhab.io.neeo.internal.NeeoDeviceKeys;
32 import org.openhab.io.neeo.internal.NeeoUtil;
33 import org.openhab.io.neeo.internal.ServiceContext;
34 import org.openhab.io.neeo.internal.models.NeeoDevice;
35 import org.openhab.io.neeo.internal.models.NeeoDeviceChannel;
36 import org.openhab.io.neeo.internal.models.NeeoDeviceTiming;
37 import org.openhab.io.neeo.internal.models.NeeoDeviceType;
38 import org.openhab.io.neeo.internal.models.NeeoThingUID;
40 import com.google.gson.JsonArray;
41 import com.google.gson.JsonDeserializationContext;
42 import com.google.gson.JsonDeserializer;
43 import com.google.gson.JsonElement;
44 import com.google.gson.JsonObject;
45 import com.google.gson.JsonParseException;
46 import com.google.gson.JsonSerializationContext;
47 import com.google.gson.JsonSerializer;
50 * Implementation of {@link JsonSerializer} and {@link JsonDeserializer} to serialize/deserial
51 * {@link NeeoDevice}. This implementation should NOT be used in communications with the NEEO brain (use
52 * {@link NeeoBrainDeviceSerializer} instead)
54 * @author Tim Roberts - Initial Contribution
57 public class NeeoDeviceSerializer implements JsonSerializer<NeeoDevice>, JsonDeserializer<NeeoDevice> {
61 private final NeeoService service;
63 /** The service context */
65 private final ServiceContext context;
68 * Constructs the object with no service or context
70 public NeeoDeviceSerializer() {
75 * Constructs the object from the service and context. A null service or context will suppress certain values on the
76 * returned json object
78 * @param service the possibly null service
79 * @param context the possibly null context
81 public NeeoDeviceSerializer(@Nullable NeeoService service, @Nullable ServiceContext context) {
82 this.service = service;
83 this.context = context;
87 public @Nullable NeeoDevice deserialize(JsonElement elm, Type type, JsonDeserializationContext jsonContext)
88 throws JsonParseException {
89 if (!(elm instanceof JsonObject)) {
90 throw new JsonParseException("Element not an instance of JsonObject: " + elm);
93 final JsonObject jo = (JsonObject) elm;
94 final NeeoThingUID uid = jsonContext.deserialize(jo.get("uid"), NeeoThingUID.class);
95 final NeeoDeviceType devType = jsonContext.deserialize(jo.get("type"), NeeoDeviceType.class);
96 final String manufacturer = NeeoUtil.getString(jo, "manufacturer");
97 final String name = NeeoUtil.getString(jo, "name");
98 final NeeoDeviceChannel[] channels = jsonContext.deserialize(jo.get("channels"), NeeoDeviceChannel[].class);
99 final NeeoDeviceTiming timing = jo.has("timing")
100 ? jsonContext.deserialize(jo.get("timing"), NeeoDeviceTiming.class)
103 final String[] deviceCapabilities = jo.has("deviceCapabilities")
104 ? jsonContext.deserialize(jo.get("deviceCapabilities"), String[].class)
107 final String specificName = jo.has("specificName") ? jo.get("specificName").getAsString() : null;
109 final String iconName = jo.has("iconName") ? jo.get("iconName").getAsString() : null;
110 final int driverVersion = jo.has("driverVersion") ? jo.get("driverVersion").getAsInt() : 0;
113 return new NeeoDevice(uid, driverVersion, devType,
114 manufacturer == null || StringUtils.isEmpty(manufacturer) ? NeeoUtil.NOTAVAILABLE : manufacturer,
115 name, Arrays.asList(channels), timing,
116 deviceCapabilities == null ? null : Arrays.asList(deviceCapabilities), specificName, iconName);
117 } catch (NullPointerException | IllegalArgumentException e) {
118 throw new JsonParseException(e);
123 public JsonElement serialize(NeeoDevice device, @Nullable Type deviceType,
124 @Nullable JsonSerializationContext jsonContext) {
125 Objects.requireNonNull(device, "device cannot be null");
126 Objects.requireNonNull(deviceType, "deviceType cannot be null");
127 Objects.requireNonNull(jsonContext, "jsonContext cannot be null");
129 final JsonObject jsonObject = new JsonObject();
131 final NeeoThingUID uid = device.getUid();
132 jsonObject.add("uid", jsonContext.serialize(uid));
133 jsonObject.add("type", jsonContext.serialize(device.getType()));
134 jsonObject.addProperty("manufacturer", device.getManufacturer());
135 jsonObject.addProperty("name", device.getName());
136 jsonObject.addProperty("specificName", device.getSpecificName());
137 jsonObject.addProperty("iconName", device.getIconName());
138 jsonObject.addProperty("driverVersion", device.getDriverVersion());
140 final JsonArray channels = (JsonArray) jsonContext.serialize(device.getChannels());
142 final NeeoDeviceTiming timing = device.getDeviceTiming();
143 jsonObject.add("timing", jsonContext.serialize(timing == null ? new NeeoDeviceTiming() : timing));
145 jsonObject.add("deviceCapabilities", jsonContext.serialize(device.getDeviceCapabilities()));
147 jsonObject.addProperty("thingType", uid.getThingType());
149 if (StringUtils.equalsIgnoreCase(NeeoConstants.NEEOIO_BINDING_ID, uid.getBindingId())) {
150 jsonObject.addProperty("thingStatus", uid.getThingType().toUpperCase());
153 final ServiceContext localContext = context;
154 if (localContext != null) {
155 if (!StringUtils.equalsIgnoreCase(NeeoConstants.NEEOIO_BINDING_ID, uid.getBindingId())) {
156 final Thing thing = localContext.getThingRegistry().get(device.getUid().asThingUID());
157 jsonObject.addProperty("thingStatus",
158 thing == null ? ThingStatus.UNKNOWN.name() : thing.getStatus().name());
161 final ThingType thingType = localContext.getThingTypeRegistry()
162 .getThingType(thing.getThingTypeUID());
164 if (thingType != null) {
165 for (JsonElement chnl : channels) {
166 JsonObject jo = (JsonObject) chnl;
167 if (jo.has("groupId") && jo.has("itemLabel")) {
168 final String groupId = jo.get("groupId").getAsString();
169 final String groupLabel = NeeoUtil.getGroupLabel(thingType, groupId);
170 if (StringUtils.isNotEmpty(groupLabel)) {
171 final JsonElement itemLabel = jo.remove("itemLabel");
172 jo.addProperty("itemLabel", groupLabel + "#" + itemLabel.getAsString());
173 } else if (StringUtils.isNotEmpty("groupId")) {
174 // have a groupid but no group definition found (usually error on binding)
175 // just default to "Others" like the Paperui does.
176 final JsonElement itemLabel = jo.remove("itemLabel");
177 jo.addProperty("itemLabel", "Others#" + itemLabel.getAsString());
186 jsonObject.add("channels", channels);
188 final NeeoService localService = service;
189 if (localService != null) {
190 List<String> foundKeys = new ArrayList<>();
191 for (final NeeoBrainServlet servlet : localService.getServlets()) {
192 final NeeoDeviceKeys servletKeys = servlet.getDeviceKeys();
193 final Set<String> keys = servletKeys.get(device.getUid());
194 foundKeys.addAll(keys);
196 jsonObject.add("keys", jsonContext.serialize(foundKeys));