public ObserveResponse(ResponseCode code, LwM2mNode content, List<TimestampedLwM2mNode> timestampedValues, Observation observation, String errorMessage, Object coapResponse) { super(code, timestampedValues != null && !timestampedValues.isEmpty() ? timestampedValues.get(0).getNode() : content, errorMessage, coapResponse); // CHANGED is out of spec but is supported for backward compatibility. (previous draft version) if (ResponseCode.CHANGED.equals(code)) { if (content == null) throw new InvalidResponseException("Content is mandatory for successful response"); } this.observation = observation; this.timestampedValues = timestampedValues; }
private static List<TimestampedLwM2mNode> toTimestampedNodes(LwM2mNode node) { if (node == null) return Collections.emptyList(); ArrayList<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>(1); timestampedNodes.add(new TimestampedLwM2mNode(null, node)); return Collections.unmodifiableList(timestampedNodes); }
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(); }
if (timestampedNodes.size() == 1 && !timestampedNodes.get(0).isTimestamped()) { return new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()), timestampedNodes.get(0).getNode(), null, observation, null, coapResponse); } else { return new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()), null, timestampedNodes,
@SuppressWarnings("unchecked") public static <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel model, Class<T> nodeClass) throws CodecException { try { String jsonStrValue = content != null ? new String(content) : ""; JsonRootObject json = LwM2mJson.fromJsonLwM2m(jsonStrValue); List<TimestampedLwM2mNode> timestampedNodes = parseJSON(json, path, model, nodeClass); if (timestampedNodes.size() == 0) { return null; } else { // return the most recent value return (T) timestampedNodes.get(0).getNode(); } } catch (LwM2mJsonException e) { throw new CodecException(e, "Unable to deserialize json [path:%s]", path); } }
timestampedNodes.add(new TimestampedLwM2mNode(timestamp, node));