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.silvercrestwifisocket.internal.discovery;
15 import java.util.HashMap;
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;
33 * This is the {@link DiscoveryService} for the Silvercrest Items.
35 * @author Jaime Vaz - Initial contribution
38 @Component(service = DiscoveryService.class, configurationPid = "discovery.silvercrestwifisocket")
39 public class SilvercrestWifiSocketDiscoveryService extends AbstractDiscoveryService {
41 private final Logger logger = LoggerFactory.getLogger(SilvercrestWifiSocketDiscoveryService.class);
42 private SilvercrestWifiSocketMediator mediator;
45 * Used by OSGI to inject the mediator in the discovery service.
47 * @param mediator the mediator
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);
57 * Used by OSGI to unset the mediator in the discovery service.
59 * @param mediator the mediator
61 public void unsetMediator(final SilvercrestWifiSocketMediator mitsubishiMediator) {
62 logger.debug("Mediator has been unsetted from discovery service.");
63 this.mediator.setDiscoveryService(null);
68 * Constructor of the discovery service.
70 * @throws IllegalArgumentException if the timeout < 0
72 public SilvercrestWifiSocketDiscoveryService() throws IllegalArgumentException {
73 super(SilvercrestWifiSocketBindingConstants.SUPPORTED_THING_TYPES_UIDS,
74 SilvercrestWifiSocketBindingConstants.DISCOVERY_TIMEOUT_SECONDS);
78 public Set<ThingTypeUID> getSupportedThingTypes() {
79 return SilvercrestWifiSocketBindingConstants.SUPPORTED_THING_TYPES_UIDS;
83 protected void startScan() {
84 logger.debug("Don't need to start new scan... background scanning in progress by mediator.");
88 * Method called by mediator, when receive one packet from one unknown Wifi Socket.
90 * @param macAddress the mack address from the device.
91 * @param hostAddress the host address from the device.
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);
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();
102 logger.debug("Discovered new thing with mac address '{}' and host address '{}'", macAddress, hostAddress);
104 this.thingDiscovered(discoveryResult);
107 // SETTERS AND GETTERS
109 * Gets the {@link SilvercrestWifiSocketMediator} of this binding.
111 * @return {@link SilvercrestWifiSocketMediator}.
113 public SilvercrestWifiSocketMediator getMediator() {
114 return this.mediator;