tbf/GenesisCordonelTester/UI/StaraTuraAPI_GenesisCordonelInterface/FrmGCIAPI.cs

193 lines
5.6 KiB
C#

using System;
using System.Windows.Forms;
using GenesisCordonelInterface.API;
using Xylem.Common.Hardware.Interfaces.Ports.PortCore;
using static Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore.GenesisMeter;
namespace GenesisCordonelInterface.UI.StaraTuraAPI_GenesisCordonelInterface
{
public partial class FrmGCIAPI : Form
{
private readonly InterfaceOutsideToGCI _api = new InterfaceOutsideToGCI();
public FrmGCIAPI()
{
InitializeComponent();
InitializeDefaults();
}
private void InitializeDefaults()
{
cmbConfigSource.DataSource = Enum.GetValues(typeof(ConfigSource));
cmbPasswordSource.DataSource = Enum.GetValues(typeof(PasswordSource));
cmbConfigSource.SelectedItem = ConfigSource.ExternStorage;
cmbPasswordSource.SelectedItem = PasswordSource.OfflineFile;
}
private void ExecuteApiAction(Action action)
{
try
{
action();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "API call failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private int GetSlot()
{
if (!int.TryParse(txtSlot.Text, out int slot))
throw new Exception("Invalid slot number.");
return slot;
}
private object ParseValue(string input)
{
if (int.TryParse(input, out int intValue))
return intValue;
if (uint.TryParse(input, out uint uintValue))
return uintValue;
if (bool.TryParse(input, out bool boolValue))
return boolValue;
return input;
}
private PortConfig? CreatePortConfig(string portName, string baudRateText)
{
if (string.IsNullOrWhiteSpace(portName))
return null;
if (!int.TryParse(baudRateText, out int baudRate))
throw new Exception($"Invalid baud rate for port {portName}.");
var cfg = new PortConfig
{
PortName = portName,
Type = "Serial"
};
var sp = cfg.GetSerialPort();
if (sp == null)
throw new Exception($"Serial port object was not created for {portName}.");
sp.BaudRate = baudRate;
return cfg;
}
private ConfigSource GetConfigSource()
{
if (cmbConfigSource.SelectedItem == null)
throw new Exception("ConfigSource is not selected.");
return (ConfigSource)cmbConfigSource.SelectedItem;
}
private PasswordSource GetPasswordSource()
{
if (cmbPasswordSource.SelectedItem == null)
throw new Exception("PasswordSource is not selected.");
return (PasswordSource)cmbPasswordSource.SelectedItem;
}
private void btnInit_Click(object sender, EventArgs e)
{
ExecuteApiAction(() =>
{
int slot = GetSlot();
var requestPort = CreatePortConfig(txtRequestPort.Text, txtRequestBaudRate.Text);
var streamingPort = CreatePortConfig(txtStreamingPort.Text, txtStreamingBaudRate.Text);
_api.InitOneMeterFromExtern(
slot,
GetConfigSource(),
GetPasswordSource(),
requestPort,
streamingPort);
});
}
private void btnConnectOne_Click(object sender, EventArgs e)
{
ExecuteApiAction(() =>
{
_api.ConnectOneMeter(GetSlot());
});
}
private void btnConnectAll_Click(object sender, EventArgs e)
{
ExecuteApiAction(() =>
{
_api.ConnectAllMeters(GetSlot());
});
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
ExecuteApiAction(() =>
{
_api.Disconnect();
});
}
private void btnGetPcbId_Click(object sender, EventArgs e)
{
ExecuteApiAction(() =>
{
_api.GetPcbId(GetSlot());
});
}
private void btnReadRegister_Click(object sender, EventArgs e)
{
ExecuteApiAction(() =>
{
string registerName = txtRegister.Text;
if (string.IsNullOrWhiteSpace(registerName))
throw new Exception("Register name is empty.");
_api.ReadRegister(registerName);
});
}
private void btnWriteRegister_Click(object sender, EventArgs e)
{
ExecuteApiAction(() =>
{
string registerName = txtRegister.Text;
string valueText = txtValue.Text;
if (string.IsNullOrWhiteSpace(registerName))
throw new Exception("Register name is empty.");
object value = ParseValue(valueText);
_api.WriteRegister(registerName, value, false, false);
});
}
private void btnSetPassword_Click(object sender, EventArgs e)
{
ExecuteApiAction(() =>
{
string password = txtPassword.Text;
if (string.IsNullOrWhiteSpace(password))
throw new Exception("Password is empty.");
_api.SetMeterPassword(password);
});
}
}
}