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.danfossairunit.internal.discovery;
15 import static org.openhab.binding.danfossairunit.internal.DanfossAirUnitBindingConstants.*;
17 import java.io.IOException;
18 import java.net.DatagramPacket;
19 import java.net.DatagramSocket;
20 import java.net.InetAddress;
21 import java.net.InterfaceAddress;
22 import java.net.NetworkInterface;
23 import java.net.SocketTimeoutException;
24 import java.util.Arrays;
25 import java.util.Enumeration;
26 import java.util.HashMap;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.core.config.discovery.AbstractDiscoveryService;
33 import org.openhab.core.config.discovery.DiscoveryResult;
34 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
35 import org.openhab.core.config.discovery.DiscoveryService;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.osgi.service.component.annotations.Component;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The Discovery service implementation to scan for available air units in the network via broadcast.
45 * @author Ralf Duckstein - Initial contribution
46 * @author Robert Bach - heavy refactorings
48 @Component(service = DiscoveryService.class)
50 public class DanfossAirUnitDiscoveryService extends AbstractDiscoveryService {
52 private static final int BROADCAST_PORT = 30045;
53 private static final byte[] DISCOVER_SEND = { 0x0c, 0x00, 0x30, 0x00, 0x11, 0x00, 0x12, 0x00, 0x13 };
54 private static final byte[] DISCOVER_RECEIVE = { 0x0d, 0x00, 0x07, 0x00, 0x02, 0x02, 0x00 };
55 private static final int TIMEOUT_IN_SECONDS = 15;
57 private final Logger logger = LoggerFactory.getLogger(DanfossAirUnitDiscoveryService.class);
59 public DanfossAirUnitDiscoveryService() {
60 super(SUPPORTED_THING_TYPES_UIDS, TIMEOUT_IN_SECONDS, true);
64 public Set<ThingTypeUID> getSupportedThingTypes() {
65 return SUPPORTED_THING_TYPES_UIDS;
69 protected void startBackgroundDiscovery() {
70 logger.debug("Start Danfoss Air CCM background discovery");
71 scheduler.execute(this::discover);
75 public void startScan() {
76 logger.debug("Start Danfoss Air CCM scan");
80 private synchronized void discover() {
81 logger.debug("Try to discover all Danfoss Air CCM devices");
83 try (DatagramSocket socket = new DatagramSocket()) {
84 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
85 while (interfaces.hasMoreElements()) {
87 NetworkInterface networkInterface = interfaces.nextElement();
88 if (networkInterface.isLoopback() || !networkInterface.isUp()) {
91 for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
92 if (interfaceAddress.getBroadcast() == null) {
95 logger.debug("Sending broadcast on interface {} to discover Danfoss Air CCM device...",
96 interfaceAddress.getAddress());
97 sendBroadcastToDiscoverThing(socket, interfaceAddress.getBroadcast());
100 } catch (IOException e) {
101 logger.debug("No Danfoss Air CCM device found. Diagnostic: {}", e.getMessage());
105 private void sendBroadcastToDiscoverThing(DatagramSocket socket, InetAddress broadcastAddress) throws IOException {
106 socket.setBroadcast(true);
107 socket.setSoTimeout(500);
109 byte[] sendBuffer = DISCOVER_SEND;
110 DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, broadcastAddress, BROADCAST_PORT);
111 socket.send(sendPacket);
112 logger.debug("Discover message sent");
114 // wait for responses
116 byte[] receiveBuffer = new byte[7];
117 DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
119 socket.receive(receivePacket);
120 } catch (SocketTimeoutException e) {
121 break; // leave the endless loop
124 byte[] data = receivePacket.getData();
125 if (Arrays.equals(data, DISCOVER_RECEIVE)) {
126 logger.debug("Discover received correct response");
128 String host = receivePacket.getAddress().getHostName();
129 Map<String, Object> properties = new HashMap<>();
130 properties.put("host", host);
132 logger.debug("Adding a new Danfoss Air Unit CCM '{}' to inbox", host);
134 ThingUID uid = new ThingUID(THING_TYPE_AIRUNIT, String.valueOf(receivePacket.getAddress().hashCode()));
136 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withRepresentationProperty("host")
137 .withProperties(properties).withLabel("Danfoss HRV").build();
138 thingDiscovered(result);
140 logger.debug("Thing discovered '{}'", result);