public LwM2mObjectInstance(int id, Collection<LwM2mResource> resources) { Validate.notNull(resources); this.id = id; Map<Integer, LwM2mResource> resourcesMap = new HashMap<>(resources.size()); for (LwM2mResource resource : resources) { resourcesMap.put(resource.getId(), resource); } this.resources = Collections.unmodifiableMap(resourcesMap); }
public void setObjectModel(ObjectModel objectModel) { this.objectModel = objectModel; // initialize resources for (ResourceModel resourceModel : objectModel.resources.values()) { if (resourceModel.operations.isReadable()) { LwM2mResource newResource = createResource(objectModel, resourceModel); if (newResource != null) { resources.put(newResource.getId(), newResource); } } } }
private Tlv[] encodeResources(Collection<LwM2mResource> resources, LwM2mPath instancePath) { Tlv[] rTlvs = new Tlv[resources.size()]; int i = 0; for (LwM2mResource resource : resources) { rTlvs[i] = encodeResource(resource, instancePath.append(resource.getId())); i++; } return rTlvs; }
private static LwM2mObjectInstance parseObjectInstanceTlv(Tlv[] rscTlvs, int objectId, int instanceId, LwM2mModel model) throws CodecException { // read resources Map<Integer, LwM2mResource> resources = new HashMap<>(rscTlvs.length); for (Tlv rscTlv : rscTlvs) { LwM2mPath resourcePath = new LwM2mPath(objectId, instanceId, rscTlv.getIdentifier()); LwM2mResource resource = parseResourceTlv(rscTlv, resourcePath, model); LwM2mResource previousResource = resources.put(resource.getId(), resource); if (previousResource != null) { throw new CodecException("2 RESOURCE nodes (%s,%s) with the same identifier %d for path %s", previousResource, resource, resource.getId(), resourcePath); } } return new LwM2mObjectInstance(instanceId, resources.values()); }
@Override public void visit(LwM2mObjectInstance instance) { LOG.trace("Encoding object instance {} into JSON", instance); resourceList = new ArrayList<>(); for (LwM2mResource resource : instance.getResources().values()) { // Validate request path & compute resource path String prefixPath; if (requestPath.isObject()) { prefixPath = instance.getId() + "/" + resource.getId(); } else if (requestPath.isObjectInstance()) { prefixPath = Integer.toString(resource.getId()); } else { throw new CodecException("Invalid request path %s for JSON instance encoding", requestPath); } // Create resources resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } }
@Override public void visit(LwM2mObject object) { LOG.trace("Encoding Object {} into JSON", object); // Validate request path if (!requestPath.isObject()) { throw new CodecException("Invalid request path %s for JSON object encoding", requestPath); } // Create resources resourceList = new ArrayList<>(); for (LwM2mObjectInstance instance : object.getInstances().values()) { for (LwM2mResource resource : instance.getResources().values()) { String prefixPath = Integer.toString(instance.getId()) + "/" + Integer.toString(resource.getId()); resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } } }
private Tlv encodeResource(LwM2mResource resource, LwM2mPath resourcePath) { ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); Type expectedType = rSpec != null ? rSpec.type : resource.getType(); Tlv rTlv; if (resource.isMultiInstances()) { Tlv[] instances = new Tlv[resource.getValues().size()]; int i = 0; for (Entry<Integer, ?> entry : resource.getValues().entrySet()) { LwM2mPath resourceInstancePath = resourcePath.append(entry.getKey()); Object convertedValue = converter.convertValue(entry.getValue(), resource.getType(), expectedType, resourceInstancePath); instances[i] = new Tlv(TlvType.RESOURCE_INSTANCE, null, this.encodeTlvValue(convertedValue, expectedType, resourceInstancePath), entry.getKey()); i++; } rTlv = new Tlv(TlvType.MULTIPLE_RESOURCE, instances, null, resource.getId()); } else { Object convertedValue = converter.convertValue(resource.getValue(), resource.getType(), expectedType, resourcePath); rTlv = new Tlv(TlvType.RESOURCE_VALUE, null, this.encodeTlvValue(convertedValue, expectedType, resourcePath), resource.getId()); } return rTlv; }
@Override protected CreateResponse doCreate(CreateRequest request) { Integer instanceId = request.getInstanceId(); if (instanceId == null) { // the client is in charge to generate the id of the new instance if (instances.isEmpty()) { instanceId = 0; } else { instanceId = Collections.max(instances.keySet()) + 1; } } LwM2mInstanceEnabler newInstance = instanceFactory.create(getObjectModel()); for (LwM2mResource resource : request.getResources()) { newInstance.write(resource.getId(), resource); } instances.put(instanceId, newInstance); listenInstance(newInstance, instanceId); return CreateResponse.success(new LwM2mPath(request.getPath().getObjectId(), instanceId).toString()); }
ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); Type expectedType = rSpec != null ? rSpec.type : resource.getType(); Object val = converter.convertValue(resource.getValue(), resource.getType(), expectedType, path);
@Override public void visit(LwM2mResource resource) { if (resource.isMultiInstances()) { throw new CodecException("Multiple instances resource %s cannot be encoded in opaque format", path); } ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); if (rSpec != null && rSpec.type != Type.OPAQUE) { throw new CodecException("Only single opaque resource can be encoded in opaque format. [%s]", path); } LOG.trace("Encoding resource {} into text", resource); Object value = converter.convertValue(resource.getValue(), resource.getType(), Type.OPAQUE, path); encoded = (byte[]) value; } }
if (previousRes != null) { throw new CodecException("2 RESOURCE nodes (%s,%s) with the same identifier %d for path %s", previousRes, res, res.getId(), nodePath); if (previousRes != null) { throw new CodecException("2 RESOURCE nodes (%s,%s) with the same identifier %d for path %s", previousRes, resource, resource.getId(), resourcePath);
private ArrayList<JsonArrayEntry> lwM2mResourceToJsonArrayEntry(String resourcePath, Long timestamp, LwM2mResource resource) { ResourceModel rSpec = model.getResourceModel(objectId, resource.getId()); Type expectedType = rSpec != null ? rSpec.type : resource.getType(); ArrayList<JsonArrayEntry> resourcesList = new ArrayList<>();
instance.write(resource.getId(), resource);
JsonObject resources = Json.object(); for (LwM2mResource resource : ((LwM2mObjectInstance) n).getResources().values()) { resources.add(String.valueOf(resource.getId()), jSerialize(resource));
JsonObject resources = Json.object(); for (LwM2mResource resource : ((LwM2mObjectInstance) n).getResources().values()) { resources.add(String.valueOf(resource.getId()), jSerialize(resource));