2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tradfri.internal;
15 import java.util.concurrent.CompletableFuture;
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;
26 import com.google.gson.JsonParseException;
27 import com.google.gson.JsonParser;
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.
33 * @author Kai Kreuzer - Initial contribution
36 public class TradfriCoapHandler implements CoapHandler {
38 private final Logger logger = LoggerFactory.getLogger(TradfriCoapHandler.class);
40 private @Nullable CoapCallback callback;
41 private @Nullable CompletableFuture<String> future;
44 * Constructor for using a callback
46 * @param callback the callback to use for responses
48 public TradfriCoapHandler(CoapCallback callback) {
49 this.callback = callback;
53 * Constructor for using a future
55 * @param future the future to use for responses
57 public TradfriCoapHandler(CompletableFuture<String> future) {
62 public void onLoad(@Nullable CoapResponse response) {
63 if (response == null) {
64 logger.trace("received empty CoAP response");
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) {
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());
78 final CompletableFuture<String> future = this.future;
80 String data = response.getResponseText();
81 future.complete(data);
84 logger.debug("CoAP error {}", response.getCode());
85 if (callback != null) {
86 callback.setStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
89 future.completeExceptionally(new RuntimeException("Response " + response.getCode().toString()));
95 public void onError() {
96 logger.debug("CoAP onError");
97 if (callback != null) {
98 callback.setStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
100 if (future != null) {
101 future.completeExceptionally(new RuntimeException("CoAP GET resulted in an error."));