Skip to content

Commit

Permalink
actual blocking of messages, color keywords for mapped mode
Browse files Browse the repository at this point in the history
  • Loading branch information
LeHuman committed Feb 16, 2021
1 parent 14b92e0 commit 306a0bd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 46 deletions.
Binary file modified .vs/ComMonitor/v16/.suo
Binary file not shown.
103 changes: 61 additions & 42 deletions MainClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public class MainClass
private static Func<byte[], string> dataFunction;
private static PipeServer priorityNotify;
private static ConsoleColor Cfg = ConsoleColor.White;

Dictionary<string, ConsoleColor> logLevel = new Dictionary<string, ConsoleColor>{
readonly Dictionary<string, ConsoleColor> logLevel = new Dictionary<string, ConsoleColor>{
{ "[DEBUG]", ConsoleColor.Magenta },
{ "[FATAL]", ConsoleColor.DarkRed },
{ "[ERROR]", ConsoleColor.Red },
Expand All @@ -71,13 +70,13 @@ private void ColorConsole()
ColorConsole(Cfg);
}

private void LogLevelColor(string str) // IMPROVE: Add option to still detect keywords despite datatype
private void LogLevelColor(string str)
{
if (logKeyword)
{
foreach (KeyValuePair<string, ConsoleColor> entry in logLevel)
{
if (str.StartsWith(entry.Key))
if (str.Contains(entry.Key)) // What is the performance impact of this vs StartsWith?
{
Console.ForegroundColor = entry.Value;
return;
Expand All @@ -86,6 +85,28 @@ private void LogLevelColor(string str) // IMPROVE: Add option to still detect ke
Console.ForegroundColor = Cfg;
}
}

private void ConsolePrint(string str)
{
_ConsolePrintData(str, false);
}
private void ConsolePrintLine(string str)
{
_ConsolePrintData(str, true);
}
private void _ConsolePrintData(string str, bool newline)
{
if (str.Length > 0)
{
LogLevelColor(str.ToUpper()); // TODO: trim to last bracket to reduce text that is searched
if (newline)
Console.WriteLine(str);
else
Console.Write(str);
ColorConsole();
}
}

#endregion

#region Data Interpreters
Expand All @@ -101,17 +122,12 @@ void AsciiDataReceived(object sender, DataStreamEventArgs e)
string[] lines = data.Replace('\r', '\0').Split('\n');
foreach (string line in lines)
{
if (line.Length > 0)
{
LogLevelColor(line.ToUpper());
Console.WriteLine(line);
}
ConsolePrintLine(line);
}
}
else
{
LogLevelColor(data.ToUpper());
Console.Write(data);
ConsolePrint(data);
}

}
Expand All @@ -120,25 +136,23 @@ void SerialDataReceived(object sender, DataStreamEventArgs e)
{
if (e.Data.Length == 0)
return;
string prnt = dataFunction(e.Data);
if (prnt.Length > 0)
Console.WriteLine(prnt);
ConsolePrintLine(dataFunction(e.Data));
}

void SerialChunkedDataReceived(object sender, DataStreamEventArgs e)
{
int remain = e.Data.Length;
Span<byte> rawData = e.Data.AsSpan();
int remain = rawData.Length;
int i = 0;
while (remain > 0)
while (remain >= maxBytes)
{
int copyBytes = Math.Min(remain, maxBytes);
byte[] block = new byte[copyBytes];
Array.Copy(e.Data, i, block, 0, copyBytes);
i += maxBytes;
string prnt = dataFunction(e.Data);
if (prnt.Length > 0)
Console.WriteLine(prnt);
ConsolePrintLine(dataFunction(rawData.Slice(i, maxBytes).ToArray()));
remain -= maxBytes;
i += maxBytes;
}
if (remain > 0)
{
ConsolePrintLine(dataFunction(rawData.Slice(i).ToArray()));
}
}

Expand All @@ -156,37 +170,42 @@ public static string GetMappedMessage(Span<byte> data)
str = "Bad String ID";
return id + " " + str + " " + num + "\n";
}
private static List<byte> saveBuffer = new List<byte>();

List<byte> saveBuffer = new List<byte>();

void SerialMappedDataReceived(object sender, DataStreamEventArgs e)
{
if (e.Data.Length == 0)
return;
saveBuffer.AddRange(e.Data);
Span<byte> rawData = new Span<byte>(saveBuffer.ToArray());
saveBuffer.Clear();
byte[] data = new byte[0];
if (e.Data.Length % maxBytes != 0)
Console.WriteLine("WARN: Data may have dysynced, or badly formatted data was received");

for (int j = 0; j < e.Data.Length; j++)
byte[] stichedData;
Span<byte> rawData;

if (saveBuffer.Count > 0) // There is part of a message that was uncomplete, prepend it to our span
{
if (j / maxBytes >= 1)
{
int b = j - maxBytes;
data = rawData.Slice(b).ToArray();
saveBuffer.AddRange(rawData.Slice(0, b).ToArray());
break;
}
stichedData = new byte[e.Data.Length + saveBuffer.Count];
Buffer.BlockCopy(saveBuffer.ToArray(), 0, stichedData, 0, saveBuffer.Count);
Buffer.BlockCopy(e.Data, 0, stichedData, saveBuffer.Count, e.Data.Length);
rawData = stichedData.AsSpan();
}
else
{
rawData = e.Data.AsSpan();
}

byte[] block = new byte[maxBytes];
int remain = data.Length;
int remain = rawData.Length;
int i = 0;
while (remain >= maxBytes)
{
Array.Copy(data, i, block, 0, maxBytes);
i += maxBytes;
Console.Write(GetMappedMessage(new Span<byte>(block)));
ConsolePrint(GetMappedMessage(rawData.Slice(i, maxBytes)));
remain -= maxBytes;
i += maxBytes;
}
if (remain > 0)
{
saveBuffer.AddRange(rawData.Slice(i).ToArray());
}
}

Expand Down Expand Up @@ -314,7 +333,7 @@ public MainClass(string[] args)
retries = MAX_RETRY;
waitForConn = options.wait;
mappedMode = options.jsonPath != null && maxBytes != 0 /*&& options.jsonBlock != null*/; // TODO: custom message block structure for matching strings
logKeyword = setColor && options.setDataType == DataType.Ascii;
logKeyword = setColor && (options.setDataType == DataType.Ascii || mappedMode);
});
#endregion

Expand Down
2 changes: 1 addition & 1 deletion Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Options
[Option("timeout", HelpText = "Set the number of retries for both -w -r options", Default = 200)]
public int retryTimeout { get; set; }

[Option('c', "color", HelpText = "Disable console color")]
[Option('c', "color", HelpText = "Disable console color, may help with latency")]
public bool setColor { get; set; }

[Option('f', "frequency", HelpText = "Manually Set the critical frequency of communication (Hz)", Default = 20)]
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.3.4")]
[assembly: AssemblyFileVersion("1.0.3.4")]
[assembly: AssemblyVersion("1.0.3.6")]
[assembly: AssemblyFileVersion("1.0.3.6")]
[assembly: NeutralResourcesLanguage("en")]
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Arguments are not case sensitive
* -r, --retry Keep console open and wait for port to reopen
* -w, --wait If the port is not open when the console starts, wait for it to open
* --timeout (Default: 200) Set the number of retries for both -w -r options [Integer]
* -c, --color Disable changing color of console
* -c, --color Disable changing color of console, may help with latency
* -f, --frequency (Default: 20) Manually Set the critical frequency of communication (Hz)
* -m, --maxBytes Set a max number of bytes to print for each line when not in Ascii mode [Integer]
* --priority Take priority of a port if inuse by another instance of ComMonitor
Expand All @@ -45,6 +45,8 @@ Strings with special prefixes ( not case sensitive ) are highlighted in the cons

Every newline that is received is interpreted separately and colored accordingly

NOTE: Because each message must be searched for keywords (both in ascii and mapped mode), disabling color (-c) might help reduce latency if needed

### JSON Mapping

NOTE: Option `--jsonBlock` is not needed, WIP
Expand Down

0 comments on commit 306a0bd

Please sign in to comment.