diff options
| author | Shlomo Hecht <shlomo@twine-s.com> | 2019-02-25 11:02:07 +0200 |
|---|---|---|
| committer | Shlomo Hecht <shlomo@twine-s.com> | 2019-02-25 11:02:07 +0200 |
| commit | b462fdd744f491b04137fd99863f6cb6c623cfa0 (patch) | |
| tree | 2f69f8e953450a0452442c24f3809aec84ce7db0 /Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs | |
| parent | a8ddb23154fe5111c9509fa0a83dcc36adcba4ee (diff) | |
| parent | 44a35db8a2929fb3f6aa56ed08185e066292f106 (diff) | |
| download | Tango-b462fdd744f491b04137fd99863f6cb6c623cfa0.tar.gz Tango-b462fdd744f491b04137fd99863f6cb6c623cfa0.zip | |
Merge branch 'master' of https://twinetfs.visualstudio.com/Tango/_git/Tango
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs | 250 |
1 files changed, 147 insertions, 103 deletions
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 8b9aede87..64827f15f 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs @@ -113,13 +113,19 @@ namespace Tango.PPC.Common.MachineUpdate await Task.Delay(1000); IMachineOperator op = _machineProvider.MachineOperator; - if (op.State != Transport.TransportComponentState.Connected) - { - throw new InvalidOperationException("Could not perform an update while the machine is not connected."); - } - if (op.Status != MachineStatuses.ReadyToDye) + + if (_machineProvider.Machine.SetupFirmware) { - throw new InvalidOperationException($"Could not perform an update while the machine is in a {op.Status} status."); + LogManager.Log("Machine is configured to update firmware..."); + + if (op.State != Transport.TransportComponentState.Connected) + { + throw LogManager.Log(new InvalidOperationException("Could not perform an update while the machine is not connected.")); + } + if (op.Status != MachineStatuses.ReadyToDye) + { + throw LogManager.Log(new InvalidOperationException($"Could not perform an update while the machine is in a {op.Status} status.")); + } } //Connect to machine service and get matching packages for this machine. @@ -130,6 +136,7 @@ namespace Tango.PPC.Common.MachineUpdate await Login(serialNumber); DownloadUpdateRequest request = new DownloadUpdateRequest(); + request.SerialNumber = serialNumber; DownloadUpdateResponse update_response = null; @@ -202,7 +209,7 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Initializing {nameof(ExaminerSequenceConfigurationRunner)}..."); - UpdateProgress("Updating Database", "Initializing provisioning sequence..."); + UpdateProgress("Updating Database", "Initializing update sequence..."); ExaminerSequenceConfigurationRunner runner = new ExaminerSequenceConfigurationRunner( Path.Combine(_newPackageTempFolder, "Update Scripts", "config.xml"), @@ -237,38 +244,48 @@ namespace Tango.PPC.Common.MachineUpdate } //Updating firmware - UpdateProgress("Updating Firmware", "Connecting to firmware device..."); - LogManager.Log(""); - LogManager.Log("-------------------------------------------------------------------------"); - LogManager.Log("Updating Firmware..."); - - 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) => + if (_machineProvider.Machine.SetupFirmware) { - stream.Dispose(); - result.SetException(ex); - }; - handler.Completed += (_, __) => + UpdateProgress("Updating Firmware", "Connecting to firmware device..."); + LogManager.Log(""); + LogManager.Log("-------------------------------------------------------------------------"); + LogManager.Log("Updating Firmware..."); + + 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) => + { + 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); + }; + } + else { - 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) { @@ -298,6 +315,7 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Checking if updates available..."); CheckForUpdateRequest request = new CheckForUpdateRequest(); + request.SerialNumber = serialNumber; request.Version = _app_manager.Version.ToString(); CheckForUpdateResponse update_response = null; @@ -316,62 +334,70 @@ namespace Tango.PPC.Common.MachineUpdate /// <param name="serialNumber">The serial number.</param> /// <param name="machineServiceAddress">The machine service address.</param> /// <returns></returns> - public Task UpdateDB(DbCompareResult dbCompareResult) + public Task UpdateDB(DbCompareResult dbCompareResult, String serialNumber) { return Task.Factory.StartNew(() => { LogManager.Log("Starting database update..."); - LogManager.Log("Looking for OverrideData script on application path..."); - - String config_file = Path.Combine(PathHelper.GetStartupPath(), "Update Scripts", "OverrideData.xml"); + LogManager.Log("Looking for update scripts configuration on application path..."); - if (!File.Exists(config_file)) - { - config_file = Path.Combine(PathHelper.GetStartupPath(), "Provision Scripts", "OverrideData.xml"); - } + String config_file = Path.Combine(PathHelper.GetStartupPath(), "Update Scripts", "config.xml"); if (!File.Exists(config_file)) { - throw LogManager.Log(new FileNotFoundException("Could not locate OverrideData.xml file on application folder.")); + throw LogManager.Log(new FileNotFoundException($"Could not locate '{config_file}' file on application folder.")); } UpdateDBResponse update_response = dbCompareResult.UpdateDBResponse; var localDataSource = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource; - LogManager.Log($"Overriding database static tables '{update_response.DataSource.ToString()}' => '{localDataSource.ToString()}'..."); + LogManager.Log($"Updating database '{update_response.DataSource.ToString()}' => '{localDataSource.ToString()}'..."); - ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(config_file); - builder.SetSource(update_response.DataSource); - builder.SetTarget(localDataSource); - builder.Synchronize(); + ExaminerSequenceConfiguration config_sequence = ExaminerSequenceConfiguration.FromFile(config_file); - var config = builder.Build(); - - ExaminerProcess process = new ExaminerProcess(config, ExaminerProcessType.Data); - process.Progress += (x, msg) => + foreach (var item in config_sequence.Items.Where(x => x.Type == ExaminerSequenceItemType.Data).OrderBy(x => x.Index)) { - LogManager.Log(msg); - }; + LogManager.Log($"Executing update script '{item.FileName}...'"); - LogManager.Log("Starting synchronization process..."); + ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(Path.Combine(Path.GetDirectoryName(config_file), item.FileName)); + builder.SetSource(update_response.DataSource); + builder.SetTarget(localDataSource); - try - { - var result = process.Execute().Result; - - if (result.ExitCode != ExaminerProcessExitCode.Success) + if (item.RequiresSerialNumber) { - throw LogManager.Log(new InvalidDataException(String.Format("OverrideData script has terminated with exit code '{0}'.", result.ExitCode))); + builder.SetMachineSerialNumber(serialNumber); } - LogManager.Log("Synchronization completed successfully!"); - } - catch (Exception ex) - { - throw LogManager.Log(ex, "Setup manager error while trying to update the database."); + builder.Synchronize(); + + var config = builder.Build(); + + ExaminerProcess process = new ExaminerProcess(config, ExaminerProcessType.Data); + process.Progress += (x, msg) => + { + LogManager.Log(msg); + }; + + try + { + var result = process.Execute().Result; + + if (result.ExitCode != ExaminerProcessExitCode.Success) + { + throw LogManager.Log(new InvalidDataException(String.Format("OverrideData script has terminated with exit code '{0}'.", result.ExitCode))); + } + + LogManager.Log("Script executed successfully."); + } + catch (Exception ex) + { + throw LogManager.Log(ex, "Setup manager error while trying to update the database."); + } } + + LogManager.Log("Update completed successfully."); }); } @@ -389,18 +415,13 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Checking if database update is required for serial number {serialNumber}..."); - LogManager.Log("Looking for OverrideData script on application path..."); - - String config_file = Path.Combine(PathHelper.GetStartupPath(), "Update Scripts", "OverrideData.xml"); + LogManager.Log("Looking for update scripts configuration on application path..."); - if (!File.Exists(config_file)) - { - config_file = Path.Combine(PathHelper.GetStartupPath(), "Provision Scripts", "OverrideData.xml"); - } + String config_file = Path.Combine(PathHelper.GetStartupPath(), "Update Scripts", "config.xml"); if (!File.Exists(config_file)) { - throw LogManager.Log(new FileNotFoundException("Could not locate OverrideData.xml file on application folder.")); + throw LogManager.Log(new FileNotFoundException($"Could not locate '{config_file}' file on application folder.")); } LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); @@ -408,6 +429,7 @@ namespace Tango.PPC.Common.MachineUpdate Login(serialNumber).Wait(); UpdateDBRequest request = new UpdateDBRequest(); + request.SerialNumber = serialNumber; UpdateDBResponse update_response = null; @@ -417,53 +439,75 @@ namespace Tango.PPC.Common.MachineUpdate var localDataSource = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource; - LogManager.Log($"Comparing database static tables '{update_response.DataSource.ToString()}' => '{localDataSource.ToString()}'..."); + LogManager.Log($"Comparing database '{update_response.DataSource.ToString()}' => '{localDataSource.ToString()}'..."); var report_file = TemporaryManager.CreateFile(".xml"); - ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(config_file); - builder.SetSource(update_response.DataSource); - builder.SetTarget(localDataSource); - builder.SetReportFile(report_file); + ExaminerSequenceConfiguration config_sequence = ExaminerSequenceConfiguration.FromFile(config_file); - var config = builder.Build(); + bool has_differences = false; - ExaminerProcess process = new ExaminerProcess(config, ExaminerProcessType.Data); - process.Progress += (x, msg) => + foreach (var item in config_sequence.Items.Where(x => x.Type == ExaminerSequenceItemType.Data).OrderBy(x => x.Index)) { - LogManager.Log(msg); - }; + LogManager.Log($"Executing update script '{item.FileName}...'"); - LogManager.Log("Starting comparison process..."); - LogManager.Log("Generating report on " + report_file); + ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(Path.Combine(Path.GetDirectoryName(config_file), item.FileName)); + builder.SetSource(update_response.DataSource); + builder.SetTarget(localDataSource); + builder.SetReportFile(report_file); - try - { - var result = process.Execute().Result; - - if (result.ExitCode != ExaminerProcessExitCode.Success) + if (item.RequiresSerialNumber) { - throw LogManager.Log(new InvalidDataException(String.Format("OverrideData script has terminated with exit code '{0}'.", result.ExitCode))); + builder.SetMachineSerialNumber(serialNumber); } - LogManager.Log("Comparison completed successfully!"); - LogManager.Log("Loading report file..."); + var config = builder.Build(); - ExaminerDataReport report = ExaminerDataReport.FromFile(report_file); - report_file.Delete(); + ExaminerProcess process = new ExaminerProcess(config, ExaminerProcessType.Data); + process.Progress += (x, msg) => + { + LogManager.Log(msg); + }; - LogManager.Log("Comparison summary: \n" + report.Totals.ToJsonString()); + LogManager.Log("Starting comparison process..."); + LogManager.Log("Generating report on " + report_file); - return new DbCompareResult() + try { - RequiresUpdate = report.HasDifferences, - UpdateDBResponse = update_response, - }; + var result = process.Execute().Result; + + if (result.ExitCode != ExaminerProcessExitCode.Success) + { + throw LogManager.Log(new InvalidDataException(String.Format("Update script has terminated with exit code '{0}'.", result.ExitCode))); + } + + LogManager.Log("Comparison completed successfully!"); + LogManager.Log("Loading report file..."); + + ExaminerDataReport report = ExaminerDataReport.FromFile(report_file); + report_file.Delete(); + + LogManager.Log("Comparison summary: \n" + report.Totals.ToJsonString()); + + if (report.HasDifferences) + { + has_differences = true; + break; + } + } + catch (Exception ex) + { + throw LogManager.Log(ex, "Update manager error while trying to compare the database."); + } } - catch (Exception ex) + + LogManager.Log("Comparison completed successfully."); + + return new DbCompareResult() { - throw LogManager.Log(ex, "Update manager error while trying to compare the database."); - } + RequiresUpdate = has_differences, + UpdateDBResponse = update_response, + }; }); } |
