tbf/DeviceTest/DeviceTestDlg.cs

319 lines
9.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using TBF.BenchControl;
using TBF.BenchControl.Generic;
using TBF.BenchControl.GenericDevices;
namespace DeviceTest
{
public partial class DeviceTestDlg : Form
{
IList<string> compClasses;
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();
}
public DeviceTestDlg()
{
InitializeComponent();
compClasses = new List<string>();
radioButton0.Checked = true;
stopButton.Enabled = false;
foreach (var componentFactory in TbfComponents.Factories)
{
compClasses.Add(componentFactory.ClassName);
}
}
private void DeviceTestDlg_Load(object sender, EventArgs e)
{
foreach (var clName in compClasses)
{
parentClassNameComboBox.Items.Add(clName);
classNameComboBox.Items.Add(clName);
}
}
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
}
private void configureParentButton_Click(object sender, EventArgs e)
{
bool factoryChanged = false;
bool classNotFound = true;
foreach (var factory in TbfComponents.Factories)
{
if (parentClassNameComboBox.Text == factory.ClassName)
{
if (parentFactory != factory)
{
parentFactory = factory;
factoryChanged = true;
}
classNotFound = false;
break;
}
}
if (classNotFound)
{
MessageBox.Show("Invalid parent component class", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (factoryChanged || parentCfg == null)
{
parentCfg = parentFactory.DefaultConfig();
}
IComponentCfgCtrl cfgControl = parentCfg.GetControl();
cfgControl.Config = parentCfg;
cfgControl.Config.ItemNr = 0;
TBF.ComponentParametersDlg cfgForm = new TBF.ComponentParametersDlg();
cfgForm.TbfComponents = new List<Config.Entities.Component>();
cfgForm.ComponentCfgCtrl = cfgControl;
cfgForm.UnlockAfterStart = true;
DialogResult dr = cfgForm.ShowDialog();
if (dr != DialogResult.OK) return;
parentNameTextBox.Text = parentCfg.Name;
}
private void configureButton_Click(object sender, EventArgs e)
{
bool factoryChanged = false;
bool classNotFound = true;
foreach (var factory in TbfComponents.Factories)
{
if (classNameComboBox.Text == factory.ClassName)
{
if (componentFactory != factory)
{
componentFactory = factory;
factoryChanged = true;
}
classNotFound = false;
break;
}
}
if (classNotFound)
{
MessageBox.Show("Invalid parent component class", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
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;
nameTextBox.Text = componentCfg.Name;
}
private void startButton_Click(object sender, EventArgs e)
{
tbfComponents = new List<TBF.BenchControl.Generic.IComponent>();
tbfDevices = new List<IDevice>();
tbfComponent4Op = null;
operation1 = null;
operation2 = null;
operation3 = null;
operation4 = null;
workerThreadRunning = false;
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) d.Initialize();
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(string.Format("Exception: {0}", exc.Message), "Initialization failed",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
startButton.Enabled = true;
stopButton.Enabled = false;
configureButton.Enabled = true;
configureParentButton.Enabled = true;
}
}
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);
}
while (workerThreadRunning)
{
Thread.Sleep(1000);
if (workerThreadRunning)
{
foreach (var d in tbfDevices) { d.RunDeviceBefore(); Debug.WriteLine("{0}.RunDeviceBefore()", d.Name); }
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}", evnt); 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(); Debug.WriteLine("{0}.RunDeviceAfter()", d.Name); }
}
}
foreach (var d in tbfDevices) { d.StopDevice(); Debug.WriteLine("{0}.StopDevice()", d.Name); }
}
private void stopButton_Click(object sender, EventArgs e)
{
workerThreadRunning = false;
workerThread.Join(3000);
workerThread = null;
startButton.Enabled = true;
stopButton.Enabled = false;
configureButton.Enabled = true;
configureParentButton.Enabled = true;
}
private void DeviceTestDlg_FormClosing(object sender, FormClosingEventArgs e)
{
if (workerThread != null)
{
workerThreadRunning = false;
workerThread.Join(3000);
workerThread = null;
}
}
}
}