]> git.basschouten.com Git - openhab-addons.git/blob
de087e316340e80303c305a917a24324f2898e34
[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.wemo.internal.discovery;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.jupnp.UpnpService;
18 import org.jupnp.model.message.header.RootDeviceHeader;
19 import org.openhab.core.config.discovery.AbstractDiscoveryService;
20 import org.openhab.core.config.discovery.DiscoveryService;
21 import org.osgi.service.component.annotations.Component;
22 import org.osgi.service.component.annotations.Reference;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link WemoDiscoveryService} is a {@link DiscoveryService} implementation, which can find WeMo UPnP devices in
28  * the network.
29  *
30  * @author Hans-Jörg Merk - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 @Component(service = DiscoveryService.class, configurationPid = "discovery.wemo")
35 public class WemoDiscoveryService extends AbstractDiscoveryService {
36
37     private Logger logger = LoggerFactory.getLogger(WemoDiscoveryService.class);
38
39     public WemoDiscoveryService() {
40         super(5);
41     }
42
43     private @Nullable UpnpService upnpService;
44
45     @Reference
46     protected void setUpnpService(UpnpService upnpService) {
47         this.upnpService = upnpService;
48     }
49
50     protected void unsetUpnpService(UpnpService upnpService) {
51         this.upnpService = null;
52     }
53
54     public void activate() {
55         logger.debug("Starting WeMo UPnP discovery...");
56         startScan();
57     }
58
59     @Override
60     public void deactivate() {
61         logger.debug("Stopping WeMo UPnP discovery...");
62         stopScan();
63     }
64
65     @Override
66     protected void startScan() {
67         logger.debug("Starting UPnP RootDevice search...");
68         UpnpService localService = upnpService;
69         if (localService != null) {
70             localService.getControlPoint().search(new RootDeviceHeader());
71         } else {
72             logger.debug("upnpService not set");
73         }
74     }
75
76     @Override
77     protected synchronized void stopScan() {
78         removeOlderResults(getTimestampOfLastScan());
79         super.stopScan();
80     }
81 }