use of org.eclipse.leshan.core.node.TimestampedLwM2mNode in project leshan by eclipse.
the class LwM2mNodeDecoderTest method json_timestamped_Object.
@Test
public void json_timestamped_Object() throws CodecException {
// json content for instance 0 of device object
StringBuilder b = new StringBuilder();
b.append("{\"e\":[");
b.append("{\"n\":\"0/1\",\"v\":22.9,\"t\":-30},");
b.append("{\"n\":\"0/1\",\"v\":22.4,\"t\":-5},");
b.append("{\"n\":\"0/0\",\"sv\":\"a string\",\"t\":-5},");
b.append("{\"n\":\"1/1\",\"v\":23,\"t\":-5},");
b.append("{\"n\":\"0/1\",\"v\":24.1,\"t\":-50}],");
b.append("\"bt\":25462634}");
List<TimestampedLwM2mNode> timestampedResources = decoder.decodeTimestampedData(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(1024), model);
assertEquals(3, timestampedResources.size());
assertEquals(Long.valueOf(25462634L - 5), timestampedResources.get(0).getTimestamp());
assertEquals(22.4d, ((LwM2mObject) timestampedResources.get(0).getNode()).getInstance(0).getResource(1).getValue());
assertEquals("a string", ((LwM2mObject) timestampedResources.get(0).getNode()).getInstance(0).getResource(0).getValue());
assertEquals(23.0d, ((LwM2mObject) timestampedResources.get(0).getNode()).getInstance(1).getResource(1).getValue());
assertEquals(Long.valueOf(25462634L - 30), timestampedResources.get(1).getTimestamp());
assertEquals(22.9d, ((LwM2mObject) timestampedResources.get(1).getNode()).getInstance(0).getResource(1).getValue());
assertEquals(Long.valueOf(25462634 - 50), timestampedResources.get(2).getTimestamp());
assertEquals(24.1d, ((LwM2mObject) timestampedResources.get(2).getNode()).getInstance(0).getResource(1).getValue());
}
use of org.eclipse.leshan.core.node.TimestampedLwM2mNode in project leshan by eclipse.
the class DefaultLwM2mNodeDecoder method toTimestampedNodes.
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);
}
use of org.eclipse.leshan.core.node.TimestampedLwM2mNode in project leshan by eclipse.
the class LwM2mNodeJsonDecoder method decode.
@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);
}
}
Aggregations