Search in sources :

Example 11 with TimestampedLwM2mNode

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());
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

Example 12 with TimestampedLwM2mNode

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);
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) ArrayList(java.util.ArrayList)

Example 13 with TimestampedLwM2mNode

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);
    }
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) JsonRootObject(org.eclipse.leshan.json.JsonRootObject) CodecException(org.eclipse.leshan.core.node.codec.CodecException) LwM2mJsonException(org.eclipse.leshan.json.LwM2mJsonException)

Aggregations

TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)13 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)6 Response (org.eclipse.californium.core.coap.Response)3 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)3 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)3 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)3 Observation (org.eclipse.leshan.core.observation.Observation)3 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)3 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)3 CodecException (org.eclipse.leshan.core.node.codec.CodecException)2 JsonArrayEntry (org.eclipse.leshan.json.JsonArrayEntry)2 JsonRootObject (org.eclipse.leshan.json.JsonRootObject)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1