tbf/DeviceTest/DeviceTestDlg.cs

430 lines
14 KiB
C#
Raw Normal View History

2016-10-11 14:18:54 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
2016-10-11 14:18:54 +00:00
using System.Windows.Forms;
using TBF.BenchControl;
2016-10-14 12:28:23 +00:00
using TBF.BenchControl.Generic;
using TBF.BenchControl.GenericDevices;
using System.Xml.Serialization;
2016-10-14 12:28:23 +00:00
2016-10-11 14:18:54 +00:00
namespace DeviceTest
{
public partial class DeviceTestDlg : Form
{
IList<string> compClasses;
2016-10-14 12:28:23 +00:00
IComponentFactory parentFactory;
IComponentFactory componentFactory;
IComponentCfg parentCfg;
IComponentCfg componentCfg;
static IList<TBF.BenchControl.Generic.IDevice> tbfDevices;
static IList<TBF.BenchControl.Generic.IComponent> tbfComponents;
static TBF.BenchControl.Generic.IComponent tbfComponent4Op;
static Thread workerThread;
static bool workerThreadRunning;
static int activeOperationId;
static TBF.Boxes.FloatBox floatBox;
static IOperation operation1;
static IOperation operation2;
static IOperation operation3;
static IOperation operation4;
static Event evnt;
static DeviceTestDlg()
{
floatBox = new TBF.Boxes.FloatBox();
floatBox.Format = "F2";
}
2016-10-14 12:28:23 +00:00
2016-10-11 14:18:54 +00:00
public DeviceTestDlg()
{
InitializeComponent();
compClasses = new List<string>();
radioButton0.Checked = true;
stopButton.Enabled = false;
foreach (var componentFactory in TbfComponents.Factories)
2016-10-11 14:18:54 +00:00
{
compClasses.Add(componentFactory.ClassName);
}
}
private void DeviceTestDlg_Load(object sender, EventArgs e)
{
foreach (var clName in compClasses)
{
parentClassNameComboBox.Items.Add(clName);
componentClassNameComboBox.Items.Add(clName);
}
try
{
if (!string.IsNullOrEmpty(Program.LocalSettings.ParentClassName) &&
!string.IsNullOrEmpty(Program.LocalSettings.ParentName) &&
!string.IsNullOrEmpty(Program.LocalSettings.ParentSettings))
{
FactoryFromClassName(Program.LocalSettings.ParentClassName, ref parentFactory);
using (var reader = new StringReader(Program.LocalSettings.ParentSettings))
{
IComponentCfg config = (IComponentCfg)(new XmlSerializer(parentFactory.DefaultConfig().GetType())).Deserialize(reader);
config.Name = Program.LocalSettings.ParentName;
config.ParentName = string.Empty;
config.ItemNr = 0;
config.DebugLevel = Config.Entities.DebugMode.Normal;
config.LogLevel = Config.Entities.LogLevel.Debug;
config.Corrections = null;
config.Factory = parentFactory;
parentCfg = config;
}
parentClassNameComboBox.Text = Program.LocalSettings.ParentClassName;
parentNameTextBox.Text = Program.LocalSettings.ParentName;
}
}
catch
{
parentFactory = null;
parentCfg = null;
parentClassNameComboBox.Text = string.Empty;
parentNameTextBox.Text = string.Empty;
}
try
{
if (!string.IsNullOrEmpty(Program.LocalSettings.ComponentClassName) &&
!string.IsNullOrEmpty(Program.LocalSettings.ComponentName) &&
!string.IsNullOrEmpty(Program.LocalSettings.ComponentSettings))
{
FactoryFromClassName(Program.LocalSettings.ComponentClassName, ref componentFactory);
using (var reader = new StringReader(Program.LocalSettings.ComponentSettings))
{
IComponentCfg config = (IComponentCfg)(new XmlSerializer(componentFactory.DefaultConfig().GetType())).Deserialize(reader);
config.Name = Program.LocalSettings.ComponentName;
config.ParentName = string.IsNullOrEmpty(Program.LocalSettings.ParentComponentName) ? string.Empty : Program.LocalSettings.ParentComponentName;
config.ItemNr = 1;
config.DebugLevel = Config.Entities.DebugMode.Normal;
config.LogLevel = Config.Entities.LogLevel.Debug;
config.Corrections = null;
config.Factory = componentFactory;
componentCfg = config;
}
componentClassNameComboBox.Text = Program.LocalSettings.ComponentClassName;
componentNameTextBox.Text = Program.LocalSettings.ComponentName;
}
}
catch
{
componentFactory = null;
componentCfg = null;
componentClassNameComboBox.Text = string.Empty;
componentNameTextBox.Text = string.Empty;
}
2016-10-11 14:18:54 +00:00
}
void SaveSettings()
{
Program.LocalSettings.ParentClassName = parentClassNameComboBox.Text;
Program.LocalSettings.ParentName = parentNameTextBox.Text;
Program.LocalSettings.ParentSettings = (parentCfg != null) ? parentCfg.CreateDbEntity().Parameters : string.Empty;
Program.LocalSettings.ComponentClassName = componentClassNameComboBox.Text;
Program.LocalSettings.ComponentName = componentNameTextBox.Text;
Program.LocalSettings.ParentComponentName = componentCfg.ParentName;
Program.LocalSettings.ComponentSettings = (componentCfg != null) ? componentCfg.CreateDbEntity().Parameters : string.Empty;
Program.LocalSettings.Save();
}
private void radioButton0_CheckedChanged(object sender, EventArgs e) { activeOperationId = GetActiveOperationId(); }
private void radioButton1_CheckedChanged(object sender, EventArgs e) { activeOperationId = GetActiveOperationId(); }
private void radioButton2_CheckedChanged(object sender, EventArgs e) { activeOperationId = GetActiveOperationId(); }
private void radioButton3_CheckedChanged(object sender, EventArgs e) { activeOperationId = GetActiveOperationId(); }
private void radioButton4_CheckedChanged(object sender, EventArgs e) { activeOperationId = GetActiveOperationId(); }
int GetActiveOperationId()
{
if (radioButton1.Checked) return 1;
else if (radioButton2.Checked) return 2;
else if (radioButton3.Checked) return 3;
else if (radioButton4.Checked) return 4;
else return 0; /// stopOperationRadioButton.Checked
}
/// <summary>
/// Updates component factory
/// </summary>
/// <param name="className">Component class name</param>
/// <param name="factory">Reference to a factory</param>
/// <returns>true when factory changed</returns>
bool FactoryFromClassName(string className, ref IComponentFactory factory)
{
foreach (var fac in TbfComponents.Factories)
2016-10-14 12:28:23 +00:00
{
if (className == fac.ClassName)
2016-10-14 12:28:23 +00:00
{
if (factory != fac)
{
factory = fac;
return true;
}
else
{
return false;
}
2016-10-14 12:28:23 +00:00
}
}
if (factory != null)
{
factory = null;
return true;
}
return false;
}
private void configureParentButton_Click(object sender, EventArgs e)
{
bool factoryChanged = FactoryFromClassName(parentClassNameComboBox.Text, ref parentFactory);
if (parentFactory == null)
{
MessageBox.Show("Invalid parent class", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
2016-10-14 12:28:23 +00:00
return;
}
if (factoryChanged || parentCfg == null)
{
parentCfg = parentFactory.DefaultConfig();
}
IComponentCfgCtrl cfgControl = parentCfg.GetControl();
cfgControl.Config = parentCfg;
2016-10-14 12:28:23 +00:00
cfgControl.Config.ItemNr = 0;
2016-10-11 14:18:54 +00:00
2016-10-14 12:28:23 +00:00
TBF.ComponentParametersDlg cfgForm = new TBF.ComponentParametersDlg();
cfgForm.TbfComponents = new List<Config.Entities.Component>();
2016-10-14 12:28:23 +00:00
cfgForm.ComponentCfgCtrl = cfgControl;
cfgForm.UnlockAfterStart = true;
DialogResult dr = cfgForm.ShowDialog();
if (dr != DialogResult.OK) return;
parentNameTextBox.Text = parentCfg.Name;
2016-10-11 14:18:54 +00:00
}
2016-10-14 12:28:23 +00:00
private void configureButton_Click(object sender, EventArgs e)
{
bool factoryChanged = FactoryFromClassName(componentClassNameComboBox.Text, ref componentFactory);
if (componentFactory == null)
{
MessageBox.Show("Invalid component class", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
2016-10-14 12:28:23 +00:00
if (factoryChanged || componentCfg == null)
{
componentCfg = componentFactory.DefaultConfig();
}
IComponentCfgCtrl cfgControl = componentCfg.GetControl();
cfgControl.Config = componentCfg;
cfgControl.Config.ItemNr = 1;
TBF.ComponentParametersDlg cfgForm = new TBF.ComponentParametersDlg();
///
IList<Config.Entities.Component> compEntities = new List<Config.Entities.Component>();
if (parentFactory != null && parentCfg != null) compEntities.Add(parentCfg.CreateDbEntity());
cfgForm.TbfComponents = compEntities;
cfgForm.ComponentCfgCtrl = cfgControl;
cfgForm.UnlockAfterStart = true;
DialogResult dr = cfgForm.ShowDialog();
if (dr != DialogResult.OK) return;
componentNameTextBox.Text = componentCfg.Name;
2016-10-14 12:28:23 +00:00
}
private void startButton_Click(object sender, EventArgs e)
2016-10-14 12:28:23 +00:00
{
tbfComponents = new List<TBF.BenchControl.Generic.IComponent>();
tbfDevices = new List<IDevice>();
tbfComponent4Op = null;
operation1 = null;
operation2 = null;
operation3 = null;
operation4 = null;
workerThreadRunning = false;
string initializedDeviceName = "none";
try
{
if (parentFactory != null && parentCfg != null)
{
TBF.BenchControl.Generic.IComponent component = parentFactory.GetComponent(parentCfg, tbfComponents);
tbfComponents.Add(component);
if (component is IDevice) tbfDevices.Add(component as IDevice);
}
if (componentFactory != null && componentCfg != null)
{
TBF.BenchControl.Generic.IComponent component = componentFactory.GetComponent(componentCfg, tbfComponents);
tbfComponents.Add(component);
tbfComponent4Op = component;
if (component is IDevice) tbfDevices.Add(component as IDevice);
}
if (tbfComponents.Count == 0) throw new Exception("There are no components");
foreach (var d in tbfDevices)
{
initializedDeviceName = d.Name;
d.Initialize();
}
2016-10-14 12:28:23 +00:00
SaveSettings(); /// Save settings after successful initialization
workerThread = new Thread(Worker);
workerThread.CurrentCulture = Thread.CurrentThread.CurrentCulture;
workerThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture;
workerThreadRunning = true;
workerThread.Start();
startButton.Enabled = false;
stopButton.Enabled = true;
configureButton.Enabled = false;
configureParentButton.Enabled = false;
}
catch (Exception exc)
{
workerThreadRunning = false;
workerThread = null;
MessageBox.Show(exc.Message,
string.Format("'{0}' initialization failed", initializedDeviceName),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
startButton.Enabled = true;
stopButton.Enabled = false;
configureButton.Enabled = true;
configureParentButton.Enabled = true;
}
2016-10-14 12:28:23 +00:00
}
2016-10-11 14:18:54 +00:00
static void Worker()
{
int activeOperationLocal = 0;
int previousOperation = 0;
if (tbfComponent4Op is TBF.BenchControl.Modbus.PressureMeter.Meret.PressureMeter)
{
operation1 = (tbfComponent4Op as TBF.BenchControl.Modbus.PressureMeter.Meret.PressureMeter).ReadPressureOp(ref floatBox);
}
else if (tbfComponent4Op is TBF.BenchControl.Network.Camera.CLP1611.Camera)
{
operation1 = (tbfComponent4Op as TBF.BenchControl.Network.Camera.CLP1611.Camera).LiveStreamOp();
}
while (workerThreadRunning)
{
Thread.Sleep(1000);
if (workerThreadRunning)
{
foreach (var d in tbfDevices) d.RunDeviceBefore();
if (tbfComponent4Op != null)
{
previousOperation = activeOperationLocal;
activeOperationLocal = activeOperationId;
if (previousOperation == 0 && activeOperationLocal != 0)
{
switch (activeOperationLocal)
{
case 1: if (operation1 != null) operation1.Start(); Debug.WriteLine("op1.Start()"); break;
case 2: if (operation2 != null) operation2.Start(); Debug.WriteLine("op2.Start()"); break;
case 3: if (operation3 != null) operation3.Start(); Debug.WriteLine("op3.Start()"); break;
case 4: if (operation4 != null) operation4.Start(); Debug.WriteLine("op4.Start()"); break;
}
}
else if (previousOperation == activeOperationLocal && activeOperationLocal != 0)
{
switch (activeOperationLocal)
{
case 1: if (operation1 != null) evnt = operation1.Run(); Debug.WriteLine("op1.Run() -> {0}, pressure = {1} kPa", evnt, floatBox); break;
case 2: if (operation2 != null) evnt = operation2.Run(); Debug.WriteLine("op2.Run() -> {0}", evnt); break;
case 3: if (operation3 != null) evnt = operation3.Run(); Debug.WriteLine("op3.Run() -> {0}", evnt); break;
case 4: if (operation4 != null) evnt = operation4.Run(); Debug.WriteLine("op4.Run() -> {0}", evnt); break;
default: evnt = Event.None; break;
}
}
else if (previousOperation != 0 && activeOperationLocal == 0)
{
switch (previousOperation)
{
case 1: if (operation1 != null) operation1.Stop(); Debug.WriteLine("op1.Stop()"); break;
case 2: if (operation2 != null) operation2.Stop(); Debug.WriteLine("op2.Stop()"); break;
case 3: if (operation3 != null) operation3.Stop(); Debug.WriteLine("op3.Stop()"); break;
case 4: if (operation4 != null) operation4.Stop(); Debug.WriteLine("op4.Stop()"); break;
}
}
}
foreach (var d in tbfDevices) d.RunDeviceAfter();
}
}
foreach (var d in tbfDevices) d.StopDevice();
}
private void stopButton_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
workerThreadRunning = false;
workerThread.Join(3000);
workerThread = null;
Cursor = Cursors.Default;
startButton.Enabled = true;
stopButton.Enabled = false;
configureButton.Enabled = true;
configureParentButton.Enabled = true;
}
2016-10-11 14:18:54 +00:00
private void DeviceTestDlg_FormClosing(object sender, FormClosingEventArgs e)
{
Cursor = Cursors.WaitCursor;
if (workerThread != null)
{
workerThreadRunning = false;
workerThread.Join(3000);
workerThread = null;
}
SaveSettings();
Cursor = Cursors.Default;
}
2016-10-11 14:18:54 +00:00
}
}