]> git.basschouten.com Git - openhab-addons.git/blob
e1312b3e2e060ebe24da518a5a9de551761a2164
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tradfri.internal;
14
15 import java.util.concurrent.CompletableFuture;
16
17 import org.eclipse.californium.core.CoapHandler;
18 import org.eclipse.californium.core.CoapResponse;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.gson.JsonParseException;
27 import com.google.gson.JsonParser;
28
29 /**
30  * The {@link TradfriCoapHandler} is used to handle the asynchronous coap reponses.
31  * It can either be used with a callback class or with a future.
32  *
33  * @author Kai Kreuzer - Initial contribution
34  */
35 @NonNullByDefault
36 public class TradfriCoapHandler implements CoapHandler {
37
38     private final Logger logger = LoggerFactory.getLogger(TradfriCoapHandler.class);
39
40     private @Nullable CoapCallback callback;
41     private @Nullable CompletableFuture<String> future;
42
43     /**
44      * Constructor for using a callback
45      *
46      * @param callback the callback to use for responses
47      */
48     public TradfriCoapHandler(CoapCallback callback) {
49         this.callback = callback;
50     }
51
52     /**
53      * Constructor for using a future
54      *
55      * @param future the future to use for responses
56      */
57     public TradfriCoapHandler(CompletableFuture<String> future) {
58         this.future = future;
59     }
60
61     @Override
62     public void onLoad(@Nullable CoapResponse response) {
63         if (response == null) {
64             logger.trace("received empty CoAP response");
65             return;
66         }
67         logger.debug("CoAP response\noptions: {}\npayload: {}", response.getOptions(), response.getResponseText());
68         if (response.isSuccess()) {
69             final CoapCallback callback = this.callback;
70             if (callback != null) {
71                 try {
72                     callback.onUpdate(JsonParser.parseString(response.getResponseText()));
73                     callback.setStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
74                 } catch (JsonParseException e) {
75                     logger.warn("Observed value is no valid json: {}, {}", response.getResponseText(), e.getMessage());
76                 }
77             }
78             final CompletableFuture<String> future = this.future;
79             if (future != null) {
80                 String data = response.getResponseText();
81                 future.complete(data);
82             }
83         } else {
84             logger.debug("CoAP error {}", response.getCode());
85             if (callback != null) {
86                 callback.setStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
87             }
88             if (future != null) {
89                 future.completeExceptionally(new RuntimeException("Response " + response.getCode().toString()));
90             }
91         }
92     }
93
94     @Override
95     public void onError() {
96         logger.debug("CoAP onError");
97         if (callback != null) {
98             callback.setStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
99         }
100         if (future != null) {
101             future.completeExceptionally(new RuntimeException("CoAP GET resulted in an error."));
102         }
103     }
104 }