From fdae50a9721f5ecd6432f96903d587c3a3afc870 Mon Sep 17 00:00:00 2001 From: Nick Genovese Date: Mon, 31 Aug 2020 15:34:12 -0400 Subject: [PATCH 1/2] Adds a generic queue and string/byte implementations. Also adds a class that processes string responses from a IBasicCommunication or Gather --- .../PepperDash_Essentials_Core.csproj | 11 +- .../Queues/BytesQueue.cs | 67 ++++++++ .../Queues/GenericQueue.cs | 156 ++++++++++++++++++ .../PepperDashEssentialsBase/Queues/IQueue.cs | 15 ++ .../Queues/StringQueue.cs | 66 ++++++++ .../Queues/StringResponseProcessor.cs | 104 ++++++++++++ .../PepperDash_Essentials_DM.csproj | 6 +- .../Essentials Devices Common.csproj | 6 +- 8 files changed, 422 insertions(+), 9 deletions(-) create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/BytesQueue.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueue.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringQueue.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringResponseProcessor.cs diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj b/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj index c31a46b8..fad95abf 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj @@ -98,12 +98,12 @@ False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpCustomAttributesInterface.dll + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpCustomAttributesInterface.dll False False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpHelperInterface.dll + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpHelperInterface.dll False @@ -113,7 +113,7 @@ False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpPro.exe + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpPro.exe False @@ -158,6 +158,7 @@ + @@ -222,8 +223,10 @@ + + @@ -313,6 +316,8 @@ + + diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/BytesQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/BytesQueue.cs new file mode 100644 index 00000000..aad96cf1 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/BytesQueue.cs @@ -0,0 +1,67 @@ +using System; +using PepperDash.Core; + +namespace PepperDash_Essentials_Core.Queues +{ + /// + /// Byte implementation of Action queue + /// + public class BytesQueue : IQueue + { + private readonly IQueue _queue; + + /// + /// Constructor for BytesQueue + /// + /// Key + /// Action to process queued bytes + public BytesQueue(string key, Action processBytesAction) + { + _queue = new GenericQueue(key, processBytesAction); + } + + /// + /// Constructor for BytesQueue + /// + /// Key + /// Action to process queued bytes + /// Delay in ms between actions being invoked + public BytesQueue(string key, Action processBytesAction, int pacing) + { + _queue = new GenericQueue(key, processBytesAction, pacing); + } + + /// + /// Enqueue a byte array to be processed + /// + /// Byte array to be processed + public void Enqueue(byte[] item) + { + _queue.Enqueue(item); + } + + /// + /// If the instance has been disposed + /// + public bool Disposed + { + get { return _queue.Disposed; } + } + + /// + /// Key + /// + public string Key + { + get { return _queue.Key; } + } + + /// + /// Disposes of resources + /// + public void Dispose() + { + _queue.Dispose(); + } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs new file mode 100644 index 00000000..6a06cab8 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs @@ -0,0 +1,156 @@ +using System; +using Crestron.SimplSharp; +using Crestron.SimplSharpPro.CrestronThread; +using PepperDash.Core; + +namespace PepperDash_Essentials_Core.Queues +{ + /// + /// Threadsafe processing of queued items with pacing if required + /// + /// Type of item to be processed + public class GenericQueue : IQueue where T : class + { + private readonly string _key; + protected readonly CrestronQueue _queue; + protected readonly Thread _worker; + protected readonly CEvent _waitHandle = new CEvent(); + + private readonly bool _delayEnabled; + private readonly int _delayTime; + + /// + /// If the instance has been disposed. + /// + public bool Disposed { get; private set; } + + /// + /// Constructor for generic queue with no pacing + /// + /// Key + /// Action to process items in the queue + public GenericQueue(string key, Action processQueueAction) + { + _key = key; + _queue = new CrestronQueue(); + _worker = new Thread(ProcessQueue, processQueueAction, Thread.eThreadStartOptions.Running); + + CrestronEnvironment.ProgramStatusEventHandler += programEvent => + { + if (programEvent != eProgramStatusEventType.Stopping) + return; + + Dispose(); + }; + } + + /// + /// Constructor for generic queue with no pacing + /// + /// Key + /// Action to process items in the queue + /// Pacing in ms between actions + public GenericQueue(string key, Action processQueueAction, int pacing) + : this(key, processQueueAction) + { + _delayEnabled = pacing > 0; + _delayTime = pacing; + } + + /// + /// Thread callback + /// + /// The action used to process dequeued items + /// Null when the thread is exited + private object ProcessQueue(object obj) + { + var action = obj as Action; + if (action == null) + throw new ArgumentNullException("obj"); + + while (true) + { + T item = null; + + if (_queue.Count > 0) + { + item = _queue.Dequeue(); + if (item == null) + break; + } + if (item != null) + { + try + { + Debug.Console(2, this, "Processing queue item: '{0}'", item.ToString()); + action(item); + + if (_delayEnabled) + Thread.Sleep(_delayTime); + } + catch (Exception ex) + { + Debug.ConsoleWithLog(0, this, "Caught an exception in the ComsQueue {0}\r{1}\r{2}", ex.Message, ex.InnerException, ex.StackTrace); + } + } + else _waitHandle.Wait(); + } + + return null; + } + + /// + /// Enqueues an item and processes the queue. If 'null' is enqueued the thread will close and + /// the class must be reinstantiated. + /// + /// Item to be processed + public virtual void Enqueue(T item) + { + _queue.Enqueue(item); + _waitHandle.Set(); + } + + /// + /// Disposes the thread and cleans up resources. Thread cannot be restarted once + /// disposed. + /// + public void Dispose() + { + Dispose(true); + CrestronEnvironment.GC.SuppressFinalize(this); + } + + /// + /// Actually does the disposing. If you override this method, be sure to either call the base implementation + /// or clean up all the resources yourself. + /// + /// set to true unless called from finalizer + protected void Dispose(bool disposing) + { + if (Disposed) + return; + + if (disposing) + { + Enqueue(null); + _worker.Join(); + _waitHandle.Close(); + } + + Disposed = true; + } + + ~GenericQueue() + { + Dispose(false); + } + + /// + /// Key + /// + public string Key + { + get { return _key; } + } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueue.cs new file mode 100644 index 00000000..9d69f7fa --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueue.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using PepperDash.Core; + +namespace PepperDash_Essentials_Core.Queues +{ + public interface IQueue : IKeyed, IDisposable where T : class + { + void Enqueue(T item); + bool Disposed { get; } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringQueue.cs new file mode 100644 index 00000000..2be49f74 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringQueue.cs @@ -0,0 +1,66 @@ +using System; + +namespace PepperDash_Essentials_Core.Queues +{ + /// + /// String implementation of Action Queue + /// + public class StringQueue : IQueue + { + private readonly IQueue _queue; + + /// + /// Constructor for BytesQueue + /// + /// Key + /// Action to process queued strings + public StringQueue(string key, Action processStringAction) + { + _queue = new GenericQueue(key, processStringAction); + } + + /// + /// Constructor for StringQueue + /// + /// Key + /// Action to process queued strings + /// Delay in ms between actions being invoked + public StringQueue(string key, Action processStringAction, int pacing) + { + _queue = new GenericQueue(key, processStringAction, pacing); + } + + /// + /// Enqueue a byte array to be processed + /// + /// Byte array to be processed + public void Enqueue(string item) + { + _queue.Enqueue(item); + } + + /// + /// Key + /// + public string Key + { + get { return _queue.Key; } + } + + /// + /// Disposes of resources + /// + public void Dispose() + { + _queue.Dispose(); + } + + /// + /// If the instance has been disposed + /// + public bool Disposed + { + get { return _queue.Disposed; } + } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringResponseProcessor.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringResponseProcessor.cs new file mode 100644 index 00000000..a3c90689 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringResponseProcessor.cs @@ -0,0 +1,104 @@ +using System; +using Crestron.SimplSharp; +using PepperDash.Core; + +namespace PepperDash_Essentials_Core.Queues +{ + public sealed class StringResponseProcessor : IKeyed, IDisposable + { + private readonly IQueue _queue; + private readonly IBasicCommunication _coms; + private readonly CommunicationGather _gather; + + private StringResponseProcessor(string key, Action processStringAction) + { + _queue = new StringQueue(key, processStringAction); + + CrestronEnvironment.ProgramStatusEventHandler += programEvent => + { + if (programEvent != eProgramStatusEventType.Stopping) + return; + + Dispose(); + }; + } + + /// + /// Constructor that builds an instance and subscribes to coms TextReceived for processing + /// + /// Com port to process strings from + /// Action to process the incoming strings + public StringResponseProcessor(IBasicCommunication coms, Action processStringAction) + : this(coms.Key, processStringAction) + { + _coms = coms; + coms.TextReceived += OnResponseReceived; + } + + /// + /// Constructor that builds an instance and subscribes to gather Line Received for processing + /// + /// Gather to process strings from + /// Action to process the incoming strings + public StringResponseProcessor(CommunicationGather gather, Action processStringAction) + : this(gather.Port.Key, processStringAction) + { + _gather = gather; + gather.LineReceived += OnResponseReceived; + } + + private void OnResponseReceived(object sender, GenericCommMethodReceiveTextArgs args) + { + _queue.Enqueue(args.Text); + } + + /// + /// Key + /// + public string Key + { + get { return _queue.Key; } + } + + /// + /// Disposes the instance and cleans up resources. + /// + public void Dispose() + { + Dispose(true); + CrestronEnvironment.GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (Disposed) + return; + + if (disposing) + { + if (_coms != null) + _coms.TextReceived -= OnResponseReceived; + + if (_gather != null) + { + _gather.LineReceived -= OnResponseReceived; + _gather.Stop(); + } + + _queue.Dispose(); + } + + Disposed = true; + } + + /// + /// If the instance has been disposed or not. If it has, you can not use it anymore + /// + public bool Disposed { get; private set; } + + ~StringResponseProcessor() + { + Dispose(false); + } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials DM/Essentials_DM/PepperDash_Essentials_DM.csproj b/essentials-framework/Essentials DM/Essentials_DM/PepperDash_Essentials_DM.csproj index a8843496..7c8b163a 100644 --- a/essentials-framework/Essentials DM/Essentials_DM/PepperDash_Essentials_DM.csproj +++ b/essentials-framework/Essentials DM/Essentials_DM/PepperDash_Essentials_DM.csproj @@ -68,12 +68,12 @@ False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpCustomAttributesInterface.dll + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpCustomAttributesInterface.dll False False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpHelperInterface.dll + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpHelperInterface.dll False @@ -83,7 +83,7 @@ False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpPro.exe + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpPro.exe False diff --git a/essentials-framework/Essentials Devices Common/Essentials Devices Common/Essentials Devices Common.csproj b/essentials-framework/Essentials Devices Common/Essentials Devices Common/Essentials Devices Common.csproj index 7c978bd8..22a1dc1d 100644 --- a/essentials-framework/Essentials Devices Common/Essentials Devices Common/Essentials Devices Common.csproj +++ b/essentials-framework/Essentials Devices Common/Essentials Devices Common/Essentials Devices Common.csproj @@ -77,12 +77,12 @@ False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpCustomAttributesInterface.dll + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpCustomAttributesInterface.dll False False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpHelperInterface.dll + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpHelperInterface.dll False @@ -92,7 +92,7 @@ False - ..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpPro.exe + ..\..\..\..\..\..\..\ProgramData\Crestron\SDK\SimplSharpPro.exe False From b9fff952150ea86a5c0926ea024e32b25d7d0b33 Mon Sep 17 00:00:00 2001 From: Nick Genovese Date: Thu, 3 Sep 2020 10:57:06 -0400 Subject: [PATCH 2/2] added IQueueMessage and two implementations; GenericQueue now accepts type IQueueMessage --- .../Crestron/CrestronGenericBaseDevice.cs | 2 +- .../PepperDash_Essentials_Core.csproj | 5 +- .../Queues/BytesQueue.cs | 67 ----------------- .../Queues/ComsMessage.cs | 73 +++++++++++++++++++ .../Queues/GenericQueue.cs | 36 +++------ .../PepperDashEssentialsBase/Queues/IQueue.cs | 2 +- .../Queues/IQueueMessage.cs | 7 ++ .../Queues/ProcessStringMessage.cs | 44 +++++++++++ .../Queues/StringQueue.cs | 66 ----------------- .../Queues/StringResponseProcessor.cs | 8 +- 10 files changed, 146 insertions(+), 164 deletions(-) delete mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/BytesQueue.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/ComsMessage.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueueMessage.cs create mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/ProcessStringMessage.cs delete mode 100644 essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringQueue.cs diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron/CrestronGenericBaseDevice.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron/CrestronGenericBaseDevice.cs index 9db81122..56b36791 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron/CrestronGenericBaseDevice.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Crestron/CrestronGenericBaseDevice.cs @@ -179,7 +179,7 @@ namespace PepperDash.Essentials.Core { public static eDeviceRegistrationUnRegistrationResponse RegisterWithLogging(this GenericBase device, string key) { - var result = device.Register(); + var result = device.Register(); var level = result == eDeviceRegistrationUnRegistrationResponse.Success ? Debug.ErrorLogLevel.Notice : Debug.ErrorLogLevel.Error; Debug.Console(0, level, "Register device result: '{0}', type '{1}', result {2}", key, device, result); diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj b/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj index fad95abf..077d46fa 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj @@ -158,7 +158,6 @@ - @@ -223,6 +222,8 @@ + + @@ -241,6 +242,7 @@ + @@ -316,7 +318,6 @@ - diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/BytesQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/BytesQueue.cs deleted file mode 100644 index aad96cf1..00000000 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/BytesQueue.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using PepperDash.Core; - -namespace PepperDash_Essentials_Core.Queues -{ - /// - /// Byte implementation of Action queue - /// - public class BytesQueue : IQueue - { - private readonly IQueue _queue; - - /// - /// Constructor for BytesQueue - /// - /// Key - /// Action to process queued bytes - public BytesQueue(string key, Action processBytesAction) - { - _queue = new GenericQueue(key, processBytesAction); - } - - /// - /// Constructor for BytesQueue - /// - /// Key - /// Action to process queued bytes - /// Delay in ms between actions being invoked - public BytesQueue(string key, Action processBytesAction, int pacing) - { - _queue = new GenericQueue(key, processBytesAction, pacing); - } - - /// - /// Enqueue a byte array to be processed - /// - /// Byte array to be processed - public void Enqueue(byte[] item) - { - _queue.Enqueue(item); - } - - /// - /// If the instance has been disposed - /// - public bool Disposed - { - get { return _queue.Disposed; } - } - - /// - /// Key - /// - public string Key - { - get { return _queue.Key; } - } - - /// - /// Disposes of resources - /// - public void Dispose() - { - _queue.Dispose(); - } - } -} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/ComsMessage.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/ComsMessage.cs new file mode 100644 index 00000000..92c97248 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/ComsMessage.cs @@ -0,0 +1,73 @@ +using System; +using PepperDash.Core; + +namespace PepperDash_Essentials_Core.Queues +{ + /// + /// IBasicCommunication Message for IQueue + /// + public class ComsMessage : IQueueMessage + { + private readonly byte[] _bytes; + private readonly IBasicCommunication _coms; + private readonly string _string; + private readonly bool _isByteMessage; + + /// + /// Constructor for a string message + /// + /// IBasicCommunication to send the message + /// Message to send + public ComsMessage(IBasicCommunication coms, string message) + { + Validate(coms, message); + _coms = coms; + _string = message; + } + + /// + /// Constructor for a byte message + /// + /// IBasicCommunication to send the message + /// Message to send + public ComsMessage(IBasicCommunication coms, byte[] message) + { + Validate(coms, message); + _coms = coms; + _bytes = message; + _isByteMessage = true; + } + + private void Validate(IBasicCommunication coms, object message) + { + if (_coms == null) + throw new ArgumentNullException("coms"); + + if (message == null) + throw new ArgumentNullException("message"); + } + + /// + /// Dispatchs the string/byte[] to the IBasicCommunication specified + /// + public void Dispatch() + { + if (_isByteMessage) + { + _coms.SendBytes(_bytes); + } + else + { + _coms.SendText(_string); + } + } + + /// + /// Shows either the byte[] or string to be sent + /// + public override string ToString() + { + return _bytes != null ? _bytes.ToString() : _string; + } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs index 6a06cab8..1f27fe1e 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/GenericQueue.cs @@ -8,17 +8,16 @@ namespace PepperDash_Essentials_Core.Queues /// /// Threadsafe processing of queued items with pacing if required /// - /// Type of item to be processed - public class GenericQueue : IQueue where T : class + public class GenericQueue : IQueue { private readonly string _key; - protected readonly CrestronQueue _queue; + protected readonly CrestronQueue _queue; protected readonly Thread _worker; protected readonly CEvent _waitHandle = new CEvent(); private readonly bool _delayEnabled; private readonly int _delayTime; - + /// /// If the instance has been disposed. /// @@ -28,12 +27,11 @@ namespace PepperDash_Essentials_Core.Queues /// Constructor for generic queue with no pacing /// /// Key - /// Action to process items in the queue - public GenericQueue(string key, Action processQueueAction) + public GenericQueue(string key) { _key = key; - _queue = new CrestronQueue(); - _worker = new Thread(ProcessQueue, processQueueAction, Thread.eThreadStartOptions.Running); + _queue = new CrestronQueue(); + _worker = new Thread(ProcessQueue, null, Thread.eThreadStartOptions.Running); CrestronEnvironment.ProgramStatusEventHandler += programEvent => { @@ -48,10 +46,9 @@ namespace PepperDash_Essentials_Core.Queues /// Constructor for generic queue with no pacing /// /// Key - /// Action to process items in the queue /// Pacing in ms between actions - public GenericQueue(string key, Action processQueueAction, int pacing) - : this(key, processQueueAction) + public GenericQueue(string key, int pacing) + : this(key) { _delayEnabled = pacing > 0; _delayTime = pacing; @@ -64,13 +61,9 @@ namespace PepperDash_Essentials_Core.Queues /// Null when the thread is exited private object ProcessQueue(object obj) { - var action = obj as Action; - if (action == null) - throw new ArgumentNullException("obj"); - while (true) { - T item = null; + IQueueMessage item = null; if (_queue.Count > 0) { @@ -83,14 +76,14 @@ namespace PepperDash_Essentials_Core.Queues try { Debug.Console(2, this, "Processing queue item: '{0}'", item.ToString()); - action(item); + item.Dispatch(); if (_delayEnabled) Thread.Sleep(_delayTime); } catch (Exception ex) { - Debug.ConsoleWithLog(0, this, "Caught an exception in the ComsQueue {0}\r{1}\r{2}", ex.Message, ex.InnerException, ex.StackTrace); + Debug.ConsoleWithLog(0, this, "Caught an exception in the Queue {0}\r{1}\r{2}", ex.Message, ex.InnerException, ex.StackTrace); } } else _waitHandle.Wait(); @@ -99,12 +92,7 @@ namespace PepperDash_Essentials_Core.Queues return null; } - /// - /// Enqueues an item and processes the queue. If 'null' is enqueued the thread will close and - /// the class must be reinstantiated. - /// - /// Item to be processed - public virtual void Enqueue(T item) + public void Enqueue(IQueueMessage item) { _queue.Enqueue(item); _waitHandle.Set(); diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueue.cs index 9d69f7fa..852542d5 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueue.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueue.cs @@ -7,7 +7,7 @@ using PepperDash.Core; namespace PepperDash_Essentials_Core.Queues { - public interface IQueue : IKeyed, IDisposable where T : class + public interface IQueue : IKeyed, IDisposable where T : class { void Enqueue(T item); bool Disposed { get; } diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueueMessage.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueueMessage.cs new file mode 100644 index 00000000..ee0d87d2 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/IQueueMessage.cs @@ -0,0 +1,7 @@ +namespace PepperDash_Essentials_Core.Queues +{ + public interface IQueueMessage + { + void Dispatch(); + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/ProcessStringMessage.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/ProcessStringMessage.cs new file mode 100644 index 00000000..a15f7231 --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/ProcessStringMessage.cs @@ -0,0 +1,44 @@ +using System; + +namespace PepperDash_Essentials_Core.Queues +{ + /// + /// Message class for processing strings via an IQueue + /// + public class ProcessStringMessage : IQueueMessage + { + private readonly Action _action; + private readonly string _message; + + /// + /// Constructor + /// + /// Message to be processed + /// Action to invoke on the message + public ProcessStringMessage(string message, Action action) + { + _message = message; + _action = action; + } + + /// + /// Processes the string with the given action + /// + public void Dispatch() + { + if (_action == null || String.IsNullOrEmpty(_message)) + return; + + _action(_message); + } + + /// + /// To string + /// + /// The current message + public override string ToString() + { + return _message ?? String.Empty; + } + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringQueue.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringQueue.cs deleted file mode 100644 index 2be49f74..00000000 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringQueue.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; - -namespace PepperDash_Essentials_Core.Queues -{ - /// - /// String implementation of Action Queue - /// - public class StringQueue : IQueue - { - private readonly IQueue _queue; - - /// - /// Constructor for BytesQueue - /// - /// Key - /// Action to process queued strings - public StringQueue(string key, Action processStringAction) - { - _queue = new GenericQueue(key, processStringAction); - } - - /// - /// Constructor for StringQueue - /// - /// Key - /// Action to process queued strings - /// Delay in ms between actions being invoked - public StringQueue(string key, Action processStringAction, int pacing) - { - _queue = new GenericQueue(key, processStringAction, pacing); - } - - /// - /// Enqueue a byte array to be processed - /// - /// Byte array to be processed - public void Enqueue(string item) - { - _queue.Enqueue(item); - } - - /// - /// Key - /// - public string Key - { - get { return _queue.Key; } - } - - /// - /// Disposes of resources - /// - public void Dispose() - { - _queue.Dispose(); - } - - /// - /// If the instance has been disposed - /// - public bool Disposed - { - get { return _queue.Disposed; } - } - } -} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringResponseProcessor.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringResponseProcessor.cs index a3c90689..7f6477a3 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringResponseProcessor.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Queues/StringResponseProcessor.cs @@ -6,13 +6,15 @@ namespace PepperDash_Essentials_Core.Queues { public sealed class StringResponseProcessor : IKeyed, IDisposable { - private readonly IQueue _queue; + private readonly Action _processStringAction; + private readonly IQueue _queue; private readonly IBasicCommunication _coms; private readonly CommunicationGather _gather; private StringResponseProcessor(string key, Action processStringAction) { - _queue = new StringQueue(key, processStringAction); + _processStringAction = processStringAction; + _queue = new GenericQueue(key); CrestronEnvironment.ProgramStatusEventHandler += programEvent => { @@ -49,7 +51,7 @@ namespace PepperDash_Essentials_Core.Queues private void OnResponseReceived(object sender, GenericCommMethodReceiveTextArgs args) { - _queue.Enqueue(args.Text); + _queue.Enqueue(new ProcessStringMessage(args.Text, _processStringAction)); } ///