common/Ui/LegacyGenesisControl/TestForm.cs
2026-04-23 17:50:07 +02:00

391 lines
12 KiB
C#

using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Xylem.Common.Hardware.Interface.Com;
using Xylem.Common.Hardware.WaterMeter;
using Xylem.Common.Hardware.WaterMeter.Genesis;
using Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages;
using Xylem.Common.Utils.Logging;
namespace LegacyGenesisControl
{
public partial class TestForm : Form
{
private static readonly Lazy<ILogger> Logger = new Lazy<ILogger>(() =>
{
return NLogHelper.CreateOrGetLogger("GenesisToolBox");
});
MeterBatch mb = new MeterBatch();
DataTable dt = new DataTable();
private DateTime? startTime = null;
private DateTime? endTime = null;
public bool isMeasurement { get; private set; }
public bool isSlotSelect { get; private set; }
public bool isInit { get; private set; }
public bool isConnected { get; private set; }
public bool isMeasurementStarted { get; private set; }
public bool isCalibrationCalculated { get; private set; }
public bool IsBusy { get; private set; }
#region bussy
private void setBussy(bool val, string action = "")
{
IsBusy = val;
if (IsBusy)
{
Invoke(new Action(() => { }));
}
else
{
Invoke(new Action(() => { }));
}
}
private void setProgress(string text)
{
Invoke(new Action(() => { }));
}
#endregion
private void stateChanged()
{
Invoke(new Action(() =>
{
btnInit.Enabled = isSlotSelect;
btnLogin.Enabled = isInit;
btnMeasurementPreparation.Enabled = isConnected;
btnMeasurementStart.Enabled = isConnected && !isMeasurementStarted;
btnMeasurementStop.Enabled = isMeasurementStarted;
cblSlots.Enabled = !isInit;
btnDisconnect.Enabled = isInit;
rbFlowCalibration.Enabled = !isInit;
rbMeasurement.Enabled = !isInit;
}));
}
public TestForm()
{
InitializeComponent();
var configfile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(Xylem.Common.Hardware.WaterMeter.Genesis), Const.SerialConfigFileName);
if (!File.Exists(configfile))
{
throw new ApplicationException($"Configuration file {configfile} not found ");
}
var tr = new StreamReader(configfile);
var meterConfigList = JsonConvert.DeserializeObject<RootComPortConfigStore[]>(tr.ReadToEnd());
cblSlots.Items.Clear();
foreach (var item in meterConfigList)
{
cblSlots.Items.Add(item.Slot);
}
}
private void TestForm_Load(object sender, EventArgs e)
{
}
private void btnInit_Click(object sender, EventArgs e)
{
if (rbMeasurement.Checked)
{
isMeasurement = true;
}
else
{
isMeasurement = false;
}
setBussy(true, "Init");
Task.Factory.StartNew(() => { init(); }).ContinueWith(
delegate
{
Invoke(new Action(() =>
{
setBussy(false);
isInit = true;
isCalibrationCalculated = false;
stateChanged();
}));
});
}
private void btnLogin_Click(object sender, EventArgs e)
{
setBussy(true, "Connect");
Task.Factory.StartNew(() => { conntect(); }).ContinueWith(
delegate
{
Invoke(new Action(() =>
{
setBussy(false);
isConnected = true;
stateChanged();
}));
});
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
setBussy(true, "Disconnect");
Task.Factory.StartNew(() => { disconnect(); }).ContinueWith(
delegate
{
Invoke(new Action(() =>
{
setBussy(false);
isInit = false;
isCalibrationCalculated = false;
isConnected = false;
stateChanged();
}));
});
}
private void btnMeasurementPreparation_Click(object sender, EventArgs e)
{
setBussy(true, "Preparation");
Task.Factory.StartNew(() => { Preparation(); }).ContinueWith(
delegate
{
Invoke(new Action(() =>
{
setBussy(false);
}));
});
}
private void btnMeasurementStart_Click(object sender, EventArgs e)
{
if (isMeasurement)
{
setBussy(true, "Start Measurement");
}
else
{
setBussy(true, "Start Calibration");
}
startTime = DateTime.Now;
endTime = null;
Task.Factory.StartNew(() => { StartMeasurement(); }).ContinueWith(
delegate
{
Invoke(new Action(() =>
{
})); setBussy(false);
isMeasurementStarted = true;
stateChanged();
});
}
private void btnUpdate_Click(object sender, EventArgs e)
{
var flowrateRef = 0.23;
if (!double.TryParse(textBox1.Text, out flowrateRef))
{
var rnd = new Random(1);
flowrateRef = flowrateRef + (rnd.NextDouble() / 100);
}
controlBatch.SetRefFlowRate(flowrateRef);
}
private void btnMeasurementStop_Click(object sender, EventArgs e)
{
if (isMeasurement)
{
setBussy(true, "Stop Measurement and wait for result");
}
else
{
setBussy(true, "Stop Calibration and wait for result");
}
endTime = DateTime.Now;
Task.Factory.StartNew(() => { StoptMeasurement(); })
.ContinueWith(
delegate
{
Invoke(new Action(() =>
{
setBussy(false);
isMeasurementStarted = false;
stateChanged();
if (endTime.HasValue && startTime.HasValue)
{
var timeMeasurement = (endTime.Value - startTime.Value);
MessageBox.Show($"MeasurementBuffered rund for {timeMeasurement}S");
}
}));
});
}
private void cblSlots_SelectedIndexChanged(object sender, EventArgs e)
{
cblSlots.SetItemChecked(cblSlots.SelectedIndex, !cblSlots.GetItemChecked(cblSlots.SelectedIndex));
isSlotSelect = cblSlots.CheckedItems.Count >= 1;
stateChanged();
}
private void cblSlots_ItemCheck(object sender, ItemCheckEventArgs e)
{
isSlotSelect = cblSlots.CheckedItems.Count >= 1;
stateChanged();
}
private void init()
{
try
{
if (mb != null && mb.ListOfMeters.Any())
{
disconnect();
}
mb = new MeterBatch();
foreach (var item in cblSlots.CheckedItems)
{
int slotNr = 0;
if (int.TryParse(item.ToString(), out slotNr))
{
var currentGenesis = new GenesisMeter();
currentGenesis.SetupFromConfigFile(slotNr);
mb.AddMeter(currentGenesis);
currentGenesis.LogRawData(true);
}
}
}
catch (Exception ex)
{
Logger.Value.Error(ex, ex.Message);
}
}
private void conntect()
{
try
{
mb.MetersLogin();
}
catch (Exception ex)
{
Logger.Value.Error(ex, ex.Message);
}
}
private void disconnect()
{
try
{
mb.RemoveAllMeters();
}
catch (Exception ex)
{
Logger.Value.Error(ex, ex.Message);
}
}
private void Preparation()
{
try
{
if (isMeasurement)
{
mb.MetersInitMeasurement();
}
else
{
mb.MetersInitCalibration();
}
}
catch (Exception ex)
{
Logger.Value.Error(ex, ex.Message);
}
}
private void StartMeasurement()
{
try
{
if (isMeasurement)
{
mb.MetersStartMeasurement();
}
else
{
mb.MetersStartCalibration();
}
}
catch (Exception ex)
{
Logger.Value.Error(ex, ex.Message);
}
}
private void StoptMeasurement()
{
try
{
if (isMeasurement)
{
mb.MetersStopMeasurement();
}
else
{
mb.MetersStopCalibration();
}
}
catch (Exception ex)
{
Logger.Value.Error(ex, ex.Message);
}
}
ctlBatch controlBatch;
private void button1_Click(object sender, EventArgs e)
{
controlBatch = new ctlBatch();
this.Controls.Add(controlBatch);
controlBatch.Location = new System.Drawing.Point(0, 200);
this.Size = new Size(controlBatch.Size.Width, controlBatch.Size.Height + 300);
controlBatch.MeterDn = "1";
controlBatch.RefPulseValence = "2";
controlBatch.PredictedQFlow = "3 m³/h";
controlBatch.PredictedTs = "4";
controlBatch.RefError = "5%";
controlBatch.CurrentPulse = "6";
controlBatch.CurrentRefVolume = "7³";
controlBatch.RemainingPulse = "8";
controlBatch.Period = "9";
controlBatch.RefFlow = "10,5m³/h";
controlBatch.RefFlowCorrected = "11,5m³/h";
controlBatch.batch = mb;
controlBatch.Prepare();
}
}
}