2 * Copyright (c) 2010-2020 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
14 package org.openhab.binding.gree.internal.discovery;
16 import static org.openhab.binding.gree.internal.GreeBindingConstants.*;
18 import java.net.DatagramSocket;
19 import java.net.SocketException;
20 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.gree.internal.GreeException;
26 import org.openhab.binding.gree.internal.GreeTranslationProvider;
27 import org.openhab.binding.gree.internal.handler.GreeAirDevice;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.net.NetworkAddressService;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingUID;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Modified;
38 import org.osgi.service.component.annotations.Reference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * {@link GreeDiscoveryService} implements the device discovery service. UDP broadtcast ius used to find the devices on
46 * @author Markus Michels - Initial contribution
50 @Component(service = DiscoveryService.class, configurationPid = "discovery.gree")
51 public class GreeDiscoveryService extends AbstractDiscoveryService {
52 private static final int TIMEOUT_SEC = 10;
53 private final Logger logger = LoggerFactory.getLogger(GreeDiscoveryService.class);
54 private final GreeDeviceFinder deviceFinder;
55 private final GreeTranslationProvider messages;
56 private final String broadcastAddress;
59 public GreeDiscoveryService(@Reference GreeDeviceFinder deviceFinder,
60 @Reference NetworkAddressService networkAddressService,
61 @Reference GreeTranslationProvider translationProvider,
62 @Nullable Map<String, @Nullable Object> configProperties) {
63 super(SUPPORTED_THING_TYPES_UIDS, TIMEOUT_SEC);
64 this.messages = translationProvider;
65 this.deviceFinder = deviceFinder;
66 String ip = networkAddressService.getConfiguredBroadcastAddress();
67 broadcastAddress = ip != null ? ip : "";
68 activate(configProperties);
73 protected void modified(@Nullable Map<String, @Nullable Object> configProperties) {
74 super.modified(configProperties);
78 protected void startBackgroundDiscovery() {
79 // It's very unusual that a new unit gets installed frequently so we run the discovery once when the binding is
80 // started, but not frequently
81 scheduler.execute(this::startScan);
85 protected void stopBackgroundDiscovery() {
90 protected void startScan() {
91 try (DatagramSocket clientSocket = new DatagramSocket()) {
92 deviceFinder.scan(clientSocket, broadcastAddress, true);
94 int count = deviceFinder.getScannedDeviceCount();
95 logger.debug("{}", messages.get("discovery.result", count));
97 logger.debug("Adding uinits to Inbox");
98 createResult(deviceFinder.getDevices());
100 } catch (GreeException e) {
101 logger.info("Discovery: {}", messages.get("discovery.exception", e.getMessage()));
102 } catch (SocketException | RuntimeException e) {
103 logger.warn("Discovery: {}", messages.get("discovery.exception", "RuntimeException"), e);
107 public void createResult(Map<String, GreeAirDevice> deviceList) {
108 for (GreeAirDevice device : deviceList.values()) {
109 String ipAddress = device.getAddress().getHostAddress();
110 logger.debug("{}", messages.get("discovery.newunit", device.getName(), ipAddress, device.getId()));
111 Map<String, Object> properties = new HashMap<>();
112 properties.put(Thing.PROPERTY_VENDOR, device.getVendor());
113 properties.put(Thing.PROPERTY_MODEL_ID, device.getModel());
114 properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getId());
115 properties.put(PROPERTY_IP, ipAddress);
116 properties.put(PROPERTY_BROADCAST, broadcastAddress);
117 ThingUID thingUID = new ThingUID(THING_TYPE_GREEAIRCON, device.getId());
118 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
119 .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).withLabel(device.getName()).build();
120 thingDiscovered(result);
125 public void deactivate() {
126 removeOlderResults(getTimestampOfLastScan());