From 5c330a1d78b9d6108544b94e756a6457f162a468 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 5 Sep 2018 17:28:54 +0300 Subject: Added missing event type. Working on PPC DB update.. --- .../MachineUpdate/MachineUpdateManager.cs | 165 +++++++++++++++++++++ 1 file changed, 165 insertions(+) (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 b86fb88d4..5c7b7554d 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs @@ -80,6 +80,8 @@ namespace Tango.PPC.Common.MachineUpdate #endregion + #region Constructors + /// /// Initializes a new instance of the class. /// @@ -89,6 +91,8 @@ namespace Tango.PPC.Common.MachineUpdate _app_manager = applicationManager; } + #endregion + #region Public Methods /// @@ -282,6 +286,167 @@ namespace Tango.PPC.Common.MachineUpdate }); } + /// + /// Updates all the "overwrite-able" database tables. + /// + /// The serial number. + /// The machine service address. + /// + public Task UpdateDB(DbCompareResult dbCompareResult) + { + 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"); + + if (!File.Exists(config_file)) + { + config_file = Path.Combine(PathHelper.GetStartupPath(), "Provision Scripts", "OverrideData.xml"); + } + + if (!File.Exists(config_file)) + { + throw LogManager.Log(new FileNotFoundException("Could not locate OverrideData.xml file on application folder.")); + } + + UpdateDBResponse update_response = dbCompareResult.UpdateDBResponse; + + String db_name = "Tango"; + String localAddress = SettingsManager.Default.GetOrCreate().DataSource.Address; + String remote_address = update_response.DbAddress; + + LogManager.Log($"Overriding database static tables '{remote_address}\\{db_name}' => '{localAddress}\\{db_name}'..."); + + ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(config_file); + builder.SetSourceServer(remote_address, db_name, false, update_response.DbUserName, update_response.DbPassword); + builder.SetTargetServer(localAddress, db_name, true); + builder.Synchronize(); + + var config = builder.Build(); + + ExaminerProcess process = new ExaminerProcess(config, ExaminerProcessType.Data); + process.Progress += (x, msg) => + { + LogManager.Log(msg); + }; + + LogManager.Log("Starting synchronization process..."); + + 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("Synchronization completed successfully!"); + } + catch (Exception ex) + { + throw LogManager.Log(ex, "Setup manager error while trying to update the database."); + } + }); + } + + /// + /// Checks whether it is necessary to updates all the "overwrite-able" database tables. + /// + /// The serial number. + /// The machine service address. + /// + public Task UpdateDBCheck(string serialNumber, string machineServiceAddress) + { + return Task.Factory.StartNew(() => + { + 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"); + + if (!File.Exists(config_file)) + { + config_file = Path.Combine(PathHelper.GetStartupPath(), "Provision Scripts", "OverrideData.xml"); + } + + if (!File.Exists(config_file)) + { + throw LogManager.Log(new FileNotFoundException("Could not locate OverrideData.xml file on application folder.")); + } + + LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); + + UpdateDBRequest request = new UpdateDBRequest(); + request.SerialNumber = serialNumber; + + UpdateDBResponse update_response = null; + + using (var http = new ProtoWebClient()) + { + update_response = http.Post(machineServiceAddress + "/api/Synchronization/UpdateDB", request).Result; + } + + LogManager.Log($"Update DB response received: {Environment.NewLine}{update_response.ToJsonString()}"); + + String db_name = "Tango"; + String localAddress = SettingsManager.Default.GetOrCreate().DataSource.Address; + String remote_address = update_response.DbAddress; + + LogManager.Log($"Comparing database static tables '{remote_address}\\{db_name}' => '{localAddress}\\{db_name}'..."); + + var report_file = TemporaryManager.CreateFile(".xml"); + + ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(config_file); + builder.SetSourceServer(remote_address, db_name, false, update_response.DbUserName, update_response.DbPassword); + builder.SetTargetServer(localAddress, db_name, true); + builder.SetReportFile(report_file); + + var config = builder.Build(); + + ExaminerProcess process = new ExaminerProcess(config, ExaminerProcessType.Data); + process.Progress += (x, msg) => + { + LogManager.Log(msg); + }; + + LogManager.Log("Starting comparison process..."); + LogManager.Log("Generating report on " + report_file); + + 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("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()); + + return new DbCompareResult() + { + RequiresUpdate = report.HasDifferences, + UpdateDBResponse = update_response, + }; + } + catch (Exception ex) + { + throw LogManager.Log(ex, "Setup manager error while trying to compare the database."); + } + }); + } + #endregion } } -- cgit v1.3.1