Compare commits

...

1 Commits

Author SHA1 Message Date
Andrew Welker
acde389505 Adds CCriticalSection and try/finally for all Device Dictionary accesses 2020-05-06 08:45:20 -06:00

View File

@@ -17,6 +17,7 @@ namespace PepperDash.Essentials.Core
{ {
public static class DeviceManager public static class DeviceManager
{ {
private static readonly CCriticalSection DeviceCriticalSection = new CCriticalSection();
//public static List<Device> Devices { get { return _Devices; } } //public static List<Device> Devices { get { return _Devices; } }
//static List<Device> _Devices = new List<Device>(); //static List<Device> _Devices = new List<Device>();
@@ -58,6 +59,9 @@ namespace PepperDash.Essentials.Core
/// </summary> /// </summary>
public static void ActivateAll() public static void ActivateAll()
{ {
try
{
DeviceCriticalSection.Enter();
// PreActivate all devices // PreActivate all devices
foreach (var d in Devices.Values) foreach (var d in Devices.Values)
{ {
@@ -100,18 +104,31 @@ namespace PepperDash.Essentials.Core
} }
} }
} }
finally
{
DeviceCriticalSection.Leave();
}
}
/// <summary> /// <summary>
/// Calls activate on all Device class items /// Calls activate on all Device class items
/// </summary> /// </summary>
public static void DeactivateAll() public static void DeactivateAll()
{ {
try
{
DeviceCriticalSection.Enter();
foreach (var d in Devices.Values) foreach (var d in Devices.Values)
{ {
if (d is Device) if (d is Device)
(d as Device).Deactivate(); (d as Device).Deactivate();
} }
} }
finally
{
DeviceCriticalSection.Leave();
}
}
//static void ListMethods(string devKey) //static void ListMethods(string devKey)
//{ //{
@@ -136,7 +153,10 @@ namespace PepperDash.Essentials.Core
static void ListDevices(string s) static void ListDevices(string s)
{ {
Debug.Console(0, "{0} Devices registered with Device Mangager:",Devices.Count); try
{
DeviceCriticalSection.Enter();
Debug.Console(0, "{0} Devices registered with Device Manager:", Devices.Count);
var sorted = Devices.Values.ToList(); var sorted = Devices.Values.ToList();
sorted.Sort((a, b) => a.Key.CompareTo(b.Key)); sorted.Sort((a, b) => a.Key.CompareTo(b.Key));
@@ -146,9 +166,14 @@ namespace PepperDash.Essentials.Core
Debug.Console(0, " [{0}] {1}", d.Key, name); Debug.Console(0, " [{0}] {1}", d.Key, name);
} }
} }
finally {DeviceCriticalSection.Leave();}
}
static void ListDeviceFeedbacks(string devKey) static void ListDeviceFeedbacks(string devKey)
{ {
try
{
DeviceCriticalSection.Enter();
var dev = GetDeviceForKey(devKey); var dev = GetDeviceForKey(devKey);
if (dev == null) if (dev == null)
{ {
@@ -163,6 +188,11 @@ namespace PepperDash.Essentials.Core
} }
statusDev.DumpFeedbacksToConsole(true); statusDev.DumpFeedbacksToConsole(true);
} }
finally
{
DeviceCriticalSection.Leave();
}
}
//static void ListDeviceCommands(string devKey) //static void ListDeviceCommands(string devKey)
//{ //{
@@ -177,14 +207,24 @@ namespace PepperDash.Essentials.Core
static void ListDeviceCommStatuses(string input) static void ListDeviceCommStatuses(string input)
{ {
try
{
DeviceCriticalSection.Enter();
var sb = new StringBuilder(); var sb = new StringBuilder();
foreach (var dev in Devices.Values) foreach (var dev in Devices.Values)
{ {
if (dev is ICommunicationMonitor) if (dev is ICommunicationMonitor)
sb.Append(string.Format("{0}: {1}\r", dev.Key, (dev as ICommunicationMonitor).CommunicationMonitor.Status)); sb.Append(string.Format("{0}: {1}\r", dev.Key,
(dev as ICommunicationMonitor).CommunicationMonitor.Status));
} }
CrestronConsole.ConsoleCommandResponse(sb.ToString()); CrestronConsole.ConsoleCommandResponse(sb.ToString());
} }
finally
{
DeviceCriticalSection.Leave();
}
}
//static void DoDeviceCommand(string command) //static void DoDeviceCommand(string command)
@@ -194,6 +234,9 @@ namespace PepperDash.Essentials.Core
public static void AddDevice(IKeyed newDev) public static void AddDevice(IKeyed newDev)
{ {
try
{
DeviceCriticalSection.Enter();
// Check for device with same key // Check for device with same key
//var existingDevice = _Devices.FirstOrDefault(d => d.Key.Equals(newDev.Key, StringComparison.OrdinalIgnoreCase)); //var existingDevice = _Devices.FirstOrDefault(d => d.Key.Equals(newDev.Key, StringComparison.OrdinalIgnoreCase));
////// If it exists, remove or warn?? ////// If it exists, remove or warn??
@@ -207,9 +250,14 @@ namespace PepperDash.Essentials.Core
//if (!(_Devices.Contains(newDev))) //if (!(_Devices.Contains(newDev)))
// _Devices.Add(newDev); // _Devices.Add(newDev);
} }
finally {DeviceCriticalSection.Leave();}
}
public static void RemoveDevice(IKeyed newDev) public static void RemoveDevice(IKeyed newDev)
{ {
try
{
DeviceCriticalSection.Enter();
if (newDev == null) if (newDev == null)
return; return;
if (Devices.ContainsKey(newDev.Key)) if (Devices.ContainsKey(newDev.Key))
@@ -219,27 +267,53 @@ namespace PepperDash.Essentials.Core
else else
Debug.Console(0, "Device manager: Device '{0}' does not exist in manager. Cannot remove", newDev.Key); Debug.Console(0, "Device manager: Device '{0}' does not exist in manager. Cannot remove", newDev.Key);
} }
finally
{
DeviceCriticalSection.Leave();
}
}
public static IEnumerable<string> GetDeviceKeys() public static IEnumerable<string> GetDeviceKeys()
{ {
try
{
DeviceCriticalSection.Enter();
//return _Devices.Select(d => d.Key).ToList(); //return _Devices.Select(d => d.Key).ToList();
return Devices.Keys; return Devices.Keys;
} }
finally
{
DeviceCriticalSection.Leave();
}
}
public static IEnumerable<IKeyed> GetDevices() public static IEnumerable<IKeyed> GetDevices()
{ {
try
{
DeviceCriticalSection.Enter();
//return _Devices.Select(d => d.Key).ToList(); //return _Devices.Select(d => d.Key).ToList();
return Devices.Values; return Devices.Values;
} }
finally {DeviceCriticalSection.Leave();}
}
public static IKeyed GetDeviceForKey(string key) public static IKeyed GetDeviceForKey(string key)
{ {
try
{
DeviceCriticalSection.Enter();
//return _Devices.FirstOrDefault(d => d.Key.Equals(key, StringComparison.OrdinalIgnoreCase)); //return _Devices.FirstOrDefault(d => d.Key.Equals(key, StringComparison.OrdinalIgnoreCase));
if (key != null && Devices.ContainsKey(key)) if (key != null && Devices.ContainsKey(key))
return Devices[key]; return Devices[key];
return null; return null;
} }
finally
{
DeviceCriticalSection.Leave();
}
}
/// <summary> /// <summary>
/// Console handler that simulates com port data receive /// Console handler that simulates com port data receive