Search in sources :

Example 1 with NeeoDeviceType

use of org.openhab.io.neeo.internal.models.NeeoDeviceType in project openhab-addons by openhab.

the class NeeoDeviceSerializer method deserialize.

@Override
@Nullable
public NeeoDevice deserialize(JsonElement elm, Type type, JsonDeserializationContext jsonContext) throws JsonParseException {
    if (!(elm instanceof JsonObject)) {
        throw new JsonParseException("Element not an instance of JsonObject: " + elm);
    }
    final JsonObject jo = (JsonObject) elm;
    final NeeoThingUID uid = jsonContext.deserialize(jo.get("uid"), NeeoThingUID.class);
    final NeeoDeviceType devType = jsonContext.deserialize(jo.get("type"), NeeoDeviceType.class);
    final String manufacturer = NeeoUtil.getString(jo, "manufacturer");
    final String name = NeeoUtil.getString(jo, "name");
    final NeeoDeviceChannel[] channels = jsonContext.deserialize(jo.get("channels"), NeeoDeviceChannel[].class);
    final NeeoDeviceTiming timing = jo.has("timing") ? jsonContext.deserialize(jo.get("timing"), NeeoDeviceTiming.class) : null;
    final String[] deviceCapabilities = jo.has("deviceCapabilities") ? jsonContext.deserialize(jo.get("deviceCapabilities"), String[].class) : null;
    final String specificName = jo.has("specificName") ? jo.get("specificName").getAsString() : null;
    final String iconName = jo.has("iconName") ? jo.get("iconName").getAsString() : null;
    final int driverVersion = jo.has("driverVersion") ? jo.get("driverVersion").getAsInt() : 0;
    try {
        return new NeeoDevice(uid, driverVersion, devType, manufacturer == null || manufacturer.isEmpty() ? NeeoUtil.NOTAVAILABLE : manufacturer, name, Arrays.asList(channels), timing, deviceCapabilities == null ? null : Arrays.asList(deviceCapabilities), specificName, iconName);
    } catch (NullPointerException | IllegalArgumentException e) {
        throw new JsonParseException(e);
    }
}
Also used : NeeoDeviceChannel(org.openhab.io.neeo.internal.models.NeeoDeviceChannel) NeeoDeviceTiming(org.openhab.io.neeo.internal.models.NeeoDeviceTiming) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice) NeeoThingUID(org.openhab.io.neeo.internal.models.NeeoThingUID) NeeoDeviceType(org.openhab.io.neeo.internal.models.NeeoDeviceType) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 NeeoDevice (org.openhab.io.neeo.internal.models.NeeoDevice)1 NeeoDeviceChannel (org.openhab.io.neeo.internal.models.NeeoDeviceChannel)1 NeeoDeviceTiming (org.openhab.io.neeo.internal.models.NeeoDeviceTiming)1 NeeoDeviceType (org.openhab.io.neeo.internal.models.NeeoDeviceType)1 NeeoThingUID (org.openhab.io.neeo.internal.models.NeeoThingUID)1