From 66dc46ad7547c9d51fef406186a705dce0ccd7a3 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sun, 30 Aug 2020 15:23:44 +0300 Subject: Improved connection attempts file logging optimization by pre-testing using a simple open/close serial port. Logs can still be viewed using "Debug" on tech module. --- .../Connection/DefaultMachineProvider.cs | 42 ++++++++++------------ .../Visual_Studio/PPC/Tango.PPC.UI/app.manifest | 2 +- 2 files changed, 20 insertions(+), 24 deletions(-) (limited to 'Software/Visual_Studio/PPC') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Connection/DefaultMachineProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Connection/DefaultMachineProvider.cs index 794e37694..244d26f28 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Connection/DefaultMachineProvider.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Connection/DefaultMachineProvider.cs @@ -21,6 +21,7 @@ using Tango.Integration; using Tango.Transport; using System.Threading; using Tango.Core.ExtensionMethods; +using System.IO.Ports; namespace Tango.PPC.Common.Connection { @@ -34,7 +35,6 @@ namespace Tango.PPC.Common.Connection private bool _isInitialized; private Thread _connection_thread; private ObservablesContext _context; - private bool disableConnectionFileLogging; /// /// Occurs when the machine has connected. @@ -143,23 +143,21 @@ namespace Tango.PPC.Common.Connection private async void ConnectionThreadMethod() { + bool fileLoggingDisabled = false; + while (true) { if (MachineOperator.State != TransportComponentState.Connected) { - var fileLogger = LogManager.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)); - try { Thread.Sleep(2000); - if (fileLogger != null && disableConnectionFileLogging) + if (!fileLoggingDisabled) { - fileLogger.Enabled = false; + LogManager.Log("Starting machine connection procedure...", LogCategory.Info); } - LogManager.Log("Starting machine connection procedure...", LogCategory.Info); - var settings = SettingsManager.Default.GetOrCreate(); if (!Machine.IsDemo) @@ -190,8 +188,6 @@ namespace Tango.PPC.Common.Connection settings.FirmwareVersion = MachineOperator.DeviceInformation.Version; settings.Save(); } - - disableConnectionFileLogging = false; } catch (Exception) { @@ -206,6 +202,16 @@ namespace Tango.PPC.Common.Connection } else { + //Perform a pre-test to not overload the log file when machine is off for a long time. + using (SerialPort preCheckSerialPort = new SerialPort(settings.EmbeddedComPort)) + { + preCheckSerialPort.BaudRate = UsbSerialBaudRates.BR_115200.ToInt32(); + preCheckSerialPort.Open(); + preCheckSerialPort.Close(); + fileLoggingDisabled = false; + Thread.Sleep(500); //Wait a little while to not scare the other side?.. + } + LogManager.Log($"Connecting to machine on {settings.EmbeddedComPort}...", LogCategory.Info); UsbTransportAdapter adapter = new UsbTransportAdapter(settings.EmbeddedComPort, UsbSerialBaudRates.BR_115200); @@ -220,8 +226,6 @@ namespace Tango.PPC.Common.Connection settings.FirmwareVersion = MachineOperator.DeviceInformation.Version; settings.Save(); } - - disableConnectionFileLogging = false; } catch (Exception) { @@ -252,8 +256,6 @@ namespace Tango.PPC.Common.Connection LogManager.Log("Connecting machine operator..."); await MachineOperator.Connect(); - disableConnectionFileLogging = false; - if (MachineOperator.DeviceInformation != null) { settings.FirmwareVersion = MachineOperator.DeviceInformation.Version; @@ -266,17 +268,11 @@ namespace Tango.PPC.Common.Connection } catch (Exception ex) { - LogManager.Log(ex, "Error while trying to scan and connect to the machine."); - LogManager.Log("File logging of further connection attempts is now disabled and will resume when connection is successful."); - disableConnectionFileLogging = true; - } - finally - { - await Task.Delay(100); - - if (fileLogger != null) + if (!fileLoggingDisabled || LogManager.Categories.Contains(LogCategory.Debug)) { - fileLogger.Enabled = true; + LogManager.Log(ex, "Error while trying to scan and connect to the machine."); + LogManager.Log("Application logging of further connection attempts is now disabled and will resume when connection is successful."); + fileLoggingDisabled = true; } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/app.manifest b/Software/Visual_Studio/PPC/Tango.PPC.UI/app.manifest index d72e75011..efc5f8179 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/app.manifest +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/app.manifest @@ -16,7 +16,7 @@ Remove this element if your application requires this virtualization for backwards compatibility. --> - + -- cgit v1.3.1