Skip to content

Commit

Permalink
Support of ObserveComposite for timestamped data on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslawLegierski committed Dec 11, 2023
1 parent ab645dd commit aa5031c
Show file tree
Hide file tree
Showing 18 changed files with 416 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public void visit(ReadCompositeRequest request) {

@Override
public void visit(ObserveCompositeRequest request) {
response = new ObserveCompositeResponse(code, null, errorMessage, null, null);
response = new ObserveCompositeResponse(code, null, errorMessage, null, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ public List<TimestampedLwM2mNode> decodeTimestampedData(byte[] content, ContentF
}

@Override
public TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, ContentFormat format, LwM2mModel model)
throws CodecException {
public TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, ContentFormat format, LwM2mModel model,
List<LwM2mPath> paths) throws CodecException {
LOG.trace("Decoding value for format {}: {}", format, content);

if (format == null) {
Expand All @@ -221,7 +221,7 @@ public TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, ContentForma
}

if (decoder instanceof TimestampedMultiNodeDecoder) {
return ((TimestampedMultiNodeDecoder) decoder).decodeTimestampedNodes(content, model);
return ((TimestampedMultiNodeDecoder) decoder).decodeTimestampedNodes(content, model, paths);
} else if (decoder instanceof MultiNodeDecoder) {
return new TimestampedLwM2mNodes.Builder()
.addNodes(((MultiNodeDecoder) decoder).decodeNodes(content, null, model)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ List<TimestampedLwM2mNode> decodeTimestampedData(byte[] content, ContentFormat f
* @return the decoded timestamped nodes represented by {@link TimestampedLwM2mNodes}
* @throws CodecException if content is malformed.
*/
TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, ContentFormat format, LwM2mModel model)
throws CodecException;
TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, ContentFormat format, LwM2mModel model,
List<LwM2mPath> paths) throws CodecException;

/**
* Deserializes a binary content into a list of {@link LwM2mPath}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*******************************************************************************/
package org.eclipse.leshan.core.node.codec;

import java.util.List;

import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.TimestampedLwM2mNodes;

/**
Expand All @@ -33,6 +36,7 @@ public interface TimestampedMultiNodeDecoder {
* @return the decoded timestamped nodes represented by {@link TimestampedLwM2mNodes}
* @throws CodecException if content is malformed.
*/
TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, LwM2mModel model) throws CodecException;
TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, LwM2mModel model, List<LwM2mPath> paths)
throws CodecException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public List<TimestampedLwM2mNode> decodeTimestampedData(byte[] content, LwM2mPat
}

@Override
public TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, LwM2mModel model) throws CodecException {
public TimestampedLwM2mNodes decodeTimestampedNodes(byte[] content, LwM2mModel model, List<LwM2mPath> paths)
throws CodecException {
try {
// Decode SenML pack
SenMLPack pack = decoder.fromSenML(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CancelCompositeObservationResponse extends ObserveCompositeResponse

public CancelCompositeObservationResponse(ResponseCode code, Map<LwM2mPath, LwM2mNode> content, String errorMessage,
Object coapResponse, CompositeObservation observation) {
super(code, content, errorMessage, coapResponse, observation);
super(code, content, errorMessage, coapResponse, observation, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.TimestampedLwM2mNodes;
import org.eclipse.leshan.core.observation.CompositeObservation;

/**
Expand All @@ -30,11 +31,13 @@
public class ObserveCompositeResponse extends ReadCompositeResponse {

protected final CompositeObservation observation;
protected final TimestampedLwM2mNodes timestampedValues;

public ObserveCompositeResponse(ResponseCode code, Map<LwM2mPath, LwM2mNode> content, String errorMessage,
Object coapResponse, CompositeObservation observation) {
Object coapResponse, CompositeObservation observation, TimestampedLwM2mNodes timestampedValues) {
super(code, content, errorMessage, coapResponse);
this.observation = observation;
this.timestampedValues = timestampedValues;
}

public CompositeObservation getObservation() {
Expand Down Expand Up @@ -69,30 +72,38 @@ public boolean isValid() {
// Syntactic sugar static constructors:

public static ObserveCompositeResponse success(Map<LwM2mPath, LwM2mNode> content) {
return new ObserveCompositeResponse(ResponseCode.CONTENT, content, null, null, null);
return new ObserveCompositeResponse(ResponseCode.CONTENT, content, null, null, null, null);
}

public static ObserveCompositeResponse success(TimestampedLwM2mNodes timestampedValues) {
return new ObserveCompositeResponse(ResponseCode.CONTENT, null, null, null, null, timestampedValues);
}

public static ObserveCompositeResponse badRequest(String errorMessage) {
return new ObserveCompositeResponse(ResponseCode.BAD_REQUEST, null, errorMessage, null, null);
return new ObserveCompositeResponse(ResponseCode.BAD_REQUEST, null, errorMessage, null, null, null);
}

public static ObserveCompositeResponse notFound() {
return new ObserveCompositeResponse(ResponseCode.NOT_FOUND, null, null, null, null);
return new ObserveCompositeResponse(ResponseCode.NOT_FOUND, null, null, null, null, null);
}

public static ObserveCompositeResponse unauthorized() {
return new ObserveCompositeResponse(ResponseCode.UNAUTHORIZED, null, null, null, null);
return new ObserveCompositeResponse(ResponseCode.UNAUTHORIZED, null, null, null, null, null);
}

public static ObserveCompositeResponse methodNotAllowed() {
return new ObserveCompositeResponse(ResponseCode.METHOD_NOT_ALLOWED, null, null, null, null);
return new ObserveCompositeResponse(ResponseCode.METHOD_NOT_ALLOWED, null, null, null, null, null);
}

public static ObserveCompositeResponse notAcceptable() {
return new ObserveCompositeResponse(ResponseCode.UNSUPPORTED_CONTENT_FORMAT, null, null, null, null);
return new ObserveCompositeResponse(ResponseCode.UNSUPPORTED_CONTENT_FORMAT, null, null, null, null, null);
}

public static ObserveCompositeResponse internalServerError(String errorMessage) {
return new ObserveCompositeResponse(ResponseCode.INTERNAL_SERVER_ERROR, null, errorMessage, null, null);
return new ObserveCompositeResponse(ResponseCode.INTERNAL_SERVER_ERROR, null, errorMessage, null, null, null);
}

public TimestampedLwM2mNodes getTimestampedLwM2mNodes() {
return timestampedValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ public void senml_multiple_timestamped_nodes() throws CodecException {

// when
TimestampedLwM2mNodes data = decoder.decodeTimestampedNodes(b.toString().getBytes(), ContentFormat.SENML_JSON,
model);
model, null);

// then
Instant timestamp = Instant.ofEpochSecond(268600000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void should_create_response_with_content() {
exampleContent.put(new LwM2mPath("/2/3/4"), newResource(16, "example 2"));

// when
ObserveCompositeResponse response = new ObserveCompositeResponse(CONTENT, exampleContent, null, null, null);
ObserveCompositeResponse response = new ObserveCompositeResponse(CONTENT, exampleContent, null, null, null,
null);

// then
assertEquals(exampleContent, response.getContent());
Expand Down
Loading

0 comments on commit aa5031c

Please sign in to comment.