-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
read composite on root path #1520
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,6 @@ public interface LwM2mNodeVisitor { | |
|
||
void visit(LwM2mResourceInstance instance); | ||
|
||
default void visit(LwM2mRoot root) { | ||
} | ||
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure we should add a default visit. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2013-2015 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Orange Polska S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.core.node; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
public class LwM2mRoot implements LwM2mNode { | ||
private final Map<Integer, LwM2mObject> objects; | ||
|
||
public LwM2mRoot(Collection<LwM2mObject> objects) { | ||
LwM2mNodeUtil.validateNotNull(objects, "objects MUST NOT be null"); | ||
HashMap<Integer, LwM2mObject> objectsMap = new HashMap<>(objects.size()); | ||
for (LwM2mObject object : objects) { | ||
LwM2mObject previous = objectsMap.put(object.getId(), object); | ||
if (previous != null) { | ||
throw new LwM2mNodeException( | ||
"Unable to create LwM2mRoot : there are several objects with the same id %d", object.getId()); | ||
} | ||
} | ||
this.objects = Collections.unmodifiableMap(objectsMap); | ||
} | ||
|
||
@Override | ||
public int getId() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void accept(LwM2mNodeVisitor visitor) { | ||
visitor.visit(this); | ||
} | ||
|
||
@Override | ||
public String toPrettyString(LwM2mPath path) { | ||
return appendPrettyNode(new StringBuilder(), path).toString(); | ||
} | ||
|
||
@Override | ||
public StringBuilder appendPrettyNode(StringBuilder b, LwM2mPath path) { | ||
if (!path.isRoot()) | ||
throw new IllegalArgumentException("Path must be a root path"); | ||
if (path.getObjectId() != getId()) | ||
throw new IllegalArgumentException("Path id must match this LwM2mMultipleResource id"); | ||
|
||
if (objects.isEmpty()) { | ||
b.append(path).append(" : {}"); | ||
} else { | ||
boolean first = true; | ||
for (Map.Entry<Integer, LwM2mObject> entry : new TreeMap<>(objects).entrySet()) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
b.append("\n"); | ||
} | ||
entry.getValue().appendPrettyNode(b, path.append(entry.getKey())); | ||
} | ||
} | ||
return b; | ||
} | ||
|
||
public Map<Integer, LwM2mObject> getObjects() { | ||
return objects; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
LwM2mRoot other = (LwM2mRoot) obj; | ||
if (objects == null) { | ||
if (other.objects != null) | ||
return false; | ||
} else if (!objects.equals(other.objects)) | ||
return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + getId(); | ||
result = prime * result + ((objects == null) ? 0 : objects.hashCode()); | ||
return result; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import org.eclipse.leshan.core.node.LwM2mPath; | ||
import org.eclipse.leshan.core.node.LwM2mResource; | ||
import org.eclipse.leshan.core.node.LwM2mResourceInstance; | ||
import org.eclipse.leshan.core.node.LwM2mRoot; | ||
import org.eclipse.leshan.core.node.ObjectLink; | ||
import org.eclipse.leshan.core.node.TimestampedLwM2mNode; | ||
import org.eclipse.leshan.core.node.TimestampedLwM2mNodes; | ||
|
@@ -200,19 +201,41 @@ public byte[] encodeTimestampedNodes(TimestampedLwM2mNodes timestampedNodes, LwM | |
|
||
private class InternalEncoder implements LwM2mNodeVisitor { | ||
// visitor inputs | ||
private int objectId; | ||
private Integer objectId; | ||
private LwM2mModel model; | ||
private LwM2mPath requestPath; | ||
private LwM2mValueConverter converter; | ||
|
||
// visitor output | ||
private ArrayList<SenMLRecord> records = new ArrayList<>(); | ||
|
||
@Override | ||
public void visit(LwM2mRoot root) { | ||
LOG.trace("Encoding Root into SenML"); | ||
// Validate request path | ||
if (!requestPath.isRoot()) { | ||
throw new CodecException("Invalid request path %s for root encoding", requestPath); | ||
} | ||
|
||
// Create SenML records | ||
for (LwM2mObject object : root.getObjects().values()) { | ||
for (LwM2mObjectInstance instance : object.getInstances().values()) { | ||
for (LwM2mResource resource : instance.getResources().values()) { | ||
String prefixPath = object.getId() + "/" + instance.getId() + "/" + resource.getId(); | ||
this.objectId = object.getId(); | ||
lwM2mResourceToSenMLRecord(prefixPath, resource); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void visit(LwM2mObject object) { | ||
LOG.trace("Encoding Object {} into SenML", object); | ||
// Validate request path | ||
if (!requestPath.isObject()) { | ||
if (requestPath.isRoot()) { | ||
throw new CodecException("Invalid request path %s for root encoding", requestPath); | ||
} else if (!requestPath.isObject()) { | ||
throw new CodecException("Invalid request path %s for object encoding", requestPath); | ||
} | ||
|
||
|
@@ -302,7 +325,7 @@ private void addSenMLRecord(String recordName, Type valueType, Type expectedType | |
String n = recordName == null ? "" : recordName; | ||
|
||
// Add slash if necessary | ||
if (!n.isEmpty()) { | ||
if (!n.isEmpty() && !bn.equals("/")) { | ||
bn += "/"; | ||
Comment on lines
+328
to
329
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds strange I need to look at this deeper. |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep I think some check should be done here.
The spec says :
(http://www.openmobilealliance.org/release/LightweightM2M/V1_1_1-20190617-A/HTML-Version/OMA-TS-LightweightM2M_Core-V1_1_1-20190617-A.html#6-3-8-0-638-Read-Composite-Operation)
So I think on failure, we should not stop the read-composite operation but just log as error.
(so error will not be seen at server side)