2 * Copyright (c) 2010-2021 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.shelly.internal.coap;
15 import static org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.COIOT_PORT;
17 import java.net.InetAddress;
18 import java.net.InetSocketAddress;
19 import java.net.SocketException;
20 import java.net.UnknownHostException;
22 import java.util.concurrent.ConcurrentHashMap;
24 import org.eclipse.californium.core.CoapResource;
25 import org.eclipse.californium.core.CoapServer;
26 import org.eclipse.californium.core.coap.CoAP;
27 import org.eclipse.californium.core.coap.CoAP.Code;
28 import org.eclipse.californium.core.coap.CoAP.ResponseCode;
29 import org.eclipse.californium.core.coap.Request;
30 import org.eclipse.californium.core.coap.Response;
31 import org.eclipse.californium.core.network.CoapEndpoint;
32 import org.eclipse.californium.core.network.Exchange;
33 import org.eclipse.californium.core.network.config.NetworkConfig;
34 import org.eclipse.californium.elements.UdpMulticastConnector;
35 import org.eclipse.jdt.annotation.NonNullByDefault;
36 import org.eclipse.jdt.annotation.Nullable;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link ShellyCoapServer} implements the UDP listener and status event processor (for /cit/s messages)
43 * @author Markus Michels - Initial contribution
46 public class ShellyCoapServer {
47 private final Logger logger = LoggerFactory.getLogger(ShellyCoapServer.class);
49 boolean started = false;
50 private CoapEndpoint statusEndpoint = new CoapEndpoint.Builder().build();
51 private @Nullable UdpMulticastConnector statusConnector;
52 private final CoapServer server = new CoapServer(NetworkConfig.getStandard(), COIOT_PORT);;
53 private final Set<ShellyCoapListener> coapListeners = ConcurrentHashMap.newKeySet();
55 protected class ShellyStatusListener extends CoapResource {
56 private ShellyCoapServer listener;
58 public ShellyStatusListener(String uri, ShellyCoapServer listener) {
60 getAttributes().setTitle("ShellyCoapListener");
61 this.listener = listener;
65 public void handleRequest(@Nullable final Exchange exchange) {
66 if (exchange != null) {
67 Request request = exchange.getRequest();
68 Code code = exchange.getRequest().getCode();
71 listener.processResponse(createResponse(request));
74 super.handleRequest(exchange);
80 public synchronized void start(String localIp, ShellyCoapListener listener)
81 throws UnknownHostException, SocketException {
83 logger.debug("Initializing CoIoT listener (local IP={}:{})", localIp, COIOT_PORT);
84 NetworkConfig nc = NetworkConfig.getStandard();
85 InetAddress localAddr = InetAddress.getByName(localIp);
86 InetSocketAddress localPort = new InetSocketAddress(COIOT_PORT);
88 // Join the multicast group on the selected network interface
89 statusConnector = new UdpMulticastConnector(localAddr, localPort, CoAP.MULTICAST_IPV4); // bind UDP listener
90 statusEndpoint = new CoapEndpoint.Builder().setNetworkConfig(nc).setConnector(statusConnector).build();
91 server.addEndpoint(statusEndpoint);
92 CoapResource cit = new ShellyStatusListener("cit", this);
93 CoapResource s = new ShellyStatusListener("s", this);
99 if (!coapListeners.contains(listener)) {
100 coapListeners.add(listener);
104 protected void processResponse(Response response) {
105 coapListeners.forEach(listener -> listener.processResponse(response));
108 public static Response createResponse(Request request) {
109 Response response = Response.createResponse(request, ResponseCode.CONTENT);
110 response.setType(request.getType());
111 response.setSourceContext(request.getSourceContext());
112 response.setMID(request.getMID());
113 response.setOptions(request.getOptions());
114 response.setPayload(request.getPayload());
119 public CoapEndpoint getEndpoint() {
120 return statusEndpoint;
124 * Cancel pending requests and shutdown the client
126 public void stop(ShellyCoapListener listener) {
127 coapListeners.remove(listener);
128 if (coapListeners.isEmpty()) {
133 private synchronized void stop() {
137 statusEndpoint.stop();
138 coapListeners.clear();
140 logger.debug("CoAP Listener stopped");
144 public void dispose() {