]> git.basschouten.com Git - openhab-addons.git/blob
48370155c89e64c8b509c55391860b5ec38c84a8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
14 package org.openhab.binding.gree.internal.discovery;
15
16 import static org.openhab.binding.gree.internal.GreeBindingConstants.*;
17
18 import java.net.DatagramSocket;
19 import java.net.SocketException;
20 import java.util.HashMap;
21 import java.util.Map;
22
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;
41
42 /**
43  * {@link GreeDiscoveryService} implements the device discovery service. UDP broadtcast ius used to find the devices on
44  * the local subnet.
45  *
46  * @author Markus Michels - Initial contribution
47  *
48  */
49 @NonNullByDefault
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;
57
58     @Activate
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);
69     }
70
71     @Override
72     @Modified
73     protected void modified(@Nullable Map<String, @Nullable Object> configProperties) {
74         super.modified(configProperties);
75     }
76
77     @Override
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);
82     }
83
84     @Override
85     protected void stopBackgroundDiscovery() {
86         stopScan();
87     }
88
89     @Override
90     protected void startScan() {
91         try (DatagramSocket clientSocket = new DatagramSocket()) {
92             deviceFinder.scan(clientSocket, broadcastAddress, true);
93
94             int count = deviceFinder.getScannedDeviceCount();
95             logger.debug("{}", messages.get("discovery.result", count));
96             if (count > 0) {
97                 logger.debug("Adding uinits to Inbox");
98                 createResult(deviceFinder.getDevices());
99             }
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);
104         }
105     }
106
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);
121         }
122     }
123
124     @Override
125     public void deactivate() {
126         removeOlderResults(getTimestampOfLastScan());
127         super.deactivate();
128     }
129 }