mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-16 05:05:00 +00:00
Adds CCriticalSection and try/finally for all Device Dictionary accesses
This commit is contained in:
@@ -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,47 +59,55 @@ namespace PepperDash.Essentials.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void ActivateAll()
|
public static void ActivateAll()
|
||||||
{
|
{
|
||||||
// PreActivate all devices
|
try
|
||||||
foreach (var d in Devices.Values)
|
{
|
||||||
{
|
DeviceCriticalSection.Enter();
|
||||||
try
|
// PreActivate all devices
|
||||||
{
|
foreach (var d in Devices.Values)
|
||||||
if (d is Device)
|
{
|
||||||
(d as Device).PreActivate();
|
try
|
||||||
}
|
{
|
||||||
catch (Exception e)
|
if (d is Device)
|
||||||
{
|
(d as Device).PreActivate();
|
||||||
Debug.Console(0, d, "ERROR: Device PreActivation failure:\r{0}", e);
|
}
|
||||||
}
|
catch (Exception e)
|
||||||
}
|
{
|
||||||
|
Debug.Console(0, d, "ERROR: Device PreActivation failure:\r{0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Activate all devices
|
// Activate all devices
|
||||||
foreach (var d in Devices.Values)
|
foreach (var d in Devices.Values)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (d is Device)
|
if (d is Device)
|
||||||
(d as Device).Activate();
|
(d as Device).Activate();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.Console(0, d, "ERROR: Device Activation failure:\r{0}", e);
|
Debug.Console(0, d, "ERROR: Device Activation failure:\r{0}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostActivate all devices
|
// PostActivate all devices
|
||||||
foreach (var d in Devices.Values)
|
foreach (var d in Devices.Values)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (d is Device)
|
if (d is Device)
|
||||||
(d as Device).PostActivate();
|
(d as Device).PostActivate();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.Console(0, d, "ERROR: Device PostActivation failure:\r{0}", e);
|
Debug.Console(0, d, "ERROR: Device PostActivation failure:\r{0}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
DeviceCriticalSection.Leave();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -106,11 +115,19 @@ namespace PepperDash.Essentials.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void DeactivateAll()
|
public static void DeactivateAll()
|
||||||
{
|
{
|
||||||
foreach (var d in Devices.Values)
|
try
|
||||||
{
|
{
|
||||||
if (d is Device)
|
DeviceCriticalSection.Enter();
|
||||||
(d as Device).Deactivate();
|
foreach (var d in Devices.Values)
|
||||||
}
|
{
|
||||||
|
if (d is Device)
|
||||||
|
(d as Device).Deactivate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
DeviceCriticalSection.Leave();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//static void ListMethods(string devKey)
|
//static void ListMethods(string devKey)
|
||||||
@@ -136,32 +153,45 @@ 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
|
||||||
var sorted = Devices.Values.ToList();
|
{
|
||||||
sorted.Sort((a, b) => a.Key.CompareTo(b.Key));
|
DeviceCriticalSection.Enter();
|
||||||
|
Debug.Console(0, "{0} Devices registered with Device Manager:", Devices.Count);
|
||||||
|
var sorted = Devices.Values.ToList();
|
||||||
|
sorted.Sort((a, b) => a.Key.CompareTo(b.Key));
|
||||||
|
|
||||||
foreach (var d in sorted)
|
foreach (var d in sorted)
|
||||||
{
|
{
|
||||||
var name = d is IKeyName ? (d as IKeyName).Name : "---";
|
var name = d is IKeyName ? (d as IKeyName).Name : "---";
|
||||||
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)
|
||||||
{
|
{
|
||||||
var dev = GetDeviceForKey(devKey);
|
try
|
||||||
if(dev == null)
|
{
|
||||||
{
|
DeviceCriticalSection.Enter();
|
||||||
Debug.Console(0, "Device '{0}' not found", devKey);
|
var dev = GetDeviceForKey(devKey);
|
||||||
return;
|
if (dev == null)
|
||||||
}
|
{
|
||||||
var statusDev = dev as IHasFeedback;
|
Debug.Console(0, "Device '{0}' not found", devKey);
|
||||||
if(statusDev == null)
|
return;
|
||||||
{
|
}
|
||||||
Debug.Console(0, "Device '{0}' does not have visible feedbacks", devKey);
|
var statusDev = dev as IHasFeedback;
|
||||||
return;
|
if (statusDev == null)
|
||||||
}
|
{
|
||||||
statusDev.DumpFeedbacksToConsole(true);
|
Debug.Console(0, "Device '{0}' does not have visible feedbacks", devKey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
statusDev.DumpFeedbacksToConsole(true);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
DeviceCriticalSection.Leave();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//static void ListDeviceCommands(string devKey)
|
//static void ListDeviceCommands(string devKey)
|
||||||
@@ -177,13 +207,23 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
static void ListDeviceCommStatuses(string input)
|
static void ListDeviceCommStatuses(string input)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
try
|
||||||
foreach (var dev in Devices.Values)
|
{
|
||||||
{
|
DeviceCriticalSection.Enter();
|
||||||
if (dev is ICommunicationMonitor)
|
|
||||||
sb.Append(string.Format("{0}: {1}\r", dev.Key, (dev as ICommunicationMonitor).CommunicationMonitor.Status));
|
var sb = new StringBuilder();
|
||||||
}
|
foreach (var dev in Devices.Values)
|
||||||
CrestronConsole.ConsoleCommandResponse(sb.ToString());
|
{
|
||||||
|
if (dev is ICommunicationMonitor)
|
||||||
|
sb.Append(string.Format("{0}: {1}\r", dev.Key,
|
||||||
|
(dev as ICommunicationMonitor).CommunicationMonitor.Status));
|
||||||
|
}
|
||||||
|
CrestronConsole.ConsoleCommandResponse(sb.ToString());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
DeviceCriticalSection.Leave();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -194,51 +234,85 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
public static void AddDevice(IKeyed newDev)
|
public static void AddDevice(IKeyed newDev)
|
||||||
{
|
{
|
||||||
// Check for device with same key
|
try
|
||||||
//var existingDevice = _Devices.FirstOrDefault(d => d.Key.Equals(newDev.Key, StringComparison.OrdinalIgnoreCase));
|
{
|
||||||
////// If it exists, remove or warn??
|
DeviceCriticalSection.Enter();
|
||||||
//if (existingDevice != null)
|
// Check for device with same key
|
||||||
if(Devices.ContainsKey(newDev.Key))
|
//var existingDevice = _Devices.FirstOrDefault(d => d.Key.Equals(newDev.Key, StringComparison.OrdinalIgnoreCase));
|
||||||
{
|
////// If it exists, remove or warn??
|
||||||
Debug.Console(0, newDev, "WARNING: A device with this key already exists. Not added to manager");
|
//if (existingDevice != null)
|
||||||
return;
|
if (Devices.ContainsKey(newDev.Key))
|
||||||
}
|
{
|
||||||
Devices.Add(newDev.Key, newDev);
|
Debug.Console(0, newDev, "WARNING: A device with this key already exists. Not added to manager");
|
||||||
//if (!(_Devices.Contains(newDev)))
|
return;
|
||||||
// _Devices.Add(newDev);
|
}
|
||||||
|
Devices.Add(newDev.Key, newDev);
|
||||||
|
//if (!(_Devices.Contains(newDev)))
|
||||||
|
// _Devices.Add(newDev);
|
||||||
|
}
|
||||||
|
finally {DeviceCriticalSection.Leave();}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RemoveDevice(IKeyed newDev)
|
public static void RemoveDevice(IKeyed newDev)
|
||||||
{
|
{
|
||||||
if(newDev == null)
|
try
|
||||||
return;
|
{
|
||||||
if (Devices.ContainsKey(newDev.Key))
|
DeviceCriticalSection.Enter();
|
||||||
Devices.Remove(newDev.Key);
|
if (newDev == null)
|
||||||
//if (_Devices.Contains(newDev))
|
return;
|
||||||
// _Devices.Remove(newDev);
|
if (Devices.ContainsKey(newDev.Key))
|
||||||
else
|
Devices.Remove(newDev.Key);
|
||||||
Debug.Console(0, "Device manager: Device '{0}' does not exist in manager. Cannot remove", newDev.Key);
|
//if (_Devices.Contains(newDev))
|
||||||
|
// _Devices.Remove(newDev);
|
||||||
|
else
|
||||||
|
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()
|
||||||
{
|
{
|
||||||
//return _Devices.Select(d => d.Key).ToList();
|
try
|
||||||
return Devices.Keys;
|
{
|
||||||
|
DeviceCriticalSection.Enter();
|
||||||
|
//return _Devices.Select(d => d.Key).ToList();
|
||||||
|
return Devices.Keys;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
DeviceCriticalSection.Leave();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<IKeyed> GetDevices()
|
public static IEnumerable<IKeyed> GetDevices()
|
||||||
{
|
{
|
||||||
//return _Devices.Select(d => d.Key).ToList();
|
try
|
||||||
return Devices.Values;
|
{
|
||||||
|
DeviceCriticalSection.Enter();
|
||||||
|
//return _Devices.Select(d => d.Key).ToList();
|
||||||
|
return Devices.Values;
|
||||||
|
}
|
||||||
|
finally {DeviceCriticalSection.Leave();}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IKeyed GetDeviceForKey(string key)
|
public static IKeyed GetDeviceForKey(string key)
|
||||||
{
|
{
|
||||||
//return _Devices.FirstOrDefault(d => d.Key.Equals(key, StringComparison.OrdinalIgnoreCase));
|
try
|
||||||
if (key != null && Devices.ContainsKey(key))
|
{
|
||||||
return Devices[key];
|
DeviceCriticalSection.Enter();
|
||||||
|
//return _Devices.FirstOrDefault(d => d.Key.Equals(key, StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (key != null && Devices.ContainsKey(key))
|
||||||
|
return Devices[key];
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
DeviceCriticalSection.Leave();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user