diff options
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/Connection/DefaultMachineProvider.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Common/Connection/DefaultMachineProvider.cs | 136 |
1 files changed, 123 insertions, 13 deletions
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 a16e2f649..b2c752ca8 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 { @@ -35,6 +36,26 @@ namespace Tango.PPC.Common.Connection private Thread _connection_thread; private ObservablesContext _context; + /// <summary> + /// Occurs when the machine has connected. + /// </summary> + public event EventHandler MachineConnected; + + /// <summary> + /// Occurs when the machine has disconnected. + /// </summary> + public event EventHandler MachineDisconnected; + + private bool _isConnected; + /// <summary> + /// Gets a value indicating whether the machine is currently connected. + /// </summary> + public bool IsConnected + { + get { return _isConnected; } + private set { _isConnected = value; RaisePropertyChangedAuto(); } + } + private Machine _machine; /// <summary> /// Gets the database machine entity associated with the current machine. @@ -74,18 +95,24 @@ namespace Tango.PPC.Common.Connection /// </summary> public DefaultMachineProvider() { + var settings = SettingsManager.Default.GetOrCreate<PPCSettings>(); + MachineOperator = new MachineOperator(); + MachineOperator.StatusChanged += MachineOperator_StatusChanged; MachineOperator.EnableEventsNotification = true; MachineOperator.EnableJobResume = true; MachineOperator.UseKeepAlive = true; MachineOperator.EnableMachineStatusUpdates = true; - MachineOperator.EnableDiagnostics = false; - MachineOperator.EnableEmbeddedDebugging = false; - MachineOperator.FirmwareUpgradeMode = Integration.Upgrade.FirmwareUpgradeModes.DFU | Integration.Upgrade.FirmwareUpgradeModes.TFP_PACKAGE; + MachineOperator.EnableDiagnostics = true; + MachineOperator.EnablePowerUpSequence = true; + MachineOperator.EnableEmbeddedDebugging = settings.EnableEmbeddedDebugLogs; + MachineOperator.EnableAutomaticThreadLoading = settings.EnableAutomaticThreadLoading; + MachineOperator.JobRunsLogger.JobSource = BL.Enumerations.JobSource.Local; - var settings = SettingsManager.Default.GetOrCreate<PPCSettings>(); + MachineOperator.FirmwareUpgradeMode = Integration.Upgrade.FirmwareUpgradeModes.DFU | Integration.Upgrade.FirmwareUpgradeModes.TFP_PACKAGE; MachineOperator.JobUploadStrategy = settings.JobUploadStrategy; + MachineOperator.JobUnitsMethod = settings.JobUnitsMethod; MachineOperator.GradientGenerationConfiguration.IsEnabled = settings.EnableGradientGeneration; MachineOperator.GradientGenerationConfiguration.ResolutionCM = settings.GradientGenerationResolution; @@ -96,8 +123,28 @@ namespace Tango.PPC.Common.Connection MachineOperator.EnableJobLiquidQuantityValidation = settings.EnableJobLiquidQuantityValidation; } + private void MachineOperator_StatusChanged(object sender, MachineStatuses status) + { + if (status != MachineStatuses.Disconnected) + { + if (!IsConnected) + { + OnMachineConnected(); + } + } + else + { + if (IsConnected) + { + OnMachineDisconnected(); + } + } + } + private async void ConnectionThreadMethod() { + bool fileLoggingDisabled = false; + while (true) { if (MachineOperator.State != TransportComponentState.Connected) @@ -106,7 +153,10 @@ namespace Tango.PPC.Common.Connection { Thread.Sleep(2000); - LogManager.Log("Starting machine connection procedure...", LogCategory.Debug); + if (!fileLoggingDisabled) + { + LogManager.Log("Starting machine connection procedure...", LogCategory.Info); + } var settings = SettingsManager.Default.GetOrCreate<PPCSettings>(); @@ -116,22 +166,28 @@ namespace Tango.PPC.Common.Connection { TimeSpan timeout = TimeSpan.FromSeconds(SettingsManager.Default.GetOrCreate<PPCSettings>().MachineScanningTimeoutSeconds); - LogManager.Log("Scanning for machine on available serial ports...", LogCategory.Debug); + LogManager.Log("Scanning for machine on available serial ports...", LogCategory.Info); Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse> scanner = new Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse>(UsbSerialBaudRates.BR_115200); var response = await scanner.Scan(new ConnectRequest() { Password = "1234" }, settings.EmbeddedDeviceHint, timeout); - LogManager.Log("Machine discovered on port: " + response.Adapter.Address, LogCategory.Debug); + LogManager.Log("Machine discovered on port: " + response.Adapter.Address, LogCategory.Info); LogManager.Log("Device Information:", LogCategory.Debug); - LogManager.Log(response.Response.DeviceInformation.ToJsonString(), LogCategory.Debug); + LogManager.Log(response.Response.DeviceInformation.ToJsonString(), LogCategory.Info); - LogManager.Log("Disconnecting machine operator...", LogCategory.Debug); + LogManager.Log("Disconnecting machine operator...", LogCategory.Info); await MachineOperator.Disconnect(); MachineOperator.Adapter = response.Adapter; MachineOperator.JobHandlingMode = JobHandlerModes.SettingUp; - LogManager.Log("Connecting machine operator...", LogCategory.Debug); + LogManager.Log("Connecting machine operator...", LogCategory.Info); try { await MachineOperator.Connect(); + + if (MachineOperator.DeviceInformation != null) + { + settings.FirmwareVersion = MachineOperator.DeviceInformation.Version; + settings.Save(); + } } catch (Exception) { @@ -146,7 +202,17 @@ namespace Tango.PPC.Common.Connection } else { - LogManager.Log($"Connecting to machine on {settings.EmbeddedComPort}...", LogCategory.Debug); + //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); MachineOperator.Adapter = adapter; @@ -154,6 +220,12 @@ namespace Tango.PPC.Common.Connection try { await MachineOperator.Connect(); + + if (MachineOperator.DeviceInformation != null) + { + settings.FirmwareVersion = MachineOperator.DeviceInformation.Version; + settings.Save(); + } } catch (Exception) { @@ -184,13 +256,24 @@ namespace Tango.PPC.Common.Connection LogManager.Log("Connecting machine operator..."); await MachineOperator.Connect(); + if (MachineOperator.DeviceInformation != null) + { + settings.FirmwareVersion = MachineOperator.DeviceInformation.Version; + settings.Save(); + } + await Task.Delay(1000); await MachineOperator.UploadHardwareConfiguration(Machine.Configuration.HardwareVersion, Machine.Configuration); } } catch (Exception ex) { - LogManager.Log(ex, LogCategory.Debug, "Error while trying to scan and connect to the machine."); + if (!fileLoggingDisabled || LogManager.Categories.Contains(LogCategory.Debug)) + { + 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; + } } } @@ -217,6 +300,15 @@ namespace Tango.PPC.Common.Connection if (Machine != null) { LogManager.Log("First machine entry found. Machine serial number is: " + Machine.SerialNumber + "."); + + if (Machine.IsDemo) + { + LogManager.Log("Machine is in demo mode. Changing firmware upgrade mode to TFP package only."); + MachineOperator.FirmwareUpgradeMode = Integration.Upgrade.FirmwareUpgradeModes.TFP_PACKAGE; + } + + MachineOperator.JobRunsLogger.SetDefaultMachine(Machine); + ConnectToMachine(); } else @@ -247,7 +339,7 @@ namespace Tango.PPC.Common.Connection public async Task SaveMachine() { await _context.SaveChangesAsync(); - Machine = await new MachineBuilder(_context).SetFirst().WithSettings().BuildAsync(); + Machine = await new MachineBuilder(_context).SetFirst().BuildAsync(); TangoMessenger.Default.Send(new MachineSettingsSavedMessage() { Machine = Machine }); } @@ -304,5 +396,23 @@ namespace Tango.PPC.Common.Connection return machineOperator; } + + /// <summary> + /// Called when the machine has connected. + /// </summary> + protected virtual void OnMachineConnected() + { + IsConnected = true; + MachineConnected?.Invoke(this, new EventArgs()); + } + + /// <summary> + /// Called when the machine has disconnected. + /// </summary> + protected virtual void OnMachineDisconnected() + { + IsConnected = false; + MachineDisconnected?.Invoke(this, new EventArgs()); + } } } |
