326 lines
9.9 KiB
C#
326 lines
9.9 KiB
C#
using GenesisCordonelInterface.UI.LaatzenAPI_CordonelPreadjustmentUI;
|
|
using GenesisCordonelInterface.UI.StaraTuraAPI_GenesisCordonelInterface;
|
|
using System;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Text.RegularExpressions;
|
|
using System.Windows.Forms;
|
|
using Xylem.Common.Ui.GenesisToolBox;
|
|
using Xylem.Common.Utils.Logging;
|
|
using GenesisCordonelInterface.API;
|
|
using GenesisCordonelInterface.UI.Debug;
|
|
|
|
namespace GenesisCordonelInterface.UI
|
|
{
|
|
public partial class MainView : UserControl
|
|
{
|
|
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);
|
|
private readonly InterfaceOutsideToGCI _api = new InterfaceOutsideToGCI();
|
|
private MeterBatchConfigPanel _batchPanel;
|
|
|
|
public MainView()
|
|
{
|
|
InitializeComponent();
|
|
InitializeDebugPanels();
|
|
//InitializeWorkerDebugPanel();
|
|
|
|
UiLogBus.MessageReceived += UiLogBus_MessageReceived;
|
|
|
|
rtbMainLog.BackColor = Color.Black;
|
|
rtbMainLog.ForeColor = Color.Gainsboro;
|
|
rtbMainLog.Font = new Font("Consolas", 9f);
|
|
rtbMainLog.ReadOnly = true;
|
|
rtbMainLog.HideSelection = false;
|
|
}
|
|
|
|
private void InitializeWorkerDebugPanel()
|
|
{
|
|
pnlWorkerDebug.Controls.Clear();
|
|
|
|
var debugPanel = new WorkerDebugPanel(_api)
|
|
{
|
|
Dock = DockStyle.Fill
|
|
};
|
|
|
|
pnlWorkerDebug.Controls.Add(debugPanel);
|
|
}
|
|
private void InitializeDebugPanels()
|
|
{
|
|
_batchPanel = new MeterBatchConfigPanel(_api)
|
|
{
|
|
Dock = DockStyle.Fill
|
|
};
|
|
|
|
pnlSlotConfig.Controls.Add(_batchPanel);
|
|
|
|
pnlWorkerDebug.Controls.Add(new WorkerDebugPanel(_api)
|
|
{
|
|
Dock = DockStyle.Fill
|
|
});
|
|
}
|
|
|
|
public void AddSlotRow()
|
|
{
|
|
_batchPanel?.AddEmptySlotRow();
|
|
}
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
base.OnLoad(e);
|
|
|
|
BeginInvoke(new Action(() =>
|
|
{
|
|
SetSafeSplitterDistance(splitWorkArea, 420);
|
|
}));
|
|
}
|
|
|
|
private void SetSafeSplitterDistance(SplitContainer split, int desired)
|
|
{
|
|
int width = split.ClientSize.Width;
|
|
|
|
int min1 = split.Panel1MinSize;
|
|
int min2 = split.Panel2MinSize;
|
|
int splitter = split.SplitterWidth;
|
|
|
|
int max = width - min2 - splitter;
|
|
|
|
if (width <= min1 + min2 + splitter)
|
|
return;
|
|
|
|
if (desired < min1)
|
|
desired = min1;
|
|
|
|
if (desired > max)
|
|
desired = max;
|
|
|
|
split.SplitterDistance = desired;
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
UiLogBus.MessageReceived -= UiLogBus_MessageReceived;
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
private IWin32Window DialogOwner
|
|
{
|
|
get
|
|
{
|
|
Form owner = FindForm();
|
|
return owner ?? (IWin32Window)this;
|
|
}
|
|
}
|
|
|
|
private void btnSetup_Click(object sender, EventArgs e)
|
|
{
|
|
Logger.Trace("FORM: ---------------------------------");
|
|
Logger.Trace("FORM: Setup open");
|
|
|
|
using (FrmSetup frm = new FrmSetup())
|
|
{
|
|
frm.ShowDialog(DialogOwner);
|
|
}
|
|
|
|
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(DialogOwner);
|
|
}
|
|
|
|
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(DialogOwner);
|
|
}
|
|
|
|
Logger.Trace("FORM: Pulse Setup closed.");
|
|
}
|
|
|
|
private void preadjustmentButton_Click(object sender, EventArgs e)
|
|
{
|
|
Logger.Trace("FORM: ---------------------------------");
|
|
Logger.Trace("FORM: Preadjustment open.");
|
|
|
|
using (FrmCordonelPreadjustmentUI frm = new FrmCordonelPreadjustmentUI())
|
|
{
|
|
frm.ShowDialog(DialogOwner);
|
|
}
|
|
|
|
Logger.Trace("FORM: Preadjustment closed.");
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
Logger.Trace("FORM: ---------------------------------");
|
|
Logger.Trace("FORM: GCI GUI interface open.");
|
|
|
|
using (var frm = new FrmGCIAPI(_api))
|
|
{
|
|
frm.ShowDialog(this);
|
|
}
|
|
|
|
Logger.Trace("FORM: GCI GUI interface closed.");
|
|
}
|
|
|
|
private void SwitchGciView(string name, Control view)
|
|
{
|
|
Logger.Trace("FORM: ---------------------------------");
|
|
Logger.Trace($"FORM: GCI VIEW -> {name} OPEN");
|
|
|
|
pnlGciViewHost.Controls.Clear();
|
|
|
|
view.Dock = DockStyle.Fill;
|
|
pnlGciViewHost.Controls.Add(view);
|
|
view.BringToFront();
|
|
|
|
Logger.Trace($"FORM: GCI VIEW -> {name} LOADED");
|
|
}
|
|
|
|
private void btnMetersAction_Click(object sender, EventArgs e)
|
|
{
|
|
SwitchGciView(
|
|
"MetersAction",
|
|
new MetersActionView(_api));
|
|
}
|
|
|
|
public void ClearLog()
|
|
{
|
|
rtbMainLog.Clear();
|
|
Logger.Trace("Log cleared.");
|
|
}
|
|
|
|
private void UiLogBus_MessageReceived(string loggerName, string msg)
|
|
{
|
|
if (loggerName != "GenesisCordonelInterface")
|
|
return;
|
|
|
|
if (IsDisposed || !IsHandleCreated)
|
|
return;
|
|
|
|
if (InvokeRequired)
|
|
{
|
|
BeginInvoke(new Action<string, string>(UiLogBus_MessageReceived), loggerName, msg);
|
|
return;
|
|
}
|
|
|
|
string[] lines = msg.Replace("\r\n", "\n").Split('\n');
|
|
|
|
foreach (string originalLine in lines)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(originalLine))
|
|
continue;
|
|
|
|
string line = originalLine;
|
|
Match m = LogLevelRegex.Match(line);
|
|
|
|
string indent = "";
|
|
if (m.Success)
|
|
{
|
|
int indentLength = m.Index + m.Length;
|
|
indent = new string(' ', indentLength);
|
|
}
|
|
|
|
string[] subLines = line.Split(new[] { " - " }, 2, StringSplitOptions.None);
|
|
|
|
string firstLine = line;
|
|
string rest = null;
|
|
|
|
if (subLines.Length == 2 && subLines[1].Length > 120)
|
|
{
|
|
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 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 static readonly Tuple<Color, string[]>[] KeywordGroups =
|
|
{
|
|
Tuple.Create(Color.DeepSkyBlue, new[] { "REQUEST" }),
|
|
Tuple.Create(Color.Lime, new[] { "RESPONSE" }),
|
|
Tuple.Create(Color.Gold, new[] { "READ-REGISTER-SESSION", "UI-CLICK" })
|
|
};
|
|
|
|
private void HighlightKeywordsInLine(string line, int lineStartIndex)
|
|
{
|
|
foreach (var group in KeywordGroups)
|
|
{
|
|
foreach (var keyword in group.Item2)
|
|
{
|
|
int index = 0;
|
|
|
|
while ((index = line.IndexOf(keyword, index, StringComparison.Ordinal)) >= 0)
|
|
{
|
|
rtbMainLog.SelectionStart = lineStartIndex + index;
|
|
rtbMainLog.SelectionLength = keyword.Length;
|
|
rtbMainLog.SelectionColor = group.Item1;
|
|
|
|
index += keyword.Length;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ShowGciView(Control view)
|
|
{
|
|
pnlGciViewHost.Controls.Clear();
|
|
|
|
view.Dock = DockStyle.Fill;
|
|
pnlGciViewHost.Controls.Add(view);
|
|
}
|
|
}
|
|
} |