Search in sources :

Example 1 with TimestampedLwM2mNode

use of org.eclipse.leshan.core.node.TimestampedLwM2mNode in project leshan by eclipse.

the class LwM2mNodeJsonDecoder method parseJSON.

private static List<TimestampedLwM2mNode> parseJSON(JsonRootObject jsonObject, LwM2mPath path, LwM2mModel model, Class<? extends LwM2mNode> nodeClass) throws CodecException {
    LOG.trace("Parsing JSON content for path {}: {}", path, jsonObject);
    // Group JSON entry by time-stamp
    Map<Long, Collection<JsonArrayEntry>> jsonEntryByTimestamp = groupJsonEntryByTimestamp(jsonObject);
    // Extract baseName
    LwM2mPath baseName = extractAndValidateBaseName(jsonObject, path);
    if (baseName == null)
        // if no base name, use request path as base name
        baseName = path;
    // fill time-stamped nodes collection
    List<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>();
    for (Entry<Long, Collection<JsonArrayEntry>> entryByTimestamp : jsonEntryByTimestamp.entrySet()) {
        // Group JSON entry by instance
        Map<Integer, Collection<JsonArrayEntry>> jsonEntryByInstanceId = groupJsonEntryByInstanceId(entryByTimestamp.getValue(), baseName);
        // Create lwm2m node
        LwM2mNode node;
        if (nodeClass == LwM2mObject.class) {
            Collection<LwM2mObjectInstance> instances = new ArrayList<>();
            for (Entry<Integer, Collection<JsonArrayEntry>> entryByInstanceId : jsonEntryByInstanceId.entrySet()) {
                Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(entryByInstanceId.getValue(), baseName, model);
                instances.add(new LwM2mObjectInstance(entryByInstanceId.getKey(), resourcesMap.values()));
            }
            node = new LwM2mObject(baseName.getObjectId(), instances);
        } else if (nodeClass == LwM2mObjectInstance.class) {
            // validate we have resources for only 1 instance
            if (jsonEntryByInstanceId.size() != 1)
                throw new CodecException("One instance expected in the payload [path:%s]", path);
            // Extract resources
            Entry<Integer, Collection<JsonArrayEntry>> instanceEntry = jsonEntryByInstanceId.entrySet().iterator().next();
            Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(instanceEntry.getValue(), baseName, model);
            // Create instance
            node = new LwM2mObjectInstance(instanceEntry.getKey(), resourcesMap.values());
        } else if (nodeClass == LwM2mResource.class) {
            // validate we have resources for only 1 instance
            if (jsonEntryByInstanceId.size() > 1)
                throw new CodecException("Only one instance expected in the payload [path:%s]", path);
            // Extract resources
            Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(jsonEntryByInstanceId.values().iterator().next(), baseName, model);
            // validate there is only 1 resource
            if (resourcesMap.size() != 1)
                throw new CodecException("One resource should be present in the payload [path:%s]", path);
            node = resourcesMap.values().iterator().next();
        } else {
            throw new IllegalArgumentException("invalid node class: " + nodeClass);
        }
        // compute time-stamp
        Long timestamp = computeTimestamp(jsonObject.getBaseTime(), entryByTimestamp.getKey());
        // add time-stamped node
        timestampedNodes.add(new TimestampedLwM2mNode(timestamp, node));
    }
    return timestampedNodes;
}
Also used : ArrayList(java.util.ArrayList) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry) Entry(java.util.Map.Entry) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Collection(java.util.Collection) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) CodecException(org.eclipse.leshan.core.node.codec.CodecException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry)

Example 2 with TimestampedLwM2mNode

use of org.eclipse.leshan.core.node.TimestampedLwM2mNode in project leshan by eclipse.

the class LwM2mNodeJsonEncoder method encodeTimestampedData.

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

Example 3 with TimestampedLwM2mNode

use of org.eclipse.leshan.core.node.TimestampedLwM2mNode in project leshan by eclipse.

the class LwM2mNodeEncoderTest method json_encode_timestamped_Object.

@Test
public void json_encode_timestamped_Object() throws CodecException {
    List<TimestampedLwM2mNode> data = new ArrayList<>();
    LwM2mObject objectAt210 = new LwM2mObject(1204, new LwM2mObjectInstance(0, LwM2mSingleResource.newFloatResource(1, 22.9)));
    LwM2mObject objectAt220 = new LwM2mObject(1204, new LwM2mObjectInstance(0, LwM2mSingleResource.newFloatResource(1, 22.4), LwM2mSingleResource.newStringResource(0, "a string")), new LwM2mObjectInstance(1, LwM2mSingleResource.newFloatResource(1, 23)));
    LwM2mObject objetAt230 = new LwM2mObject(1204, new LwM2mObjectInstance(0, LwM2mSingleResource.newFloatResource(1, 24.1)));
    data.add(new TimestampedLwM2mNode(210L, objectAt210));
    data.add(new TimestampedLwM2mNode(220L, objectAt220));
    data.add(new TimestampedLwM2mNode(230L, objetAt230));
    byte[] encoded = encoder.encodeTimestampedData(data, ContentFormat.JSON, new LwM2mPath(1024), model);
    StringBuilder b = new StringBuilder();
    b.append("{\"bn\":\"/1024\",\"e\":[");
    b.append("{\"n\":\"0/1\",\"v\":22.9,\"t\":210},");
    b.append("{\"n\":\"0/0\",\"sv\":\"a string\",\"t\":220},");
    b.append("{\"n\":\"0/1\",\"v\":22.4,\"t\":220},");
    b.append("{\"n\":\"1/1\",\"v\":23,\"t\":220},");
    b.append("{\"n\":\"0/1\",\"v\":24.1,\"t\":230}]}");
    String expected = b.toString();
    Assert.assertEquals(expected, new String(encoded));
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ArrayList(java.util.ArrayList) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Example 4 with TimestampedLwM2mNode

use of org.eclipse.leshan.core.node.TimestampedLwM2mNode in project leshan by eclipse.

the class LwM2mNodeEncoderTest method json_encode_timestamped_resources.

@Test
public void json_encode_timestamped_resources() throws CodecException {
    List<TimestampedLwM2mNode> data = new ArrayList<>();
    data.add(new TimestampedLwM2mNode(500L, LwM2mSingleResource.newFloatResource(1, 22.9)));
    data.add(new TimestampedLwM2mNode(510L, LwM2mSingleResource.newFloatResource(1, 22.4)));
    data.add(new TimestampedLwM2mNode(520L, LwM2mSingleResource.newFloatResource(1, 24.1)));
    byte[] encoded = encoder.encodeTimestampedData(data, ContentFormat.JSON, new LwM2mPath(1024, 0, 1), model);
    StringBuilder b = new StringBuilder();
    b.append("{\"bn\":\"/1024/0/1\",\"e\":[");
    b.append("{\"n\":\"\",\"v\":22.9,\"t\":500},");
    b.append("{\"n\":\"\",\"v\":22.4,\"t\":510},");
    b.append("{\"n\":\"\",\"v\":24.1,\"t\":520}]}");
    String expected = b.toString();
    Assert.assertEquals(expected, new String(encoded));
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with TimestampedLwM2mNode

use of org.eclipse.leshan.core.node.TimestampedLwM2mNode in project leshan by eclipse.

the class LwM2mNodeDecoderTest method json_timestamped_instances.

@Test
public void json_timestamped_instances() throws CodecException {
    // json content for instance 0 of device object
    StringBuilder b = new StringBuilder();
    b.append("{\"e\":[");
    b.append("{\"n\":\"1\",\"v\":22.9,\"t\":-30},");
    b.append("{\"n\":\"1\",\"v\":22.4,\"t\":-5},");
    b.append("{\"n\":\"0\",\"sv\":\"a string\",\"t\":-5},");
    b.append("{\"n\":\"1\",\"v\":24.1,\"t\":-50}],");
    b.append("\"bt\":25462634}");
    List<TimestampedLwM2mNode> timestampedResources = decoder.decodeTimestampedData(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(1024, 0), model);
    assertEquals(3, timestampedResources.size());
    assertEquals(Long.valueOf(25462634L - 5), timestampedResources.get(0).getTimestamp());
    assertEquals("a string", ((LwM2mObjectInstance) timestampedResources.get(0).getNode()).getResource(0).getValue());
    assertEquals(22.4d, ((LwM2mObjectInstance) timestampedResources.get(0).getNode()).getResource(1).getValue());
    assertEquals(Long.valueOf(25462634L - 30), timestampedResources.get(1).getTimestamp());
    assertEquals(22.9d, ((LwM2mObjectInstance) timestampedResources.get(1).getNode()).getResource(1).getValue());
    assertEquals(Long.valueOf(25462634 - 50), timestampedResources.get(2).getTimestamp());
    assertEquals(24.1d, ((LwM2mObjectInstance) timestampedResources.get(2).getNode()).getResource(1).getValue());
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

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