104 lines
2.8 KiB
C#
104 lines
2.8 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace TBF.Rig.BridgeComponents.GciBridge.UI.StaraTuraAPI_GciBridge
|
|
{
|
|
public partial class UniDataSorageActionsView : UserControl
|
|
{
|
|
private readonly MainView _mainView;
|
|
private readonly GciBridge _bridge;
|
|
private CancellationTokenSource _cts;
|
|
|
|
public UniDataSorageActionsView(MainView mainView, GciBridge bridge)
|
|
{
|
|
_mainView = mainView ?? throw new ArgumentNullException(nameof(mainView));
|
|
_bridge = bridge ?? throw new ArgumentNullException(nameof(bridge));
|
|
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void btnGetPasswordByPcb_Click(object sender, EventArgs e)
|
|
{
|
|
ExecuteAsync(async token =>
|
|
{
|
|
string pcbId = txtPcbId.Text.Trim();
|
|
|
|
if (string.IsNullOrWhiteSpace(pcbId))
|
|
throw new Exception("PCB ID is empty.");
|
|
|
|
var result = await _bridge.GetPasswordAsync(pcbId, token);
|
|
|
|
LogResult("GetPasswordAsync PCB=" + pcbId, result);
|
|
});
|
|
}
|
|
|
|
private void btnCancel_Click(object sender, EventArgs e)
|
|
{
|
|
if (_cts != null)
|
|
_cts.Cancel();
|
|
|
|
Log("Cancel requested.");
|
|
}
|
|
|
|
private async void ExecuteAsync(Func<CancellationToken, Task> action)
|
|
{
|
|
try
|
|
{
|
|
SetBusy(true);
|
|
|
|
_cts = new CancellationTokenSource();
|
|
|
|
await action(_cts.Token);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
Log("Operation canceled.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log("ERROR: " + ex);
|
|
|
|
MessageBox.Show(
|
|
ex.Message,
|
|
"Storage API call failed",
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
finally
|
|
{
|
|
if (_cts != null)
|
|
{
|
|
_cts.Dispose();
|
|
_cts = null;
|
|
}
|
|
|
|
SetBusy(false);
|
|
}
|
|
}
|
|
|
|
private void SetBusy(bool busy)
|
|
{
|
|
Cursor = busy ? Cursors.WaitCursor : Cursors.Default;
|
|
|
|
btnGetPasswordByPcb.Enabled = !busy;
|
|
btnCancel.Enabled = busy;
|
|
}
|
|
|
|
private void LogResult(string methodName, object result)
|
|
{
|
|
Log(methodName + " result:");
|
|
Log(result == null ? "<null>" : result.ToString());
|
|
}
|
|
|
|
private void Log(string message)
|
|
{
|
|
txtLog.AppendText(
|
|
DateTime.Now.ToString("HH:mm:ss.fff") +
|
|
" " +
|
|
message +
|
|
Environment.NewLine);
|
|
}
|
|
}
|
|
} |