【问题标题】:Receiving Observed object changes on Leshan server在乐山服务器上接收观察到的对象变化
【发布时间】:2018-10-23 18:41:34
【问题描述】:

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

我找到的最接近的答案来自 2015 年 (How to retrieve updated content on an Observed resource in Leshan?) - 但随后对乐山代码库的更改使相同的技术无法使用。

我尝试使用 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");
        }
    });
}

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

【问题讨论】:

    标签: java coap leshan


    【解决方案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 }] } }

    【讨论】:

      【解决方案2】:

      如果您正在使用 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);
            }
      

      【讨论】:

        猜你喜欢
        • 2021-08-12
        • 1970-01-01
        • 1970-01-01
        • 2016-04-15
        • 1970-01-01
        • 2018-09-02
        • 2014-01-07
        • 2012-09-29
        • 2018-04-10
        相关资源
        最近更新 更多