18
18
import java .util .concurrent .CountDownLatch ;
19
19
import java .util .concurrent .ScheduledFuture ;
20
20
import java .util .concurrent .TimeUnit ;
21
- import java .util .stream .Stream ;
22
21
23
22
import org .eclipse .jdt .annotation .NonNullByDefault ;
24
23
import org .eclipse .jdt .annotation .Nullable ;
25
24
import org .openhab .binding .openwebnet .internal .OpenWebNetBindingConstants ;
25
+ import org .openhab .binding .openwebnet .internal .serial .SerialPortProviderAdapter ;
26
26
import org .openhab .core .config .discovery .AbstractDiscoveryService ;
27
27
import org .openhab .core .config .discovery .DiscoveryResult ;
28
28
import org .openhab .core .config .discovery .DiscoveryResultBuilder ;
44
44
import org .slf4j .LoggerFactory ;
45
45
46
46
/**
47
- * The {@link UsbGatewayDiscoveryService} extends {@link AbstractDiscoveryService} to detect Zigbee USB gateways
48
- * connected via serial port. The service will iterate over the available serial ports and open each one to test if a
49
- * OpenWebNet Zigbee USB gateway is connected. On successful connection, a new DiscoveryResult is created.
47
+ * The {@link UsbGatewayDiscoveryService} extends
48
+ * {@link AbstractDiscoveryService} to detect Zigbee USB gateways connected via
49
+ * serial port. The service will iterate over the available serial ports and
50
+ * open each one to test if a OpenWebNet Zigbee USB gateway is connected. On
51
+ * successful connection, a new DiscoveryResult is created.
50
52
*
51
- * @author Massimo Valla - Initial contribution
53
+ * @author Massimo Valla - Initial contribution. Inject SerialPortManager to
54
+ * openwebnet4j lib.
52
55
*/
53
56
@ NonNullByDefault
54
57
@ Component (service = DiscoveryService .class , configurationPid = "discovery.openwebnet" )
@@ -64,6 +67,8 @@ public class UsbGatewayDiscoveryService extends AbstractDiscoveryService impleme
64
67
private @ Nullable ScheduledFuture <?> connectTimeout ;
65
68
66
69
private final SerialPortManager serialPortManager ;
70
+ private final SerialPortProviderAdapter transportAdapter ;
71
+
67
72
private @ Nullable USBGateway zbGateway ;
68
73
69
74
private String currentScannedPortName = "" ;
@@ -74,27 +79,35 @@ public class UsbGatewayDiscoveryService extends AbstractDiscoveryService impleme
74
79
private boolean scanning ;
75
80
76
81
/**
77
- * Constructs a new UsbGatewayDiscoveryService with the specified Zigbee USB Bridge ThingTypeUID
82
+ * Constructs a new UsbGatewayDiscoveryService with the specified Zigbee USB
83
+ * Bridge ThingTypeUID
78
84
*/
79
85
@ Activate
80
86
public UsbGatewayDiscoveryService (final @ Reference SerialPortManager spm ) {
81
87
super (Set .of (OpenWebNetBindingConstants .THING_TYPE_ZB_GATEWAY ), DISCOVERY_TIMEOUT_SECONDS , false );
82
- // Obtain the serial port manager service using an OSGi reference
88
+ // Inject the SerialPortManager passed via @Reference into the adapter
83
89
serialPortManager = spm ;
90
+ SerialPortProviderAdapter .setSerialPortManager (spm );
91
+ this .transportAdapter = new SerialPortProviderAdapter ();
92
+ logger .debug ("**** -SPI- **** Set SerialPortManager to: {}" , SerialPortProviderAdapter .serialPortManager );
84
93
}
85
94
86
95
/**
87
- * Starts a new discovery scan. All available Serial Ports are scanned.
96
+ * Starts a new discovery scan. All available SerialPortsIdentifiers returned by
97
+ * SerialPortManager are scanned.
88
98
*/
89
99
@ Override
90
100
protected void startScan () {
91
101
logger .debug ("Started OpenWebNet Zigbee USB Gateway discovery scan" );
92
102
removeOlderResults (getTimestampOfLastScan ());
93
103
scanning = true ;
94
- Stream <SerialPortIdentifier > portEnum = serialPortManager .getIdentifiers ();
104
+
105
+ SerialPortIdentifier [] foundSerialPortIds = serialPortManager .getIdentifiers ()
106
+ .toArray (SerialPortIdentifier []::new );
107
+
95
108
// Check each available serial port
96
109
try {
97
- for (SerialPortIdentifier portIdentifier : portEnum . toArray ( SerialPortIdentifier []:: new ) ) {
110
+ for (SerialPortIdentifier portIdentifier : foundSerialPortIds ) {
98
111
if (scanning ) {
99
112
currentScannedPortName = portIdentifier .getName ();
100
113
logger .debug ("[{}] == checking serial port" , currentScannedPortName );
@@ -104,6 +117,7 @@ protected void startScan() {
104
117
} else {
105
118
logger .debug ("[{}] trying to connect to a Zigbee USB Gateway..." , currentScannedPortName );
106
119
USBGateway gw = new USBGateway (currentScannedPortName );
120
+ gw .setSerialPortProvider (transportAdapter );
107
121
zbGateway = gw ;
108
122
gw .subscribe (this );
109
123
portCheckLatch = new CountDownLatch (1 );
0 commit comments