Search in sources :

Example 11 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse 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 12 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse in project leshan by eclipse.

the class ResponseSerDes method jSerialize.

public static JsonObject jSerialize(LwM2mResponse r) {
    final JsonObject o = Json.object();
    o.add("code", r.getCode().toString());
    if (r.isFailure()) {
        o.add("errorMessage", r.getErrorMessage());
        return o;
    }
    if (r instanceof ReadResponse) {
        o.add("kind", "read");
        o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent()));
    } else if (r instanceof ObserveResponse) {
        o.add("kind", "observe");
        o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent()));
    } else if (r instanceof DiscoverResponse) {
        o.add("kind", "discover");
        o.add("objectLinks", Link.serialize(((DiscoverResponse) r).getObjectLinks()));
    } else if (r instanceof DeleteResponse) {
        o.add("kind", "delete");
    } else if (r instanceof ExecuteResponse) {
        o.add("kind", "execute");
    } else if (r instanceof WriteResponse) {
        o.add("kind", "write");
    } else if (r instanceof WriteAttributesResponse) {
        o.add("kind", "writeAttributes");
    } else if (r instanceof CreateResponse) {
        o.add("kind", "create");
        o.add("location", ((CreateResponse) r).getLocation());
    }
    return o;
}
Also used : DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) JsonObject(com.eclipsesource.json.JsonObject) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse)

Example 13 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse in project leshan by eclipse.

the class WriteTest method write_opaque_resource.

private void write_opaque_resource(ContentFormat format) throws InterruptedException {
    // write resource
    byte[] expectedvalue = new byte[] { 1, 2, 3 };
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, OPAQUE_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, OPAQUE_RESOURCE_ID));
    LwM2mResource resource = (LwM2mResource) readResponse.getContent();
    assertArrayEquals(expectedvalue, (byte[]) 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 WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse in project leshan by eclipse.

the class WriteTest method can_write_single_instance_objlnk_resource_in_tlv.

@Test
public void can_write_single_instance_objlnk_resource_in_tlv() throws InterruptedException {
    ObjectLink data = new ObjectLink(10245, 1);
    // Write objlnk resource in TLV format
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(ContentFormat.TLV, IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_SINGLE_INSTANCE_RESOURCE_ID, data));
    // 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_SINGLE_INSTANCE_RESOURCE_ID));
    LwM2mSingleResource resource = (LwM2mSingleResource) readResponse.getContent();
    // verify read value
    assertEquals(((ObjectLink) resource.getValue()).getObjectId(), 10245);
    assertEquals(((ObjectLink) resource.getValue()).getObjectInstanceId(), 1);
}
Also used : LwM2mSingleResource(org.eclipse.leshan.core.node.LwM2mSingleResource) 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 15 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse in project leshan by eclipse.

the class WriteTest method can_write_updating_object_instance.

@Test
public void can_write_updating_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);
    response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.UPDATE, 1, 0, lifetime, defaultMinPeriod));
    // 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));
    // no resources are removed when updating
    assertNotNull(instance.getResource(3));
    assertNotNull(instance.getResource(6));
    assertNotNull(instance.getResource(7));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) 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) Test(org.junit.Test)

Aggregations

WriteResponse (org.eclipse.leshan.core.response.WriteResponse)21 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)19 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)14 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)12 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)10 Test (org.junit.Test)8 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)4 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)4 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)3 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)3 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)3 WriteAttributesResponse (org.eclipse.leshan.core.response.WriteAttributesResponse)3 JsonObject (com.eclipsesource.json.JsonObject)2 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)2 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)2 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)2 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)2 LwM2mSingleResource (org.eclipse.leshan.core.node.LwM2mSingleResource)2 CodecException (org.eclipse.leshan.core.node.codec.CodecException)2