use of org.eclipse.leshan.core.request.WriteRequest in project leshan by eclipse.
the class WriteTest method can_write_multi_instance_objlnk_resource_in_tlv.
@Test
public void can_write_multi_instance_objlnk_resource_in_tlv() throws InterruptedException {
Map<Integer, ObjectLink> neighbourCellReport = new HashMap<>();
neighbourCellReport.put(0, new ObjectLink(10245, 1));
neighbourCellReport.put(1, new ObjectLink(10242, 2));
neighbourCellReport.put(2, new ObjectLink(10244, 3));
// Write objlnk resource in TLV format
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(ContentFormat.TLV, IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_MULTI_INSTANCE_RESOURCE_ID, neighbourCellReport, Type.OBJLNK));
// Verify Write result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// Reading back the written OBJLNK value
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_MULTI_INSTANCE_RESOURCE_ID));
LwM2mMultipleResource resource = (LwM2mMultipleResource) readResponse.getContent();
// verify read value
assertEquals(((ObjectLink) resource.getValue(0)).getObjectId(), 10245);
assertEquals(((ObjectLink) resource.getValue(0)).getObjectInstanceId(), 1);
assertEquals(((ObjectLink) resource.getValue(1)).getObjectId(), 10242);
assertEquals(((ObjectLink) resource.getValue(1)).getObjectInstanceId(), 2);
assertEquals(((ObjectLink) resource.getValue(2)).getObjectId(), 10244);
assertEquals(((ObjectLink) resource.getValue(2)).getObjectInstanceId(), 3);
}
use of org.eclipse.leshan.core.request.WriteRequest in project leshan by eclipse.
the class WriteTest method write_boolean_resource.
private void write_boolean_resource(ContentFormat format) throws InterruptedException {
// write resource
boolean expectedvalue = true;
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, BOOLEAN_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, BOOLEAN_RESOURCE_ID));
LwM2mResource resource = (LwM2mResource) readResponse.getContent();
assertEquals(expectedvalue, resource.getValue());
}
use of org.eclipse.leshan.core.request.WriteRequest in project leshan by eclipse.
the class WriteTest method write_integer_resource.
private void write_integer_resource(ContentFormat format) throws InterruptedException {
// write resource
long expectedvalue = 999l;
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, INTEGER_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, INTEGER_RESOURCE_ID));
LwM2mResource resource = (LwM2mResource) readResponse.getContent();
assertEquals(expectedvalue, resource.getValue());
}
use of org.eclipse.leshan.core.request.WriteRequest in project leshan by eclipse.
the class DownlinkRequestSerDes method deserialize.
public static DownlinkRequest<?> deserialize(JsonObject o) {
String kind = o.getString("kind", null);
String path = o.getString("path", null);
switch(kind) {
case "observe":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
return new ObserveRequest(ContentFormat.fromCode(format), path);
}
case "delete":
return new DeleteRequest(path);
case "discover":
return new DiscoverRequest(path);
case "create":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
int instanceId = o.getInt("instanceId", LwM2mObjectInstance.UNDEFINED);
Collection<LwM2mResource> resources = new ArrayList<>();
JsonArray jResources = (JsonArray) o.get("resources");
for (JsonValue jResource : jResources) {
LwM2mResource resource = (LwM2mResource) LwM2mNodeSerDes.deserialize((JsonObject) jResource);
resources.add(resource);
}
return new CreateRequest(ContentFormat.fromCode(format), path, new LwM2mObjectInstance(instanceId, resources));
}
case "execute":
String parameters = o.getString("parameters", null);
return new ExecuteRequest(path, parameters);
case "writeAttributes":
{
String observeSpec = o.getString("observeSpec", null);
return new WriteAttributesRequest(path, ObserveSpec.parse(observeSpec));
}
case "write":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
Mode mode = o.getString("mode", "REPLACE").equals("REPLACE") ? Mode.REPLACE : Mode.UPDATE;
LwM2mNode node = LwM2mNodeSerDes.deserialize((JsonObject) o.get("node"));
return new WriteRequest(mode, ContentFormat.fromCode(format), path, node);
}
case "read":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
return new ReadRequest(ContentFormat.fromCode(format), path);
}
default:
throw new IllegalStateException("Invalid request missing kind attribute");
}
}
use of org.eclipse.leshan.core.request.WriteRequest in project leshan by eclipse.
the class ObjectEnabler method doWrite.
@Override
protected BootstrapWriteResponse doWrite(ServerIdentity identity, BootstrapWriteRequest request) {
LwM2mPath path = request.getPath();
// Manage Object case
if (path.isObject()) {
for (LwM2mObjectInstance instanceNode : ((LwM2mObject) request.getNode()).getInstances().values()) {
LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId());
if (instanceEnabler == null) {
doCreate(new CreateRequest(path.getObjectId(), instanceNode));
} else {
doWrite(identity, new WriteRequest(Mode.REPLACE, path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
}
}
return BootstrapWriteResponse.success();
}
// Manage Instance case
if (path.isObjectInstance()) {
LwM2mObjectInstance instanceNode = (LwM2mObjectInstance) request.getNode();
LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
if (instanceEnabler == null) {
doCreate(new CreateRequest(path.getObjectId(), instanceNode));
} else {
doWrite(identity, new WriteRequest(Mode.REPLACE, request.getContentFormat(), path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
}
return BootstrapWriteResponse.success();
}
// Manage resource case
LwM2mResource resource = (LwM2mResource) request.getNode();
LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
if (instanceEnabler == null) {
doCreate(new CreateRequest(path.getObjectId(), new LwM2mObjectInstance(path.getObjectInstanceId(), resource)));
} else {
instanceEnabler.write(path.getResourceId(), resource);
}
return BootstrapWriteResponse.success();
}
Aggregations