]> git.basschouten.com Git - openhab-addons.git/blob
237a39183f1906be3dc3c28f671955132f70a594
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.rfxcom.internal.discovery;
14
15 import java.io.IOException;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
22 import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
23 import org.openhab.binding.rfxcom.internal.config.RFXComBridgeConfiguration;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import jd2xx.JD2XX;
35
36 /**
37  * The {@link RFXComBridgeDiscovery} is responsible for discovering new RFXCOM
38  * transceivers.
39  *
40  * @author Pauli Anttila - Initial contribution
41  *
42  */
43 @Component(service = DiscoveryService.class, configurationPid = "discovery.rfxcom")
44 public class RFXComBridgeDiscovery extends AbstractDiscoveryService {
45     private static final long REFRESH_INTERVAL_IN_SECONDS = 600;
46
47     private final Logger logger = LoggerFactory.getLogger(RFXComBridgeDiscovery.class);
48
49     private boolean unsatisfiedLinkErrorLogged = false;
50
51     private ScheduledFuture<?> discoveryJob;
52
53     public RFXComBridgeDiscovery() {
54         super(RFXComBindingConstants.DISCOVERABLE_BRIDGE_THING_TYPES_UIDS, 10, false);
55     }
56
57     @Override
58     public Set<ThingTypeUID> getSupportedThingTypes() {
59         return RFXComBindingConstants.DISCOVERABLE_BRIDGE_THING_TYPES_UIDS;
60     }
61
62     @Override
63     public void startScan() {
64         logger.debug("Start discovery scan for RFXCOM transceivers");
65         discoverRfxcom();
66     }
67
68     @Override
69     protected void startBackgroundDiscovery() {
70         logger.debug("Start background discovery for RFXCOM transceivers");
71         discoveryJob = scheduler.scheduleWithFixedDelay(this::discoverRfxcom, 0, REFRESH_INTERVAL_IN_SECONDS,
72                 TimeUnit.SECONDS);
73     }
74
75     @Override
76     protected void stopBackgroundDiscovery() {
77         logger.debug("Stop background discovery for RFXCOM transceivers");
78         if (discoveryJob != null && !discoveryJob.isCancelled()) {
79             discoveryJob.cancel(true);
80             discoveryJob = null;
81         }
82     }
83
84     private synchronized void discoverRfxcom() {
85         try {
86             JD2XX jd2xx = new JD2XX();
87             logger.debug("Discovering RFXCOM transceiver devices by JD2XX version {}", jd2xx.getLibraryVersion());
88             String[] devDescriptions = (String[]) jd2xx.listDevicesByDescription();
89             String[] devSerialNumbers = (String[]) jd2xx.listDevicesBySerialNumber();
90             logger.debug("Discovered {} FTDI device(s)", devDescriptions.length);
91
92             for (int i = 0; i < devSerialNumbers.length; ++i) {
93                 if (devDescriptions.length > 0) {
94                     switch (devDescriptions[i]) {
95                         case RFXComBindingConstants.BRIDGE_TYPE_RFXTRX433:
96                             addBridge(RFXComBindingConstants.BRIDGE_RFXTRX443, devSerialNumbers[i]);
97                             break;
98                         case RFXComBindingConstants.BRIDGE_TYPE_RFXTRX315:
99                             addBridge(RFXComBindingConstants.BRIDGE_RFXTRX315, devSerialNumbers[i]);
100                             break;
101                         case RFXComBindingConstants.BRIDGE_TYPE_RFXREC433:
102                             addBridge(RFXComBindingConstants.BRIDGE_RFXREC443, devSerialNumbers[i]);
103                             break;
104                         default:
105                             logger.trace("Ignore unknown device '{}'", devDescriptions[i]);
106                     }
107                 }
108             }
109
110             logger.debug("Discovery done");
111
112         } catch (IOException e) {
113             logger.error("Error occurred during discovery", e);
114         } catch (UnsatisfiedLinkError e) {
115             if (unsatisfiedLinkErrorLogged) {
116                 logger.debug(
117                         "Error occurred when trying to load native library for OS '{}' version '{}', processor '{}'",
118                         System.getProperty("os.name"), System.getProperty("os.version"), System.getProperty("os.arch"),
119                         e);
120             } else {
121                 logger.error(
122                         "Error occurred when trying to load native library for OS '{}' version '{}', processor '{}'",
123                         System.getProperty("os.name"), System.getProperty("os.version"), System.getProperty("os.arch"),
124                         e);
125                 unsatisfiedLinkErrorLogged = true;
126             }
127         }
128     }
129
130     private void addBridge(ThingTypeUID bridgeType, String bridgeId) {
131         logger.debug("Discovered RFXCOM transceiver, bridgeType='{}', bridgeId='{}'", bridgeType, bridgeId);
132
133         Map<String, Object> properties = new HashMap<>();
134         properties.put(RFXComBridgeConfiguration.BRIDGE_ID, bridgeId);
135
136         ThingUID uid = new ThingUID(bridgeType, bridgeId);
137         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
138                 .withLabel("RFXCOM transceiver").build();
139         thingDiscovered(result);
140     }
141 }