From f7d320bcc6651d0c85bad73b52b14ba050a40c18 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Sun, 9 Dec 2018 16:59:07 +0200 Subject: Refactored PPC firmware upgrade. --- .../MachineUpdate/MachineUpdateManager.cs | 160 ++++++++++++--------- 1 file changed, 92 insertions(+), 68 deletions(-) (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs index 5fcb7e209..4932d1e72 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs @@ -15,7 +15,9 @@ using Tango.Core.Helpers; using Tango.Core.IO; using Tango.PMR.Synchronization; using Tango.PPC.Common.Application; +using Tango.PPC.Common.Connection; using Tango.Settings; +using Tango.SharedUI.Helpers; using Tango.SQLExaminer; using Tango.Transport.Web; @@ -24,6 +26,7 @@ namespace Tango.PPC.Common.MachineUpdate public class MachineUpdateManager : ExtendedObject, IMachineUpdateManager { private IPPCApplicationManager _app_manager; + private IMachineProvider _machineProvider; #region Events @@ -33,51 +36,19 @@ namespace Tango.PPC.Common.MachineUpdate public event EventHandler ProgressLog; /// - /// Occurs when the has changed. + /// Occurs when the update has made some progress. /// - public event EventHandler ProgressStep; + public event EventHandler Progress; #endregion #region Properties - private MachineUpdateSteps _currentStep; - /// - /// Gets the current setup step. - /// - public MachineUpdateSteps CurrentStep - { - get { return _currentStep; } - set - { - if (_currentStep != value) - { - _currentStep = value; - RaisePropertyChangedAuto(); - ProgressStep?.Invoke(this, _currentStep); - LogManager.Log("Machine Setup Manager Step: " + value.ToString()); - } - } - } - - private double _downloadProgress; - /// - /// Gets the downloading packages step progress. - /// - public double DownloadingPackagesProgress - { - get { return _downloadProgress; } - private set { _downloadProgress = value; RaisePropertyChangedAuto(); } - } - - private String _updatingPackagesStatus; - /// - /// Gets the downloading packages step status. - /// - public String DownloadingPackagesStatus + private MachineUpdateProgress _status; + public MachineUpdateProgress Status { - get { return _updatingPackagesStatus; } - set { _updatingPackagesStatus = value; RaisePropertyChangedAuto(); } + get { return _status; } + private set { _status = value; RaisePropertyChangedAuto(); } } #endregion @@ -88,8 +59,9 @@ namespace Tango.PPC.Common.MachineUpdate /// Initializes a new instance of the class. /// /// The application manager. - public MachineUpdateManager(IPPCApplicationManager applicationManager) + public MachineUpdateManager(IPPCApplicationManager applicationManager, IMachineProvider machineProvider) { + _machineProvider = machineProvider; _app_manager = applicationManager; } @@ -103,17 +75,16 @@ namespace Tango.PPC.Common.MachineUpdate /// The serial number. /// The machine service address. /// - public Task Update(string serialNumber, string machineServiceAddress) + public async Task Update(string serialNumber, string machineServiceAddress) { - return Task.Factory.StartNew(() => - { + TaskCompletionSource result = new TaskCompletionSource(); + try + { LogManager.Log($"Starting machine update for serial number {serialNumber}..."); //Connect to machine service and get matching packages for this machine. - CurrentStep = MachineUpdateSteps.DownloadingPackage; - DownloadingPackagesProgress = 0; - DownloadingPackagesStatus = "Connecting to machine service..."; + UpdateProgress("Downloading software package", "Connecting to machine service..."); LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); @@ -124,7 +95,7 @@ namespace Tango.PPC.Common.MachineUpdate using (var http = new ProtoWebClient()) { - update_response = http.Post(machineServiceAddress + "/api/Synchronization/MachineUpdate", request).Result; + update_response = await http.Post(machineServiceAddress + "/api/Synchronization/MachineUpdate", request); } LogManager.Log($"Machine update response received: {Environment.NewLine}{update_response.ToJsonString()}"); @@ -140,19 +111,14 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Temporary package zip file created: {tempFile}."); - DownloadingPackagesStatus = "Downloading software package..."; - LogManager.Log("Downloading software package..."); long fileSize = 0; - DownloadingPackagesProgress = 0; + UpdateProgress("Downloading software package", "Downloading...", false); using (FileStreamWrapper fs = new FileStreamWrapper(tempFile.Path, FileMode.Create, (current) => { - InvokeUINow(() => - { - DownloadingPackagesProgress = ((double)current / (double)fileSize) * 100d; - }); + UpdateProgress("Downloading software package", "Downloading...", false, current, fileSize); })) { LogManager.Log($"Connecting to storage blob with address {update_response.BlobAddress}"); @@ -165,6 +131,8 @@ namespace Tango.PPC.Common.MachineUpdate blob.DownloadToStream(fs); } + UpdateProgress("Downloading software package", "Extracting package..."); + LogManager.Log("Extracting downloaded zip file..."); //Extract software package. ZipFile.ExtractToDirectory(tempFile, _newPackageTempFolder); @@ -176,7 +144,7 @@ namespace Tango.PPC.Common.MachineUpdate //Synchronize database - CurrentStep = MachineUpdateSteps.SynchronizingSchema; + UpdateProgress("Updating Database", "Initializing..."); String db_name = "Tango"; String localAddress = SettingsManager.Default.GetOrCreate().DataSource.Address; @@ -184,6 +152,7 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Synchronizing database '{remote_address}\\{db_name}' => '{localAddress}\\{db_name}'..."); + UpdateProgress("Updating Database", "Connecting to local database..."); LogManager.Log("Initializing database manager..."); DbManager db = DbManager.FromAddressAndName(localAddress, db_name); @@ -198,6 +167,8 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Initializing {nameof(ExaminerSequenceConfigurationRunner)}..."); + UpdateProgress("Updating Database", "Initializing provisioning sequence..."); + ExaminerSequenceConfigurationRunner runner = new ExaminerSequenceConfigurationRunner( Path.Combine(_newPackageTempFolder, "Update Scripts", "config.xml"), Path.Combine(_newPackageTempFolder, "Update Scripts"), @@ -225,34 +196,64 @@ namespace Tango.PPC.Common.MachineUpdate runner.ScriptExecuting += (x, item) => { LogManager.Log($"Executing script {item.ToString()}..."); - - if (item.Type == ExaminerSequenceItemType.Data && item.RequiresSerialNumber) - { - CurrentStep = MachineUpdateSteps.SynchronizingMachineConfiguration; - } - else if (item.Type == ExaminerSequenceItemType.Data) - { - CurrentStep = MachineUpdateSteps.SynchronizingData; - } + UpdateProgress("Updating Database", item.Name + "..."); }; LogManager.Log("Starting synchronization process..."); try { - runner.Run().Wait(); + await runner.Run(); LogManager.Log("Synchronization completed successfully!"); + UpdateProgress("Updating Database", "Database synchronization completed successfully."); } catch (Exception ex) { throw LogManager.Log(ex, "Setup manager error while trying to synchronize database."); } - return new MachineUpdateResult() + //Updating firmware + UpdateProgress("Updating Firmware", "Connecting to firmware device..."); + LogManager.Log(""); + LogManager.Log("-------------------------------------------------------------------------"); + LogManager.Log("Updating Firmware..."); + var op = _machineProvider.MachineOperator; + + UpdateProgress("Updating Firmware", "Loading firmware package..."); + var tfpPath = Path.Combine(_newPackageTempFolder, "firmware_package.tfp"); + var stream = new FileStream(tfpPath, FileMode.Open); + var handler = await op.UpgradeFirmware(stream); + handler.Failed += (_, ex) => { - UpdatePackagePath = _newPackageTempFolder, + stream.Dispose(); + result.SetException(ex); }; - }); + handler.Completed += (_, __) => + { + UpdateProgress("Updating Firmware", "Firmware update completed successfully."); + stream.Dispose(); + result.SetResult(new MachineUpdateResult() + { + UpdatePackagePath = _newPackageTempFolder, + }); + }; + handler.Canceled += (_, __) => + { + stream.Dispose(); + result.SetException(new Exception("The operation has been canceled.")); + }; + handler.Progress += (_, e) => + { + UpdateProgress("Updating Firmware", e.Message, false, e.Current, e.Total); + }; + } + catch (Exception ex) + { + LogManager.Log(ex, "An error occurred in machine update."); + result.SetException(ex); + } + + return await result.Task; } /// @@ -485,7 +486,7 @@ namespace Tango.PPC.Common.MachineUpdate /// public Task GetUpdatePackageFileInfo(string filePath) { - return Task.Factory.StartNew(() => + return Task.Factory.StartNew(() => { UpdatePackageFile file = new UpdatePackageFile(); var tempFolder = TemporaryManager.CreateFolder(); @@ -506,5 +507,28 @@ namespace Tango.PPC.Common.MachineUpdate } #endregion + + #region Protected Methods + + protected virtual void UpdateProgress(String name, String message = "", bool isIntermediate = true, double progress = 0, double total = 0) + { + InvokeUINow(() => + { + Status = new MachineUpdateProgress() + { + Name = name, + Message = message, + IsIntermediate = isIntermediate, + Progress = progress, + Total = total, + }; + + Progress?.Invoke(this, Status); + }); + + UIHelper.DoEvents(); + } + + #endregion } } -- cgit v1.3.1