Skip to content

Commit a136918

Browse files
author
Ibrahim Ethem Gursoy
committed
Enhance discovery process with configurable timeout and continuous search
Key improvements: - Added a configurable timeout for the device discovery process (defaulting to 60 seconds). - Modified StartDiscovery method in DiscoveryService to accept the timeout value. - Implemented a continuous device search loop with periodic searches based on the timeout. - Added logging messages to indicate when device searches are initiated. - Minor code formatting adjustments for readability.
1 parent bca50d8 commit a136918

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

Services/CheckerService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ public CheckerService(ILogger<CheckerService> logger, IConfiguration configurati
3939
var iface = _configuration.GetValue<string>("AppConfiguration:NetworkInterface");
4040
var bindIp = Helper.GetIPAddress(iface);
4141

42+
var timeout = _configuration.GetValue<int>("AppConfiguration:DiscoveryTimeout");
43+
if (timeout <= 0)
44+
timeout = 60000;
4245
_discoveryService = new DiscoveryService();
43-
_discoveryService.StartDiscovery(bindIp?.ToString() ?? "", OnDiscoveredDevice);
46+
_discoveryService.StartDiscovery(bindIp?.ToString() ?? "", OnDiscoveredDevice, timeout);
4447
}
4548

4649
var devicesFile = _configuration.GetValue<string>("AppConfiguration:DefaultDeviceJsonPath");

Services/DiscoveryService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ public class DiscoveryService : IDisposable
66
{
77
private SsdpDeviceLocator? _deviceLocator;
88

9-
public void StartDiscovery(string bindIp, EventHandler<DeviceAvailableEventArgs> cb)
9+
public void StartDiscovery(string bindIp, EventHandler<DeviceAvailableEventArgs> cb, int timeout = 60000)
1010
{
1111
_deviceLocator = new SsdpDeviceLocator(bindIp) { NotificationFilter = "upnp:rootdevice" };
1212
_deviceLocator.DeviceAvailable += cb;
13+
_deviceLocator.StartListeningForNotifications();
1314

1415
new Thread(() =>
1516
{
16-
_deviceLocator.StartListeningForNotifications();
17-
_deviceLocator.SearchAsync();
17+
while (true) {
18+
Console.WriteLine("***************************\n** Searching for devices **\n***************************");
19+
_ = _deviceLocator.SearchAsync();
20+
Thread.Sleep(timeout);
21+
}
1822
}).Start();
1923
}
2024

appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"EnableCheckPort": true,
3434
"EnableCheckOnvifMedia": true,
3535
"EnableCheckRTSPLinks": true,
36+
"DiscoveryTimeout": 6000,
3637
"ThumbnailsPath": "/tmp/thumbnails/",
3738
"DefaultDeviceJsonPath": "device_list.json",
3839
"NetworkInterface": "eth0",

0 commit comments

Comments
 (0)