]> git.basschouten.com Git - openhab-addons.git/blob
2cb0878d27e8e38f1896b9e500676adb2eb11bca
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.neeo.internal.serialization;
14
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;
20 import java.util.Set;
21
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;
39
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;
48
49 /**
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)
53  *
54  * @author Tim Roberts - Initial Contribution
55  */
56 @NonNullByDefault
57 public class NeeoDeviceSerializer implements JsonSerializer<NeeoDevice>, JsonDeserializer<NeeoDevice> {
58
59     /** The service */
60     @Nullable
61     private final NeeoService service;
62
63     /** The service context */
64     @Nullable
65     private final ServiceContext context;
66
67     /**
68      * Constructs the object with no service or context
69      */
70     public NeeoDeviceSerializer() {
71         this(null, null);
72     }
73
74     /**
75      * Constructs the object from the service and context. A null service or context will suppress certain values on the
76      * returned json object
77      *
78      * @param service the possibly null service
79      * @param context the possibly null context
80      */
81     public NeeoDeviceSerializer(@Nullable NeeoService service, @Nullable ServiceContext context) {
82         this.service = service;
83         this.context = context;
84     }
85
86     @Override
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);
91         }
92
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)
101                 : null;
102
103         final String[] deviceCapabilities = jo.has("deviceCapabilities")
104                 ? jsonContext.deserialize(jo.get("deviceCapabilities"), String[].class)
105                 : null;
106
107         final String specificName = jo.has("specificName") ? jo.get("specificName").getAsString() : null;
108
109         final String iconName = jo.has("iconName") ? jo.get("iconName").getAsString() : null;
110         final int driverVersion = jo.has("driverVersion") ? jo.get("driverVersion").getAsInt() : 0;
111
112         try {
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);
119         }
120     }
121
122     @Override
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");
128
129         final JsonObject jsonObject = new JsonObject();
130
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());
139
140         final JsonArray channels = (JsonArray) jsonContext.serialize(device.getChannels());
141
142         final NeeoDeviceTiming timing = device.getDeviceTiming();
143         jsonObject.add("timing", jsonContext.serialize(timing == null ? new NeeoDeviceTiming() : timing));
144
145         jsonObject.add("deviceCapabilities", jsonContext.serialize(device.getDeviceCapabilities()));
146
147         jsonObject.addProperty("thingType", uid.getThingType());
148
149         if (StringUtils.equalsIgnoreCase(NeeoConstants.NEEOIO_BINDING_ID, uid.getBindingId())) {
150             jsonObject.addProperty("thingStatus", uid.getThingType().toUpperCase());
151         }
152
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());
159
160                 if (thing != null) {
161                     final ThingType thingType = localContext.getThingTypeRegistry()
162                             .getThingType(thing.getThingTypeUID());
163
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());
178                                 }
179                             }
180                         }
181                     }
182                 }
183             }
184         }
185
186         jsonObject.add("channels", channels);
187
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);
195             }
196             jsonObject.add("keys", jsonContext.serialize(foundKeys));
197         }
198
199         return jsonObject;
200     }
201 }