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();
}
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;
}
}
Aggregations