180 lines
5.4 KiB
C#
180 lines
5.4 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using GenesisCordonelInterface.API;
|
|
|
|
namespace GenesisCordonelInterface.UI.StaraTuraAPI_GenesisCordonelInterface
|
|
{
|
|
public partial class MetersActionView : UserControl
|
|
{
|
|
private readonly InterfaceOutsideToGCI _api;
|
|
|
|
public MetersActionView(InterfaceOutsideToGCI api)
|
|
{
|
|
_api = api ?? throw new ArgumentNullException(nameof(api));
|
|
InitializeComponent();
|
|
}
|
|
|
|
private async Task ExecuteApiActionAsync(Func<Task> action)
|
|
{
|
|
try
|
|
{
|
|
SetBusy(true);
|
|
await action();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "API call failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
finally
|
|
{
|
|
SetBusy(false);
|
|
}
|
|
}
|
|
|
|
private void SetBusy(bool busy)
|
|
{
|
|
Cursor = busy ? Cursors.WaitCursor : Cursors.Default;
|
|
|
|
btnConnectSelected.Enabled = !busy;
|
|
btnReadRegister.Enabled = !busy;
|
|
btnWriteRegister.Enabled = !busy;
|
|
btnSetPassword.Enabled = !busy;
|
|
btnDisconnectSelected.Enabled = !busy;
|
|
}
|
|
|
|
private object ParseValue(string input)
|
|
{
|
|
if (int.TryParse(input, out int i)) return i;
|
|
if (uint.TryParse(input, out uint ui)) return ui;
|
|
if (bool.TryParse(input, out bool b)) return b;
|
|
|
|
return input;
|
|
}
|
|
|
|
private void ValidateSelectedSlots()
|
|
{
|
|
if (_api.GetSelectedSlots().Count == 0)
|
|
throw new Exception("No slots selected.");
|
|
}
|
|
|
|
private async void btnConnectSelected_Click(object sender, EventArgs e)
|
|
{
|
|
await ExecuteApiActionAsync(async () =>
|
|
{
|
|
ValidateSelectedSlots();
|
|
|
|
foreach (int slot in _api.GetSelectedSlots())
|
|
{
|
|
var result = await _api.ConnectOneSlotAsync(slot);
|
|
|
|
if (!result.Success)
|
|
throw new Exception($"Slot {slot}: {result.Message}");
|
|
}
|
|
|
|
MessageBox.Show("Selected meters connected.");
|
|
});
|
|
}
|
|
|
|
private async void btnReadRegister_Click(object sender, EventArgs e)
|
|
{
|
|
string registerName = txtRegister.Text;
|
|
|
|
if (string.IsNullOrWhiteSpace(registerName))
|
|
{
|
|
MessageBox.Show("Register name is empty.", "Invalid input");
|
|
return;
|
|
}
|
|
|
|
await ExecuteApiActionAsync(async () =>
|
|
{
|
|
ValidateSelectedSlots();
|
|
|
|
foreach (int slot in _api.GetSelectedSlots())
|
|
{
|
|
var result = await _api.ReadRegisterAsync(slot, registerName);
|
|
|
|
if (!result.Success)
|
|
throw new Exception($"Slot {slot}: {result.ErrorMessage}");
|
|
}
|
|
|
|
MessageBox.Show("Read register finished.");
|
|
});
|
|
}
|
|
|
|
private async void btnWriteRegister_Click(object sender, EventArgs e)
|
|
{
|
|
string registerName = txtRegister.Text;
|
|
string valueText = txtValue.Text;
|
|
|
|
if (string.IsNullOrWhiteSpace(registerName))
|
|
{
|
|
MessageBox.Show("Register name is empty.", "Invalid input");
|
|
return;
|
|
}
|
|
|
|
await ExecuteApiActionAsync(async () =>
|
|
{
|
|
ValidateSelectedSlots();
|
|
|
|
object value = ParseValue(valueText);
|
|
|
|
foreach (int slot in _api.GetSelectedSlots())
|
|
{
|
|
var result = await _api.WriteRegisterAsync(
|
|
slot,
|
|
registerName,
|
|
value,
|
|
false,
|
|
false);
|
|
|
|
if (!result.Success)
|
|
throw new Exception($"Slot {slot}: {result.ErrorMessage}");
|
|
}
|
|
|
|
MessageBox.Show("Write register finished.");
|
|
});
|
|
}
|
|
|
|
private async void btnSetPassword_Click(object sender, EventArgs e)
|
|
{
|
|
string password = txtPassword.Text;
|
|
|
|
if (string.IsNullOrWhiteSpace(password))
|
|
{
|
|
MessageBox.Show("Password is empty.", "Invalid input");
|
|
return;
|
|
}
|
|
|
|
await ExecuteApiActionAsync(async () =>
|
|
{
|
|
ValidateSelectedSlots();
|
|
|
|
foreach (int slot in _api.GetSelectedSlots())
|
|
{
|
|
PublicModels.GciSetPasswordResult result = await _api.SetMeterPasswordAsync(slot, password);
|
|
|
|
if (!result.Success)
|
|
throw new Exception($"Slot {slot}: Set password failed.");
|
|
}
|
|
|
|
MessageBox.Show("Password set for selected meters.");
|
|
});
|
|
}
|
|
|
|
private async void btnDisconnectSelected_Click(object sender, EventArgs e)
|
|
{
|
|
await ExecuteApiActionAsync(async () =>
|
|
{
|
|
ValidateSelectedSlots();
|
|
|
|
foreach (int slot in _api.GetSelectedSlots())
|
|
{
|
|
await _api.DisconnectAsync(slot);
|
|
}
|
|
|
|
MessageBox.Show("Selected meters disconnected.");
|
|
});
|
|
}
|
|
}
|
|
} |