Search in sources :

Example 1 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class ObjectEnabler method doObserve.

@Override
protected ObserveResponse doObserve(final ServerIdentity identity, final ObserveRequest request) {
    final LwM2mPath path = request.getPath();
    // Manage Object case
    if (path.isObject()) {
        List<LwM2mObjectInstance> lwM2mObjectInstances = new ArrayList<>();
        for (Entry<Integer, LwM2mInstanceEnabler> entry : instances.entrySet()) {
            lwM2mObjectInstances.add(getLwM2mObjectInstance(entry.getKey(), entry.getValue(), identity, true));
        }
        return ObserveResponse.success(new LwM2mObject(getId(), lwM2mObjectInstances));
    }
    // Manage Instance case
    final LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
    if (instance == null)
        return ObserveResponse.notFound();
    if (path.getResourceId() == null) {
        return ObserveResponse.success(getLwM2mObjectInstance(path.getObjectInstanceId(), instance, identity, true));
    }
    // Manage Resource case
    return instance.observe(path.getResourceId());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ArrayList(java.util.ArrayList) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject)

Example 2 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class ServersInfoExtractor method getInfo.

public static ServersInfo getInfo(Map<Integer, LwM2mObjectEnabler> objectEnablers) {
    LwM2mObjectEnabler securityEnabler = objectEnablers.get(SECURITY);
    LwM2mObjectEnabler serverEnabler = objectEnablers.get(SERVER);
    if (securityEnabler == null || serverEnabler == null)
        return null;
    ServersInfo infos = new ServersInfo();
    LwM2mObject securities = (LwM2mObject) securityEnabler.read(SYSTEM, new ReadRequest(SECURITY)).getContent();
    LwM2mObject servers = (LwM2mObject) serverEnabler.read(SYSTEM, new ReadRequest(SERVER)).getContent();
    for (LwM2mObjectInstance security : securities.getInstances().values()) {
        try {
            if ((boolean) security.getResource(SEC_BOOTSTRAP).getValue()) {
                if (infos.bootstrap != null) {
                    LOG.warn("There is more than one bootstrap configuration in security object.");
                } else {
                    // create bootstrap info
                    ServerInfo info = new ServerInfo();
                    LwM2mResource serverIdResource = security.getResource(SEC_SERVER_ID);
                    if (serverIdResource != null && serverIdResource.getValue() != null)
                        info.serverId = (long) serverIdResource.getValue();
                    else
                        info.serverId = 0;
                    info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                    info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                    infos.bootstrap = info;
                }
            } else {
                // create device management info
                DmServerInfo info = new DmServerInfo();
                info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                info.serverId = (long) security.getResource(SEC_SERVER_ID).getValue();
                info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                // search corresponding device management server
                for (LwM2mObjectInstance server : servers.getInstances().values()) {
                    if (info.serverId == (Long) server.getResource(SRV_SERVER_ID).getValue()) {
                        info.lifetime = (long) server.getResource(SRV_LIFETIME).getValue();
                        info.binding = BindingMode.valueOf((String) server.getResource(SRV_BINDING).getValue());
                        infos.deviceMangements.put(info.serverId, info);
                        break;
                    }
                }
            }
        } catch (URISyntaxException e) {
            LOG.error(String.format("Invalid URI %s", (String) security.getResource(SEC_SERVER_URI).getValue()), e);
        }
    }
    return infos;
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 3 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class LwM2mNodeJsonDecoder method parseJSON.

private static List<TimestampedLwM2mNode> parseJSON(JsonRootObject jsonObject, LwM2mPath path, LwM2mModel model, Class<? extends LwM2mNode> nodeClass) throws CodecException {
    LOG.trace("Parsing JSON content for path {}: {}", path, jsonObject);
    // Group JSON entry by time-stamp
    Map<Long, Collection<JsonArrayEntry>> jsonEntryByTimestamp = groupJsonEntryByTimestamp(jsonObject);
    // Extract baseName
    LwM2mPath baseName = extractAndValidateBaseName(jsonObject, path);
    if (baseName == null)
        // if no base name, use request path as base name
        baseName = path;
    // fill time-stamped nodes collection
    List<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>();
    for (Entry<Long, Collection<JsonArrayEntry>> entryByTimestamp : jsonEntryByTimestamp.entrySet()) {
        // Group JSON entry by instance
        Map<Integer, Collection<JsonArrayEntry>> jsonEntryByInstanceId = groupJsonEntryByInstanceId(entryByTimestamp.getValue(), baseName);
        // Create lwm2m node
        LwM2mNode node;
        if (nodeClass == LwM2mObject.class) {
            Collection<LwM2mObjectInstance> instances = new ArrayList<>();
            for (Entry<Integer, Collection<JsonArrayEntry>> entryByInstanceId : jsonEntryByInstanceId.entrySet()) {
                Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(entryByInstanceId.getValue(), baseName, model);
                instances.add(new LwM2mObjectInstance(entryByInstanceId.getKey(), resourcesMap.values()));
            }
            node = new LwM2mObject(baseName.getObjectId(), instances);
        } else if (nodeClass == LwM2mObjectInstance.class) {
            // validate we have resources for only 1 instance
            if (jsonEntryByInstanceId.size() != 1)
                throw new CodecException("One instance expected in the payload [path:%s]", path);
            // Extract resources
            Entry<Integer, Collection<JsonArrayEntry>> instanceEntry = jsonEntryByInstanceId.entrySet().iterator().next();
            Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(instanceEntry.getValue(), baseName, model);
            // Create instance
            node = new LwM2mObjectInstance(instanceEntry.getKey(), resourcesMap.values());
        } else if (nodeClass == LwM2mResource.class) {
            // validate we have resources for only 1 instance
            if (jsonEntryByInstanceId.size() > 1)
                throw new CodecException("Only one instance expected in the payload [path:%s]", path);
            // Extract resources
            Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(jsonEntryByInstanceId.values().iterator().next(), baseName, model);
            // validate there is only 1 resource
            if (resourcesMap.size() != 1)
                throw new CodecException("One resource should be present in the payload [path:%s]", path);
            node = resourcesMap.values().iterator().next();
        } else {
            throw new IllegalArgumentException("invalid node class: " + nodeClass);
        }
        // compute time-stamp
        Long timestamp = computeTimestamp(jsonObject.getBaseTime(), entryByTimestamp.getKey());
        // add time-stamped node
        timestampedNodes.add(new TimestampedLwM2mNode(timestamp, node));
    }
    return timestampedNodes;
}
Also used : ArrayList(java.util.ArrayList) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry) Entry(java.util.Map.Entry) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Collection(java.util.Collection) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) CodecException(org.eclipse.leshan.core.node.codec.CodecException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry)

Example 4 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class LwM2mNodeDeserializer method deserialize.

@Override
public LwM2mNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null) {
        return null;
    }
    LwM2mNode node;
    if (json.isJsonObject()) {
        JsonObject object = (JsonObject) json;
        if (!object.has("id")) {
            throw new JsonParseException("Missing id");
        }
        int id = object.get("id").getAsInt();
        if (object.has("instances")) {
            JsonArray array = object.get("instances").getAsJsonArray();
            LwM2mObjectInstance[] instances = new LwM2mObjectInstance[array.size()];
            for (int i = 0; i < array.size(); i++) {
                instances[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObject(id, instances);
        } else if (object.has("resources")) {
            JsonArray array = object.get("resources").getAsJsonArray();
            LwM2mResource[] resources = new LwM2mResource[array.size()];
            for (int i = 0; i < array.size(); i++) {
                resources[i] = context.deserialize(array.get(i), LwM2mNode.class);
            }
            node = new LwM2mObjectInstance(id, resources);
        } else if (object.has("value")) {
            // single value resource
            JsonPrimitive val = object.get("value").getAsJsonPrimitive();
            org.eclipse.leshan.core.model.ResourceModel.Type expectedType = getTypeFor(val);
            node = LwM2mSingleResource.newResource(id, deserializeValue(val, expectedType), expectedType);
        } else if (object.has("values")) {
            // multi-instances resource
            Map<Integer, Object> values = new HashMap<>();
            org.eclipse.leshan.core.model.ResourceModel.Type expectedType = null;
            for (Entry<String, JsonElement> entry : object.get("values").getAsJsonObject().entrySet()) {
                JsonPrimitive pval = entry.getValue().getAsJsonPrimitive();
                expectedType = getTypeFor(pval);
                values.put(Integer.valueOf(entry.getKey()), deserializeValue(pval, expectedType));
            }
            // use string by default;
            if (expectedType == null)
                expectedType = org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
            node = LwM2mMultipleResource.newResource(id, values, expectedType);
        } else {
            throw new JsonParseException("Invalid node element");
        }
    } else {
        throw new JsonParseException("Invalid node element");
    }
    return node;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) JsonParseException(com.google.gson.JsonParseException) JsonArray(com.google.gson.JsonArray) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Type(java.lang.reflect.Type) Entry(java.util.Map.Entry) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class LwM2mNodeSerDes method jSerialize.

public static JsonObject jSerialize(LwM2mNode n) {
    JsonObject o = Json.object();
    o.add("id", n.getId());
    if (n instanceof LwM2mObject) {
        o.add("kind", "object");
        JsonObject instances = Json.object();
        for (LwM2mObjectInstance instance : ((LwM2mObject) n).getInstances().values()) {
            instances.add(String.valueOf(instance.getId()), jSerialize(instance));
        }
        o.add("instances", instances);
    } else if (n instanceof LwM2mObjectInstance) {
        o.add("kind", "instance");
        JsonObject resources = Json.object();
        for (LwM2mResource resource : ((LwM2mObjectInstance) n).getResources().values()) {
            resources.add(String.valueOf(resource.getId()), jSerialize(resource));
        }
        o.add("resources", resources);
    } else if (n instanceof LwM2mResource) {
        LwM2mResource r = (LwM2mResource) n;
        o.add("type", r.getType().toString());
        if (r.isMultiInstances()) {
            o.add("kind", "multipleResource");
            JsonObject values = Json.object();
            for (Entry<Integer, ?> value : r.getValues().entrySet()) {
                values.add(value.getKey().toString(), ValueSerDes.jSerialize(value.getValue(), r.getType()));
            }
            o.add("values", values);
        } else {
            o.add("kind", "singleResource");
            o.add("value", ValueSerDes.jSerialize(r.getValue(), r.getType()));
        }
    }
    return o;
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) JsonObject(com.eclipsesource.json.JsonObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Aggregations

LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)21 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)13 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)6 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)5 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)4 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 Tlv (org.eclipse.leshan.tlv.Tlv)3 JsonObject (com.eclipsesource.json.JsonObject)2 Collection (java.util.Collection)2 Entry (java.util.Map.Entry)2 Type (org.eclipse.leshan.core.model.ResourceModel.Type)2 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 CodecException (org.eclipse.leshan.core.node.codec.CodecException)2 Observation (org.eclipse.leshan.core.observation.Observation)2 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)2