/// /// Copyright (c) 2015-2020 Sensus Slovensko a.s. /// using System; using System.Windows.Forms; using Common; using TBF.Resources; using TBF.Rig.Generic; namespace TBF.Rig.TestMethods.GenesisCommunication { public partial class TestMethodCfgCtrl : Configs.ConfigCtrlUtils, IComponentCfgCtrl { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(TestMethodCfgCtrl)); public bool ShowMore { get { return false; } } TestMethodCfg config; public IComponentCfg Config { get { return config as IComponentCfg; } set { config = value as TestMethodCfg; if (config == null) log.ErrorFormat("Genesis Properties configuration rejected: expected {0}, received {1}", typeof(TestMethodCfg).FullName, value == null ? "null" : value.GetType().FullName); else LogConfiguration("loaded"); Redraw(); } } public TestMethodCfgCtrl() { InitializeComponent(); InitializeSettingToolTips(); dfltQ2corrFactorsGroupBox.Visible = false; useWebServiceGroupBox.Visible = false; var factorHint = new System.Windows.Forms.Label { AutoSize = true, Location = dfltQ2corrFactorsGroupBox.Location, Text = "Q3 factors: Water meters / Text1, Text2, Text3 (CH1, CH2, CH3)." }; Controls.Add(factorHint); } private void EntryFormCfgCtrl_Load(object sender, EventArgs e) { Redraw(); } public void Closing() { } void Redraw() { if (config == null) return; /// Control was not loaded, settings were not changed classNameLabel.Text = config.Factory == null ? "TestMethods.GenesisCommunication" : config.Factory.ClassName; nameTextBox.Text = config.Name; commTimeoutTextBox.Text = config.CommTimeout.ToString(); maxCommRetriesTextBox.Text = config.MaxCommRetries.ToString(); delayBetweenRetriesTextBox.Text = config.DelayBetweenRetries.ToString(); nrThreadsTextBox.Text = config.NrThreads.ToString(); iperlCheckErrorsToStopTextBox.Text = config.IperlCheckErrorsToStop.ToString(); } public void Unlock() { nameTextBox.Enabled = true; commTimeoutTextBox.Enabled = true; maxCommRetriesTextBox.Enabled = true; delayBetweenRetriesTextBox.Enabled = true; nrThreadsTextBox.Enabled = true; iperlCheckErrorsToStopTextBox.Enabled = true; } public CfgUpdateFlags VerifyCfg(ref string message) { CfgUpdateFlags flags = CfgUpdateFlags.None; if (config == null) { message += Environment.NewLine + "Genesis configuration is not loaded."; log.Error("Genesis Properties validation failed: configuration is not loaded."); return CfgUpdateFlags.Error; } int dummy; if (!int.TryParse(commTimeoutTextBox.Text, out dummy) || dummy < 500 || dummy > 5000) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Comm. timeout' should be in range 500 .. 5000"; } if (!int.TryParse(maxCommRetriesTextBox.Text, out dummy) || dummy < 1 || dummy > 10) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Max. retries' should be in range 1 .. 10"; } if (!int.TryParse(delayBetweenRetriesTextBox.Text, out dummy) || dummy < 0 || dummy > 5000) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Delay between retries' should be in range 0 .. 5000"; } if (!int.TryParse(nrThreadsTextBox.Text, out dummy) || (dummy < 1 || dummy > 10)) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Nr. threads' should be in range 1 .. 10"; } if (!int.TryParse(iperlCheckErrorsToStopTextBox.Text, out dummy) || ((dummy < 1) || (dummy > 40))) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + string.Format(Strings.Invalid_0, iperlCheckErrorsToStopLabel.Text); } if ((flags & CfgUpdateFlags.Error) != 0) log.WarnFormat("Genesis Properties validation failed: {0}", message); return flags; } public CfgUpdateFlags UpdateCfg() { CfgUpdateFlags flags = CfgUpdateFlags.None; string message = string.Empty; if ((VerifyCfg(ref message) & CfgUpdateFlags.Error) != 0) return CfgUpdateFlags.Error; if (config.Name != nameTextBox.Text) { config.Name = nameTextBox.Text; flags |= (CfgUpdateFlags.AnyChange | CfgUpdateFlags.RestartRqrd); } var CommTimeout = config.CommTimeout; var MaxCommRetries = config.MaxCommRetries; var DelayBetweenRetries = config.DelayBetweenRetries; var NrThreads = config.NrThreads; var IperlCheckErrorsToStop = config.IperlCheckErrorsToStop; flags |= UpdateDifferent(ref CommTimeout, commTimeoutTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); flags |= UpdateDifferent(ref MaxCommRetries, maxCommRetriesTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); flags |= UpdateDifferent(ref DelayBetweenRetries, delayBetweenRetriesTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); flags |= UpdateDifferent(ref NrThreads, nrThreadsTextBox.Text, CfgUpdateFlags.RestartRqrd | CfgUpdateFlags.AnyChange); flags |= UpdateDifferent(ref IperlCheckErrorsToStop, iperlCheckErrorsToStopTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); config.CommTimeout = CommTimeout; config.MaxCommRetries = MaxCommRetries; config.DelayBetweenRetries = DelayBetweenRetries; config.NrThreads = NrThreads; config.IperlCheckErrorsToStop = IperlCheckErrorsToStop; LogConfiguration("updated; flags=" + flags); if ((flags & CfgUpdateFlags.InvokeCfgChange) != 0) { TestMethod.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.CfgChange, config)); } return flags; } private ToolTip settingsToolTip; private void InitializeSettingToolTips() { if (components == null) components = new System.ComponentModel.Container(); settingsToolTip = new ToolTip(components) { AutoPopDelay = 30000, InitialDelay = 400, ReshowDelay = 100, ShowAlways = true }; settingsToolTip.SetToolTip(nameTextBox, Strings.ResourceManager.GetString("GenesisTooltip_nameTextBox", Strings.Culture)); settingsToolTip.SetToolTip(nameLabel, settingsToolTip.GetToolTip(nameTextBox)); settingsToolTip.SetToolTip(commTimeoutTextBox, Strings.ResourceManager.GetString("GenesisTooltip_commTimeoutTextBox", Strings.Culture)); settingsToolTip.SetToolTip(commTimeoutLabel, settingsToolTip.GetToolTip(commTimeoutTextBox)); settingsToolTip.SetToolTip(maxCommRetriesTextBox, Strings.ResourceManager.GetString("GenesisTooltip_maxCommRetriesTextBox", Strings.Culture)); settingsToolTip.SetToolTip(maxNrRetriesLabel, settingsToolTip.GetToolTip(maxCommRetriesTextBox)); settingsToolTip.SetToolTip(delayBetweenRetriesTextBox, Strings.ResourceManager.GetString("GenesisTooltip_delayBetweenRetriesTextBox", Strings.Culture)); settingsToolTip.SetToolTip(delayBetweenRetriesLabel, settingsToolTip.GetToolTip(delayBetweenRetriesTextBox)); settingsToolTip.SetToolTip(nrThreadsTextBox, Strings.ResourceManager.GetString("GenesisTooltip_nrThreadsTextBox", Strings.Culture)); settingsToolTip.SetToolTip(nrThreadsLabel, settingsToolTip.GetToolTip(nrThreadsTextBox)); settingsToolTip.SetToolTip(iperlCheckErrorsToStopTextBox, Strings.ResourceManager.GetString("GenesisTooltip_iperlCheckErrorsToStopTextBox", Strings.Culture)); settingsToolTip.SetToolTip(iperlCheckErrorsToStopLabel, settingsToolTip.GetToolTip(iperlCheckErrorsToStopTextBox)); } private void LogConfiguration(string action) { log.InfoFormat("Genesis Properties {0}: Name={1}, CommTimeout={2}ms, MaxRetries={3}, DelayBetweenRetries={4}ms, NrThreads={5}, ErrorsToStop={6}; Q3 source=WaterMeterData.Text1-Text3; legacy fields preserved", action, config.Name, config.CommTimeout, config.MaxCommRetries, config.DelayBetweenRetries, config.NrThreads, config.IperlCheckErrorsToStop); } private void ManageCheckGroupBox(CheckBox chk, GroupBox grp) { /// Make sure the CheckBox isn't in the GroupBox. This will only happen the first time. if (chk.Parent == grp) { grp.Parent.Controls.Add(chk); /// Reparent the CheckBox so it's not in the GroupBox. chk.Location = new System.Drawing.Point(chk.Left + grp.Left, chk.Top + grp.Top); /// Adjust the CheckBox's location. chk.BringToFront(); /// Move the CheckBox to the top of the stacking order. } /// Enable or disable the GroupBox. grp.Enabled = chk.Checked; } private void useWebServiceCheckBox_CheckedChanged(object sender, EventArgs e) { useWebServiceGroupBox.Enabled = useWebServiceCheckBox.Checked; } } }