Search in sources :

Example 1 with ReadRequest

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

the class ServersInfoExtractor method getInfo.

public static ServersInfo getInfo(Map<Integer, LwM2mObjectEnabler> objectEnablers) {
    LwM2mObjectEnabler securityEnabler = objectEnablers.get(SECURITY);
    LwM2mObjectEnabler serverEnabler = objectEnablers.get(SERVER);
    if (securityEnabler == null || serverEnabler == null)
        return null;
    ServersInfo infos = new ServersInfo();
    LwM2mObject securities = (LwM2mObject) securityEnabler.read(SYSTEM, new ReadRequest(SECURITY)).getContent();
    LwM2mObject servers = (LwM2mObject) serverEnabler.read(SYSTEM, new ReadRequest(SERVER)).getContent();
    for (LwM2mObjectInstance security : securities.getInstances().values()) {
        try {
            if ((boolean) security.getResource(SEC_BOOTSTRAP).getValue()) {
                if (infos.bootstrap != null) {
                    LOG.warn("There is more than one bootstrap configuration in security object.");
                } else {
                    // create bootstrap info
                    ServerInfo info = new ServerInfo();
                    LwM2mResource serverIdResource = security.getResource(SEC_SERVER_ID);
                    if (serverIdResource != null && serverIdResource.getValue() != null)
                        info.serverId = (long) serverIdResource.getValue();
                    else
                        info.serverId = 0;
                    info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                    info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                    infos.bootstrap = info;
                }
            } else {
                // create device management info
                DmServerInfo info = new DmServerInfo();
                info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                info.serverId = (long) security.getResource(SEC_SERVER_ID).getValue();
                info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                // search corresponding device management server
                for (LwM2mObjectInstance server : servers.getInstances().values()) {
                    if (info.serverId == (Long) server.getResource(SRV_SERVER_ID).getValue()) {
                        info.lifetime = (long) server.getResource(SRV_LIFETIME).getValue();
                        info.binding = BindingMode.valueOf((String) server.getResource(SRV_BINDING).getValue());
                        infos.deviceMangements.put(info.serverId, info);
                        break;
                    }
                }
            }
        } catch (URISyntaxException e) {
            LOG.error(String.format("Invalid URI %s", (String) security.getResource(SEC_SERVER_URI).getValue()), e);
        }
    }
    return infos;
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 2 with ReadRequest

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

the class ObjectResource method handleGET.

@Override
public void handleGET(CoapExchange exchange) {
    ServerIdentity identity = extractServerIdentity(exchange, bootstrapHandler);
    String URI = exchange.getRequestOptions().getUriPathString();
    // Manage Discover Request
    if (exchange.getRequestOptions().getAccept() == MediaTypeRegistry.APPLICATION_LINK_FORMAT) {
        DiscoverResponse response = nodeEnabler.discover(identity, new DiscoverRequest(URI));
        if (response.getCode().isError()) {
            exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
        } else {
            exchange.respond(toCoapResponseCode(response.getCode()), Link.serialize(response.getObjectLinks()), MediaTypeRegistry.APPLICATION_LINK_FORMAT);
        }
    } else {
        // handle content format for Read and Observe Request
        // use TLV as default format
        ContentFormat format = ContentFormat.TLV;
        if (exchange.getRequestOptions().hasAccept()) {
            format = ContentFormat.fromCode(exchange.getRequestOptions().getAccept());
            if (!encoder.isSupported(format)) {
                exchange.respond(ResponseCode.NOT_ACCEPTABLE);
                return;
            }
        }
        // Manage Observe Request
        if (exchange.getRequestOptions().hasObserve()) {
            ObserveResponse response = nodeEnabler.observe(identity, new ObserveRequest(URI));
            if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
                LwM2mPath path = new LwM2mPath(URI);
                LwM2mNode content = response.getContent();
                LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
                exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model), format.getCode());
                return;
            } else {
                exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                return;
            }
        } else // Manage Read Request
        {
            ReadResponse response = nodeEnabler.read(identity, new ReadRequest(URI));
            if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
                LwM2mPath path = new LwM2mPath(URI);
                LwM2mNode content = response.getContent();
                LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
                exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model), format.getCode());
                return;
            } else {
                exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                return;
            }
        }
    }
}
Also used : ResourceUtil.extractServerIdentity(org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity) ServerIdentity(org.eclipse.leshan.client.request.ServerIdentity) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 3 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest 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));
}
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)

Example 4 with ReadRequest

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

the class WriteTest method write_time_resource.

private void write_time_resource(ContentFormat format) throws InterruptedException {
    // write resource
    // second accuracy
    Date expectedvalue = new Date(946681000l);
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, TIME_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, TIME_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) Date(java.util.Date) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 5 with ReadRequest

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

the class WriteTest method write_float_resource.

private void write_float_resource(ContentFormat format) throws InterruptedException {
    // write resource
    double expectedvalue = 999.99;
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, FLOAT_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, FLOAT_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)

Aggregations

ReadRequest (org.eclipse.leshan.core.request.ReadRequest)43 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)34 Test (org.junit.Test)30 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)19 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)14 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)12 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)10 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)9 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)7 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)7 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)5 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)5 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)5 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)5 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)4 TimeoutException (org.eclipse.leshan.core.request.exception.TimeoutException)4 JsonObject (com.eclipsesource.json.JsonObject)3 Request (org.eclipse.californium.core.coap.Request)3 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3