use of org.eclipse.leshan.core.node.LwM2mResource 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;
}
use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.
the class LwM2mNodeSerializer method serialize.
@Override
public JsonElement serialize(LwM2mNode src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject element = new JsonObject();
element.addProperty("id", src.getId());
if (typeOfSrc == LwM2mObject.class) {
element.add("instances", context.serialize(((LwM2mObject) src).getInstances().values()));
} else if (typeOfSrc == LwM2mObjectInstance.class) {
element.add("resources", context.serialize(((LwM2mObjectInstance) src).getResources().values()));
} else if (LwM2mResource.class.isAssignableFrom((Class<?>) typeOfSrc)) {
LwM2mResource rsc = (LwM2mResource) src;
if (rsc.isMultiInstances()) {
JsonObject values = new JsonObject();
for (Entry<Integer, ?> entry : rsc.getValues().entrySet()) {
if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) {
values.add(entry.getKey().toString(), context.serialize(Hex.encodeHex((byte[]) entry.getValue())));
} else {
values.add(entry.getKey().toString(), context.serialize(entry.getValue()));
}
}
element.add("values", values);
} else {
if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) {
element.add("value", context.serialize(Hex.encodeHex((byte[]) rsc.getValue())));
} else {
element.add("value", context.serialize(rsc.getValue()));
}
}
}
return element;
}
use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.
the class DownlinkRequestSerDes method jSerialize.
public static JsonObject jSerialize(DownlinkRequest<?> r) {
final JsonObject o = Json.object();
o.add("path", r.getPath().toString());
r.accept(new DownLinkRequestVisitorAdapter() {
@Override
public void visit(ObserveRequest request) {
o.add("kind", "observe");
if (request.getContentFormat() != null)
o.add("contentFormat", request.getContentFormat().getCode());
}
@Override
public void visit(DeleteRequest request) {
o.add("kind", "delete");
}
@Override
public void visit(DiscoverRequest request) {
o.add("kind", "discover");
}
@Override
public void visit(CreateRequest request) {
o.add("kind", "create");
o.add("contentFormat", request.getContentFormat().getCode());
if (request.getInstanceId() != null)
o.add("instanceId", request.getInstanceId());
JsonArray resources = new JsonArray();
for (LwM2mResource resource : request.getResources()) {
resources.add(LwM2mNodeSerDes.jSerialize(resource));
}
o.add("resources", resources);
}
@Override
public void visit(ExecuteRequest request) {
o.add("kind", "execute");
o.add("parameters", request.getParameters());
}
@Override
public void visit(WriteAttributesRequest request) {
o.add("kind", "writeAttributes");
o.add("observeSpec", request.getObserveSpec().toString());
}
@Override
public void visit(WriteRequest request) {
o.add("kind", "write");
o.add("contentFormat", request.getContentFormat().getCode());
o.add("mode", request.isPartialUpdateRequest() ? "UPDATE" : "REPLACE");
o.add("node", LwM2mNodeSerDes.jSerialize(request.getNode()));
}
@Override
public void visit(ReadRequest request) {
o.add("kind", "read");
if (request.getContentFormat() != null)
o.add("contentFormat", request.getContentFormat().getCode());
}
});
return o;
}
Aggregations