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