eclipse-leshan / leshan

Java Library for LWM2M
https://www.eclipse.org/leshan/
BSD 3-Clause "New" or "Revised" License
653 stars 408 forks source link

Including timestamped send option to SendService #1584

Closed Ozame closed 8 months ago

Ozame commented 9 months ago

Question

Currently the default LeshanClientcreates an implementation of SendServicethat can be used to send data from the client to the server. This implementation contains both the sync and async versions of the send. However, DataSenderManagerused to send the data also supports the versions of sendData that take TimestampedNodesinstead of plain LwM2mNodes:

public SendResponse sendData(LwM2mServer server, ContentFormat format, TimestampedLwM2mNodes nodes,
            long timeoutInMs)

vs.

public SendResponse sendData(LwM2mServer server, ContentFormat format, Map<LwM2mPath, LwM2mNode> nodes,
            long timeoutInMs)

Would it make sense to include this timestamped option to the default client / SendService interface? Or would this be unnecessary bloat for most users? Currently, if the timestamps are needed, a custom client class, or at least a clientBuilder class, is needed.

Changes would be needed to SendServiceinterface and the LeshanClient for this. Implementation itself is trivial, e.g.

 public SendResponse sendTimeStampedData(LwM2mServer server, ContentFormat format, List<String> paths, long timeoutInMs) throws InterruptedException {
                Validate.notNull(server);
                Validate.notEmpty(paths);
                Map<LwM2mPath, LwM2mNode> collectedData = Client.this.dataSenderManager.getCurrentValues(server, LwM2mPath.getLwM2mPathList(paths));
                var now = Instant.now();
                TimestampedLwM2mNodes nodes = TimestampedLwM2mNodes.builder()
                        .addNodes(now, collectedData)
                        .build();
                return Client.this.dataSenderManager.sendData(server, format, nodes, timeoutInMs);
            }

Depending of course if we want the timestamp to be passed or automatically created.

This case comes up when we use the LWM2M send service to push event related measurements from the client. I'm not sure if it's a common use case or not.

If this sounds good, I could submit a PR for this.

sbernard31 commented 9 months ago

Iam not sure but maybeManualDataSender is what need ?

Do you test the it ? it is able to collect timestamped data then send all collected data ?

You can test its behavior with leshan-client-demo ? using this interactive command

help collect

Usage:  collect [<paths>...]
Collect data to send it later with 'send' command
      [<paths>...]   Paths of data to collect.

help send

Usage:  send [-c=<contentFormat>] (current-value | collected-value)
Send data to server
  -c, --content-format=<contentFormat>
         Name (e.g. SENML_JSON) or code (e.g. 110) of Content Format used to
           send data.
         Default : SENML_CBOR
Commands:
  current-value    Send current value
  collected-value  Send values collected with 'collect' command

You could have code example here :

https://github.com/eclipse-leshan/leshan/blob/f23254c85928bf0eba54c10b515b36ef050a6959/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/cli/interactive/InteractiveCommands.java#L337-L357

and

https://github.com/eclipse-leshan/leshan/blob/f23254c85928bf0eba54c10b515b36ef050a6959/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/cli/interactive/InteractiveCommands.java#L307-L335

If this doesn't match you need, could you try to explain why ? :pray:

sbernard31 commented 8 months ago

Does that work for you ? should we close this issue ?

Ozame commented 8 months ago

Yes, this actually looks to be exactly what we needed, thank you! I will close the issue.