Search in sources :

Example 1 with UpdateRequest

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

the class RegisterResource method handleUpdate.

private void handleUpdate(CoapExchange exchange, Request request, String registrationId) {
    // Get identity
    Identity sender = extractIdentity(request.getSourceContext());
    // Create LwM2m request from CoAP request
    Long lifetime = null;
    String smsNumber = null;
    BindingMode binding = null;
    Link[] objectLinks = null;
    Map<String, String> additionalParams = new HashMap<>();
    for (String param : request.getOptions().getUriQuery()) {
        if (param.startsWith(QUERY_PARAM_LIFETIME)) {
            lifetime = Long.valueOf(param.substring(3));
        } else if (param.startsWith(QUERY_PARAM_SMS)) {
            smsNumber = param.substring(4);
        } else if (param.startsWith(QUERY_PARAM_BINDING_MODE)) {
            binding = BindingMode.valueOf(param.substring(2));
        } else {
            String[] tokens = param.split("\\=");
            if (tokens != null && tokens.length == 2) {
                additionalParams.put(tokens[0], tokens[1]);
            }
        }
    }
    if (request.getPayload() != null && request.getPayload().length > 0) {
        objectLinks = Link.parse(request.getPayload());
    }
    UpdateRequest updateRequest = new UpdateRequest(registrationId, lifetime, smsNumber, binding, objectLinks, additionalParams);
    // Handle request
    final SendableResponse<UpdateResponse> sendableResponse = registrationHandler.update(sender, updateRequest);
    UpdateResponse updateResponse = sendableResponse.getResponse();
    // Create CoAP Response from LwM2m request
    if (updateResponse.getCode().isError()) {
        exchange.respond(toCoapResponseCode(updateResponse.getCode()), updateResponse.getErrorMessage());
    } else {
        exchange.respond(toCoapResponseCode(updateResponse.getCode()));
    }
    sendableResponse.sent();
}
Also used : UpdateResponse(org.eclipse.leshan.core.response.UpdateResponse) HashMap(java.util.HashMap) UpdateRequest(org.eclipse.leshan.core.request.UpdateRequest) Identity(org.eclipse.leshan.core.request.Identity) EndpointContextUtil.extractIdentity(org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity) BindingMode(org.eclipse.leshan.core.request.BindingMode) Link(org.eclipse.leshan.Link)

Example 2 with UpdateRequest

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

the class RegistrationEngine method update.

private boolean update() throws InterruptedException {
    ServersInfo serversInfo = ServersInfoExtractor.getInfo(objectEnablers);
    DmServerInfo dmInfo = serversInfo.deviceMangements.values().iterator().next();
    if (dmInfo == null) {
        LOG.error("Trying to update registration but there is no LWM2M server config.");
        return false;
    }
    // Send update
    LOG.info("Trying to update registration to {} ...", dmInfo.getFullUri());
    UpdateResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(), new UpdateRequest(registrationID, null, null, null, null, null), DEFAULT_TIMEOUT);
    if (response == null) {
        registrationID = null;
        LOG.error("Registration update failed: Timeout.");
        if (observer != null) {
            observer.onUpdateTimeout(dmInfo);
        }
        return false;
    } else if (response.getCode() == ResponseCode.CHANGED) {
        // Update successful, so we reschedule new update
        LOG.info("Registration update succeed.");
        scheduleUpdate(dmInfo);
        if (observer != null) {
            observer.onUpdateSuccess(dmInfo, registrationID);
        }
        return true;
    } else {
        LOG.error("Registration update failed: {} {}.", response.getCode(), response.getErrorMessage());
        if (observer != null) {
            observer.onUpdateFailure(dmInfo, response.getCode(), response.getErrorMessage());
        }
        return false;
    }
}
Also used : UpdateResponse(org.eclipse.leshan.core.response.UpdateResponse) UpdateRequest(org.eclipse.leshan.core.request.UpdateRequest)

Aggregations

UpdateRequest (org.eclipse.leshan.core.request.UpdateRequest)2 UpdateResponse (org.eclipse.leshan.core.response.UpdateResponse)2 HashMap (java.util.HashMap)1 Link (org.eclipse.leshan.Link)1 EndpointContextUtil.extractIdentity (org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity)1 BindingMode (org.eclipse.leshan.core.request.BindingMode)1 Identity (org.eclipse.leshan.core.request.Identity)1