@Override protected ReadResponse doRead(ReadRequest request) { LwM2mPath path = request.getPath(); // Manage Object case if (path.isObject()) { List<LwM2mObjectInstance> lwM2mObjectInstances = new ArrayList<>(); for (Entry<Integer, LwM2mInstanceEnabler> entry : instances.entrySet()) { lwM2mObjectInstances.add(getLwM2mObjectInstance(entry.getKey(), entry.getValue())); } return ReadResponse.success( new LwM2mObject(getId(), lwM2mObjectInstances.toArray(new LwM2mObjectInstance[0]))); } // Manage Instance case LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId()); if (instance == null) return ReadResponse.notFound(); if (path.getResourceId() == null) { return ReadResponse.success(getLwM2mObjectInstance(path.getObjectInstanceId(), instance)); } // Manage Resource case return instance.read(path.getResourceId()); }
@Override protected ExecuteResponse doExecute(ExecuteRequest request) { LwM2mPath path = request.getPath(); LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId()); if (instance == null) { return ExecuteResponse.notFound(); } return instance.execute(path.getResourceId(), request.getParameters()); }
private void listenInstance(LwM2mInstanceEnabler instance, final int instanceId) { instance.addResourceChangedListener( new ResourceChangedListener() { @Override public void resourceChanged(int resourceId) { getNotifySender().sendNotify(getId() + "/" + instanceId + "/" + resourceId); } }); }
@Override protected WriteResponse doWrite(WriteRequest request) { LwM2mPath path = request.getPath(); // Manage Instance case LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId()); if (instance == null) return WriteResponse.notFound(); if (path.getResourceId() == null) { for (LwM2mResource resource : ((LwM2mObjectInstance) request.getNode()).getResources().values()) { instance.write(resource.getId(), resource); } return WriteResponse.success(); } // Manage Resource case return instance.write(path.getResourceId(), (LwM2mResource) request.getNode()); }
LwM2mObjectInstance getLwM2mObjectInstance(int instanceid, LwM2mInstanceEnabler instance) { List<LwM2mResource> resources = new ArrayList<>(); for (ResourceModel resourceModel : getObjectModel().resources.values()) { if (resourceModel.operations.isReadable()) { ReadResponse response = instance.read(resourceModel.id); if (response.getCode() == ResponseCode.CONTENT && response.getContent() instanceof LwM2mResource) resources.add((LwM2mResource) response.getContent()); } } return new LwM2mObjectInstance(instanceid, resources.toArray(new LwM2mResource[0])); }
@Override protected synchronized CreateResponse doCreate(CreateRequest request) { Integer instanceId = request.getPath().getObjectInstanceId(); 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()); }