From b608fb77b903e1bd90e091318e42b7a72a6e07d5 Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Tue, 18 Oct 2016 13:15:15 +0200 Subject: [PATCH] DeviceTest saves settings of both components, it's PERFECT !!! --- DeviceTest/DeviceTest.csproj | 1 + DeviceTest/DeviceTestDlg.Designer.cs | 32 ++--- DeviceTest/DeviceTestDlg.cs | 171 +++++++++++++++++++++------ DeviceTest/LocalSettings.cs | 115 ++++++++++++++++++ DeviceTest/Program.cs | 87 ++++++++++++-- 5 files changed, 343 insertions(+), 63 deletions(-) create mode 100644 DeviceTest/LocalSettings.cs diff --git a/DeviceTest/DeviceTest.csproj b/DeviceTest/DeviceTest.csproj index 75e9b2409..e5f004099 100644 --- a/DeviceTest/DeviceTest.csproj +++ b/DeviceTest/DeviceTest.csproj @@ -56,6 +56,7 @@ DeviceTestDlg.cs + diff --git a/DeviceTest/DeviceTestDlg.Designer.cs b/DeviceTest/DeviceTestDlg.Designer.cs index e82293350..d60efef68 100644 --- a/DeviceTest/DeviceTestDlg.Designer.cs +++ b/DeviceTest/DeviceTestDlg.Designer.cs @@ -42,10 +42,10 @@ 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.componentNameTextBox = 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.componentClassNameComboBox = new System.Windows.Forms.ComboBox(); this.stopButton = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); @@ -127,10 +127,10 @@ 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.componentNameTextBox); this.groupBox2.Controls.Add(this.label1); this.groupBox2.Controls.Add(this.label2); - this.groupBox2.Controls.Add(this.classNameComboBox); + this.groupBox2.Controls.Add(this.componentClassNameComboBox); this.groupBox2.Location = new System.Drawing.Point(12, 127); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(405, 238); @@ -210,11 +210,11 @@ // // nameTextBox // - this.nameTextBox.Enabled = false; - this.nameTextBox.Location = new System.Drawing.Point(77, 44); - this.nameTextBox.Name = "nameTextBox"; - this.nameTextBox.Size = new System.Drawing.Size(299, 20); - this.nameTextBox.TabIndex = 3; + this.componentNameTextBox.Enabled = false; + this.componentNameTextBox.Location = new System.Drawing.Point(77, 44); + this.componentNameTextBox.Name = "nameTextBox"; + this.componentNameTextBox.Size = new System.Drawing.Size(299, 20); + this.componentNameTextBox.TabIndex = 3; // // label1 // @@ -236,11 +236,11 @@ // // classNameComboBox // - this.classNameComboBox.FormattingEnabled = true; - this.classNameComboBox.Location = new System.Drawing.Point(77, 19); - this.classNameComboBox.Name = "classNameComboBox"; - this.classNameComboBox.Size = new System.Drawing.Size(299, 21); - this.classNameComboBox.TabIndex = 0; + this.componentClassNameComboBox.FormattingEnabled = true; + this.componentClassNameComboBox.Location = new System.Drawing.Point(77, 19); + this.componentClassNameComboBox.Name = "classNameComboBox"; + this.componentClassNameComboBox.Size = new System.Drawing.Size(299, 21); + this.componentClassNameComboBox.TabIndex = 0; // // stopButton // @@ -284,10 +284,10 @@ private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Button configureButton; - private System.Windows.Forms.TextBox nameTextBox; + private System.Windows.Forms.TextBox componentNameTextBox; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; - private System.Windows.Forms.ComboBox classNameComboBox; + private System.Windows.Forms.ComboBox componentClassNameComboBox; private System.Windows.Forms.RadioButton radioButton4; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.RadioButton radioButton2; diff --git a/DeviceTest/DeviceTestDlg.cs b/DeviceTest/DeviceTestDlg.cs index c0fe98eba..19acc135c 100644 --- a/DeviceTest/DeviceTestDlg.cs +++ b/DeviceTest/DeviceTestDlg.cs @@ -1,11 +1,14 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; +using System.Text; using System.Threading; using System.Windows.Forms; using TBF.BenchControl; using TBF.BenchControl.Generic; using TBF.BenchControl.GenericDevices; +using System.Xml.Serialization; namespace DeviceTest @@ -41,6 +44,7 @@ namespace DeviceTest static DeviceTestDlg() { floatBox = new TBF.Boxes.FloatBox(); + floatBox.Format = "F2"; } public DeviceTestDlg() @@ -62,10 +66,89 @@ namespace DeviceTest foreach (var clName in compClasses) { parentClassNameComboBox.Items.Add(clName); - classNameComboBox.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 = parentClassNameComboBox.Text; + 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; } } + 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.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(); } @@ -75,34 +158,52 @@ namespace DeviceTest int GetActiveOperationId() { - if (radioButton1.Checked) return 1; + 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) + /// + /// Updates component factory + /// + /// Component class name + /// Reference to a factory + /// true when factory changed + bool FactoryFromClassName(string className, ref IComponentFactory factory) + { + foreach (var fac in TbfComponents.Factories) { - if (parentClassNameComboBox.Text == factory.ClassName) + if (className == fac.ClassName) { - if (parentFactory != factory) + if (factory != fac) { - parentFactory = factory; - factoryChanged = true; + factory = fac; + return true; + } + else + { + return false; } - classNotFound = false; - break; } } - if (classNotFound) + + if (factory != null) { - MessageBox.Show("Invalid parent component class", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + 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); return; } @@ -128,24 +229,10 @@ namespace DeviceTest private void configureButton_Click(object sender, EventArgs e) { - bool factoryChanged = false; - bool classNotFound = true; - foreach (var factory in TbfComponents.Factories) + bool factoryChanged = FactoryFromClassName(componentClassNameComboBox.Text, ref componentFactory); + if (componentFactory == null) { - 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); + MessageBox.Show("Invalid component class", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } @@ -168,7 +255,7 @@ namespace DeviceTest DialogResult dr = cfgForm.ShowDialog(); if (dr != DialogResult.OK) return; - nameTextBox.Text = componentCfg.Name; + componentNameTextBox.Text = componentCfg.Name; } @@ -201,8 +288,10 @@ namespace DeviceTest if (tbfComponents.Count == 0) throw new Exception("There are no components"); - foreach (var d in tbfDevices) d.Initialize(); + foreach (var d in tbfDevices) { d.Initialize(); Debug.WriteLine("{0}.Initialize()", d.Name); } + SaveSettings(); /// Save settings after successful initialization + workerThread = new Thread(Worker); workerThread.CurrentCulture = Thread.CurrentThread.CurrentCulture; workerThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture; @@ -267,7 +356,7 @@ namespace DeviceTest { switch (activeOperationLocal) { - case 1: if (operation1 != null) evnt = operation1.Run(); Debug.WriteLine("op1.Run() -> {0}", evnt); break; + 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; @@ -295,10 +384,14 @@ namespace DeviceTest 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; @@ -307,12 +400,18 @@ namespace DeviceTest 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; } } } diff --git a/DeviceTest/LocalSettings.cs b/DeviceTest/LocalSettings.cs new file mode 100644 index 000000000..1a04b1fc1 --- /dev/null +++ b/DeviceTest/LocalSettings.cs @@ -0,0 +1,115 @@ +using System; +using System.IO; +using System.Text; +using System.Xml.Serialization; + +namespace DeviceTest +{ + /// + /// Class to be serialized to an XML file... + /// + [XmlRootAttribute("DeviceTest")] + public class LocalSettings + { + public string ParentClassName; + public string ParentName; + public string ParentSettings; + + public string ComponentClassName; + public string ComponentName; + public string ComponentSettings; + + public LocalSettings() + { + } + + /// + /// Load public fields of this class from the XML file. + /// + /// LocalSettings object or null when Load() fails + public static LocalSettings Load(string fileName) + { + try + { + using (TextReader reader = new StreamReader(fileName)) + { + LocalSettings ls = (new XmlSerializer(typeof(LocalSettings))).Deserialize(reader) as LocalSettings; + // Copy the settings just in case De-serialize() completes OK + return ls; + } + } + catch (Exception e) + { + string msg = e.Message; + return null; + } + } + + /// + /// Save public fields of this class to the XML file. + /// + public void Save() + { + try + { + using (TextWriter writer = new StreamWriter(Program.LocalSettingsFileName)) + { + (new XmlSerializer(typeof(LocalSettings))).Serialize(writer, this); + } + } + catch (Exception e) + { + string msg = e.Message; + } + } + + /// + /// Updates history stored in a string array by a new latest string. + /// + /// Last entered string + /// true = updated and saved + public bool UpdateHistory(string lastStrValue, ref string[] history) + { + const int MaxHistoryLen = 10; + + if (string.IsNullOrEmpty(lastStrValue)) return false; + + int currentHistoryLength = (history != null) ? history.Length : 0; + + int match = -1; + for (int i = 0; i < currentHistoryLength; i++) + { + if (history[i] == lastStrValue) + { + match = i; + break; + } + } + + if (match >= 0 || currentHistoryLength >= MaxHistoryLen) + { + /// History does not have to be extended (because of a match) or should not be extended (because of the lenght) + if (match < 0) match = currentHistoryLength - 1; + + for (int j = match; j > 0; j--) + { + history[j] = history[j - 1]; + } + history[0] = lastStrValue; + } + else + { + /// History will be extended, new item inserted at the beginning + string[] newHistory = new string[currentHistoryLength + 1]; + newHistory[0] = lastStrValue; + for (int j = 1; j <= currentHistoryLength; j++) + { + newHistory[j] = history[j - 1]; + } + history = newHistory; + } + Save(); + return true; + } + } +} diff --git a/DeviceTest/Program.cs b/DeviceTest/Program.cs index 0e8fa92d0..78cd15d7b 100644 --- a/DeviceTest/Program.cs +++ b/DeviceTest/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Windows.Forms; @@ -7,15 +8,79 @@ namespace DeviceTest { static class Program { - /// - /// The main entry point for the application. - /// - [STAThread] - static void Main() - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new DeviceTestDlg()); - } - } + /// Program information + public static string Version; /// version string + public static DateTime BuildDateTime; /// date and time of program build + + /// Local settings + public static readonly string LocAppDataDir; + public static readonly string LocalSettingsFileName; + public static readonly string LocalSettingsBackupName; + public static LocalSettings LocalSettings; + + static Program() + { + /// Program version string + System.Reflection.Assembly thisAssembly = System.Reflection.Assembly.GetExecutingAssembly(); + Version ver = thisAssembly.GetName().Version; + Version = string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build); + BuildDateTime = new System.IO.FileInfo(thisAssembly.Location).LastWriteTime; + + /// Local program configuration directory including the trailing backslash + LocAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + + Path.DirectorySeparatorChar + "TestBenchFramework" + Path.DirectorySeparatorChar; + LocalSettingsFileName = LocAppDataDir + "DeviceTest.xml"; + LocalSettingsBackupName = LocAppDataDir + "DeviceTest.backup.xml"; + } + + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + /// + /// Load the local settings + /// + Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName); + if (Program.LocalSettings == null) + { + /// Loading local seetings from regular config file failed. Use the backup + Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName); + if (Program.LocalSettings == null) + { + MessageBox.Show("Na tomto počítači nebol tento program ani raz spustený.\r\nPoužijú sa základné nastavenia.", "Upozornenie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + Program.LocalSettings = new LocalSettings(); + Directory.CreateDirectory(LocAppDataDir); + Program.LocalSettings.Save(); + } + else + { + LocalSettings.Save(); /// Save the settings to overwrite the wrong file + } + } + else + { + /// Loading local seetings from the regular config file was successful. Update the backup + File.Copy(Program.LocalSettingsFileName, Program.LocalSettingsBackupName, true); + } + + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + + try + { + DeviceTestDlg dlg = new DeviceTestDlg(); + Application.Run(dlg); + } + catch (Exception exc) + { + MessageBox.Show("Program havaroval:" + + Environment.NewLine + Environment.NewLine + exc.Message + + ((exc.InnerException == null) ? string.Empty : (Environment.NewLine + exc.InnerException.Message)) + + Environment.NewLine + exc.StackTrace, + "Pozor!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + } + } + } }