598 lines
21 KiB
C#
598 lines
21 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Xylem.Common.Hardware.Interfaces.Ports.PortCore.EventArguments;
|
|
using Xylem.Common.Hardware.WaterMeter.MagFlux.DataPackages.EventArguments;
|
|
using Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore;
|
|
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore;
|
|
using Xylem.Common.Utils.ProcessExec.EventArguments;
|
|
|
|
|
|
namespace Xylem.Common.Ui.MagFluxToolBox
|
|
{
|
|
/// <summary>
|
|
/// Test form for MagFlux operation
|
|
/// </summary>
|
|
public partial class FrmTest : Form
|
|
{
|
|
#region Variables
|
|
private readonly MeterBatch _meterBatch = new MeterBatch();
|
|
private MagFluxMeter _magFluxMeter;
|
|
private const String StrConnecting = "Connecting to PCB";
|
|
private const String StrNotConnected = "NOT CONNECTED";
|
|
private const String StrPartPcbConnected = "PCB ID: ";
|
|
private const String StrOverallProcess = "Overall Process";
|
|
|
|
private String _lastFwUpdateState;
|
|
private Int32 _lastSingleProgress;
|
|
private DateTimeOffset _startTime;
|
|
private Boolean _resetTimeMeasurement;
|
|
private readonly Boolean _updateGui = true;
|
|
|
|
private Boolean _tryConnectPcb;
|
|
|
|
// external update remarks form
|
|
// private readonly FrmHistory _frmHistory = new FrmHistory();
|
|
|
|
private readonly Version _version;
|
|
|
|
// status information
|
|
private static readonly Color ColorDefault = Color.Black;
|
|
private static readonly Color ColorSuccess = Color.Green;
|
|
private static readonly Color ColorProcessFailed = Color.Red;
|
|
//private static readonly Color ColorOngoingProcess = Color.Blue;
|
|
//private static readonly Color ColorUnknownStatus = Color.Gray;
|
|
//private const String SuccessSign = @"✔";
|
|
//private const String FailedSign = @"✘";
|
|
|
|
private const String StrSeparator = "-----------------------------------------------------" +
|
|
"-----------------------------------------------------";
|
|
#endregion
|
|
|
|
#region FormControls
|
|
/// <summary>
|
|
/// Ctor FW update
|
|
/// </summary>
|
|
public FrmTest()
|
|
{
|
|
InitializeComponent();
|
|
var cultureInfo = new CultureInfo("en-GB");
|
|
Thread.CurrentThread.CurrentUICulture = cultureInfo;
|
|
Thread.CurrentThread.CurrentCulture = cultureInfo;
|
|
_version = Assembly.GetExecutingAssembly().GetName().Version;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear data table and set genesis to not connected
|
|
/// </summary>
|
|
private void Init()
|
|
{
|
|
ProcessViewControl(false);
|
|
lblConnectPcb.Text = StrNotConnected;
|
|
lblConnectPcb.ForeColor = Color.Red;
|
|
lblWaitingForMeterResponse.Visible = false;
|
|
btnConnect.Enabled = true;
|
|
SetControlDownloadsLocked();
|
|
tbxSensorNo.Visible = false;
|
|
}
|
|
|
|
private void FrmTest_Load(Object sender, EventArgs e)
|
|
{
|
|
var version = Assembly.GetExecutingAssembly().GetName().Version;
|
|
lblFwUpdateInfo.Text = $@"Version: {version.Major}.{version.Minor}.{version.Build}";
|
|
_resetTimeMeasurement = true;
|
|
Init();
|
|
|
|
LogText($"MagFluxToolBox: {_version.Major}.{_version.Minor}.{_version.Build}");
|
|
LogText(StrSeparator);
|
|
}
|
|
|
|
private void FrmTest_FormClosing(Object sender, FormClosingEventArgs e)
|
|
{
|
|
_magFluxMeter.OnStreamingRecordDecoded -= StreamingCatcher_OnStreamingRecordDecoded;
|
|
_magFluxMeter?.Logout();
|
|
_magFluxMeter?.StopStreamingCatcher();
|
|
_meterBatch?.RemoveAllMeters();
|
|
_meterBatch?.Dispose();
|
|
|
|
Dispose();
|
|
}
|
|
|
|
private void cbComSlot_SelectedIndexChanged(Object sender, EventArgs e)
|
|
{
|
|
_resetTimeMeasurement = true;
|
|
Init();
|
|
}
|
|
#endregion
|
|
#region ActivationControls
|
|
|
|
private void SetControlDownloadsLocked()
|
|
{
|
|
btnConnect.Enabled = true;
|
|
}
|
|
|
|
private void SetControlsDownloadIsOngoing()
|
|
{
|
|
btnConnect.Enabled = false;
|
|
}
|
|
|
|
private void SetControlsDownloadIsFinished()
|
|
{
|
|
btnConnect.Enabled = true;
|
|
}
|
|
private void SetControlsDownloadEnabled()
|
|
{
|
|
btnConnect.Enabled = true;
|
|
}
|
|
#endregion
|
|
|
|
#region BoardControls
|
|
|
|
/// <summary>
|
|
/// Cyclic execution of catcher on incoming events.
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void StreamingCatcher_OnStreamingRecordDecoded(Object sender, MagFluxFlowDataEventArgs e)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
tbxSensorNo.Text = e.NewData.SensorId;
|
|
tbxVolume.Text = $@"{e.NewData.VolumeCm:F3}";
|
|
tbxFlowRate.Text = $@"{e.NewData.FlowRateCmPh:F3}";
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
private void Connect()
|
|
{
|
|
try
|
|
{
|
|
Init();
|
|
|
|
if (string.IsNullOrEmpty(cbComSlot.SelectedItem.ToString()) ||
|
|
!int.TryParse(cbComSlot.SelectedItem.ToString(), out var slotNr))
|
|
{
|
|
return;
|
|
}
|
|
|
|
//lblOverall.Text = @"Establish data base connection..";
|
|
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
ViewProgressPcb(true);
|
|
|
|
_magFluxMeter?.DisposeMeter();
|
|
//dispose old meter
|
|
_meterBatch.RemoveAllMeters();
|
|
_magFluxMeter = null;
|
|
Thread.Sleep(200);
|
|
|
|
//assign new meter and assign meter to FW update file if this exists
|
|
_magFluxMeter = new MagFluxMeter();
|
|
|
|
_magFluxMeter.SetupFromConfigFile(slotNr);
|
|
_meterBatch.AddMeter(_magFluxMeter);
|
|
|
|
_magFluxMeter.Configuration.UseRegisterWatchService = false;
|
|
_magFluxMeter.Configuration.UseMinMaxCheck = false;
|
|
_magFluxMeter.OnStreamingRecordDecoded += StreamingCatcher_OnStreamingRecordDecoded;
|
|
|
|
_meterBatch.MetersLogin();
|
|
|
|
//if (!_magFluxMeter.IsLoggedOn)
|
|
//{
|
|
// //this is needed from meter as an unsuccessfully login requires a delay for the next trial
|
|
// Thread.Sleep(2000);
|
|
// _meterBatch.MetersLogin();
|
|
//}
|
|
|
|
//if (!_magFluxMeter.IsLoggedOn)
|
|
//{
|
|
// return;
|
|
//}
|
|
|
|
Invoke(new Action(() =>
|
|
{
|
|
//lblConnectPcb.Text = StrPartPcbConnected;
|
|
//tbxPcbId.Text = _magFluxMeter.PcbId;
|
|
//tbxPcbId.Visible = true;
|
|
//tbxMeterFw.Text = _magFluxMeter.FwVersion;
|
|
//tbxMeterMeterSize.Text = _magFluxMeter.MeterSize;
|
|
//tbxMeterLutCrc.Text = _magFluxMeter.LutCrc;
|
|
//tbxRegion.Text = _magFluxMeter.Region;
|
|
//LogText("Meter FW Version: " + _magFluxMeter.FwVersion);
|
|
//LogText("Meter LUT CRC: " + _magFluxMeter.LutCrc);
|
|
//LogText("Meter Size: " + _magFluxMeter.MeterSize);
|
|
//LogText("Meter Region: " + _magFluxMeter.Region);
|
|
//if (_magFluxMeter.RadioFrequencyMhz != null)
|
|
//{
|
|
// tbxRadio.Text = $@"{_magFluxMeter.RadioFrequencyMhz}";
|
|
// LogText($"Meter radio frequency [MHz]: {_magFluxMeter.RadioFrequencyMhz}");
|
|
//}
|
|
|
|
lblConnectPcb.ForeColor = Color.Green;
|
|
CheckUpdateEnabled();
|
|
_magFluxMeter.StartStreamingCatcher();
|
|
_magFluxMeter.Logout();
|
|
LogInstalledMeterFw();
|
|
}));
|
|
}).ContinueWith(delegate
|
|
{
|
|
ViewProgressPcb(false);
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
LogErrorText(ex.Message);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region ProcessControls
|
|
private void ViewProgressPcb(Boolean isActive)
|
|
{
|
|
if (_updateGui)
|
|
{
|
|
if (isActive)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
_tryConnectPcb = true;
|
|
ProcessViewControl(true);
|
|
lblActualProcess.Text = StrConnecting;
|
|
lblOverall.Text = StrOverallProcess;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
_tryConnectPcb = false;
|
|
ProcessViewControl(false);
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// View all process bars and labels
|
|
/// </summary>
|
|
private void ProcessViewControl(Boolean view)
|
|
{
|
|
if (view)
|
|
{
|
|
lblActualProcess.Visible = true;
|
|
lblOverall.Visible = true;
|
|
barOverallProgressUpdate.Visible = true;
|
|
barSingleProgressUpdate.Visible = true;
|
|
_lastFwUpdateState = "";
|
|
tmrProgressUpdate.Enabled = true;
|
|
}
|
|
else
|
|
{
|
|
lblActualProcess.Visible = false;
|
|
lblOverall.Visible = false;
|
|
barOverallProgressUpdate.Visible = false;
|
|
barSingleProgressUpdate.Visible = false;
|
|
tmrProgressUpdate.Enabled = false;
|
|
_lastFwUpdateState = "";
|
|
lblWaitingForMeterResponse.Visible = false;
|
|
lblActualProcess.Text = "";
|
|
lblOverall.Text = "";
|
|
}
|
|
|
|
//common actions and settings
|
|
lblActualProcess.Update();
|
|
lblOverall.Update();
|
|
barOverallProgressUpdate.Value = 0;
|
|
barOverallProgressUpdate.Update();
|
|
barSingleProgressUpdate.Value = 0;
|
|
barSingleProgressUpdate.Update();
|
|
Update();
|
|
}
|
|
|
|
private void StartStopDownload(Boolean isActive)
|
|
{
|
|
if (isActive)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
ProcessViewControl(true);
|
|
tmrProgressUpdate.Enabled = true;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
ProcessViewControl(false);
|
|
SetControlsDownloadIsFinished();
|
|
|
|
//// update the display of the meter size, LUT CRC and region
|
|
//tbxMeterFw.Text = _magFluxMeter.FwVersion;
|
|
//tbxMeterMeterSize.Text = _magFluxMeter.MeterSize;
|
|
//tbxMeterLutCrc.Text = _magFluxMeter.LutCrc;
|
|
//tbxRegion.Text = _magFluxMeter.Region;
|
|
|
|
//if (!_lutFileSuccessfulWritten)
|
|
//{
|
|
// lblLutFileLoadedInfo.ForeColor = Color.Red;
|
|
// lblLutFileLoadedInfo.Text = StrUpdateErrorMessage;
|
|
// var txt = StrUpdateErrorMessage;
|
|
// if (!string.IsNullOrEmpty(_meterLutUpdate?.ErrorMessage))
|
|
// {
|
|
// txt += "\n";
|
|
// txt += _meterLutUpdate?.ErrorMessage;
|
|
// }
|
|
|
|
// LogErrorText(txt);
|
|
// MessageBoxShow(txt, @"FAILED",
|
|
// MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
//}
|
|
//else
|
|
//{
|
|
// LogSuccessText(StrUpdateSuccessMessage);
|
|
// MessageBoxShow(StrUpdateSuccessMessage, @"SUCCESS",
|
|
// MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
//}
|
|
}));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Display installed meter FW.
|
|
/// </summary>
|
|
/// <remarks date="2022-Mar-25" author="Thomas Wiedebusch">
|
|
/// - Init.
|
|
/// </remarks>
|
|
private void LogInstalledMeterFw()
|
|
{
|
|
//if (_magFluxMeter == null)
|
|
// return;
|
|
//LogText(StrSeparator);
|
|
//LogText($"PCB ID: {_magFluxMeter.PcbId}");
|
|
//LogText($"Core Version: {GenesisMeter.BuildFwVersionString(_magFluxMeter.CoreRevision)}");
|
|
//LogText("");
|
|
//LogText("Installed Applications:");
|
|
|
|
//foreach (var app in _magFluxMeter.MeterAppListVersion)
|
|
//{
|
|
// var versionString = app.IsInstalled ? $"V: {app.StrVersion} - CRC: 0x{app.Crc:X4}" :
|
|
// "App NOT Installed";
|
|
// LogText($"AppId: 0x{app.AppId:X2} - {versionString} - " + $"AppName: {app.AppName}");
|
|
|
|
// if (app.IsInstalled)
|
|
// {
|
|
// var appVersion = new CordonelAppVersion(app.AppId, app.StrVersion);
|
|
|
|
// // Copy metrology update permission
|
|
// if (app.AppName == MeterFwUpdateCapability.MetrologyName &&
|
|
// _magFluxMeter.MetrologyUpgradePermission != MeterFwUpdateCapability.MetrologyUpgradePermitted)
|
|
// {
|
|
// appVersion.IsUpdateable = false;
|
|
// }
|
|
// }
|
|
//}
|
|
//LogText(StrSeparator);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Output exclusively to user update remarks text window.
|
|
/// </summary>
|
|
/// <remarks date="2021-Apr-26" author="Thomas Wiedebusch">
|
|
/// - Color added.
|
|
/// </remarks>
|
|
private void LogText(String txtHistory)
|
|
{
|
|
InfoWindowColoredText(txtHistory, ColorDefault);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Output exclusively to user update remarks text window.
|
|
/// </summary>
|
|
private void LogErrorText(String txtHistory)
|
|
{
|
|
InfoWindowColoredText(txtHistory, ColorProcessFailed);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Output exclusively to user update remarks text window.
|
|
/// </summary>
|
|
private void LogSuccessText(String txtHistory)
|
|
{
|
|
InfoWindowColoredText(txtHistory, ColorSuccess);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Output exclusively to user update remarks text window.
|
|
/// </summary>
|
|
private void InfoWindowColoredText(String txtHistory, Color color)
|
|
{
|
|
//if (_frmHistory?.rtbHistory == null)
|
|
// return;
|
|
//Invoke(new Action(() =>
|
|
//{
|
|
// _frmHistory.rtbHistory.SuspendLayout();
|
|
// _frmHistory.rtbHistory.SelectionStart = _frmHistory.rtbHistory.Text.Length;
|
|
// _frmHistory.rtbHistory.SelectionLength = 0;
|
|
// _frmHistory.rtbHistory.SelectionColor = color;
|
|
// _frmHistory.rtbHistory.AppendText($"{txtHistory}{Environment.NewLine}");
|
|
// _frmHistory.rtbHistory.SelectionColor = _frmHistory.rtbHistory.ForeColor;
|
|
// _frmHistory.rtbHistory.ScrollToCaret();
|
|
// _frmHistory.rtbHistory.ResumeLayout();
|
|
//}));
|
|
}
|
|
#endregion
|
|
|
|
#region Checks
|
|
private void CheckUpdateEnabled()
|
|
{
|
|
//this check has been placed here to force display update
|
|
if (_magFluxMeter != null )
|
|
{
|
|
SetControlsDownloadEnabled();
|
|
return;
|
|
}
|
|
SetControlDownloadsLocked();
|
|
}
|
|
#endregion
|
|
|
|
#region Events
|
|
|
|
/// <summary>
|
|
/// Process update event for displaying messages in history window
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
/// <remarks date="2020-Dec-10" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
public virtual void ProcessUpdate_Event(Object sender, ProcessExecEventArgs e)
|
|
{
|
|
LogText(e.OverallProcessMessage);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region TimerControls
|
|
private void tmrProgressUpdate_Tick(Object sender, EventArgs e)
|
|
{
|
|
//cbxManualControl.Visible = true;
|
|
if (_tryConnectPcb)
|
|
{
|
|
barOverallProgressUpdate.Value = barOverallProgressUpdate.Value + 1 > 100 ? 0 :
|
|
barOverallProgressUpdate.Value + 1;
|
|
barSingleProgressUpdate.Value = barSingleProgressUpdate.Value + 4 > 100 ? 0 :
|
|
barSingleProgressUpdate.Value + 4;
|
|
lblOverall.Text = StrOverallProcess;
|
|
}
|
|
else
|
|
{
|
|
//lblOverall.Text = _meterLutUpdate?.GetLutUpdateStateOperation();
|
|
//lblActualProcess.Text = _meterLutUpdate?.GetActualOperation();
|
|
|
|
//var overallProgress = (Int32)(_meterLutUpdate?.OverallProcessCtrPercent ?? 0);
|
|
//if (overallProgress < 0)
|
|
// overallProgress = 0;
|
|
//barOverallProgressUpdate.Value = overallProgress > 100 ? 100 : overallProgress;
|
|
//barOverallProgressUpdate.Update();
|
|
|
|
//var singleProgress = (Int32)(_meterLutUpdate?.SingleFileProcessCtrPercent ?? 0);
|
|
//if (_meterLutUpdate?.GetActualOperation() != _lastFwUpdateState)
|
|
//{
|
|
// _lastFwUpdateState = _meterLutUpdate?.GetActualOperation();
|
|
// lblWaitingForMeterResponse.Visible = false;
|
|
//}
|
|
//else
|
|
//{
|
|
// //if the communication gets frozen
|
|
// lblWaitingForMeterResponse.Visible = _lastSingleProgress == singleProgress;
|
|
//}
|
|
|
|
//barSingleProgressUpdate.Value = singleProgress > 100 ? 100 : singleProgress;
|
|
//_lastSingleProgress = singleProgress;
|
|
//barSingleProgressUpdate.Update();
|
|
}
|
|
SetTimeDisplay();
|
|
Update();
|
|
}
|
|
|
|
private void SetTimeDisplay()
|
|
{
|
|
var time = DateTimeOffset.UtcNow;
|
|
var timeSpan = time - _startTime;
|
|
lblUpdateTime.Text = $@"{(UInt32)timeSpan.TotalMinutes}:{timeSpan.Seconds:00}";
|
|
}
|
|
#endregion
|
|
#region Buttons
|
|
private void btnConnect_Click(Object sender, EventArgs e)
|
|
{
|
|
if (_resetTimeMeasurement)
|
|
{
|
|
_startTime = DateTimeOffset.UtcNow;
|
|
_resetTimeMeasurement = false;
|
|
}
|
|
Connect();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Download LUT file
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnDownloadLutFile_Click(Object sender, EventArgs e)
|
|
{
|
|
if (_magFluxMeter == null )
|
|
{
|
|
return;
|
|
}
|
|
|
|
SetControlsDownloadIsOngoing();
|
|
StartStopDownload(true);
|
|
|
|
_startTime = DateTimeOffset.UtcNow;
|
|
_resetTimeMeasurement = false;
|
|
}
|
|
|
|
#endregion
|
|
#region ExternalCalls
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public MsgEventArgs LastMsgEvent;
|
|
|
|
/// <inheritdoc />
|
|
public class MsgEventArgs : EventArgs
|
|
{
|
|
/// <summary>
|
|
/// Text to display
|
|
/// </summary>
|
|
public String Text;
|
|
/// <summary>
|
|
/// Headline
|
|
/// </summary>
|
|
public String Caption;
|
|
/// <summary>
|
|
/// Buttons
|
|
/// </summary>
|
|
public MessageBoxButtons Buttons;
|
|
/// <summary>
|
|
/// Icon
|
|
/// </summary>
|
|
public MessageBoxIcon Icon;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Message pop up event handler
|
|
/// </summary>
|
|
public event EventHandler<MsgEventArgs> OnMsgPopUp;
|
|
private void MessageBoxShow(String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon)
|
|
{
|
|
if (_updateGui)
|
|
{
|
|
MessageBox.Show(text, caption, buttons, icon);
|
|
}
|
|
else
|
|
{
|
|
LastMsgEvent = new MsgEventArgs() { Text = text, Caption = caption, Buttons = buttons, Icon = icon };
|
|
OnMsgPopUp?.Invoke(this, LastMsgEvent);
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|
|
|