use of org.eclipse.leshan.core.node.LwM2mObjectInstance 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;
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class WriteTest method can_write_replacing_object_instance.
@Test
public void can_write_replacing_object_instance() throws InterruptedException {
// setup server object
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(1, 0, 3, 60));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// write server object
LwM2mResource lifetime = LwM2mSingleResource.newIntegerResource(1, 120);
LwM2mResource defaultMinPeriod = LwM2mSingleResource.newIntegerResource(2, 10);
LwM2mResource notificationStoring = LwM2mSingleResource.newBooleanResource(6, false);
LwM2mResource binding = LwM2mSingleResource.newStringResource(7, "U");
response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.REPLACE, 1, 0, lifetime, defaultMinPeriod, notificationStoring, binding));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read the values to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(1, 0));
LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
assertEquals(lifetime, instance.getResource(1));
assertEquals(defaultMinPeriod, instance.getResource(2));
assertEquals(notificationStoring, instance.getResource(6));
assertEquals(binding, instance.getResource(7));
// removed not contained optional writable resource
assertNull(instance.getResource(3));
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance 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.LwM2mObjectInstance in project leshan by eclipse.
the class CoapRequestBuilderTest method build_create_request__with_instance_id.
@Test
public void build_create_request__with_instance_id() throws Exception {
Registration reg = newRegistration();
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
CreateRequest request = new CreateRequest(12, new LwM2mObjectInstance(26, LwM2mSingleResource.newStringResource(0, "value")));
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals(CoAP.Code.POST, coapRequest.getCode());
assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
assertEquals("coap://127.0.0.1:12354/12", coapRequest.getURI());
assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
assertNotNull(coapRequest.getPayload());
// assert it is encoded as array of instance TLV
Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
assertEquals(TlvType.OBJECT_INSTANCE, tlvs[0].getType());
assertEquals(26, tlvs[0].getIdentifier());
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance 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;
}
Aggregations