在乐山服务器上接收观察到的对象更改

时间:2018-10-23 18:41:34

标签: java coap leshan

我正在基于回购中包含的leshan-server-demo构建一个简单的原型。我正在尝试从观察到的对象接收更新。数据包捕获表明更新正在进入服务器,但是我没有收到任何通知。

我找到的最接近答案是2015年(How to retrieve updated content on an Observed resource in Leshan?)-但是后来对Leshan代码库的更改使该技术不可行。

我尝试使用observationService添加一个observationListener,但这似乎只是在请求观察者时提醒我,而不是在端点发送更改后的值时提醒我。

static private void attachListener(final LeshanServer server) {
    System.out.println("Attaching Listener");
    server.getObservationService().addListener(new ObservationListener() {
        @Override
        public void newObservation(Observation observation, Registration registration) {
            System.out.println("New Observation");
        }
        @Override
        public void cancelled(Observation observation) {
            System.out.println("Observation cancellation");
        }
        @Override
        public void onResponse(Observation observation, Registration registration, ObserveResponse response) {
            System.out.println("Observation Response");
        }
        @Override
        public void onError(Observation observation, Registration registration, Exception error) {
            System.out.println("Observation Error");
        }
    });
}

我应该如何在Leshan服务器上侦听观察到的对象?

2 个答案:

答案 0 :(得分:1)

您需要处理onResponse: 来自https://github.com/eclipse/leshan/blob/f315c66602b1061175f2441c019b862946d08a55/leshan-server-demo/src/main/java/org/eclipse/leshan/server/demo/servlet/EventServlet.java#L133

@Override
public void onResponse(Observation observation, Registration registration, ObserveResponse response) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Received notification from [{}] containing value [{}]", observation.getPath(),
                        response.getContent().toString());
            }

            if (registration != null) {
                String data = new StringBuilder("{\"ep\":\"").append(registration.getEndpoint()).append("\",\"res\":\"")
                        .append(observation.getPath().toString()).append("\",\"val\":")
                        .append(gson.toJson(response.getContent())).append("}").toString();

                sendEvent(EVENT_NOTIFICATION, data, registration.getEndpoint());
            }
}

response.getContent()包含新值。代码构建的数据json看起来像 { "ep": "rpitest", "res": "/3334/0", "val": { "id": 0, "resources": [{ "id": 5704, "value": 1.7929173707962036 }, { "id": 5702, "value": 0.9917597770690918 }, { "id": 5703, "value": 154.53704833984375 }] } }

答案 1 :(得分:1)

如果您正在使用Leshan-server-Demo,并且想要从浏览器中监听客户端的通知值更改,则前端可以使用eventsource(服务器发送的事件)从Leshan-Server Demo接收通知。

例如,在我的Angular 7.0应用程序中,我听如下的COAP消息更改,

     // Register Event Source
      constructor() {
        this.eventsource = new EventSource('server/event?ep=' + this.clientId);
      }

       // Add listener for COAP messages call back
      ngOnInit() {   
        this.eventsource.addEventListener('COAPLOG', msg => this.coapLogCallback(msg), false);
        console.error('Event Source', this.eventsource);
      }

      // modify coaplogs arrays
      coapLogCallback(msg) {
        var log = JSON.parse(msg.data);
        if (100 < this.coaplogs.length) this.coaplogs.shift();
        this.coaplogs.unshift(log);
      }