Search in sources :

Example 31 with LwM2mResource

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

the class LwM2mNodeJsonDecoder method extractLwM2mResources.

private static Map<Integer, LwM2mResource> extractLwM2mResources(Collection<JsonArrayEntry> jsonArrayEntries, LwM2mPath baseName, LwM2mModel model) throws CodecException {
    if (jsonArrayEntries == null)
        return Collections.emptyMap();
    // Extract LWM2M resources from JSON resource list
    Map<Integer, LwM2mResource> lwM2mResourceMap = new HashMap<>();
    Map<LwM2mPath, Map<Integer, JsonArrayEntry>> multiResourceMap = new HashMap<>();
    for (JsonArrayEntry resourceElt : jsonArrayEntries) {
        // Build resource path
        LwM2mPath nodePath = baseName.append(resourceElt.getName());
        // handle LWM2M resources
        if (nodePath.isResourceInstance()) {
            // Multi-instance resource
            // Store multi-instance resource values in a map
            // we will deal with it later
            LwM2mPath resourcePath = new LwM2mPath(nodePath.getObjectId(), nodePath.getObjectInstanceId(), nodePath.getResourceId());
            Map<Integer, JsonArrayEntry> multiResource = multiResourceMap.get(resourcePath);
            if (multiResource == null) {
                multiResource = new HashMap<>();
                multiResourceMap.put(resourcePath, multiResource);
            }
            multiResource.put(nodePath.getResourceInstanceId(), resourceElt);
        } else if (nodePath.isResource()) {
            // Single resource
            Type expectedType = getResourceType(nodePath, model, resourceElt);
            LwM2mResource res = LwM2mSingleResource.newResource(nodePath.getResourceId(), parseJsonValue(resourceElt.getResourceValue(), expectedType, nodePath), expectedType);
            lwM2mResourceMap.put(nodePath.getResourceId(), res);
        } else {
            throw new CodecException("Invalid path [%s] for resource, it should be a resource or a resource instance path", nodePath);
        }
    }
    // Handle multi-instance resource.
    for (Map.Entry<LwM2mPath, Map<Integer, JsonArrayEntry>> entry : multiResourceMap.entrySet()) {
        LwM2mPath resourcePath = entry.getKey();
        Map<Integer, JsonArrayEntry> jsonEntries = entry.getValue();
        if (jsonEntries != null && !jsonEntries.isEmpty()) {
            Type expectedType = getResourceType(resourcePath, model, jsonEntries.values().iterator().next());
            Map<Integer, Object> values = new HashMap<>();
            for (Entry<Integer, JsonArrayEntry> e : jsonEntries.entrySet()) {
                Integer resourceInstanceId = e.getKey();
                values.put(resourceInstanceId, parseJsonValue(e.getValue().getResourceValue(), expectedType, resourcePath));
            }
            LwM2mResource resource = LwM2mMultipleResource.newResource(resourcePath.getResourceId(), values, expectedType);
            lwM2mResourceMap.put(resourcePath.getResourceId(), resource);
        }
    }
    // If we found nothing, we try to create an empty multi-instance resource
    if (lwM2mResourceMap.isEmpty() && baseName.isResource()) {
        ResourceModel resourceModel = model.getResourceModel(baseName.getObjectId(), baseName.getResourceId());
        // We create it only if this respect the model
        if (resourceModel == null || resourceModel.multiple) {
            Type resourceType = getResourceType(baseName, model, null);
            lwM2mResourceMap.put(baseName.getResourceId(), LwM2mMultipleResource.newResource(baseName.getResourceId(), new HashMap<Integer, Object>(), resourceType));
        }
    }
    return lwM2mResourceMap;
}
Also used : HashMap(java.util.HashMap) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Type(org.eclipse.leshan.core.model.ResourceModel.Type) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) CodecException(org.eclipse.leshan.core.node.codec.CodecException) JsonRootObject(org.eclipse.leshan.json.JsonRootObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry)

Example 32 with LwM2mResource

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

the class LwM2mNodeSerializer method serialize.

@Override
public JsonElement serialize(LwM2mNode src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject element = new JsonObject();
    element.addProperty("id", src.getId());
    if (typeOfSrc == LwM2mObject.class) {
        element.add("instances", context.serialize(((LwM2mObject) src).getInstances().values()));
    } else if (typeOfSrc == LwM2mObjectInstance.class) {
        element.add("resources", context.serialize(((LwM2mObjectInstance) src).getResources().values()));
    } else if (LwM2mResource.class.isAssignableFrom((Class<?>) typeOfSrc)) {
        LwM2mResource rsc = (LwM2mResource) src;
        if (rsc.isMultiInstances()) {
            JsonObject values = new JsonObject();
            for (Entry<Integer, ?> entry : rsc.getValues().entrySet()) {
                if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) {
                    values.add(entry.getKey().toString(), context.serialize(Hex.encodeHex((byte[]) entry.getValue())));
                } else {
                    values.add(entry.getKey().toString(), context.serialize(entry.getValue()));
                }
            }
            element.add("values", values);
        } else {
            if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) {
                element.add("value", context.serialize(Hex.encodeHex((byte[]) rsc.getValue())));
            } else {
                element.add("value", context.serialize(rsc.getValue()));
            }
        }
    }
    return element;
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) JsonObject(com.google.gson.JsonObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 33 with LwM2mResource

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

the class DownlinkRequestSerDes method jSerialize.

public static JsonObject jSerialize(DownlinkRequest<?> r) {
    final JsonObject o = Json.object();
    o.add("path", r.getPath().toString());
    r.accept(new DownLinkRequestVisitorAdapter() {

        @Override
        public void visit(ObserveRequest request) {
            o.add("kind", "observe");
            if (request.getContentFormat() != null)
                o.add("contentFormat", request.getContentFormat().getCode());
        }

        @Override
        public void visit(DeleteRequest request) {
            o.add("kind", "delete");
        }

        @Override
        public void visit(DiscoverRequest request) {
            o.add("kind", "discover");
        }

        @Override
        public void visit(CreateRequest request) {
            o.add("kind", "create");
            o.add("contentFormat", request.getContentFormat().getCode());
            if (request.getInstanceId() != null)
                o.add("instanceId", request.getInstanceId());
            JsonArray resources = new JsonArray();
            for (LwM2mResource resource : request.getResources()) {
                resources.add(LwM2mNodeSerDes.jSerialize(resource));
            }
            o.add("resources", resources);
        }

        @Override
        public void visit(ExecuteRequest request) {
            o.add("kind", "execute");
            o.add("parameters", request.getParameters());
        }

        @Override
        public void visit(WriteAttributesRequest request) {
            o.add("kind", "writeAttributes");
            o.add("observeSpec", request.getObserveSpec().toString());
        }

        @Override
        public void visit(WriteRequest request) {
            o.add("kind", "write");
            o.add("contentFormat", request.getContentFormat().getCode());
            o.add("mode", request.isPartialUpdateRequest() ? "UPDATE" : "REPLACE");
            o.add("node", LwM2mNodeSerDes.jSerialize(request.getNode()));
        }

        @Override
        public void visit(ReadRequest request) {
            o.add("kind", "read");
            if (request.getContentFormat() != null)
                o.add("contentFormat", request.getContentFormat().getCode());
        }
    });
    return o;
}
Also used : CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) JsonObject(com.eclipsesource.json.JsonObject) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) DownLinkRequestVisitorAdapter(org.eclipse.leshan.core.request.DownLinkRequestVisitorAdapter) JsonArray(com.eclipsesource.json.JsonArray) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Aggregations

LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)33 Test (org.junit.Test)15 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)14 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)13 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)13 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)11 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)10 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)10 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)9 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)5 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)5 JsonObject (com.eclipsesource.json.JsonObject)4 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 JsonArray (com.eclipsesource.json.JsonArray)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JsonValue (com.eclipsesource.json.JsonValue)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2