feat: added OnSystemDeviceAddedRemoved event to IcdEnviornment for NetStandard

This commit is contained in:
Drew Tingen
2021-03-31 16:43:59 -04:00
committed by Chris Cameron
parent 985a81f961
commit f807db480e
2 changed files with 52 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Added GetLocalTimeZoneName method to IcdEnvironment
- Added MatchAny method to RegexUtils
- Added OnSystemDeviceAddedRemoved and associated raise methods to IcdEnvironment for NETSTANDARD
### Changed
- Added GetParentUri method to UriExtensions

View File

@@ -7,11 +7,29 @@ using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using ICD.Common.Properties;
using ICD.Common.Utils.EventArguments;
using ICD.Common.Utils.Timers;
namespace ICD.Common.Utils
{
public static partial class IcdEnvironment
{
/// <summary>
/// We get several device added/removed events for a single device
/// To reduce noise, we use timers in an attempt to compress them to a single event
/// </summary>
private const long DEVICE_ADDED_REMOVED_TIME = 500;
private static readonly SafeTimer s_DeviceAddedTimer = SafeTimer.Stopped(DeviceAddedTimerCallback);
private static readonly SafeTimer s_DeviceRemovedTimer = SafeTimer.Stopped(DeviceRemovedTimerCallback);
/// <summary>
/// Raised when a system device (eg USB) is added
/// May be raised once for multiple device additions
/// </summary>
public static event EventHandler<BoolEventArgs> OnSystemDeviceAddedRemoved;
public static string NewLine { get { return Environment.NewLine; } }
/// <summary>
@@ -153,6 +171,39 @@ namespace ICD.Common.Utils
if (handler != null)
handler(sessionId, reasonCode);
}
/// Call this method to raise the device added/removed event for an added device
/// Uses a timer to attempt to compress multiple events into a single event
/// </summary>
public static void RaiseSystemDeviceAddedEvent()
{
s_DeviceAddedTimer.Reset(DEVICE_ADDED_REMOVED_TIME);
}
/// <summary>
/// Call this method to raise the device added/removed event for a removed device
/// Uses a timer to attempt to compress multiple events into a single event
/// </summary>
public static void RaiseSystemDeviceRemovedEvent()
{
s_DeviceRemovedTimer.Reset(DEVICE_ADDED_REMOVED_TIME);
}
/// <summary>
/// Actually fire the added event after the timer expires
/// </summary>
private static void DeviceAddedTimerCallback()
{
OnSystemDeviceAddedRemoved.Raise(null, true);
}
/// <summary>
/// Actually fire the removed event after the timer expires
/// </summary>
private static void DeviceRemovedTimerCallback()
{
OnSystemDeviceAddedRemoved.Raise(null, false);
}
}
}
#endif