Search in sources :

Example 1 with Type

use of org.eclipse.leshan.core.model.ResourceModel.Type in project leshan by eclipse.

the class ResourceModelSerDes method deserialize.

@Override
public ResourceModel deserialize(JsonObject o) {
    if (o == null)
        return null;
    if (!o.isObject())
        return null;
    int id = o.getInt("id", -1);
    if (id < 0)
        return null;
    String name = o.getString("name", null);
    Operations operations = Operations.valueOf(o.getString("operations", null));
    String instancetype = o.getString("instancetype", null);
    boolean mandatory = o.getBoolean("mandatory", false);
    Type type = Type.valueOf(o.getString("type", "").toUpperCase());
    String range = o.getString("range", null);
    String units = o.getString("units", null);
    String description = o.getString("description", null);
    return new ResourceModel(id, name, operations, "multiple".equals(instancetype), mandatory, type, range, units, description);
}
Also used : Type(org.eclipse.leshan.core.model.ResourceModel.Type) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) Operations(org.eclipse.leshan.core.model.ResourceModel.Operations)

Example 2 with Type

use of org.eclipse.leshan.core.model.ResourceModel.Type in project leshan by eclipse.

the class LwM2mNodeTlvDecoder method parseResourceTlv.

private static LwM2mResource parseResourceTlv(Tlv tlv, int objectId, int objectInstanceId, LwM2mModel model) throws CodecException {
    LwM2mPath resourcePath = new LwM2mPath(objectId, objectInstanceId, tlv.getIdentifier());
    Type expectedType = getResourceType(resourcePath, model);
    Integer resourceId = tlv.getIdentifier();
    switch(tlv.getType()) {
        case MULTIPLE_RESOURCE:
            return LwM2mMultipleResource.newResource(resourceId, parseTlvValues(tlv.getChildren(), expectedType, resourcePath), expectedType);
        case RESOURCE_VALUE:
            return LwM2mSingleResource.newResource(resourceId, parseTlvValue(tlv.getValue(), expectedType, resourcePath), expectedType);
        default:
            throw new CodecException("Invalid TLV type %s for resource %s", tlv.getType(), resourcePath);
    }
}
Also used : TlvType(org.eclipse.leshan.tlv.Tlv.TlvType) Type(org.eclipse.leshan.core.model.ResourceModel.Type) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CodecException(org.eclipse.leshan.core.node.codec.CodecException)

Example 3 with Type

use of org.eclipse.leshan.core.model.ResourceModel.Type in project leshan by eclipse.

the class LwM2mNodeSerDes method deserialize.

public static LwM2mNode deserialize(JsonObject o) {
    String kind = o.getString("kind", null);
    int id = o.getInt("id", LwM2mObjectInstance.UNDEFINED);
    switch(kind) {
        case "object":
            {
                Collection<LwM2mObjectInstance> instances = new ArrayList<>();
                JsonArray jInstances = (JsonArray) o.get("instances");
                for (JsonValue jInstance : jInstances) {
                    LwM2mObjectInstance instance = (LwM2mObjectInstance) deserialize((JsonObject) jInstance);
                    instances.add(instance);
                }
                return new LwM2mObject(id, instances);
            }
        case "instance":
            {
                Collection<LwM2mResource> resources = new ArrayList<>();
                JsonObject jResources = (JsonObject) o.get("resources");
                for (Member jResource : jResources) {
                    LwM2mResource resource = (LwM2mResource) deserialize((JsonObject) jResource.getValue());
                    resources.add(resource);
                }
                return new LwM2mObjectInstance(id, resources);
            }
        case "singleResource":
            {
                String jType = o.getString("type", null);
                if (jType == null)
                    throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
                Type type = Enum.valueOf(Type.class, jType);
                Object value = ValueSerDes.deserialize(o.get("value"), type);
                return LwM2mSingleResource.newResource(id, value, type);
            }
        case "multipleResource":
            {
                String jType = o.getString("type", null);
                if (jType == null)
                    throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
                Type type = Enum.valueOf(Type.class, jType);
                Map<Integer, Object> values = new HashMap<>();
                JsonObject jValues = (JsonObject) o.get("values");
                for (Member jValue : jValues) {
                    Integer valueId = Integer.valueOf(jValue.getName());
                    Object value = ValueSerDes.deserialize(jValue.getValue(), type);
                    values.put(valueId, value);
                }
                return LwM2mMultipleResource.newResource(id, values, type);
            }
        default:
            throw new IllegalStateException("Invalid LwM2mNode missing kind attribute");
    }
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) JsonArray(com.eclipsesource.json.JsonArray) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Type(org.eclipse.leshan.core.model.ResourceModel.Type) Collection(java.util.Collection) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) JsonObject(com.eclipsesource.json.JsonObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Member(com.eclipsesource.json.JsonObject.Member) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Type

use of org.eclipse.leshan.core.model.ResourceModel.Type in project leshan by eclipse.

the class DDFFileParser method parseResource.

private ResourceModel parseResource(Node item) {
    Integer id = Integer.valueOf(item.getAttributes().getNamedItem("ID").getTextContent());
    String name = null;
    Operations operations = Operations.NONE;
    boolean multiple = false;
    boolean mandatory = false;
    Type type = Type.STRING;
    String rangeEnumeration = null;
    String units = null;
    String description = null;
    for (int i = 0; i < item.getChildNodes().getLength(); i++) {
        Node field = item.getChildNodes().item(i);
        switch(field.getNodeName()) {
            case "Name":
                name = field.getTextContent();
                break;
            case "Operations":
                String strOp = field.getTextContent();
                if (strOp != null && !strOp.isEmpty()) {
                    operations = Operations.valueOf(strOp);
                }
                break;
            case "MultipleInstances":
                multiple = "Multiple".equals(field.getTextContent());
                break;
            case "Mandatory":
                mandatory = "Mandatory".equals(field.getTextContent());
                break;
            case "Type":
                switch(field.getTextContent()) {
                    case "String":
                        type = Type.STRING;
                        break;
                    case "Integer":
                        type = Type.INTEGER;
                        break;
                    case "Float":
                        type = Type.FLOAT;
                        break;
                    case "Boolean":
                        type = Type.BOOLEAN;
                        break;
                    case "Opaque":
                        type = Type.OPAQUE;
                        break;
                    case "Time":
                        type = Type.TIME;
                        break;
                    case "Objlnk":
                        type = Type.OBJLNK;
                        break;
                }
                break;
            case "RangeEnumeration":
                rangeEnumeration = field.getTextContent();
                break;
            case "Units":
                units = field.getTextContent();
                break;
            case "Description":
                description = field.getTextContent();
                break;
        }
    }
    return new ResourceModel(id, name, operations, multiple, mandatory, type, rangeEnumeration, units, description);
}
Also used : Type(org.eclipse.leshan.core.model.ResourceModel.Type) Node(org.w3c.dom.Node) Operations(org.eclipse.leshan.core.model.ResourceModel.Operations)

Example 5 with Type

use of org.eclipse.leshan.core.model.ResourceModel.Type in project leshan by eclipse.

the class LwM2mNodeJsonDecoder method extractLwM2mResources.

private static Map<Integer, LwM2mResource> extractLwM2mResources(Collection<JsonArrayEntry> jsonArrayEntries, LwM2mPath baseName, LwM2mModel model) throws CodecException {
    if (jsonArrayEntries == null)
        return Collections.emptyMap();
    // Extract LWM2M resources from JSON resource list
    Map<Integer, LwM2mResource> lwM2mResourceMap = new HashMap<>();
    Map<LwM2mPath, Map<Integer, JsonArrayEntry>> multiResourceMap = new HashMap<>();
    for (JsonArrayEntry resourceElt : jsonArrayEntries) {
        // Build resource path
        LwM2mPath nodePath = baseName.append(resourceElt.getName());
        // handle LWM2M resources
        if (nodePath.isResourceInstance()) {
            // Multi-instance resource
            // Store multi-instance resource values in a map
            // we will deal with it later
            LwM2mPath resourcePath = new LwM2mPath(nodePath.getObjectId(), nodePath.getObjectInstanceId(), nodePath.getResourceId());
            Map<Integer, JsonArrayEntry> multiResource = multiResourceMap.get(resourcePath);
            if (multiResource == null) {
                multiResource = new HashMap<>();
                multiResourceMap.put(resourcePath, multiResource);
            }
            multiResource.put(nodePath.getResourceInstanceId(), resourceElt);
        } else if (nodePath.isResource()) {
            // Single resource
            Type expectedType = getResourceType(nodePath, model, resourceElt);
            LwM2mResource res = LwM2mSingleResource.newResource(nodePath.getResourceId(), parseJsonValue(resourceElt.getResourceValue(), expectedType, nodePath), expectedType);
            lwM2mResourceMap.put(nodePath.getResourceId(), res);
        } else {
            throw new CodecException("Invalid path [%s] for resource, it should be a resource or a resource instance path", nodePath);
        }
    }
    // Handle multi-instance resource.
    for (Map.Entry<LwM2mPath, Map<Integer, JsonArrayEntry>> entry : multiResourceMap.entrySet()) {
        LwM2mPath resourcePath = entry.getKey();
        Map<Integer, JsonArrayEntry> jsonEntries = entry.getValue();
        if (jsonEntries != null && !jsonEntries.isEmpty()) {
            Type expectedType = getResourceType(resourcePath, model, jsonEntries.values().iterator().next());
            Map<Integer, Object> values = new HashMap<>();
            for (Entry<Integer, JsonArrayEntry> e : jsonEntries.entrySet()) {
                Integer resourceInstanceId = e.getKey();
                values.put(resourceInstanceId, parseJsonValue(e.getValue().getResourceValue(), expectedType, resourcePath));
            }
            LwM2mResource resource = LwM2mMultipleResource.newResource(resourcePath.getResourceId(), values, expectedType);
            lwM2mResourceMap.put(resourcePath.getResourceId(), resource);
        }
    }
    // If we found nothing, we try to create an empty multi-instance resource
    if (lwM2mResourceMap.isEmpty() && baseName.isResource()) {
        ResourceModel resourceModel = model.getResourceModel(baseName.getObjectId(), baseName.getResourceId());
        // We create it only if this respect the model
        if (resourceModel == null || resourceModel.multiple) {
            Type resourceType = getResourceType(baseName, model, null);
            lwM2mResourceMap.put(baseName.getResourceId(), LwM2mMultipleResource.newResource(baseName.getResourceId(), new HashMap<Integer, Object>(), resourceType));
        }
    }
    return lwM2mResourceMap;
}
Also used : HashMap(java.util.HashMap) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Type(org.eclipse.leshan.core.model.ResourceModel.Type) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) CodecException(org.eclipse.leshan.core.node.codec.CodecException) JsonRootObject(org.eclipse.leshan.json.JsonRootObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry)

Aggregations

Type (org.eclipse.leshan.core.model.ResourceModel.Type)8 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)4 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)3 CodecException (org.eclipse.leshan.core.node.codec.CodecException)3 JsonObject (com.eclipsesource.json.JsonObject)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Operations (org.eclipse.leshan.core.model.ResourceModel.Operations)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)2 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)2 TlvType (org.eclipse.leshan.tlv.Tlv.TlvType)2 JsonArray (com.eclipsesource.json.JsonArray)1 Member (com.eclipsesource.json.JsonObject.Member)1 JsonValue (com.eclipsesource.json.JsonValue)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)1