tbf/TBF/Rig/BuiltIn/Valve/ValveCfgCtrl.cs
2021-04-03 00:54:18 +02:00

203 lines
6.9 KiB
C#

///
/// Copyright (c) 2013-2016 Sensus Metering Systems
///
using System;
using System.Windows.Forms;
using Config.Entities;
using TBF.Rig.Generic;
using TBF.UI.Bench.Components;
namespace TBF.Rig.BuiltIn.Valve
{
public partial class ValveCfgCtrl : UserControl, IComponentCfgCtrl
{
ComponentParametersDlg parent;
public bool ShowMore { get { return false; } }
ValveCfg config;
public IComponentCfg Config
{
get { return config as IComponentCfg; }
set
{
config = value as ValveCfg;
Redraw();
}
}
public ValveCfgCtrl()
{
InitializeComponent();
}
private void ValveCfgCtrl_Load(object sender, EventArgs e)
{
parent = ParentForm as ComponentParametersDlg;
if (parent == null) return;
categoryComboBox.Items.Add(ValveCategory.Feeding.ToString());
categoryComboBox.Items.Add(ValveCategory.Bench.ToString());
categoryComboBox.Items.Add(ValveCategory.Output.ToString());
categoryComboBox.Items.Add(ValveCategory.None.ToString());
if (parent.CmpntEntities != null)
{
parentNameComboBox.Items.Add("---");
coupledToComboBox.Items.Add("---");
foreach (var cmpnt in parent.CmpntEntities)
{
if (cmpnt.ClassName.Contains("ControlBoard"))
{
parentNameComboBox.Items.Add(cmpnt.Name);
}
if (TbfComponents.CmpntFactoryFromClassName(cmpnt.ClassName) is BuiltIn.Valve.ValveFactory)
{
coupledToComboBox.Items.Add(cmpnt.Name);
}
}
}
Redraw();
}
public void Closing()
{
}
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;
bitPositionTextBox.Text = config.BitNr.ToString();
invertedCheckBox.Checked = config.Inverted;
openCloseTimeTextBox.Text = config.OpenCloseTime.ToString();
categoryComboBox.Text = config.Category.ToString();
coupledToComboBox.Text = string.IsNullOrEmpty(config.CoupledToName) ? "---" : config.CoupledToName;
invertCoupleCheckBox.Checked = config.InvertCouple;
lagOpeningTextBox.Text = config.LagOpening.ToString();
lagClosingTextBox.Text = config.LagClosing.ToString();
}
public void Unlock()
{
nameTextBox.Enabled = true;
parentNameComboBox.Enabled = true;
bitPositionTextBox.Enabled = true;
invertedCheckBox.Enabled = true;
openCloseTimeTextBox.Enabled = true;
categoryComboBox.Enabled = true;
coupledToComboBox.Enabled = true;
invertCoupleCheckBox.Enabled = true;
lagOpeningTextBox.Enabled = true;
lagClosingTextBox.Enabled = true;
}
public CfgUpdateFlags UpdateCfg()
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed
string newParentName = parentNameComboBox.Text.Equals("---") ? string.Empty : parentNameComboBox.Text;
int newBitPosition = int.Parse(bitPositionTextBox.Text);
string newCoupledToName = coupledToComboBox.Text.Equals("---") ? string.Empty : coupledToComboBox.Text;
int newLagOpening = int.Parse(lagOpeningTextBox.Text);
int newLagClosing = int.Parse(lagClosingTextBox.Text);
int newOpenCloseTime = int.Parse(openCloseTimeTextBox.Text);
ValveCategory newCategory;
if (categoryComboBox.Text.Equals(ValveCategory.Feeding.ToString())) newCategory = ValveCategory.Feeding;
else if (categoryComboBox.Text.Equals(ValveCategory.Bench.ToString())) newCategory = ValveCategory.Bench;
else if (categoryComboBox.Text.Equals(ValveCategory.Output.ToString())) newCategory = ValveCategory.Output;
else newCategory = ValveCategory.None;
if (config.Name != nameTextBox.Text ||
config.ParentName != newParentName ||
config.BitNr != newBitPosition ||
config.Category != newCategory ||
config.Inverted != invertedCheckBox.Checked ||
config.CoupledToName != newCoupledToName ||
config.InvertCouple != invertCoupleCheckBox.Checked)
{
config.Name = nameTextBox.Text;
config.ParentName = newParentName;
config.BitNr = newBitPosition;
config.Category = newCategory;
config.Inverted = invertedCheckBox.Checked;
config.CoupledToName = newCoupledToName;
config.InvertCouple = invertCoupleCheckBox.Checked;
flags |= (CfgUpdateFlags.RestartRqrd | CfgUpdateFlags.AnyChange);
}
if (config.OpenCloseTime != newOpenCloseTime ||
config.LagOpening != newLagOpening ||
config.LagClosing != newLagClosing)
{
config.OpenCloseTime = newOpenCloseTime;
config.LagOpening = newLagOpening;
config.LagClosing = newLagClosing;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if ((flags & CfgUpdateFlags.InvokeCfgChange) != 0)
{
Valve.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.CfgChange, config));
}
return flags;
}
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.Items.Contains(categoryComboBox.Text))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid 'Category'";
}
if (!coupledToComboBox.Items.Contains(coupledToComboBox.Text))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid 'Coupled To'";
}
if (!int.TryParse(bitPositionTextBox.Text, out dummy) || dummy < 0 || dummy >= 128)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'Bit Position' should be between 0 and 127";
}
if (!int.TryParse(openCloseTimeTextBox.Text, out dummy) || dummy < 0 || dummy > 300 || (dummy % StateMachine.Period) != 0)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'Open/close time' should be between 0 and 300 s";
if (dummy % StateMachine.Period != 0)
message += Environment.NewLine + string.Format("and a multiple of {0}", StateMachine.Period);
}
if (!int.TryParse(lagOpeningTextBox.Text, out dummy) || (dummy % StateMachine.Period) != 0)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + string.Format("'Delay when opening' error should be a multiple of {0}", StateMachine.Period);
}
if (!int.TryParse(lagClosingTextBox.Text, out dummy) || (dummy % StateMachine.Period) != 0)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + string.Format("'Delay when closing' error should be a multiple of {0}", StateMachine.Period);
}
return flags;
}
}
}