@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()); }
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])); }