From 9e90d3a93fcdffb68101865450ba0c05b7336729 Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Tue, 18 Oct 2016 09:52:17 +0200 Subject: [PATCH] DeviceTest program functional, runs operation Modbus.PressureMeter.Meret.ReadPressureOp(...) --- DeviceTest/DeviceTestDlg.Designer.cs | 133 ++++++++++---- DeviceTest/DeviceTestDlg.cs | 167 +++++++++++++++--- .../BenchControl/Generic/IDevice.cs | 4 +- 3 files changed, 238 insertions(+), 66 deletions(-) diff --git a/DeviceTest/DeviceTestDlg.Designer.cs b/DeviceTest/DeviceTestDlg.Designer.cs index 7f92c45d7..e82293350 100644 --- a/DeviceTest/DeviceTestDlg.Designer.cs +++ b/DeviceTest/DeviceTestDlg.Designer.cs @@ -34,15 +34,19 @@ this.parentNameTextBox = new System.Windows.Forms.TextBox(); this.configureParentButton = new System.Windows.Forms.Button(); this.startButton = new System.Windows.Forms.Button(); - this.stopButton = new System.Windows.Forms.Button(); - this.initializeButton = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.radioButton4 = new System.Windows.Forms.RadioButton(); + this.radioButton3 = new System.Windows.Forms.RadioButton(); + this.radioButton2 = new System.Windows.Forms.RadioButton(); + this.radioButton1 = new System.Windows.Forms.RadioButton(); + this.radioButton0 = new System.Windows.Forms.RadioButton(); this.configureButton = new System.Windows.Forms.Button(); this.nameTextBox = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.classNameComboBox = new System.Windows.Forms.ComboBox(); + this.stopButton = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); @@ -83,7 +87,7 @@ // // configureParentButton // - this.configureParentButton.Location = new System.Drawing.Point(77, 68); + this.configureParentButton.Location = new System.Drawing.Point(77, 70); this.configureParentButton.Name = "configureParentButton"; this.configureParentButton.Size = new System.Drawing.Size(129, 28); this.configureParentButton.TabIndex = 4; @@ -93,34 +97,14 @@ // // startButton // - this.startButton.Location = new System.Drawing.Point(89, 273); + this.startButton.Location = new System.Drawing.Point(431, 17); this.startButton.Name = "startButton"; - this.startButton.Size = new System.Drawing.Size(129, 28); - this.startButton.TabIndex = 5; - this.startButton.Text = "Start device"; + this.startButton.Size = new System.Drawing.Size(53, 46); + this.startButton.TabIndex = 7; + this.startButton.Text = "Start"; this.startButton.UseVisualStyleBackColor = true; this.startButton.Click += new System.EventHandler(this.startButton_Click); // - // stopButton - // - this.stopButton.Location = new System.Drawing.Point(89, 307); - this.stopButton.Name = "stopButton"; - this.stopButton.Size = new System.Drawing.Size(129, 28); - this.stopButton.TabIndex = 6; - this.stopButton.Text = "Stop device"; - this.stopButton.UseVisualStyleBackColor = true; - this.stopButton.Click += new System.EventHandler(this.stopButton_Click); - // - // initializeButton - // - this.initializeButton.Location = new System.Drawing.Point(89, 239); - this.initializeButton.Name = "initializeButton"; - this.initializeButton.Size = new System.Drawing.Size(129, 28); - this.initializeButton.TabIndex = 7; - this.initializeButton.Text = "Start"; - this.initializeButton.UseVisualStyleBackColor = true; - this.initializeButton.Click += new System.EventHandler(this.initializeButton_Click); - // // groupBox1 // this.groupBox1.Controls.Add(this.configureParentButton); @@ -137,6 +121,11 @@ // // groupBox2 // + this.groupBox2.Controls.Add(this.radioButton4); + this.groupBox2.Controls.Add(this.radioButton3); + this.groupBox2.Controls.Add(this.radioButton2); + this.groupBox2.Controls.Add(this.radioButton1); + this.groupBox2.Controls.Add(this.radioButton0); this.groupBox2.Controls.Add(this.configureButton); this.groupBox2.Controls.Add(this.nameTextBox); this.groupBox2.Controls.Add(this.label1); @@ -144,11 +133,71 @@ this.groupBox2.Controls.Add(this.classNameComboBox); this.groupBox2.Location = new System.Drawing.Point(12, 127); this.groupBox2.Name = "groupBox2"; - this.groupBox2.Size = new System.Drawing.Size(405, 106); + this.groupBox2.Size = new System.Drawing.Size(405, 238); this.groupBox2.TabIndex = 9; this.groupBox2.TabStop = false; this.groupBox2.Text = "Component"; // + // radioButton4 + // + this.radioButton4.AutoSize = true; + this.radioButton4.Location = new System.Drawing.Point(77, 206); + this.radioButton4.Name = "radioButton4"; + this.radioButton4.Size = new System.Drawing.Size(51, 17); + this.radioButton4.TabIndex = 9; + this.radioButton4.TabStop = true; + this.radioButton4.Text = "spare"; + this.radioButton4.UseVisualStyleBackColor = true; + this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged); + // + // radioButton3 + // + this.radioButton3.AutoSize = true; + this.radioButton3.Location = new System.Drawing.Point(77, 183); + this.radioButton3.Name = "radioButton3"; + this.radioButton3.Size = new System.Drawing.Size(51, 17); + this.radioButton3.TabIndex = 8; + this.radioButton3.TabStop = true; + this.radioButton3.Text = "spare"; + this.radioButton3.UseVisualStyleBackColor = true; + this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged); + // + // radioButton2 + // + this.radioButton2.AutoSize = true; + this.radioButton2.Location = new System.Drawing.Point(77, 160); + this.radioButton2.Name = "radioButton2"; + this.radioButton2.Size = new System.Drawing.Size(51, 17); + this.radioButton2.TabIndex = 7; + this.radioButton2.TabStop = true; + this.radioButton2.Text = "spare"; + this.radioButton2.UseVisualStyleBackColor = true; + this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged); + // + // radioButton1 + // + this.radioButton1.AutoSize = true; + this.radioButton1.Location = new System.Drawing.Point(77, 137); + this.radioButton1.Name = "radioButton1"; + this.radioButton1.Size = new System.Drawing.Size(106, 17); + this.radioButton1.TabIndex = 6; + this.radioButton1.TabStop = true; + this.radioButton1.Text = "ReadPressureOp"; + this.radioButton1.UseVisualStyleBackColor = true; + this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); + // + // radioButton0 + // + this.radioButton0.AutoSize = true; + this.radioButton0.Location = new System.Drawing.Point(77, 114); + this.radioButton0.Name = "radioButton0"; + this.radioButton0.Size = new System.Drawing.Size(94, 17); + this.radioButton0.TabIndex = 5; + this.radioButton0.TabStop = true; + this.radioButton0.Text = "Stop operation"; + this.radioButton0.UseVisualStyleBackColor = true; + this.radioButton0.CheckedChanged += new System.EventHandler(this.radioButton0_CheckedChanged); + // // configureButton // this.configureButton.Location = new System.Drawing.Point(77, 68); @@ -193,18 +242,28 @@ this.classNameComboBox.Size = new System.Drawing.Size(299, 21); this.classNameComboBox.TabIndex = 0; // + // stopButton + // + this.stopButton.Location = new System.Drawing.Point(431, 73); + this.stopButton.Name = "stopButton"; + this.stopButton.Size = new System.Drawing.Size(53, 46); + this.stopButton.TabIndex = 10; + this.stopButton.Text = "Stop"; + this.stopButton.UseVisualStyleBackColor = true; + this.stopButton.Click += new System.EventHandler(this.stopButton_Click); + // // DeviceTestDlg // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(496, 377); - this.Controls.Add(this.groupBox2); - this.Controls.Add(this.initializeButton); - this.Controls.Add(this.groupBox1); this.Controls.Add(this.stopButton); + this.Controls.Add(this.groupBox2); this.Controls.Add(this.startButton); + this.Controls.Add(this.groupBox1); this.Name = "DeviceTestDlg"; this.Text = "Device test"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DeviceTestDlg_FormClosing); this.Load += new System.EventHandler(this.DeviceTestDlg_Load); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); @@ -220,10 +279,8 @@ private System.Windows.Forms.Label classLabel; private System.Windows.Forms.Label nameLabel; private System.Windows.Forms.TextBox parentNameTextBox; - private System.Windows.Forms.Button configureParentButton; - private System.Windows.Forms.Button startButton; - private System.Windows.Forms.Button stopButton; - private System.Windows.Forms.Button initializeButton; + private System.Windows.Forms.Button configureParentButton; + private System.Windows.Forms.Button startButton; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Button configureButton; @@ -231,6 +288,12 @@ private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.ComboBox classNameComboBox; + private System.Windows.Forms.RadioButton radioButton4; + private System.Windows.Forms.RadioButton radioButton3; + private System.Windows.Forms.RadioButton radioButton2; + private System.Windows.Forms.RadioButton radioButton1; + private System.Windows.Forms.RadioButton radioButton0; + private System.Windows.Forms.Button stopButton; } } diff --git a/DeviceTest/DeviceTestDlg.cs b/DeviceTest/DeviceTestDlg.cs index a1e1fe96e..c0fe98eba 100644 --- a/DeviceTest/DeviceTestDlg.cs +++ b/DeviceTest/DeviceTestDlg.cs @@ -1,9 +1,11 @@ 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 @@ -18,16 +20,37 @@ namespace DeviceTest IComponentCfg parentCfg; IComponentCfg componentCfg; - static IList tbfComponents; static IList tbfDevices; + static IList 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(); - foreach (var componentFactory in TbfComponents.Factories) + radioButton0.Checked = true; + stopButton.Enabled = false; + + foreach (var componentFactory in TbfComponents.Factories) { compClasses.Add(componentFactory.ClassName); } @@ -35,8 +58,6 @@ namespace DeviceTest private void DeviceTestDlg_Load(object sender, EventArgs e) { - startButton.Enabled = false; - stopButton.Enabled = false; foreach (var clName in compClasses) { @@ -46,6 +67,22 @@ namespace DeviceTest } + 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; @@ -135,10 +172,16 @@ namespace DeviceTest } - private void initializeButton_Click(object sender, EventArgs e) + private void startButton_Click(object sender, EventArgs e) { tbfComponents = new List(); tbfDevices = new List(); + tbfComponent4Op = null; + operation1 = null; + operation2 = null; + operation3 = null; + operation4 = null; + workerThreadRunning = false; try { @@ -152,6 +195,7 @@ namespace DeviceTest { TBF.BenchControl.Generic.IComponent component = componentFactory.GetComponent(componentCfg, tbfComponents); tbfComponents.Add(component); + tbfComponent4Op = component; if (component is IDevice) tbfDevices.Add(component as IDevice); } @@ -162,48 +206,113 @@ namespace DeviceTest 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; - startButton.Enabled = false; - return; } - configureButton.Enabled = false; - configureParentButton.Enabled = false; - startButton.Enabled = true; -// MessageBox.Show("Hura !", "Initialization successful", MessageBoxButtons.OK, MessageBoxIcon.Information); - } - - private void startButton_Click(object sender, EventArgs e) - { - startButton.Enabled = false; - stopButton.Enabled = true; - } - - private void stopButton_Click(object sender, EventArgs e) - { - startButton.Enabled = true; - stopButton.Enabled = false; - configureButton.Enabled = false; - configureParentButton.Enabled = false; } static void Worker() { - do + 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); - foreach (var d in tbfDevices) d.RunDeviceBefore(); - foreach (var d in tbfDevices) d.RunDeviceAfter(); + + 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; } - while (true); } } } diff --git a/TestBenchFramework/BenchControl/Generic/IDevice.cs b/TestBenchFramework/BenchControl/Generic/IDevice.cs index ac3e0ffa5..0fba69b36 100644 --- a/TestBenchFramework/BenchControl/Generic/IDevice.cs +++ b/TestBenchFramework/BenchControl/Generic/IDevice.cs @@ -1,9 +1,9 @@ /// -/// Copyright (c) 2013-2015 Sensus Metering Systems +/// Copyright (c) 2013-2016 Sensus Metering Systems /// namespace TBF.BenchControl.Generic { - public interface IDevice + public interface IDevice : IComponent { void Initialize();