Search in sources :

Example 6 with DiscoverRequest

use of org.eclipse.leshan.core.request.DiscoverRequest 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 7 with DiscoverRequest

use of org.eclipse.leshan.core.request.DiscoverRequest in project leshan by eclipse.

the class DiscoverTest method can_discover_object.

@Test
public void can_discover_object() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(2));
    // verify result
    assertEquals(CONTENT, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    Link[] payload = response.getObjectLinks();
    assertArrayEquals(Link.parse("</2>, </2/0/0>, </2/0/1>, </2/0/2>, </2/0/3>".getBytes()), payload);
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 8 with DiscoverRequest

use of org.eclipse.leshan.core.request.DiscoverRequest in project leshan by eclipse.

the class DiscoverTest method can_discover_resource.

@Test
public void can_discover_resource() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 0, 0));
    // verify result
    assertEquals(CONTENT, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    Link[] payload = response.getObjectLinks();
    assertArrayEquals(Link.parse("</3/0/0>".getBytes()), payload);
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 9 with DiscoverRequest

use of org.eclipse.leshan.core.request.DiscoverRequest in project leshan by eclipse.

the class DiscoverTest method cant_discover_resource_of_non_existent_object.

@Test
public void cant_discover_resource_of_non_existent_object() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(4, 0, 0));
    // verify result
    assertEquals(NOT_FOUND, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Test(org.junit.Test)

Example 10 with DiscoverRequest

use of org.eclipse.leshan.core.request.DiscoverRequest in project leshan by eclipse.

the class DiscoverTest method cant_discover_resource_of_non_existent_resource.

@Test
public void cant_discover_resource_of_non_existent_resource() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 0, 42));
    // verify result
    assertEquals(NOT_FOUND, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Test(org.junit.Test)

Aggregations

DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)14 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)11 Test (org.junit.Test)10 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)5 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)4 Link (org.eclipse.leshan.Link)3 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)3 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)3 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)3 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 JsonArray (com.eclipsesource.json.JsonArray)2 JsonObject (com.eclipsesource.json.JsonObject)2 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)2 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)2 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)2 Registration (org.eclipse.leshan.server.registration.Registration)2 JsonValue (com.eclipsesource.json.JsonValue)1 ArrayList (java.util.ArrayList)1