From b2d3faf2088195f2859737a12243e2fd04404168 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Thu, 10 Feb 2022 10:22:28 -0700 Subject: [PATCH] feat(essentials): #128 Adds ability for CommunicationGather to take multiple delimiters --- .../Comm/CommunicationGather.cs | 45 +++++++++++-------- .../Comm/CommunicationStreamDebugging.cs | 11 +++++ .../Pepperdash Core/CommunicationExtras.cs | 7 +++ 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/CommunicationGather.cs b/Pepperdash Core/Pepperdash Core/Comm/CommunicationGather.cs index 87a1dcb..8bd7669 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/CommunicationGather.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/CommunicationGather.cs @@ -48,7 +48,7 @@ namespace PepperDash.Core /// char Delimiter; - string StringDelimiter; + string[] StringDelimiters; /// /// Fires up a gather, given a IBasicCommunicaion port and char for de @@ -68,12 +68,17 @@ namespace PepperDash.Core /// /// public CommunicationGather(ICommunicationReceiver port, string delimiter) + :this(port, new string[] { delimiter} ) { - Port = port; - StringDelimiter = delimiter; - port.TextReceived += Port_TextReceivedStringDelimiter; } + public CommunicationGather(ICommunicationReceiver port, string[] delimiters) + { + Port = port; + StringDelimiters = delimiters; + port.TextReceived += Port_TextReceivedStringDelimiter; + } + /// /// Disconnects this gather from the Port's TextReceived event. This will not fire LineReceived /// after the this call. @@ -137,20 +142,24 @@ namespace PepperDash.Core // RX: \x0d\x0a+OK "value":"1234"\x0d\x0a // Split: (2) DEVICE get version, +OK "value":"1234" - var lines = Regex.Split(str, StringDelimiter); - if (lines.Length > 1) - { - for (int i = 0; i < lines.Length - 1; i++) - { - string strToSend = null; - if (IncludeDelimiter) - strToSend = lines[i] + StringDelimiter; - else - strToSend = lines[i]; - handler(this, new GenericCommMethodReceiveTextArgs(strToSend)); - } - ReceiveBuffer = new StringBuilder(lines[lines.Length - 1]); - } + // Iterate the delimiters and fire an event for any matching delimiter + foreach (var delimiter in StringDelimiters) + { + var lines = Regex.Split(str, delimiter); + if (lines.Length == 0) + continue; + + for (int i = 0; i < lines.Length - 1; i++) + { + string strToSend = null; + if (IncludeDelimiter) + strToSend = lines[i] + StringDelimiters; + else + strToSend = lines[i]; + handler(this, new GenericCommMethodReceiveTextArgs(strToSend, delimiter)); + } + ReceiveBuffer = new StringBuilder(lines[lines.Length - 1]); + } } } diff --git a/Pepperdash Core/Pepperdash Core/Comm/CommunicationStreamDebugging.cs b/Pepperdash Core/Pepperdash Core/Comm/CommunicationStreamDebugging.cs index b5b1c15..3afbf01 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/CommunicationStreamDebugging.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/CommunicationStreamDebugging.cs @@ -130,4 +130,15 @@ namespace PepperDash.Core Tx = 2, Both = Rx | Tx } + + /// + /// The available settings for stream debugging response types + /// + [Flags] + public enum eStreamDebuggingDataTypeSettings + { + Bytes = 0, + Text = 1, + Both = Bytes | Text, + } } diff --git a/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs b/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs index 9dc4b48..775fd01 100644 --- a/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs +++ b/Pepperdash Core/Pepperdash Core/CommunicationExtras.cs @@ -110,11 +110,18 @@ namespace PepperDash.Core public class GenericCommMethodReceiveTextArgs : EventArgs { public string Text { get; private set; } + public string Delimiter { get; private set; } public GenericCommMethodReceiveTextArgs(string text) { Text = text; } + public GenericCommMethodReceiveTextArgs(string text, string delimiter) + :this(text) + { + Delimiter = delimiter; + } + /// /// Stupid S+ Constructor ///