How to retrieve updated content on an Observed resource in Leshan?

我怕爱的太早我们不能终老 提交于 2019-12-12 03:20:20

问题


Currently I am working on proof of concept using Leshan to recieve log messages from devices on regular basis. We found that Observe request is the best way to go for this approach. So temporarily, since we don't have a custom object yet(Wakama client not ready yet), on the server side when the client registers, I observe resource 15 on Device in LeshanClientExample. Here is the Observe code in LeshanServer.java:

 this.clientRegistry.addListener(new ClientRegistryListener() {
    ....
    @Override
     public void registered(final Client client) {
        // TODO observe the client when it is registered.
        observeResource(client);
      }
      ....

private void observeResource(final Client client){
    ObserveRequest request = new ObserveRequest("/3/0/15");
    LwM2mResponse cResponse = this.send(client, request);
}

Next I want to capture the change every time there is a change in the resource and record in a database. I see that in

 org.eclipse.leshan.client.californium.impl.ObjectResource 

I get content that was updated (in handleGET()). Is that the correct place to retrieve updates from ? I am trying to find out where in the code base is the correct place to retrieve updated content ? Your help will be highly appreciated.

Thanks


回答1:


As suggested on another blog post. There is 2 ways to be notified of observation.

// listen all observation via the ObservationRegistry
server.getObservationRegistry().addListener(new ObservationRegistryListener() {
 @Override
 public void newValue(Observation observation, LwM2mNode value) {}

@Override
public void cancelled(Observation observation) {}

@Override
public void newObservation(Observation observation) {}

});


// listen a particular observe request (recently added in master)
ObserveRequest request = new ObserveRequest("/3/0/13");
ObserveResponse response = server.send(client, request, TIMEOUT);
response.getObservation().addListener(new ObservationListener() {
@Override
public void newValue(Observation observation, LwM2mNode value) {}

@Override
public void cancelled(Observation observation) {}

});


来源:https://stackoverflow.com/questions/33292137/how-to-retrieve-updated-content-on-an-observed-resource-in-leshan

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!