Implement scanInterval configuration and manual scan method
This commit is contained in:
parent
af77e61fa2
commit
2df88184c6
@ -16,12 +16,24 @@ final class Config
|
||||
{
|
||||
/** Base key name for properties. */
|
||||
private static final String KEY_BASE = "de.ailis.usb4java.";
|
||||
|
||||
/** The default USB communication timeout in milliseconds. */
|
||||
private static final int DEFAULT_TIMEOUT = 2500;
|
||||
|
||||
/** The default scan interval in milliseconds. */
|
||||
private static final int DEFAULT_SCAN_INTERVAL = 500;
|
||||
|
||||
/** Key name for USB communication timeout. */
|
||||
private static final String TIMEOUT_KEY = KEY_BASE + "timeout";
|
||||
|
||||
/** Key name for USB communication timeout. */
|
||||
private static final String SCAN_INTERVAL_KEY = KEY_BASE + "scanInterval";
|
||||
|
||||
/** The timeout for USB communication in milliseconds. */
|
||||
private int timeout = 2500;
|
||||
private int timeout = DEFAULT_TIMEOUT;
|
||||
|
||||
/** The scan interval in milliseconds. */
|
||||
private int scanInterval = DEFAULT_SCAN_INTERVAL;
|
||||
|
||||
/**
|
||||
* Constructs new configuration from the specified properties.
|
||||
@ -36,6 +48,13 @@ final class Config
|
||||
{
|
||||
this.timeout = Integer.valueOf(properties.getProperty(TIMEOUT_KEY));
|
||||
}
|
||||
|
||||
// Read the USB device scan interval
|
||||
if (properties.containsKey(SCAN_INTERVAL_KEY))
|
||||
{
|
||||
this.scanInterval = Integer.valueOf(properties.getProperty(
|
||||
SCAN_INTERVAL_KEY));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -47,4 +66,14 @@ final class Config
|
||||
{
|
||||
return this.timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the scan interval in milliseconds.
|
||||
*
|
||||
* @return The scan interval in milliseconds.
|
||||
*/
|
||||
public int getScanInterval()
|
||||
{
|
||||
return this.scanInterval;
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,9 +30,6 @@ import de.ailis.usb4java.libusb.LibUsbException;
|
||||
*/
|
||||
final class DeviceManager
|
||||
{
|
||||
/** The scan interval in milliseconds. */
|
||||
private static final int DEFAULT_SCAN_INTERVAL = 500;
|
||||
|
||||
/** The logger. */
|
||||
private static final Logger LOG = Logger.getLogger(DeviceManager.class
|
||||
.getName());
|
||||
@ -46,6 +43,9 @@ final class DeviceManager
|
||||
/** If scanner already scanned for devices. */
|
||||
private boolean scanned = false;
|
||||
|
||||
/** The scan interval in milliseconds. */
|
||||
private final int scanInterval;
|
||||
|
||||
/** The currently connected devices. */
|
||||
private final Map<DeviceId, AbstractDevice> devices = Collections
|
||||
.synchronizedMap(new HashMap<DeviceId, AbstractDevice>());
|
||||
@ -55,13 +55,17 @@ final class DeviceManager
|
||||
*
|
||||
* @param rootHub
|
||||
* The root hub. Must not be null.
|
||||
* @param scanInterval
|
||||
* The scan interval in milliseconds.
|
||||
* @throws UsbException
|
||||
* When USB initialization fails.
|
||||
*/
|
||||
DeviceManager(final RootHub rootHub) throws UsbException
|
||||
DeviceManager(final RootHub rootHub, final int scanInterval)
|
||||
throws UsbException
|
||||
{
|
||||
if (rootHub == null)
|
||||
throw new IllegalArgumentException("rootHub must be set");
|
||||
this.scanInterval = scanInterval;
|
||||
this.rootHub = rootHub;
|
||||
this.context = new Context();
|
||||
final int result = LibUsb.init(this.context);
|
||||
@ -77,7 +81,7 @@ final class DeviceManager
|
||||
{
|
||||
LibUsb.exit(this.context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a device ID from the specified device.
|
||||
*
|
||||
@ -266,7 +270,7 @@ final class DeviceManager
|
||||
/**
|
||||
* Scans the USB busses for new or removed devices.
|
||||
*/
|
||||
public void scan()
|
||||
public synchronized void scan()
|
||||
{
|
||||
scan(this.rootHub);
|
||||
this.scanned = true;
|
||||
@ -330,6 +334,10 @@ final class DeviceManager
|
||||
*/
|
||||
public void start()
|
||||
{
|
||||
// Do not start the scan thread when interval is set to 0
|
||||
final int scanInterval = this.scanInterval;
|
||||
if (scanInterval == 0) return;
|
||||
|
||||
final Thread thread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
@ -339,7 +347,7 @@ final class DeviceManager
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(DEFAULT_SCAN_INTERVAL);
|
||||
Thread.sleep(scanInterval);
|
||||
}
|
||||
catch (final InterruptedException e)
|
||||
{
|
||||
|
||||
@ -40,7 +40,7 @@ public final class Services implements UsbServices
|
||||
private final RootHub rootHub;
|
||||
|
||||
/** The USB device scanner. */
|
||||
private final DeviceManager deviceScanner;
|
||||
private final DeviceManager deviceManager;
|
||||
|
||||
/** If devices should be scanned by hierarchy. */
|
||||
private final Config config;
|
||||
@ -58,14 +58,15 @@ public final class Services implements UsbServices
|
||||
this.config = new Config(UsbHostManager.getProperties());
|
||||
Loader.load();
|
||||
this.rootHub = new RootHub();
|
||||
this.deviceScanner = new DeviceManager(this.rootHub);
|
||||
this.deviceScanner.start();
|
||||
this.deviceManager = new DeviceManager(this.rootHub,
|
||||
this.config.getScanInterval());
|
||||
this.deviceManager.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UsbHub getRootUsbHub()
|
||||
{
|
||||
this.deviceScanner.firstScan();
|
||||
this.deviceManager.firstScan();
|
||||
return this.rootHub;
|
||||
}
|
||||
|
||||
@ -153,4 +154,12 @@ public final class Services implements UsbServices
|
||||
+ e, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually scans for USB device connection changes.
|
||||
*/
|
||||
public void scan()
|
||||
{
|
||||
this.deviceManager.scan();
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,3 +29,23 @@ javax.usb.services = de.ailis.usb4java.Services
|
||||
+----+
|
||||
de.ailis.usb4java.timeout = 250
|
||||
+----+
|
||||
|
||||
|
||||
* Scan interval
|
||||
|
||||
The default USB device scan interval of <usb4java> is 500 milliseconds. To
|
||||
change this to 1000 milliseconds for example add this to the properties file:
|
||||
|
||||
+----+
|
||||
de.ailis.usb4java.scanInterval = 1000
|
||||
+----+
|
||||
|
||||
When you set this interval to 0 then <usb4java> only scans once during
|
||||
application startup. If you want to trigger a manual device <scan> you can
|
||||
do it by calling the scan method on the USB services class (Must be casted
|
||||
to the <usb4java> implementation, because this is not a javax.usb feature:
|
||||
|
||||
+----+
|
||||
((de.ailis.usb4java.Services) UsbHostManager.getUsbServices()).scan();
|
||||
+----+
|
||||
|
||||
Loading…
Reference in New Issue
Block a user