]> git.basschouten.com Git - openhab-addons.git/blob
42845b47b12a0efd63f8d7de6df4bfbd1ee50da0
[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.tellstick.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.openhab.binding.tellstick.internal.TellstickBindingConstants;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.config.discovery.DiscoveryService;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.ThingUID;
26 import org.osgi.service.component.annotations.Component;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.tellstick.device.TellstickController;
30
31 /**
32  * The {@link TellstickBridgeDiscovery} is responsible for discovering new Telldus gateway devices on the network
33  *
34  * @author Jarle Hjortland - Initial contribution
35  *
36  */
37 @Component(service = DiscoveryService.class, configurationPid = "discovery.tellstick")
38 public class TellstickBridgeDiscovery extends AbstractDiscoveryService {
39
40     private final Logger logger = LoggerFactory.getLogger(TellstickBridgeDiscovery.class);
41
42     static boolean discoveryRunning = false;
43     static boolean initilized = false;
44
45     public TellstickBridgeDiscovery() {
46         super(TellstickBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS, 15);
47     }
48
49     @Override
50     public Set<ThingTypeUID> getSupportedThingTypes() {
51         return TellstickBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS;
52     }
53
54     @Override
55     public void startScan() {
56         discoverController();
57     }
58
59     @Override
60     protected void startBackgroundDiscovery() {
61         discoverController();
62     }
63
64     @Override
65     public boolean isBackgroundDiscoveryEnabled() {
66         return true;
67     }
68
69     private synchronized void discoverController() {
70         if (!discoveryRunning) {
71             discoveryRunning = true;
72             listBridge();
73         }
74     }
75
76     private void listBridge() {
77         try {
78             List<TellstickController> cntrls = TellstickController.getControllers();
79             for (TellstickController contrl : cntrls) {
80                 discoveryResultSubmission(contrl);
81             }
82         } catch (UnsatisfiedLinkError e) {
83             logger.error(
84                     "Could not load telldus core, please make sure Telldus is installed and correct 32/64 bit java.",
85                     e);
86         } catch (NoClassDefFoundError e) {
87             logger.error(
88                     "Could not load telldus core, please make sure Telldus is installed and correct 32/64 bit java.",
89                     e);
90         } finally {
91             // Close the port!
92             discoveryRunning = false;
93         }
94     }
95
96     private void discoveryResultSubmission(TellstickController controller) {
97         if (controller != null && controller.isOnline()) {
98             logger.trace("Adding new Telldus Controller  {}", controller);
99             Map<String, Object> properties = new HashMap<>(2);
100             ThingUID uid = new ThingUID(TellstickBindingConstants.TELLDUSCOREBRIDGE_THING_TYPE,
101                     Integer.toString(controller.getId()));
102             thingDiscovered(DiscoveryResultBuilder.create(uid).withProperties(properties)
103                     .withLabel(controller.getType().name() + ": " + controller.getName()).build());
104         }
105     }
106 }