接收乐山服务器上观察到的对象变化

Receiving Observed object changes on Leshan server

我正在基于 repo 中包含的 leshan-server-demo 构建一个简单的原型。我正在尝试从已观察到的对象接收更新。数据包捕获显示更新正在到达服务器,但我没有收到通知。

我找到的最接近的答案来自 2015 年 () - 但随后对乐山代码库的更改使相同的技术无法使用。

我试过使用 ObservationService 添加 ObservationListener,但这似乎只在请求 Observe 时提醒我,而不是在端点发送更改的值时提醒我。

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");
        }
    });
}

我应该如何监听乐山服务器上的观察对象?

您需要处理 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 }] } }

如果你使用的是乐山服务器Demo,如果你想从浏览器监听客户端的通知值变化,你的前端可以使用eventsource(服务器发送的事件)来接收来自乐山服务器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);
      }