]> git.basschouten.com Git - openhab-addons.git/blob
69afe982d1d745e60a0943fde705e622b0c035ef
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 package org.openhab.binding.silvercrestwifisocket.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.openhab.binding.silvercrestwifisocket.internal.SilvercrestWifiSocketBindingConstants;
20 import org.openhab.binding.silvercrestwifisocket.internal.handler.SilvercrestWifiSocketMediator;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.DiscoveryService;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * This is the {@link DiscoveryService} for the Silvercrest Items.
34  *
35  * @author Jaime Vaz - Initial contribution
36  *
37  */
38 @Component(service = DiscoveryService.class, configurationPid = "discovery.silvercrestwifisocket")
39 public class SilvercrestWifiSocketDiscoveryService extends AbstractDiscoveryService {
40
41     private final Logger logger = LoggerFactory.getLogger(SilvercrestWifiSocketDiscoveryService.class);
42     private SilvercrestWifiSocketMediator mediator;
43
44     /**
45      * Used by OSGI to inject the mediator in the discovery service.
46      *
47      * @param mediator the mediator
48      */
49     @Reference
50     public void setMediator(final SilvercrestWifiSocketMediator mediator) {
51         logger.debug("Mediator has been injected on discovery service.");
52         this.mediator = mediator;
53         mediator.setDiscoveryService(this);
54     }
55
56     /**
57      * Used by OSGI to unset the mediator in the discovery service.
58      *
59      * @param mitsubishiMediator the mediator
60      */
61     public void unsetMediator(final SilvercrestWifiSocketMediator mitsubishiMediator) {
62         logger.debug("Mediator has been unsetted from discovery service.");
63         this.mediator.setDiscoveryService(null);
64         this.mediator = null;
65     }
66
67     /**
68      * Constructor of the discovery service.
69      *
70      * @throws IllegalArgumentException if the timeout {@literal < 0}
71      */
72     public SilvercrestWifiSocketDiscoveryService() throws IllegalArgumentException {
73         super(SilvercrestWifiSocketBindingConstants.SUPPORTED_THING_TYPES_UIDS,
74                 SilvercrestWifiSocketBindingConstants.DISCOVERY_TIMEOUT_SECONDS);
75     }
76
77     @Override
78     public Set<ThingTypeUID> getSupportedThingTypes() {
79         return SilvercrestWifiSocketBindingConstants.SUPPORTED_THING_TYPES_UIDS;
80     }
81
82     @Override
83     protected void startScan() {
84         logger.debug("Don't need to start new scan... background scanning in progress by mediator.");
85     }
86
87     /**
88      * Method called by mediator, when receive one packet from one unknown Wifi Socket.
89      *
90      * @param macAddress the mack address from the device.
91      * @param hostAddress the host address from the device.
92      */
93     public void discoveredWifiSocket(final String macAddress, final String hostAddress) {
94         Map<String, Object> properties = new HashMap<>(2);
95         properties.put(SilvercrestWifiSocketBindingConstants.MAC_ADDRESS_ARG, macAddress);
96         properties.put(SilvercrestWifiSocketBindingConstants.HOST_ADDRESS_ARG, hostAddress);
97
98         ThingUID newThingId = new ThingUID(SilvercrestWifiSocketBindingConstants.THING_TYPE_WIFI_SOCKET, macAddress);
99         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(newThingId).withProperties(properties)
100                 .withLabel("Silvercrest Wifi Socket").withRepresentationProperty(macAddress).build();
101
102         logger.debug("Discovered new thing with mac address '{}' and host address '{}'", macAddress, hostAddress);
103
104         this.thingDiscovered(discoveryResult);
105     }
106
107     // SETTERS AND GETTERS
108     /**
109      * Gets the {@link SilvercrestWifiSocketMediator} of this binding.
110      *
111      * @return {@link SilvercrestWifiSocketMediator}.
112      */
113     public SilvercrestWifiSocketMediator getMediator() {
114         return this.mediator;
115     }
116 }