338 lines
10 KiB
C#
338 lines
10 KiB
C#
using CordonelPreadjustmentUi;
|
|
using CordonelPreadjustmentUi.Processes.Itinerary;
|
|
using GenesisCordonelInterface.API;
|
|
using GenesisCordonelInterface.UI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore;
|
|
using Xylem.Common.Ui.CordonelPreadjustmentUi;
|
|
using static Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore.PreadjustmentMeter;
|
|
using GciPublicModels = GenesisCordonelInterface.API.PublicModels;
|
|
|
|
namespace TBF.Rig.BridgeComponents.GciBridge.UI.StaraTuraAPI_GciBridge
|
|
{
|
|
public partial class PreadjustmentActionsView : UserControl
|
|
{
|
|
private readonly GciBridge _bridge;
|
|
private readonly MainView _mainView;
|
|
private CancellationTokenSource _cts;
|
|
private readonly Action _addSlotAction;
|
|
private readonly Action _saveAction;
|
|
|
|
public PreadjustmentActionsView(
|
|
MainView mainview,
|
|
GciBridge bridge,
|
|
Action addSlotAction,
|
|
Action saveAction)
|
|
{
|
|
_mainView = mainview ?? throw new ArgumentNullException(nameof(mainview));
|
|
_bridge = bridge ?? throw new ArgumentNullException(nameof(bridge));
|
|
|
|
_addSlotAction = addSlotAction;
|
|
_saveAction = saveAction;
|
|
|
|
InitializeComponent();
|
|
|
|
LoadRegisterComboBoxes();
|
|
}
|
|
|
|
private void AddSlot()
|
|
{
|
|
_addSlotAction?.Invoke();
|
|
}
|
|
|
|
private void SaveSlots()
|
|
{
|
|
_saveAction?.Invoke();
|
|
}
|
|
|
|
private List<GciPublicModels.MeterBatchDebugStatus> GetSelectedSlots()
|
|
{
|
|
if (_mainView == null || _mainView._batchPanel == null)
|
|
throw new Exception("Meter batch grid is not available.");
|
|
|
|
var slots = _mainView._batchPanel.GetSelectedGridData();
|
|
|
|
if (slots.Count == 0)
|
|
throw new Exception("No selected slots in grid.");
|
|
|
|
return slots;
|
|
}
|
|
|
|
private void RefreshGrid(int? removedSlot = null)
|
|
{
|
|
if (removedSlot.HasValue)
|
|
{
|
|
_mainView?._gciApi.SetSlotSelected(removedSlot.Value, false);
|
|
_mainView?._batchPanel?.RemoveSlotRow(removedSlot.Value);
|
|
}
|
|
|
|
if (_bridge?.gciExternalInterface != null)
|
|
_bridge.gciExternalInterface.RaiseMeterBatchStatusChanged();
|
|
}
|
|
|
|
|
|
private void btnCancel_Click(object sender, EventArgs e)
|
|
{
|
|
_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,
|
|
"GciBridge API call failed",
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
finally
|
|
{
|
|
_cts?.Dispose();
|
|
_cts = null;
|
|
|
|
SetBusy(false);
|
|
}
|
|
}
|
|
|
|
private void SetBusy(bool busy)
|
|
{
|
|
Cursor = busy ? Cursors.WaitCursor : Cursors.Default;
|
|
|
|
preparationActionButton.Enabled = !busy;
|
|
btnUpdateSlot.Enabled = !busy;
|
|
detectActionButton.Enabled = !busy;
|
|
btnCleanAllSlots.Enabled = !busy;
|
|
btnGetPcbId.Enabled = !busy;
|
|
btnConnect.Enabled = !busy;
|
|
btnDisconnect.Enabled = !busy;
|
|
readRegisterButton.Enabled = !busy;
|
|
writeRegisterButton.Enabled = !busy;
|
|
readRegisterNameComboBox.Enabled = !busy;
|
|
writeRegisterNameComboBox.Enabled = !busy;
|
|
writeRegisterValueTextBox.Enabled = !busy;
|
|
btnLogin.Enabled = !busy;
|
|
btnCleanSlot.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);
|
|
}
|
|
|
|
private void LoadRegisterComboBoxes()
|
|
{
|
|
var registers = _bridge.GetAllRegisterNames();
|
|
|
|
readRegisterNameComboBox.Items.Clear();
|
|
writeRegisterNameComboBox.Items.Clear();
|
|
|
|
readRegisterNameComboBox.Items.AddRange(registers.ToArray());
|
|
writeRegisterNameComboBox.Items.AddRange(registers.ToArray());
|
|
}
|
|
|
|
private void grpSlots_Enter(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void preparationActionButton_Click(
|
|
object sender,
|
|
EventArgs e)
|
|
{
|
|
ExecuteAsync(async token =>
|
|
{
|
|
var selectedSlots =
|
|
_mainView._batchPanel.GetSelectedGridData();
|
|
|
|
if (!selectedSlots.Any())
|
|
{
|
|
Log("No selected slots.");
|
|
return;
|
|
}
|
|
|
|
var pp = CreatePreparationProgress();
|
|
|
|
bool success = await _bridge.Preadjustment_PreparationAsync(
|
|
pp,
|
|
token);
|
|
|
|
Log(
|
|
success
|
|
? "Preparation completed."
|
|
: "Preparation failed.");
|
|
});
|
|
}
|
|
|
|
private void detectActionButton_Click(
|
|
object sender,
|
|
EventArgs e)
|
|
{
|
|
ExecuteAsync(async token =>
|
|
{
|
|
var selectedSlots =
|
|
_mainView._batchPanel.GetSelectedGridData();
|
|
|
|
if (!selectedSlots.Any())
|
|
{
|
|
Log("Detect: no selected slots.");
|
|
return;
|
|
}
|
|
|
|
_mainView._laatzenApi._settings = CreatePreAdjustmentSettings(selectedSlots);
|
|
|
|
_mainView._laatzenApi._meterControls = CreateMeterControls(selectedSlots);
|
|
|
|
bool success = await _bridge.Preadjustment_DetectAsync(
|
|
token);
|
|
|
|
Log(
|
|
success
|
|
? "Detect completed."
|
|
: "Detect failed.");
|
|
|
|
RefreshGrid();
|
|
});
|
|
}
|
|
|
|
|
|
private ProcessProgress CreatePreparationProgress()
|
|
{
|
|
return new ProcessProgress
|
|
{
|
|
Setting = new PreAdjustmentSettingsContainer
|
|
{
|
|
|
|
TempOnly = false,
|
|
Culture =
|
|
Thread.CurrentThread.CurrentCulture
|
|
},
|
|
|
|
IsAutomaticMode = true
|
|
};
|
|
}
|
|
|
|
private List<MeterStateControl> CreateMeterControls(IEnumerable<GciPublicModels.MeterBatchDebugStatus> slots)
|
|
{
|
|
var controls =
|
|
new List<MeterStateControl>();
|
|
|
|
foreach (var slot in slots)
|
|
{
|
|
var ctl = new MeterStateControl(slot.Slot);
|
|
|
|
ctl.SetChecked(true);
|
|
ctl.IsEnabled = true;
|
|
|
|
controls.Add(ctl);
|
|
}
|
|
|
|
return controls;
|
|
}
|
|
|
|
private PreAdjustmentSettingsContainer CreatePreAdjustmentSettings(IEnumerable<GciPublicModels.MeterBatchDebugStatus> slots)
|
|
{
|
|
var settings =
|
|
new PreAdjustmentSettingsContainer();
|
|
|
|
settings.Meters =
|
|
slots.Select(s => s.Slot).ToList();
|
|
|
|
settings.TempMeters =
|
|
new List<int>();
|
|
|
|
settings.NumberOfPaths = 2;
|
|
settings.TempOnly = false;
|
|
settings.Culture =
|
|
Thread.CurrentThread.CurrentCulture;
|
|
|
|
return settings;
|
|
}
|
|
|
|
private void writeRegisterButton_Click(object sender, EventArgs e)
|
|
{
|
|
/*ExecuteAsync(async token =>
|
|
{
|
|
string registerName = Convert.ToString(writeRegisterNameComboBox.Text).Trim();
|
|
string valueText = writeRegisterValueTextBox.Text.Trim();
|
|
|
|
if (string.IsNullOrWhiteSpace(registerName))
|
|
throw new Exception("Write register name is empty.");
|
|
|
|
if (string.IsNullOrWhiteSpace(valueText))
|
|
throw new Exception("Write register value is empty.");
|
|
|
|
object value;
|
|
|
|
if (registerName == "GENESISFLOW_LedMode")
|
|
{
|
|
value = byte.Parse(valueText);
|
|
}
|
|
else
|
|
{
|
|
value = valueText;
|
|
}
|
|
|
|
var tasks = GetSelectedSlots()
|
|
.Select(async slot =>
|
|
{
|
|
//var result = await _bridge.WriteRegisterAsync(slot.Slot, registerName, value, false, false, token);
|
|
//var result = await _bridge.WriteRegisterWithRetryAsync(slot.Slot, registerName, value, false, false, token);
|
|
|
|
var result = Xylem.Common.Ui.CordonelPreadjustmentUi. Processes.WriteRegisterSafe(meter, "Calibration factor1", Register.Genesisflow.CalFactor1, setting.CalFactor1);
|
|
Processes
|
|
|
|
return new
|
|
{
|
|
Slot = slot.Slot,
|
|
Result = result
|
|
};
|
|
})
|
|
.ToList();
|
|
|
|
var results = await Task.WhenAll(tasks);
|
|
|
|
foreach (var item in results.OrderBy(x => x.Slot))
|
|
{
|
|
LogResult(
|
|
$"WriteRegisterAsync slot {item.Slot}, register {registerName}, value {value}",
|
|
item.Result);
|
|
}
|
|
|
|
RefreshGrid();
|
|
});*/
|
|
}
|
|
}
|
|
} |