Search in sources :

Example 1 with LwM2mObject

use of org.thingsboard.server.common.data.lwm2m.LwM2mObject in project thingsboard by thingsboard.

the class DefaultTbResourceService method toLwM2mObject.

private LwM2mObject toLwM2mObject(TbResource resource, boolean isSave) {
    try {
        DDFFileParser ddfFileParser = new DDFFileParser(new DefaultDDFFileValidator());
        List<ObjectModel> objectModels = ddfFileParser.parse(new ByteArrayInputStream(Base64.getDecoder().decode(resource.getData())), resource.getSearchText());
        if (objectModels.size() == 0) {
            return null;
        } else {
            ObjectModel obj = objectModels.get(0);
            LwM2mObject lwM2mObject = new LwM2mObject();
            lwM2mObject.setId(obj.id);
            lwM2mObject.setKeyId(resource.getResourceKey());
            lwM2mObject.setName(obj.name);
            lwM2mObject.setMultiple(obj.multiple);
            lwM2mObject.setMandatory(obj.mandatory);
            LwM2mInstance instance = new LwM2mInstance();
            instance.setId(0);
            List<LwM2mResourceObserve> resources = new ArrayList<>();
            obj.resources.forEach((k, v) -> {
                if (isSave) {
                    LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false);
                    resources.add(lwM2MResourceObserve);
                } else if (v.operations.isReadable()) {
                    LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false);
                    resources.add(lwM2MResourceObserve);
                }
            });
            if (isSave || resources.size() > 0) {
                instance.setResources(resources.toArray(LwM2mResourceObserve[]::new));
                lwM2mObject.setInstances(new LwM2mInstance[] { instance });
                return lwM2mObject;
            } else {
                return null;
            }
        }
    } catch (IOException | InvalidDDFFileException e) {
        log.error("Could not parse the XML of objectModel with name [{}]", resource.getSearchText(), e);
        return null;
    }
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) DefaultDDFFileValidator(org.eclipse.leshan.core.model.DefaultDDFFileValidator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DDFFileParser(org.eclipse.leshan.core.model.DDFFileParser) ByteArrayInputStream(java.io.ByteArrayInputStream) LwM2mResourceObserve(org.thingsboard.server.common.data.lwm2m.LwM2mResourceObserve) InvalidDDFFileException(org.eclipse.leshan.core.model.InvalidDDFFileException) LwM2mObject(org.thingsboard.server.common.data.lwm2m.LwM2mObject) LwM2mInstance(org.thingsboard.server.common.data.lwm2m.LwM2mInstance)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DDFFileParser (org.eclipse.leshan.core.model.DDFFileParser)1 DefaultDDFFileValidator (org.eclipse.leshan.core.model.DefaultDDFFileValidator)1 InvalidDDFFileException (org.eclipse.leshan.core.model.InvalidDDFFileException)1 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)1 LwM2mInstance (org.thingsboard.server.common.data.lwm2m.LwM2mInstance)1 LwM2mObject (org.thingsboard.server.common.data.lwm2m.LwM2mObject)1 LwM2mResourceObserve (org.thingsboard.server.common.data.lwm2m.LwM2mResourceObserve)1