/// /// Copyright (c) 2021 Sensus Slovensko a.s. /// using System; using System.Linq; using System.Windows.Forms; using log4net; using Common; using Config.Entities; using TBF.Rig.Generic; using TBF.UI.Bench.Components; using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace TBF.Rig.Uni.RegValveLowRegulTimeSaturation { 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 (Common.Unit units = 0; units < global::Common.Unit.Count; units++) { if (global::Common.Units.IsQuantity(units, Quantity.Error)) { unitsComboBox.Items.Add(units.ToString()); } } regulMinStepsComboBox.Items.Add("0"); regulMinStepsComboBox.Items.Add("50"); regulMinStepsComboBox.Items.Add("100"); regulMinStepsComboBox.Items.Add("150"); regulMinStepsComboBox.Items.Add("200"); regulMinStepsComboBox.Items.Add("250"); regulMinStepsComboBox.Items.Add("300"); 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; formatTextBox.Text = config.SetpFormat; unitsComboBox.Text = config.SetpUnit.ToString(); regulMinStepsComboBox.Text = (config.RegulationMinStep * 50).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; formatTextBox.Enabled = true; unitsComboBox.Enabled = true; regulMinStepsComboBox.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; } tmp = regulMinStepsComboBox.SelectedIndex; if (config.RegulationMinStep != tmp) { config.RegulationMinStep = tmp; flags |= CfgUpdateFlags.AnyChange; } bool btmp = storedPosReuseCheckBox.Checked; if (config.StoredPositionReuse != btmp) { config.StoredPositionReuse = btmp; flags |= CfgUpdateFlags.RestartRqrd; } if ((flags & CfgUpdateFlags.AnyChange) != 0) { RegValve.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.CfgChange, config)); } config.SetpFormat = formatTextBox.Text; config.SetpUnit = global::Common.Unit.None; for (Common.Unit units = 0; units < global::Common.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(); } } private void regulMinStepsComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (regulMinStepsComboBox.SelectedItem != null) { //config.RegulationMinStep = regulMinStepsComboBox.SelectedIndex; } } #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 CmdResponseHandler; public void StartResponseHandler() { Handlers.AdcChangedHandler += delegate(object sndr, CmdResponseArgs args) { if (InvokeRequired) { Invoke(new EventHandler(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 } }