Search in sources :

Example 1 with SenMLException

use of org.eclipse.leshan.senml.SenMLException in project leshan by eclipse.

the class LwM2mNodeSenMLDecoder method decodeNodes.

@Override
public Map<LwM2mPath, LwM2mNode> decodeNodes(byte[] content, List<LwM2mPath> paths, LwM2mModel model) throws CodecException {
    try {
        // Decode SenML pack
        SenMLPack pack = decoder.fromSenML(content);
        Map<LwM2mPath, LwM2mNode> nodes = new HashMap<>();
        if (paths != null) {
            // Resolve records & Group it by time-stamp
            Map<LwM2mPath, Collection<LwM2mResolvedSenMLRecord>> recordsByPath = groupByPath(pack.getRecords(), paths);
            for (LwM2mPath path : paths) {
                Collection<LwM2mResolvedSenMLRecord> records = recordsByPath.get(path);
                if (records.isEmpty()) {
                    // Node can be null as the LWM2M specification says that "Read-Composite operation is
                    // treated as non-atomic and handled as best effort by the client. That is, if any of the
                    // requested
                    // resources do not have a valid value to return, they will not be included in the response".
                    // Meaning that a given path could have no corresponding value.
                    nodes.put(path, null);
                } else {
                    LwM2mNode node = parseRecords(recordsByPath.get(path), path, model, DefaultLwM2mDecoder.nodeClassFromPath(path));
                    nodes.put(path, node);
                }
            }
        } else {
            // Paths are not given so we given so we can not regroup by path
            // let's assume that each path refer to a single resource or single resource instances.
            LwM2mSenMLResolver resolver = new LwM2mSenMLResolver();
            for (SenMLRecord record : pack.getRecords()) {
                LwM2mResolvedSenMLRecord resolvedRecord = resolver.resolve(record);
                LwM2mPath path = resolvedRecord.getPath();
                LwM2mNode node = parseRecords(Arrays.asList(resolvedRecord), path, model, DefaultLwM2mDecoder.nodeClassFromPath(path));
                nodes.put(path, node);
            }
        }
        return nodes;
    } catch (SenMLException e) {
        String hexValue = content != null ? Hex.encodeHexString(content) : "";
        throw new CodecException(e, "Unable to decode nodes[path:%s] : %s", paths, hexValue, e);
    }
}
Also used : HashMap(java.util.HashMap) SenMLRecord(org.eclipse.leshan.senml.SenMLRecord) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Collection(java.util.Collection) SenMLPack(org.eclipse.leshan.senml.SenMLPack) CodecException(org.eclipse.leshan.core.node.codec.CodecException) SenMLException(org.eclipse.leshan.senml.SenMLException)

Example 2 with SenMLException

use of org.eclipse.leshan.senml.SenMLException in project leshan by eclipse.

the class LwM2mNodeSenMLDecoder method decodeTimestampedNodes.

@Override
public TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, LwM2mModel model) throws CodecException {
    try {
        // Decode SenML pack
        SenMLPack pack = decoder.fromSenML(content);
        TimestampedLwM2mNodes.Builder nodes = TimestampedLwM2mNodes.builder();
        LwM2mSenMLResolver resolver = new LwM2mSenMLResolver();
        for (SenMLRecord record : pack.getRecords()) {
            LwM2mResolvedSenMLRecord resolvedRecord = resolver.resolve(record);
            LwM2mPath path = resolvedRecord.getPath();
            LwM2mNode node = parseRecords(Arrays.asList(resolvedRecord), path, model, DefaultLwM2mDecoder.nodeClassFromPath(path));
            nodes.put(resolvedRecord.getTimeStamp(), path, node);
        }
        return nodes.build();
    } catch (SenMLException e) {
        String hexValue = content != null ? Hex.encodeHexString(content) : "";
        throw new CodecException(e, "Unable to decode nodes : %s", hexValue, e);
    }
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) SenMLPack(org.eclipse.leshan.senml.SenMLPack) SenMLRecord(org.eclipse.leshan.senml.SenMLRecord) CodecException(org.eclipse.leshan.core.node.codec.CodecException) TimestampedLwM2mNodes(org.eclipse.leshan.core.node.TimestampedLwM2mNodes) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) SenMLException(org.eclipse.leshan.senml.SenMLException)

Example 3 with SenMLException

use of org.eclipse.leshan.senml.SenMLException in project leshan by eclipse.

the class LwM2mNodeSenMLDecoder method decode.

@SuppressWarnings("unchecked")
@Override
public <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel model, Class<T> nodeClass) throws CodecException {
    try {
        // Decode SenML pack
        SenMLPack pack = decoder.fromSenML(content);
        List<SenMLRecord> records = pack.getRecords();
        // Resolve records
        LwM2mSenMLResolver resolver = new LwM2mSenMLResolver();
        Collection<LwM2mResolvedSenMLRecord> resolvedRecords = new ArrayList<>(records.size());
        for (SenMLRecord record : records) {
            LwM2mResolvedSenMLRecord resolvedRecord = resolver.resolve(record);
            // Validate SenML resolved name
            if (!resolvedRecord.getPath().isResourceInstance() && !resolvedRecord.getPath().isResource()) {
                throw new CodecException("Invalid path [%s] for resource, it should be a resource or a resource instance path", resolvedRecord.getName());
            }
            if (!resolvedRecord.getPath().startWith(path)) {
                throw new CodecException("Invalid path [%s] for resource, it should start by %s", resolvedRecord.getPath(), path);
            }
            if (resolvedRecord.getTimeStamp() != null) {
                throw new CodecException("Unable to decode node[path:%s] : value should not be timestamped", path);
            }
            resolvedRecords.add(resolvedRecord);
        }
        // Parse records and create node
        return (T) parseRecords(resolvedRecords, path, model, nodeClass);
    } catch (SenMLException e) {
        String hexValue = content != null ? Hex.encodeHexString(content) : "";
        throw new CodecException(e, "Unable to decode node[path:%s] : %s", path, hexValue, e);
    }
}
Also used : ArrayList(java.util.ArrayList) SenMLPack(org.eclipse.leshan.senml.SenMLPack) SenMLRecord(org.eclipse.leshan.senml.SenMLRecord) CodecException(org.eclipse.leshan.core.node.codec.CodecException) SenMLException(org.eclipse.leshan.senml.SenMLException)

Example 4 with SenMLException

use of org.eclipse.leshan.senml.SenMLException in project leshan by eclipse.

the class LwM2mNodeSenMLDecoder method decodeTimestampedData.

@Override
public List<TimestampedLwM2mNode> decodeTimestampedData(byte[] content, LwM2mPath path, LwM2mModel model, Class<? extends LwM2mNode> nodeClass) throws CodecException {
    try {
        // Decode SenML pack
        SenMLPack pack = decoder.fromSenML(content);
        // Resolve records & Group it by time-stamp
        Map<Long, Collection<LwM2mResolvedSenMLRecord>> recordsByTimestamp = groupRecordByTimestamp(pack.getRecords(), path);
        // Fill time-stamped nodes collection
        List<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>();
        for (Entry<Long, Collection<LwM2mResolvedSenMLRecord>> entryByTimestamp : recordsByTimestamp.entrySet()) {
            LwM2mNode node = parseRecords(entryByTimestamp.getValue(), path, model, nodeClass);
            // add time-stamped node
            timestampedNodes.add(new TimestampedLwM2mNode(entryByTimestamp.getKey(), node));
        }
        return timestampedNodes;
    } catch (SenMLException e) {
        String hexValue = content != null ? Hex.encodeHexString(content) : "";
        throw new CodecException(e, "Unable to decode node[path:%s] : %s", path, hexValue, e);
    }
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) ULong(org.eclipse.leshan.core.util.datatype.ULong) ArrayList(java.util.ArrayList) Collection(java.util.Collection) SenMLPack(org.eclipse.leshan.senml.SenMLPack) CodecException(org.eclipse.leshan.core.node.codec.CodecException) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) SenMLException(org.eclipse.leshan.senml.SenMLException)

Example 5 with SenMLException

use of org.eclipse.leshan.senml.SenMLException in project leshan by eclipse.

the class LwM2mNodeSenMLEncoder method encodeTimestampedData.

@Override
public byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampedNodes, LwM2mPath path, LwM2mModel model, LwM2mValueConverter converter) throws CodecException {
    Validate.notNull(timestampedNodes);
    Validate.notNull(path);
    Validate.notNull(model);
    SenMLPack pack = new SenMLPack();
    for (TimestampedLwM2mNode timestampedLwM2mNode : timestampedNodes) {
        if (timestampedLwM2mNode.getTimestamp() < 268_435_456) {
            // see https://tools.ietf.org/html/rfc8428#section-4.5.3
            throw new CodecException("Unable to encode timestamped node[path:%s] : invalid timestamp %s, timestamp should be greater or equals to 268,435,456", path, timestampedLwM2mNode.getTimestamp());
        }
        InternalEncoder internalEncoder = new InternalEncoder();
        internalEncoder.objectId = path.getObjectId();
        internalEncoder.model = model;
        internalEncoder.requestPath = path;
        internalEncoder.converter = converter;
        internalEncoder.records = new ArrayList<>();
        timestampedLwM2mNode.getNode().accept(internalEncoder);
        internalEncoder.records.get(0).setBaseTime(timestampedLwM2mNode.getTimestamp());
        pack.addRecords(internalEncoder.records);
    }
    try {
        return encoder.toSenML(pack);
    } catch (SenMLException e) {
        throw new CodecException(e, "Unable to encode timestamped node[path:%s] : %s", path, timestampedNodes);
    }
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) SenMLPack(org.eclipse.leshan.senml.SenMLPack) CodecException(org.eclipse.leshan.core.node.codec.CodecException) SenMLException(org.eclipse.leshan.senml.SenMLException)

Aggregations

SenMLException (org.eclipse.leshan.senml.SenMLException)12 SenMLPack (org.eclipse.leshan.senml.SenMLPack)11 CodecException (org.eclipse.leshan.core.node.codec.CodecException)10 SenMLRecord (org.eclipse.leshan.senml.SenMLRecord)8 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)6 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)6 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)5 ArrayList (java.util.ArrayList)3 CBORNumber (com.upokecenter.cbor.CBORNumber)2 CBORObject (com.upokecenter.cbor.CBORObject)2 BigInteger (java.math.BigInteger)2 Collection (java.util.Collection)2 ULong (org.eclipse.leshan.core.util.datatype.ULong)2 CBORType (com.upokecenter.cbor.CBORType)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 TimestampedLwM2mNodes (org.eclipse.leshan.core.node.TimestampedLwM2mNodes)1