/** * @param path the end of the new path * @return a new path which is the concatenation of this path and the given one in parameter. */ public LwM2mPath append(String path) { LwM2mPath pathToAdd = new LwM2mPath(path); if (isRoot()) { return pathToAdd; } else { return new LwM2mPath(this.toString() + pathToAdd.toString()); } }
/** * Create a CoAP observe request context with specific keys needed for internal Leshan working. */ public static Map<String, String> createCoapObserveRequestContext(String endpoint, String registrationId, ObserveRequest request) { Map<String, String> context = new HashMap<>(); context.put(CTX_ENDPOINT, endpoint); context.put(CTX_REGID, registrationId); context.put(CTX_LWM2M_PATH, request.getPath().toString()); for (Entry<String, String> ctx : request.getContext().entrySet()) { context.put(ctx.getKey(), ctx.getValue()); } return context; }
@Override public void onResponse(Observation observation, Registration registration, ObserveResponse response) { if (LOG.isDebugEnabled()) { LOG.debug("Received notification from [{}] containing value [{}]", observation.getPath(), response.getContent().toString()); } if (registration != null) { String data = new StringBuilder("{\"ep\":\"").append(registration.getEndpoint()).append("\",\"res\":\"") .append(observation.getPath().toString()).append("\",\"val\":") .append(gson.toJson(response.getContent())).append("}").toString(); sendEvent(EVENT_NOTIFICATION, data, registration.getEndpoint()); } }
private ValueResponse buildContentResponse(final LwM2mPath path, final Response coapResponse) { final ResponseCode code = ResponseCode.CONTENT; LwM2mNode content; try { content = LwM2mNodeDecoder.decode(coapResponse.getPayload(), ContentFormat.fromCode(coapResponse.getOptions().getContentFormat()), path); } catch (final InvalidValueException e) { final String msg = String.format("[%s] ([%s])", e.getMessage(), e.getPath().toString()); throw new ResourceAccessException(code, path.toString(), msg, e); } return new ValueResponse(code, content); }
@Override protected CreateResponse doCreate(CreateRequest request) { Integer instanceId = request.getInstanceId(); if (instanceId == null) { // the client is in charge to generate the id of the new instance if (instances.isEmpty()) { instanceId = 0; } else { instanceId = Collections.max(instances.keySet()) + 1; } } LwM2mInstanceEnabler newInstance = instanceFactory.create(getObjectModel()); for (LwM2mResource resource : request.getResources()) { newInstance.write(resource.getId(), resource); } instances.put(instanceId, newInstance); listenInstance(newInstance, instanceId); return CreateResponse.success(new LwM2mPath(request.getPath().getObjectId(), instanceId).toString()); }
protected ObserveResponse doObserve(ServerIdentity identity, ObserveRequest request) { ReadResponse readResponse = this.read(identity, new ReadRequest(request.getPath().toString())); return new ObserveResponse(readResponse.getCode(), readResponse.getContent(), null, null, readResponse.getErrorMessage()); }
public static byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampedNodes, LwM2mPath path, LwM2mModel model, LwM2mValueConverter converter) { Validate.notNull(timestampedNodes); Validate.notNull(path); Validate.notNull(model); InternalEncoder internalEncoder = new InternalEncoder(); ArrayList<JsonArrayEntry> entries = new ArrayList<>(); for (TimestampedLwM2mNode timestampedLwM2mNode : timestampedNodes) { internalEncoder.objectId = path.getObjectId(); internalEncoder.model = model; internalEncoder.requestPath = path; internalEncoder.converter = converter; internalEncoder.resourceList = null; internalEncoder.timestamp = timestampedLwM2mNode.getTimestamp(); timestampedLwM2mNode.getNode().accept(internalEncoder); entries.addAll(internalEncoder.resourceList); } JsonRootObject jsonObject = new JsonRootObject(); jsonObject.setResourceList(entries); jsonObject.setBaseName(path.toString()); return LwM2mJson.toJsonLwM2m(jsonObject).getBytes(); }
@Override public void onResponse(Response coapResponse) { if (coapResponse.getCode() == CoAP.ResponseCode.CHANGED || coapResponse.getCode() == CoAP.ResponseCode.CONTENT) { try { LwM2mNode content = LwM2mNodeDecoder.decode(coapResponse.getPayload(), ContentFormat.fromCode(coapResponse.getOptions().getContentFormat()), path); ValueResponse response = new ValueResponse(ResponseCode.CHANGED, content); for (ObservationListener listener : listeners) { listener.newValue(this, response.getContent()); } } catch (InvalidValueException e) { String msg = String.format("[%s] ([%s])", e.getMessage(), e.getPath().toString()); LOG.debug(msg); } } }
public static JsonObject jSerialize(DownlinkRequest<?> r) { final JsonObject o = Json.object(); o.add("path", r.getPath().toString());
public static JsonObject jSerialize(DownlinkRequest<?> r) { final JsonObject o = Json.object(); o.add("path", r.getPath().toString());
public static byte[] encode(LwM2mNode node, LwM2mPath path, LwM2mModel model, LwM2mValueConverter converter) throws CodecException { Validate.notNull(node); Validate.notNull(path); Validate.notNull(model); InternalEncoder internalEncoder = new InternalEncoder(); internalEncoder.objectId = path.getObjectId(); internalEncoder.model = model; internalEncoder.requestPath = path; internalEncoder.converter = converter; node.accept(internalEncoder); JsonRootObject jsonObject = new JsonRootObject(); jsonObject.setResourceList(internalEncoder.resourceList); jsonObject.setBaseName(path.toString()); return LwM2mJson.toJsonLwM2m(jsonObject).getBytes(); }