tbf/TBF/Rig/TestMethods/Endurance/TestMethodCfgCtrl.cs

198 lines
5.8 KiB
C#

///
/// Copyright (c) 2016 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using TBF.Resources;
using TBF.Rig.Generic;
using TBF.Rig.GenericDevices;
using Common;
using Config.Entities;
using System.Linq;
using TBF.Rig.Sequences;
using NHibernate;
using log4net;
namespace TBF.Rig.TestMethods.Endurance
{
public partial class TestMethodCfgCtrl : UserControl, IComponentCfgCtrl
{
IList<Rig.BuiltIn.Valve.Valve> valvesFromDB = new List<Rig.BuiltIn.Valve.Valve>();
public bool ShowMore { get { return false; } }
string cycle;
TestMethodCfg config;
public IComponentCfg Config
{
get { return config as IComponentCfg; }
set
{
config = value as TestMethodCfg;
Redraw();
}
}
public TestMethodCfgCtrl()
{
InitializeComponent();
valvesFromDB = LoadValvesFromDB();
CreateValveRows(valvesFromDB.Count, valvesFromDB); // toľko riadkov, koľko ventilov
}
private void EntryFormCfgCtrl_Load(object sender, EventArgs e)
{
enduranceCycleButton.Text = Strings.Endurance_cycle;
cycle = config.Cycle;
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;
}
public void Unlock()
{
nameTextBox.Enabled = true;
enduranceCycleButton.Enabled = true;
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
return flags;
}
public CfgUpdateFlags UpdateCfg()
{
CfgUpdateFlags flags = CfgUpdateFlags.RestartRqrd;
if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed
config.Name = nameTextBox.Text;
config.Cycle = cycle;
return flags;
}
private void enduranceCycleButton_Click(object sender, EventArgs e)
{
CycleDlg dlg = new CycleDlg(CycleStep.StringToCycle(cycle), GetSelectedValves());
if (dlg.ShowDialog() == DialogResult.OK)
{
cycle = CycleStep.CycleToString(dlg.EnduranceCycle);
}
}
private void CreateValveRows(int numberOfRows, IList<Rig.BuiltIn.Valve.Valve> valves)
{
tableLayoutPanel1.RowCount = numberOfRows;
tableLayoutPanel1.ColumnCount = 2;
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
for (int i = 0; i < numberOfRows; i++)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
var label = new Label
{
Text = $"Valve {i + 1}",
Anchor = AnchorStyles.Left,
AutoSize = true,
Margin = new Padding(5)
};
var comboBox = new ComboBox
{
DropDownStyle = ComboBoxStyle.DropDownList,
Anchor = AnchorStyles.Left | AnchorStyles.Right,
Dock = DockStyle.Fill,
Margin = new Padding(5),
Name = $"comboValve{i + 1}",
DisplayMember = "Name" // zobrazenie názvu ventilu
};
comboBox.Items.Add("None");
foreach (var valve in valves)
{
comboBox.Items.Add(valve.Name/* + " (bit nr = " + valve.BitPosition + ")"*/);
}
comboBox.SelectedIndex = 0;
tableLayoutPanel1.Controls.Add(label, 0, i);
tableLayoutPanel1.Controls.Add(comboBox, 1, i);
}
}
private IList<Rig.BuiltIn.Valve.Valve> LoadValvesFromDB()
{
/// Load the list of components from the database
IList<Rig.BuiltIn.Valve.Valve> valves = new List<Rig.BuiltIn.Valve.Valve>();
using (NHibernate.ISession session = TBF.DB.CreateSession(DBKind.Config))
{
IList<IComponent> TbfComponents = Rig.TbfComponents.LoadComponentsFromDB(session);
valves = getAllValves(TbfComponents);
}
/// Find all master valvesFromDB
return valves;
}
/// <summary>
/// Process the list of components and return all valvesFromDB (masters and coupled)
/// </summary>
public static IList<Rig.BuiltIn.Valve.Valve> getAllValves(IList<Generic.IComponent> cmpnts)
{
IList<Rig.BuiltIn.Valve.Valve> result = new List<Rig.BuiltIn.Valve.Valve>();
foreach (var cmpnt in cmpnts) if (cmpnt is Rig.BuiltIn.Valve.Valve) result.Add(cmpnt as Rig.BuiltIn.Valve.Valve);
return result;
}
private List<Rig.BuiltIn.Valve.Valve> GetSelectedValves()
{
var selectedValves = new List<Rig.BuiltIn.Valve.Valve>();
foreach (Control control in tableLayoutPanel1.Controls)
{
if (control is ComboBox comboBox)
{
var selectedName = comboBox.SelectedItem?.ToString();
if (!string.IsNullOrEmpty(selectedName) && selectedName != "None")
{
// Nájdeme ventil podľa mena v zozname všetkých
var valve = valvesFromDB.FirstOrDefault(v => v.Name == selectedName);
if (valve != null)
{
selectedValves.Add(valve);
}
}
}
}
return selectedValves;
}
}
}