Search in sources :

Example 1 with ResourceModel

use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.

the class ObjectEnabler method doWrite.

@Override
protected WriteResponse doWrite(ServerIdentity identity, WriteRequest request) {
    LwM2mPath path = request.getPath();
    // Manage Instance case
    LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
    if (instance == null)
        return WriteResponse.notFound();
    if (path.isObjectInstance()) {
        // instance write
        Map<Integer, LwM2mResource> writeResources = ((LwM2mObjectInstance) request.getNode()).getResources();
        if (request.isReplaceRequest()) {
            // REPLACE
            // make them modifiable
            writeResources = new HashMap<>(writeResources);
            for (ResourceModel resourceModel : getObjectModel().resources.values()) {
                if (!identity.isLwm2mServer() || resourceModel.operations.isWritable()) {
                    LwM2mResource writeResource = writeResources.remove(resourceModel.id);
                    if (null != writeResource) {
                        instance.write(resourceModel.id, writeResource);
                    } else {
                        instance.reset(resourceModel.id);
                    }
                }
            }
        }
        // UPDATE and resources currently not in the model
        for (LwM2mResource resource : writeResources.values()) {
            instance.write(resource.getId(), resource);
        }
        return WriteResponse.success();
    }
    // Manage Resource case
    return instance.write(path.getResourceId(), (LwM2mResource) request.getNode());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 2 with ResourceModel

use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.

the class ResourceModelSerDes method deserialize.

@Override
public ResourceModel deserialize(JsonObject o) {
    if (o == null)
        return null;
    if (!o.isObject())
        return null;
    int id = o.getInt("id", -1);
    if (id < 0)
        return null;
    String name = o.getString("name", null);
    Operations operations = Operations.valueOf(o.getString("operations", null));
    String instancetype = o.getString("instancetype", null);
    boolean mandatory = o.getBoolean("mandatory", false);
    Type type = Type.valueOf(o.getString("type", "").toUpperCase());
    String range = o.getString("range", null);
    String units = o.getString("units", null);
    String description = o.getString("description", null);
    return new ResourceModel(id, name, operations, "multiple".equals(instancetype), mandatory, type, range, units, description);
}
Also used : Type(org.eclipse.leshan.core.model.ResourceModel.Type) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) Operations(org.eclipse.leshan.core.model.ResourceModel.Operations)

Example 3 with ResourceModel

use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.

the class LwM2mNodeTextDecoder method decode.

public static LwM2mNode decode(byte[] content, LwM2mPath path, LwM2mModel model) throws CodecException {
    if (!path.isResource())
        throw new CodecException("Invalid path %s : TextDecoder decodes resource only", path);
    ResourceModel rDesc = model.getResourceModel(path.getObjectId(), path.getResourceId());
    String strValue = content != null ? new String(content, StandardCharsets.UTF_8) : "";
    if (rDesc != null && rDesc.type != null) {
        return LwM2mSingleResource.newResource(path.getResourceId(), parseTextValue(strValue, rDesc.type, path), rDesc.type);
    } else {
        // unknown resource, returning a default string value
        return LwM2mSingleResource.newStringResource(path.getResourceId(), strValue);
    }
}
Also used : ResourceModel(org.eclipse.leshan.core.model.ResourceModel) CodecException(org.eclipse.leshan.core.node.codec.CodecException)

Example 4 with ResourceModel

use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.

the class ObjectModelSerDes method jSerialize.

@Override
public JsonObject jSerialize(ObjectModel m) {
    final JsonObject o = Json.object();
    o.add("name", m.name);
    o.add("id", m.id);
    o.add("instancetype", m.multiple ? "multiple" : "single");
    o.add("mandatory", m.mandatory);
    if (!ObjectModel.DEFAULT_VERSION.equals(m.version))
        o.add("version", m.version);
    o.add("description", m.description);
    // sort resources value
    List<ResourceModel> resourceSpecs = new ArrayList<>(m.resources.values());
    Collections.sort(resourceSpecs, new Comparator<ResourceModel>() {

        @Override
        public int compare(ResourceModel r1, ResourceModel r2) {
            return r1.id - r2.id;
        }
    });
    JsonArray rs = new JsonArray();
    for (ResourceModel rm : resourceSpecs) {
        rs.add(resourceModelSerDes.jSerialize(rm));
    }
    o.add("resourcedefs", rs);
    return o;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.eclipsesource.json.JsonObject) ResourceModel(org.eclipse.leshan.core.model.ResourceModel)

Example 5 with ResourceModel

use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.

the class LwM2mNodeDecoderTest method loadModel.

@BeforeClass
public static void loadModel() {
    // load default object
    List<ObjectModel> objects = ObjectLoader.loadDefault();
    // add object 65 from the LWM2M v1.0.1 specification (figure 28)
    List<ResourceModel> resForObj65 = new ArrayList<>();
    resForObj65.add(new ResourceModel(0, "res0", Operations.R, true, false, Type.OBJLNK, null, null, null));
    resForObj65.add(new ResourceModel(1, "res1", Operations.R, false, false, Type.STRING, null, null, null));
    resForObj65.add(new ResourceModel(2, "res2", Operations.R, false, false, Type.INTEGER, null, null, null));
    objects.add(new ObjectModel(65, "object link tests 65", "", ObjectModel.DEFAULT_VERSION, true, false, resForObj65));
    // add object 66 from the LWM2M v1.0.1 specification (figure 28)
    List<ResourceModel> resForObj66 = new ArrayList<>();
    resForObj66.add(new ResourceModel(0, "res0", Operations.R, true, false, Type.STRING, null, null, null));
    resForObj66.add(new ResourceModel(1, "res1", Operations.R, false, false, Type.STRING, null, null, null));
    resForObj66.add(new ResourceModel(2, "res2", Operations.R, false, false, Type.OBJLNK, null, null, null));
    objects.add(new ObjectModel(66, "object link tests 66", "", ObjectModel.DEFAULT_VERSION, true, false, resForObj66));
    model = new LwM2mModel(objects);
    decoder = new DefaultLwM2mNodeDecoder();
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) ArrayList(java.util.ArrayList) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) BeforeClass(org.junit.BeforeClass)

Aggregations

ResourceModel (org.eclipse.leshan.core.model.ResourceModel)12 ArrayList (java.util.ArrayList)4 Type (org.eclipse.leshan.core.model.ResourceModel.Type)4 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)4 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)3 CodecException (org.eclipse.leshan.core.node.codec.CodecException)3 Link (org.eclipse.leshan.Link)2 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)2 JsonArray (com.eclipsesource.json.JsonArray)1 JsonObject (com.eclipsesource.json.JsonObject)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 Operations (org.eclipse.leshan.core.model.ResourceModel.Operations)1 JsonArrayEntry (org.eclipse.leshan.json.JsonArrayEntry)1 JsonRootObject (org.eclipse.leshan.json.JsonRootObject)1