tbf/GenesisCordonelTester/UI/MainForm.cs

293 lines
9.6 KiB
C#
Raw Normal View History

using GenesisCordonelInterface.UI.LaatzenAPI_CordonelPreadjustmentUI;
using GenesisCordonelInterface.UI.StaraTuraAPI_GenesisCordonelInterface;
using NLog;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Xylem.Common.Ui.GenesisToolBox;
using Xylem.Common.Utils.Logging;
namespace GenesisCordonelInterface.UI
{
public partial class MainForm : Form
{
private static readonly NLog.ILogger Logger = NLog.LogManager.GetLogger("GenesisCordonelInterface");
private static readonly Regex LogLevelRegex = new Regex(@"\|\s*(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)\s*\|", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public MainForm()
{
InitializeComponent();
/*_logger = logger;
_logger.MessagePublished += OnLogMessagePublished;*/
UiLogBus.MessageReceived += UiLogBus_MessageReceived;
//TopRight position on screen
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, 0);
//black background
rtbMainLog.BackColor = Color.Black;
rtbMainLog.ForeColor = Color.Gainsboro;
rtbMainLog.Font = new Font("Consolas", 9f);
rtbMainLog.ReadOnly = true;
rtbMainLog.HideSelection = false;
}
private void UiLogBus_MessageReceived(string msg)
{
if (InvokeRequired)
{
BeginInvoke(new Action<string>(UiLogBus_MessageReceived), msg);
return;
}
string[] lines = msg.Replace("\r\n", "\n").Split('\n');
foreach (string originalLine in lines)
{
if (string.IsNullOrWhiteSpace(originalLine))
continue;
string line = originalLine;
// Find header (timestamp|LEVEL|)
Match m = LogLevelRegex.Match(line);
string indent = "";
if (m.Success)
{
int indentLength = m.Index + m.Length;
indent = new string(' ', indentLength);
}
// Split long message manually if needed (optional)
string[] subLines = line.Split(new[] { " - " }, 2, StringSplitOptions.None);
string firstLine = line;
string rest = null;
if (subLines.Length == 2 && subLines[1].Length > 120) // heuristic
{
firstLine = subLines[0] + " - " + subLines[1].Substring(0, 120);
rest = subLines[1].Substring(120);
}
AppendStyledLine(firstLine);
if (!string.IsNullOrEmpty(rest))
{
AppendStyledLine(indent + rest);
}
}
}
private void AppendStyledLine(string line)
{
int start = rtbMainLog.TextLength;
rtbMainLog.SelectionStart = start;
rtbMainLog.SelectionLength = 0;
rtbMainLog.SelectionColor = Color.Gainsboro;
rtbMainLog.AppendText(line + Environment.NewLine);
Match m = LogLevelRegex.Match(line);
if (m.Success)
{
rtbMainLog.SelectionStart = start + m.Index;
rtbMainLog.SelectionLength = m.Length;
rtbMainLog.SelectionColor = GetLogLevelColor(m.Groups[1].Value);
}
HighlightKeywordsInLine(line, start);
}
private int MeasureTextWidthPx(string text)
{
if (string.IsNullOrEmpty(text))
return 0;
return TextRenderer.MeasureText(text, rtbMainLog.Font).Width;
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
//_logger.MessagePublished -= OnLogMessagePublished;
UiLogBus.MessageReceived -= UiLogBus_MessageReceived;
base.OnFormClosed(e);
}
private void btnSetup_Click(object sender, EventArgs e)
{
Logger.Trace("FORM: ---------------------------------");
Logger.Trace("FORM: Setup open");
using (FrmSetup frm = new FrmSetup())
{
frm.ShowDialog(this);
}
Logger.Trace("FORM: Setup closed.");
}
private void btnRegisterStore_Click(object sender, EventArgs e)
{
Logger.Trace("FORM: ---------------------------------");
Logger.Trace("FORM: Register Store open.");
using (FrmRegisterStore frm = new FrmRegisterStore())
{
frm.ShowDialog(this);
}
Logger.Trace("FORM: Register Store closed.");
}
private void btnPulseSetup_Click(object sender, EventArgs e)
{
Logger.Trace("FORM: ---------------------------------");
Logger.Trace("FORM: Pulse Setup open.");
using (FrmConfigurations frm = new FrmConfigurations())
{
frm.ShowDialog(this);
}
Logger.Trace("FORM: Pulse Setup closed.");
}
private void miExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void miClearLog_Click(object sender, EventArgs e)
{
rtbMainLog.Clear();
Logger.Trace("Log cleared.");
}
private void miHelpAbout_Click(object sender, EventArgs e)
{
MessageBox.Show(
"Genesis Cordonel Tester",
"About",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
private Color GetLogLevelColor(string level)
{
switch (level.Trim().ToUpperInvariant())
{
case "TRACE": return Color.Gray;
case "DEBUG": return Color.DeepSkyBlue;
case "INFO": return Color.LimeGreen;
case "WARN": return Color.Orange;
case "ERROR": return Color.Red;
case "FATAL": return Color.Magenta;
default: return Color.Gainsboro;
}
}
private bool IsSeparatorLine(string text)
{
if (string.IsNullOrWhiteSpace(text))
return false;
string trimmed = text.Trim();
// Remove spaces and tab-like spacing
string compact = new string(trimmed.Where(c => !char.IsWhiteSpace(c)).ToArray());
if (compact.Length < 4)
return false;
// Count non-letter/non-digit characters
int nonAlnumCount = compact.Count(c => !char.IsLetterOrDigit(c));
// Consider it a separator if most characters are non-alphanumeric
// Examples:
// -----CommandToMeter()-----
// ==========================
// /////////
double ratio = (double)nonAlnumCount / compact.Length;
return ratio >= 0.6;
}
/// <summary>
/// special string highlighting
/// "TX FINAL" have to go before "TX"
/// else "TX" will highlighted in the middle of "TX FINAL"
/// </summary>
private static readonly (Color color, string[] keywords)[] KeywordGroups =
{
(Color.DeepSkyBlue, new[] { "REQUEST" }),
(Color.Lime, new[] { "RESPONSE" }),
//(Color.LightGreen, new[] { "START", "END" }),
//(Color.Cyan, new[] { "TX", "TX FINAL" }),
//(Color.DeepSkyBlue,new[] { "RX", "RX FINAL", "RX CHUNK" }),
(Color.Gold, new[] { "READ-REGISTER-SESSION", "UI-CLICK"}),
//(Color.Violet, new[] { "REGADDR" }),
//(Color.Khaki, new[] { "DEFAULT", "ALIGNED", "STRING" })
};
/// <summary>
/// special string highlighting
/// </summary>
/// <param name="line"></param>
/// <param name="lineStartIndex"></param>
private void HighlightKeywordsInLine(string line, int lineStartIndex)
{
foreach (var group in KeywordGroups)
{
foreach (var keyword in group.keywords)
{
int index = 0;
while ((index = line.IndexOf(keyword, index, StringComparison.Ordinal)) >= 0)
{
rtbMainLog.SelectionStart = lineStartIndex + index;
rtbMainLog.SelectionLength = keyword.Length;
rtbMainLog.SelectionColor = group.color;
index += keyword.Length;
}
}
}
}
private void preadjustmentButton_Click(object sender, EventArgs e)
{
Logger.Trace("FORM: ---------------------------------");
Logger.Trace("FORM: Preadjustment open.");
using (FrmCordonelPreadjustmentUI frm = new FrmCordonelPreadjustmentUI())
{
frm.ShowDialog(this);
}
Logger.Trace("FORM: Preadjustment closed.");
}
private void button1_Click(object sender, EventArgs e)
{
Logger.Trace("FORM: ---------------------------------");
Logger.Trace("FORM: GCI GUI interface open.");
using (FrmGCIAPI frm = new FrmGCIAPI())
{
frm.ShowDialog(this);
}
Logger.Trace("FORM: GCI GUI interface closed.");
}
}
}