aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs
diff options
context:
space:
mode:
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.cs165
1 files changed, 165 insertions, 0 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 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
+
/// <summary>
/// Initializes a new instance of the <see cref="MachineUpdateManager"/> class.
/// </summary>
@@ -89,6 +91,8 @@ namespace Tango.PPC.Common.MachineUpdate
_app_manager = applicationManager;
}
+ #endregion
+
#region Public Methods
/// <summary>
@@ -282,6 +286,167 @@ namespace Tango.PPC.Common.MachineUpdate
});
}
+ /// <summary>
+ /// Updates all the "overwrite-able" database tables.
+ /// </summary>
+ /// <param name="serialNumber">The serial number.</param>
+ /// <param name="machineServiceAddress">The machine service address.</param>
+ /// <returns></returns>
+ 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<CoreSettings>().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.");
+ }
+ });
+ }
+
+ /// <summary>
+ /// Checks whether it is necessary to updates all the "overwrite-able" database tables.
+ /// </summary>
+ /// <param name="serialNumber">The serial number.</param>
+ /// <param name="machineServiceAddress">The machine service address.</param>
+ /// <returns></returns>
+ public Task<DbCompareResult> UpdateDBCheck(string serialNumber, string machineServiceAddress)
+ {
+ return Task.Factory.StartNew<DbCompareResult>(() =>
+ {
+ 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<UpdateDBRequest, UpdateDBResponse>(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<CoreSettings>().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
}
}