use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.
the class WriteTest method write_time_resource.
private void write_time_resource(ContentFormat format) throws InterruptedException {
// write resource
// second accuracy
Date expectedvalue = new Date(946681000l);
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, TIME_RESOURCE_ID, expectedvalue));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read resource to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, TIME_RESOURCE_ID));
LwM2mResource resource = (LwM2mResource) readResponse.getContent();
assertEquals(expectedvalue, resource.getValue());
}
use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.
the class WriteTest method write_float_resource.
private void write_float_resource(ContentFormat format) throws InterruptedException {
// write resource
double expectedvalue = 999.99;
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, FLOAT_RESOURCE_ID, expectedvalue));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read resource to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, FLOAT_RESOURCE_ID));
LwM2mResource resource = (LwM2mResource) readResponse.getContent();
assertEquals(expectedvalue, resource.getValue());
}
use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.
the class WriteTest method can_write_object_instance.
public void can_write_object_instance(ContentFormat format) throws InterruptedException {
// write device timezone and offset
LwM2mResource utcOffset = LwM2mSingleResource.newStringResource(14, "+02");
LwM2mResource timeZone = LwM2mSingleResource.newStringResource(15, "Europe/Paris");
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.REPLACE, format, 3, 0, utcOffset, timeZone));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read the timezone to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0));
LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
assertEquals(utcOffset, instance.getResource(14));
assertEquals(timeZone, instance.getResource(15));
}
use of org.eclipse.leshan.core.node.LwM2mResource 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;
}
use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.
the class LwM2mNodeSerDes method deserialize.
public static LwM2mNode deserialize(JsonObject o) {
String kind = o.getString("kind", null);
int id = o.getInt("id", LwM2mObjectInstance.UNDEFINED);
switch(kind) {
case "object":
{
Collection<LwM2mObjectInstance> instances = new ArrayList<>();
JsonArray jInstances = (JsonArray) o.get("instances");
for (JsonValue jInstance : jInstances) {
LwM2mObjectInstance instance = (LwM2mObjectInstance) deserialize((JsonObject) jInstance);
instances.add(instance);
}
return new LwM2mObject(id, instances);
}
case "instance":
{
Collection<LwM2mResource> resources = new ArrayList<>();
JsonObject jResources = (JsonObject) o.get("resources");
for (Member jResource : jResources) {
LwM2mResource resource = (LwM2mResource) deserialize((JsonObject) jResource.getValue());
resources.add(resource);
}
return new LwM2mObjectInstance(id, resources);
}
case "singleResource":
{
String jType = o.getString("type", null);
if (jType == null)
throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
Type type = Enum.valueOf(Type.class, jType);
Object value = ValueSerDes.deserialize(o.get("value"), type);
return LwM2mSingleResource.newResource(id, value, type);
}
case "multipleResource":
{
String jType = o.getString("type", null);
if (jType == null)
throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
Type type = Enum.valueOf(Type.class, jType);
Map<Integer, Object> values = new HashMap<>();
JsonObject jValues = (JsonObject) o.get("values");
for (Member jValue : jValues) {
Integer valueId = Integer.valueOf(jValue.getName());
Object value = ValueSerDes.deserialize(jValue.getValue(), type);
values.put(valueId, value);
}
return LwM2mMultipleResource.newResource(id, values, type);
}
default:
throw new IllegalStateException("Invalid LwM2mNode missing kind attribute");
}
}
Aggregations