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)); } ///