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);
}
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);
}
}
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");
}
}
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);
}
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;
}
Aggregations