From f807db480ed9a848999705f97e6ffd55ec0ff4c0 Mon Sep 17 00:00:00 2001 From: Drew Tingen Date: Wed, 31 Mar 2021 16:43:59 -0400 Subject: [PATCH] feat: added OnSystemDeviceAddedRemoved event to IcdEnviornment for NetStandard --- CHANGELOG.md | 1 + ICD.Common.Utils/IcdEnvironment.Standard.cs | 51 +++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb06f5b..f2b11fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ICD.Common.Utils/IcdEnvironment.Standard.cs b/ICD.Common.Utils/IcdEnvironment.Standard.cs index c1872aa..77344f0 100644 --- a/ICD.Common.Utils/IcdEnvironment.Standard.cs +++ b/ICD.Common.Utils/IcdEnvironment.Standard.cs @@ -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 { + /// + /// 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 + /// + 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); + + /// + /// Raised when a system device (eg USB) is added + /// May be raised once for multiple device additions + /// + public static event EventHandler OnSystemDeviceAddedRemoved; + public static string NewLine { get { return Environment.NewLine; } } /// @@ -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 + /// + public static void RaiseSystemDeviceAddedEvent() + { + s_DeviceAddedTimer.Reset(DEVICE_ADDED_REMOVED_TIME); + } + + /// + /// 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 + /// + public static void RaiseSystemDeviceRemovedEvent() + { + s_DeviceRemovedTimer.Reset(DEVICE_ADDED_REMOVED_TIME); + } + + /// + /// Actually fire the added event after the timer expires + /// + private static void DeviceAddedTimerCallback() + { + OnSystemDeviceAddedRemoved.Raise(null, true); + } + + /// + /// Actually fire the removed event after the timer expires + /// + private static void DeviceRemovedTimerCallback() + { + OnSystemDeviceAddedRemoved.Raise(null, false); + } } } #endif