Skip to content

Commit

Permalink
Add default impl for write resource instance in BaseInstanceEnabler
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Sep 3, 2020
1 parent a90d957 commit 16b283b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.node.LwM2mMultipleResource;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.node.LwM2mResourceInstance;
Expand Down Expand Up @@ -173,6 +174,20 @@ public WriteResponse write(ServerIdentity identity, int resourceid, LwM2mResourc
@Override
public WriteResponse write(ServerIdentity identity, int resourceid, int resourceInstanceId,
LwM2mResourceInstance value) {
// this is a sub-optimal default implementation
ReadResponse response = read(ServerIdentity.SYSTEM, resourceid);
if (response.isSuccess()) {
LwM2mNode content = response.getContent();
if (content instanceof LwM2mMultipleResource) {
LwM2mMultipleResource multiresource = ((LwM2mMultipleResource) content);
Map<Integer, LwM2mResourceInstance> instances = new HashMap<>(multiresource.getInstances());
if (instances.containsKey(resourceInstanceId)) {
instances.put(resourceInstanceId, value);
return write(identity, resourceid,
new LwM2mMultipleResource(resourceInstanceId, value.getType(), instances.values()));
}
}
}
return WriteResponse.notFound();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.node.LwM2mMultipleResource;
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.node.LwM2mResourceInstance;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.response.ExecuteResponse;
import org.eclipse.leshan.core.response.ReadResponse;
Expand Down Expand Up @@ -75,20 +74,6 @@ public ReadResponse read(ServerIdentity identity, int resourceid) {
return ReadResponse.notFound();
}

@Override
public ReadResponse read(ServerIdentity identity, int resourceId, int resourceInstance) {
if (resources.containsKey(resourceId)) {
LwM2mMultipleResource lwM2mResource = (LwM2mMultipleResource) resources.get(resourceId);
LwM2mResourceInstance resourceIn = lwM2mResource.getInstance(resourceInstance);
if (resourceIn == null) {
return ReadResponse.notFound();
} else {
return ReadResponse.success(resourceIn);
}
}
return ReadResponse.notFound();
}

@Override
public WriteResponse write(ServerIdentity identity, int resourceid, LwM2mResource value) {
LwM2mResource previousValue = resources.put(resourceid, value);
Expand All @@ -97,25 +82,6 @@ public WriteResponse write(ServerIdentity identity, int resourceid, LwM2mResourc
return WriteResponse.success();
}

@Override
public WriteResponse write(ServerIdentity identity, int resourceId, int resourceInstance,
LwM2mResourceInstance value) {
if (resources.containsKey(resourceId)) {
LwM2mMultipleResource lwM2mResource = (LwM2mMultipleResource) resources.get(resourceId);

// Rewrite specific instance value
Map<Integer, LwM2mResourceInstance> newMap = new HashMap<>(lwM2mResource.getInstances());
newMap.put(resourceInstance, value);

LwM2mMultipleResource newResource = new LwM2mMultipleResource(resourceId, lwM2mResource.getType(),
newMap.values());
return this.write(identity, resourceId, newResource);

}
return WriteResponse.notFound();

}

@Override
public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) {
return ExecuteResponse.notFound();
Expand Down

0 comments on commit 16b283b

Please sign in to comment.