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());
}
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);
}
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);
}
}
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;
}
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();
}
Aggregations