Refactorization : BenchControl -> Rig
This commit is contained in:
@@ -0,0 +1,377 @@
|
||||
///
|
||||
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using log4net;
|
||||
using Config.Entities;
|
||||
using TBF.Rig.Generic;
|
||||
using TBF.UI.Bench.Components;
|
||||
|
||||
namespace TBF.Rig.Uni.RegValve
|
||||
{
|
||||
public partial class RegValveCfgCtrl : UserControl, IComponentCfgCtrl
|
||||
{
|
||||
static readonly ILog log = LogManager.GetLogger(typeof(RegValveCfgCtrl));
|
||||
|
||||
ComponentParametersDlg parent;
|
||||
|
||||
public bool ShowMore { get { return true; } }
|
||||
|
||||
RegValveCfg config;
|
||||
public IComponentCfg Config
|
||||
{
|
||||
get { return config as IComponentCfg; }
|
||||
set
|
||||
{
|
||||
config = value as RegValveCfg;
|
||||
Redraw();
|
||||
}
|
||||
}
|
||||
|
||||
public RegValveCfgCtrl()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void RegulationValveCfgCtrl_Load(object sender, EventArgs e)
|
||||
{
|
||||
parent = ParentForm as ComponentParametersDlg;
|
||||
if (parent == null) return;
|
||||
|
||||
if (parent.CmpntEntities != null)
|
||||
{
|
||||
foreach (var c in parent.CmpntEntities.Where(x => x.ClassName == "ControlBoard.Uni"))
|
||||
{
|
||||
parentNameComboBox.Items.Add(c.Name);
|
||||
}
|
||||
}
|
||||
|
||||
categoryComboBox.Items.Add(ValveCategory.Feeding.ToString());
|
||||
categoryComboBox.Items.Add(ValveCategory.Output.ToString());
|
||||
|
||||
if (config != null)
|
||||
{
|
||||
adc1 = config.AdcValueClosed;
|
||||
pct1 = 0;
|
||||
adc2 = config.AdcValueOpen;
|
||||
pct2 = 100;
|
||||
}
|
||||
|
||||
for (Config.Unit units = 0; units < global::Config.Unit.Count; units++)
|
||||
{
|
||||
if (global::Config.Units.IsQuantity(units, global::Config.Quantity.Error))
|
||||
{
|
||||
unitsComboBox.Items.Add(units.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
Redraw();
|
||||
StartResponseHandler();
|
||||
}
|
||||
|
||||
public void Closing()
|
||||
{
|
||||
StopResponseHandler();
|
||||
}
|
||||
|
||||
void Redraw()
|
||||
{
|
||||
if (config == null) return; /// Control was not loaded, settings were not changed
|
||||
|
||||
classNameLabel.Text = config.Factory.ClassName;
|
||||
nameTextBox.Text = config.Name;
|
||||
parentNameComboBox.Text = string.IsNullOrEmpty(config.ParentName) ? "---" : config.ParentName;
|
||||
categoryComboBox.Text = config.Category.ToString();
|
||||
positionTextBox.Text = config.Idx1.ToString();
|
||||
adcClosedTextBox.Text = config.AdcValueClosed.ToString();
|
||||
adcOpenTextBox.Text = config.AdcValueOpen.ToString();
|
||||
stableTimeTextBox.Text = config.StableTimeMs.ToString();
|
||||
flowStableSecTextBox.Text = config.FlowStableSec.ToString();
|
||||
storedPosReuseCheckBox.Checked = config.StoredPositionReuse;
|
||||
reTryCommandsCheckBox.Checked = config.ReTryCommands;
|
||||
formatTextBox.Text = config.SetpFormat;
|
||||
unitsComboBox.Text = config.SetpUnit.ToString();
|
||||
|
||||
adcTextBox1.Text = adc1.ToString();
|
||||
adcTextBox2.Text = adc2.ToString();
|
||||
pctTextBox1.Text = pct1.ToString();
|
||||
pctTextBox2.Text = pct2.ToString();
|
||||
}
|
||||
|
||||
public void Unlock()
|
||||
{
|
||||
nameTextBox.Enabled = true;
|
||||
parentNameComboBox.Enabled = true;
|
||||
categoryComboBox.Enabled = true;
|
||||
positionTextBox.Enabled = true;
|
||||
adcClosedTextBox.Enabled = true;
|
||||
adcOpenTextBox.Enabled = true;
|
||||
stableTimeTextBox.Enabled = true;
|
||||
flowStableSecTextBox.Enabled = true;
|
||||
storedPosReuseCheckBox.Enabled = true;
|
||||
reTryCommandsCheckBox.Enabled = true;
|
||||
formatTextBox.Enabled = true;
|
||||
unitsComboBox.Enabled = true;
|
||||
}
|
||||
|
||||
public CfgUpdateFlags VerifyCfg(ref string message)
|
||||
{
|
||||
CfgUpdateFlags flags = CfgUpdateFlags.None;
|
||||
|
||||
int dummy;
|
||||
if (!parentNameComboBox.Items.Contains(parentNameComboBox.Text))
|
||||
{
|
||||
flags |= CfgUpdateFlags.Error;
|
||||
message += Environment.NewLine + "Invalid 'Parent Name'";
|
||||
}
|
||||
|
||||
if (!categoryComboBox.Text.Equals(ValveCategory.Feeding.ToString()) &&
|
||||
!categoryComboBox.Text.Equals(ValveCategory.Output.ToString()))
|
||||
{
|
||||
flags |= CfgUpdateFlags.Error;
|
||||
message += Environment.NewLine + "Invalid 'Category'";
|
||||
}
|
||||
|
||||
if (!int.TryParse(positionTextBox.Text, out dummy) || dummy < 1 || dummy > 8)
|
||||
{
|
||||
flags |= CfgUpdateFlags.Error;
|
||||
message += Environment.NewLine + "'Position' should be between 1 and 8";
|
||||
}
|
||||
|
||||
if (!int.TryParse(adcClosedTextBox.Text, out dummy) || dummy < 0 || dummy > 1023)
|
||||
{
|
||||
flags |= CfgUpdateFlags.Error;
|
||||
message += Environment.NewLine + "'ADC when Closed' should be between 0 and 1023";
|
||||
}
|
||||
|
||||
if (!int.TryParse(adcOpenTextBox.Text, out dummy) || dummy < 0 || dummy > 1023)
|
||||
{
|
||||
flags |= CfgUpdateFlags.Error;
|
||||
message += Environment.NewLine + "'ADC when Open' should be between 0 and 1023";
|
||||
}
|
||||
|
||||
if (!int.TryParse(stableTimeTextBox.Text, out dummy) || dummy < 200 || dummy > 1500 || (dummy % 50) != 0)
|
||||
{
|
||||
flags |= CfgUpdateFlags.Error;
|
||||
message += Environment.NewLine + "'Stable time' should be between 200 and 1500 ms in 50 ms steps";
|
||||
}
|
||||
|
||||
if (!int.TryParse(flowStableSecTextBox.Text, out dummy) || dummy < 0 || dummy > 60)
|
||||
{
|
||||
flags |= CfgUpdateFlags.Error;
|
||||
message += Environment.NewLine + "'Flow stable' should be between 0 and 60 sec";
|
||||
}
|
||||
|
||||
if (!unitsComboBox.Items.Contains(unitsComboBox.Text))
|
||||
{
|
||||
flags |= CfgUpdateFlags.Error;
|
||||
message += Environment.NewLine + "Invalid 'Units'";
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
public CfgUpdateFlags UpdateCfg()
|
||||
{
|
||||
CfgUpdateFlags flags = CfgUpdateFlags.None;
|
||||
|
||||
if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed
|
||||
///
|
||||
if (!config.Name.Equals(nameTextBox.Text))
|
||||
{
|
||||
config.Name = nameTextBox.Text;
|
||||
flags |= CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
|
||||
string parentname = parentNameComboBox.Text.Equals("---") ? string.Empty : parentNameComboBox.Text;
|
||||
if (!config.ParentName.Equals(parentname))
|
||||
{
|
||||
config.ParentName = parentname;
|
||||
flags |= CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
|
||||
ValveCategory newCategory;
|
||||
if (categoryComboBox.Text.Equals(ValveCategory.Feeding.ToString())) newCategory = ValveCategory.Feeding;
|
||||
else if (categoryComboBox.Text.Equals(ValveCategory.Output.ToString())) newCategory = ValveCategory.Output;
|
||||
else newCategory = ValveCategory.None;
|
||||
if (newCategory != config.Category)
|
||||
{
|
||||
config.Category = newCategory;
|
||||
flags |= CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
|
||||
int tmp = int.Parse(positionTextBox.Text);
|
||||
if (config.Idx1 != tmp)
|
||||
{
|
||||
config.Idx1 = tmp;
|
||||
flags |= CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
|
||||
tmp = int.Parse(adcClosedTextBox.Text);
|
||||
if (config.AdcValueClosed != tmp)
|
||||
{
|
||||
config.AdcValueClosed = tmp;
|
||||
flags |= CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
|
||||
tmp = int.Parse(adcOpenTextBox.Text);
|
||||
if (config.AdcValueOpen != tmp)
|
||||
{
|
||||
config.AdcValueOpen = tmp;
|
||||
flags |= CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
|
||||
tmp = int.Parse(stableTimeTextBox.Text);
|
||||
if (config.StableTimeMs != tmp)
|
||||
{
|
||||
config.StableTimeMs = tmp;
|
||||
flags |= CfgUpdateFlags.AnyChange;
|
||||
}
|
||||
|
||||
tmp = int.Parse(flowStableSecTextBox.Text);
|
||||
if (config.FlowStableSec != tmp)
|
||||
{
|
||||
config.FlowStableSec = tmp;
|
||||
flags |= CfgUpdateFlags.AnyChange;
|
||||
}
|
||||
|
||||
bool btmp = storedPosReuseCheckBox.Checked;
|
||||
if (config.StoredPositionReuse != btmp)
|
||||
{
|
||||
config.StoredPositionReuse = btmp;
|
||||
flags |= CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
|
||||
btmp = reTryCommandsCheckBox.Checked;
|
||||
if (config.ReTryCommands != btmp)
|
||||
{
|
||||
config.ReTryCommands = btmp;
|
||||
flags |= CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
|
||||
if ((flags & CfgUpdateFlags.AnyChange) != 0)
|
||||
{
|
||||
RegValve.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.CfgChange, config));
|
||||
}
|
||||
|
||||
config.SetpFormat = formatTextBox.Text;
|
||||
config.SetpUnit = global::Config.Unit.None;
|
||||
for (Config.Unit units = 0; units < global::Config.Unit.Count; units++)
|
||||
{
|
||||
if (unitsComboBox.Text == units.ToString())
|
||||
{
|
||||
config.SetpUnit = units;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
int adc1;
|
||||
int pct1;
|
||||
|
||||
int adc2;
|
||||
int pct2;
|
||||
|
||||
private void openButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
RegValve.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.RVOpenStep, config));
|
||||
}
|
||||
|
||||
private void closeButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
RegValve.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.RVCloseStep, config));
|
||||
}
|
||||
|
||||
//
|
||||
private void getButton1_Click(object sender, EventArgs e)
|
||||
{
|
||||
RegValve.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.GetAdc1, config));
|
||||
}
|
||||
|
||||
private void getButton2_Click(object sender, EventArgs e)
|
||||
{
|
||||
RegValve.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.GetAdc2, config));
|
||||
}
|
||||
|
||||
private void setButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
int tmpPct1;
|
||||
int tmpPct2;
|
||||
if ((int.TryParse(pctTextBox1.Text, out tmpPct1) && (tmpPct1 >= 0) && (tmpPct1 <= 100)) &&
|
||||
(int.TryParse(pctTextBox2.Text, out tmpPct2) && (tmpPct2 >= 0) && (tmpPct2 <= 100)))
|
||||
{
|
||||
pct1 = tmpPct1;
|
||||
pct2 = tmpPct2;
|
||||
UpdateValveOpenClose();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateValveOpenClose()
|
||||
{
|
||||
if ((adc1 == adc2) || (pct1 == pct2)) return;
|
||||
|
||||
double factor = (float)(adc2 - adc1) / (float)(pct2 - pct1);
|
||||
int adcClosed = (int)((float)(0 - pct1) * factor + (float)adc1 + 0.5f);
|
||||
int adcOpen = (int)((float)(100 - pct1) * factor + (float)adc1 + 0.5f);
|
||||
|
||||
if ((adcClosed >= 0) && (adcOpen >= 0))
|
||||
{
|
||||
adcClosedTextBox.Text = adcClosed.ToString();
|
||||
adcOpenTextBox.Text = adcOpen.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
#region Configuration Change Handling
|
||||
|
||||
public static void OnCmdResponse(object sender, CmdResponseArgs args)
|
||||
{
|
||||
if (CmdResponseHandler == null) return;
|
||||
try { CmdResponseHandler(sender, args); }
|
||||
catch (Exception e) { log.Error("CmdResponseHandler(...) failed", e); }
|
||||
}
|
||||
|
||||
public static event EventHandler<CmdResponseArgs> CmdResponseHandler;
|
||||
|
||||
public void StartResponseHandler()
|
||||
{
|
||||
Handlers.AdcChangedHandler += delegate(object sndr, CmdResponseArgs args)
|
||||
{
|
||||
if (InvokeRequired) { Invoke(new EventHandler<CmdResponseArgs>(OnAdcChanged), sndr, args); }
|
||||
else OnAdcChanged(sndr, args);
|
||||
};
|
||||
|
||||
CmdResponseHandler += delegate(object sender, CmdResponseArgs args)
|
||||
{
|
||||
if (args.Command == CfgChangeCmd.GetAdc1)
|
||||
{
|
||||
adc1 = args.Response;
|
||||
adcTextBox1.Text = adc1.ToString();
|
||||
}
|
||||
else if (args.Command == CfgChangeCmd.GetAdc2)
|
||||
{
|
||||
adc2 = args.Response;
|
||||
adcTextBox2.Text = adc2.ToString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void StopResponseHandler()
|
||||
{
|
||||
CmdResponseHandler = null;
|
||||
}
|
||||
|
||||
void OnAdcChanged(object sender, CmdResponseArgs data)
|
||||
{
|
||||
if (data.Command == CfgChangeCmd.GetAdc && data.Id == config.Idx1)
|
||||
{
|
||||
adcTextBox.Text = data.Response.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Configuration Change Handling
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user