using OmniPlus.UI.Inspection; using System; using System.ComponentModel; using System.Configuration; using System.Drawing; using System.Windows.Forms; namespace OmniPlus.UI { public partial class OmniPlusUI : Form { private readonly OmniPlusConnectionsPool connectionsPool; public OmniPlusUI() { this.InitializeComponent(); var omniDatabase = ConfigurationManager.AppSettings.Get("OMNIDB"); this.connectionsPool = new OmniPlusConnectionsPool(omniDatabase); this.HandleCreated += this.OmniPlusUI_HandleCreated; this.HandleDestroyed += this.OmniPlusUI_HandleDestroyed; } private void OmniPlusUI_HandleDestroyed(object _, EventArgs _1) { this.connectionsPool.Dispose(); this.connectionsPool.StateChanged -= this.ConnectionsPool_StateChanged; this.pruefungBackground.DoWork -= this.PruefungBackground_DoWork; } private void OmniPlusUI_HandleCreated(object _, EventArgs _1) { this.connectionsPool.StateChanged += this.ConnectionsPool_StateChanged; this.pruefungBackground.DoWork += this.PruefungBackground_DoWork; this.InitializeBody(); var portNames = (ConfigurationManager .AppSettings .Get("COMS") ?? string.Empty) .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); this.connectionsPool.InitializeInspection(portNames); } private void PruefungStartBTN_Click(object _, EventArgs _1) => this.pruefungBackground.RunWorkerAsync(); private void PruefungBackground_DoWork(object _, DoWorkEventArgs _1) { this.Invoke(new Action(() => { this.Cursor = Cursors.WaitCursor; this.pruefungStartBTN.Enabled = false; })); this.ClearBody(); this.connectionsPool.StartInspection(); this.Invoke(new Action(() => { this.Cursor = Cursors.Default; this.pruefungStartBTN.Enabled = true; })); } private void ConnectionsPool_StateChanged(int column, bool? succeeded, string header, string value) { this.body.Invoke(new Action(() => { var headerIndex = this.GetRowIndex(header); var columnIndex = headerIndex + column; var controls = this.body.Controls; var controlsCount = controls.Count; if (controls[headerIndex] is Label rowHeader) { rowHeader.Text = header; rowHeader.TextAlign = ContentAlignment.MiddleLeft; rowHeader.BackColor = SystemColors.ControlLight; rowHeader.ForeColor = SystemColors.ControlText; } if (controls[columnIndex] is Label columnLabel) { if (string.IsNullOrWhiteSpace(value)) { value = "---"; } columnLabel.Text = value; columnLabel.BackColor = SystemColors.Window; if (headerIndex == 0) { columnLabel.BackColor = SystemColors.ControlLight; } columnLabel.ForeColor = SystemColors.ControlText; if (headerIndex > 0 && columnIndex > 0) { if (succeeded == true) { columnLabel.ForeColor = Color.DarkGreen; } else if (succeeded == false) { columnLabel.ForeColor = Color.Red; } else { columnLabel.ForeColor = Color.Gray; } } } })); } private int GetRowIndex(string header) { var controls = this.body.Controls; var controlsCount = controls.Count; var cols = this.body.ColumnCount; var rows = this.body.RowCount; var rowIndex = int.MinValue; int row; for (row = 0; row < rows; row++) { var headerIndex = row * cols; if (headerIndex >= controlsCount) { break; } if (controls[headerIndex] is Label label) { var labelText = label.Text?.Trim()?.ToLower(); var headerText = header?.Trim()?.ToLower(); if (string.IsNullOrWhiteSpace(labelText) || labelText == headerText) { rowIndex = headerIndex; break; } } else break; } if (rowIndex == int.MinValue) { rowIndex = (rows - 1) * cols; } return rowIndex; } private void InitializeBody() { this.body.Invoke(new Action(() => { var cols = 9; var rows = 31; this.body.RowCount = 0; this.body.ColumnCount = 0; this.body.Controls.Clear(); this.body.RowStyles.Clear(); this.body.ColumnStyles.Clear(); var bold = new Font(this.body.Font, FontStyle.Bold); for (int row = 0; row < rows; row++) { var firstRow = row == 0; this.body.RowCount++; this.body.RowStyles.Add(new RowStyle(SizeType.AutoSize)); for (int col = 0; col < cols; col++) { var firstCol = col == 0; var lastCol = col == cols - 1; if (firstRow) { this.body.ColumnCount++; this.body.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, firstCol ? 100F : 65F)); } var color = firstCol || firstRow ? SystemColors.ControlLight : SystemColors.Window; var label = new Label { Dock = DockStyle.Fill, Padding = new Padding(3), Margin = new Padding(0, 0, lastCol ? 0 : 3, 3), TextAlign = ContentAlignment.MiddleCenter, BackColor = color, ForeColor = color }; if (firstCol || firstRow) { label.Font = bold; } this.body.Controls.Add(label, col, row); } } this.body.RowCount++; this.body.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); })); } private void ClearBody() { this.body.Invoke(new Action(() => { var rows = this.body.RowCount; var cols = this.body.ColumnCount; var controls = this.body.Controls; for (int row = 1; row < rows; row++) { var offset = Math.Max(row - 1, 1) * cols; for (int col = 1; col < cols; col++) { if (controls[offset + col] is Label label) { label.Text = null; } } } })); } } }