Search in sources :

Example 11 with WriteRequest

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);
}
Also used : LwM2mMultipleResource(org.eclipse.leshan.core.node.LwM2mMultipleResource) HashMap(java.util.HashMap) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) ObjectLink(org.eclipse.leshan.core.node.ObjectLink) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 12 with WriteRequest

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());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 13 with WriteRequest

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());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 14 with WriteRequest

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");
    }
}
Also used : CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Mode(org.eclipse.leshan.core.request.WriteRequest.Mode) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) JsonArray(com.eclipsesource.json.JsonArray) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Collection(java.util.Collection) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 15 with WriteRequest

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();
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Aggregations

WriteRequest (org.eclipse.leshan.core.request.WriteRequest)27 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)19 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)18 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)14 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)13 Test (org.junit.Test)13 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)7 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)7 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)6 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)5 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)5 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)4 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)4 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)3 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 Observation (org.eclipse.leshan.core.observation.Observation)3 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)3 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)3 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)3