2

我正在基于 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");
        }
    });
}

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

4

2 回答 2

1

您需要处理 onResponse:来自https://github.com/eclipse/leshan/blob/f315c66602b1061175f2441c019b862946d08a55/leshan-server-demo/src/main/java/org/eclipse/leshan/server/demo/servlet/EventServlet。爪哇#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 }] } }

于 2018-12-10T10:02:54.850 回答
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);
      }
于 2019-04-08T04:34:40.263 回答