aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-12-09 16:59:07 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-12-09 16:59:07 +0200
commitf7d320bcc6651d0c85bad73b52b14ba050a40c18 (patch)
tree6f446d30c34da651e5cb2bed93caa0edc3a38f24 /Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup
parent58c791b68d9058516166cc26fc15563bd56ebeb6 (diff)
downloadTango-f7d320bcc6651d0c85bad73b52b14ba050a40c18.tar.gz
Tango-f7d320bcc6651d0c85bad73b52b14ba050a40c18.zip
Refactored PPC firmware upgrade.
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs19
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs177
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupProgress.cs17
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupResult.cs1
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupSteps.cs24
5 files changed, 123 insertions, 115 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs
index 145335e09..896c7b921 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs
@@ -11,10 +11,7 @@ namespace Tango.PPC.Common.MachineSetup
/// </summary>
public interface IMachineSetupManager
{
- /// <summary>
- /// Gets the current setup step.
- /// </summary>
- MachineSetupSteps CurrentStep { get; }
+ MachineSetupProgress Status { get; }
/// <summary>
/// Occurs when there is a text log message available.
@@ -22,19 +19,9 @@ namespace Tango.PPC.Common.MachineSetup
event EventHandler<String> ProgressLog;
/// <summary>
- /// Gets the downloading packages step progress.
- /// </summary>
- double DownloadingPackagesProgress { get; }
-
- /// <summary>
- /// Gets the downloading packages step status.
- /// </summary>
- String DownloadingPackagesStatus { get; }
-
- /// <summary>
- /// Occurs when the <see cref="CurrentStep"/> has changed.
+ /// Occurs when the setup has made some progress.
/// </summary>
- event EventHandler<MachineSetupSteps> ProgressStep;
+ event EventHandler<MachineSetupProgress> Progress;
/// <summary>
/// Performs a machine setup using the specified serial number and machine service address.
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs
index 02324b743..22da5c57e 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs
@@ -17,7 +17,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;
@@ -40,49 +42,17 @@ namespace Tango.PPC.Common.MachineSetup
/// <summary>
/// Occurs when the <see cref="CurrentStep" /> has changed.
/// </summary>
- public event EventHandler<MachineSetupSteps> ProgressStep;
+ public event EventHandler<MachineSetupProgress> Progress;
#endregion
#region Properties
- private MachineSetupSteps _currentStep;
- /// <summary>
- /// Gets the current setup step.
- /// </summary>
- public MachineSetupSteps 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;
- /// <summary>
- /// Gets the downloading packages step progress.
- /// </summary>
- public double DownloadingPackagesProgress
- {
- get { return _downloadProgress; }
- private set { _downloadProgress = value; RaisePropertyChangedAuto(); }
- }
-
- private String _updatingPackagesStatus;
- /// <summary>
- /// Gets the downloading packages step status.
- /// </summary>
- public String DownloadingPackagesStatus
+ private MachineSetupProgress _status;
+ public MachineSetupProgress Status
{
- get { return _updatingPackagesStatus; }
- set { _updatingPackagesStatus = value; RaisePropertyChangedAuto(); }
+ get { return _status; }
+ private set { _status = value; RaisePropertyChangedAuto(); }
}
#endregion
@@ -95,17 +65,16 @@ namespace Tango.PPC.Common.MachineSetup
/// <param name="serialNumber">The serial number.</param>
/// <param name="machineServiceAddress">The machine service address.</param>
/// <returns></returns>
- public Task<MachineSetupResult> Setup(string serialNumber, string machineServiceAddress)
+ public async Task<MachineSetupResult> Setup(string serialNumber, string machineServiceAddress)
{
- return Task.Factory.StartNew<MachineSetupResult>(() =>
- {
+ TaskCompletionSource<MachineSetupResult> result = new TaskCompletionSource<MachineSetupResult>();
+ try
+ {
LogManager.Log($"Starting machine setup for serial number {serialNumber}...");
//Connect to machine service and get matching packages for this machine.
- CurrentStep = MachineSetupSteps.DownloadingPackage;
- DownloadingPackagesProgress = 0;
- DownloadingPackagesStatus = "Connecting to machine service...";
+ UpdateProgress("Downloading software package", "Connecting to machine service...");
LogManager.Log($"Connecting to machine service on {machineServiceAddress}...");
@@ -118,7 +87,7 @@ namespace Tango.PPC.Common.MachineSetup
{
using (var http = new ProtoWebClient())
{
- setup_response = http.Post<MachineSetupRequest, MachineSetupResponse>(machineServiceAddress + "/api/Synchronization/MachineSetup", request).Result;
+ setup_response = await http.Post<MachineSetupRequest, MachineSetupResponse>(machineServiceAddress + "/api/Synchronization/MachineSetup", request);
}
}
catch (Exception ex)
@@ -139,31 +108,31 @@ namespace Tango.PPC.Common.MachineSetup
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) =>
+ await Task.Factory.StartNew(() =>
{
- InvokeUINow(() =>
+ using (FileStreamWrapper fs = new FileStreamWrapper(tempFile.Path, FileMode.Create, (current) =>
+ {
+ UpdateProgress("Downloading software package", "Downloading...", false, current, fileSize);
+ }))
{
- DownloadingPackagesProgress = ((double)current / (double)fileSize) * 100d;
- });
- }))
- {
- LogManager.Log($"Connecting to storage blob with address {setup_response.BlobAddress}");
- CloudBlockBlob blob = new CloudBlockBlob(new Uri(setup_response.BlobAddress));
- LogManager.Log("Fetching blob attributes...");
- blob.FetchAttributes();
- fileSize = blob.Properties.Length;
- LogManager.Log("Download size: " + fileSize + " bytes.");
- LogManager.Log("Starting blob download...");
- blob.DownloadToStream(fs);
- }
+ LogManager.Log($"Connecting to storage blob with address {setup_response.BlobAddress}");
+ CloudBlockBlob blob = new CloudBlockBlob(new Uri(setup_response.BlobAddress));
+ LogManager.Log("Fetching blob attributes...");
+ blob.FetchAttributes();
+ fileSize = blob.Properties.Length;
+ LogManager.Log("Download size: " + fileSize + " bytes.");
+ LogManager.Log("Starting blob download...");
+ blob.DownloadToStream(fs);
+ }
+ });
+
+ UpdateProgress("Downloading software package", "Extracting package...");
LogManager.Log("Extracting downloaded zip file...");
//Extract software package.
@@ -176,7 +145,7 @@ namespace Tango.PPC.Common.MachineSetup
//Synchronize database
- CurrentStep = MachineSetupSteps.SynchronizingSchema;
+ UpdateProgress("Updating Database", "Initializing...");
String db_name = "Tango";
String localAddress = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource.Address;
@@ -184,12 +153,14 @@ namespace Tango.PPC.Common.MachineSetup
LogManager.Log($"Synchronizing database '{remote_address}\\{db_name}' => '{localAddress}\\{db_name}'...");
+ UpdateProgress("Updating Database", "Connecting to local database...");
LogManager.Log($"Connecting to local database at {localAddress}...");
DbManager db = DbManager.FromAddress(localAddress);
LogManager.Log($"Ensuring {db_name} database exists on the local machine...");
if (!db.Exists(db_name))
{
+ UpdateProgress("Updating Database", "Creating new database...");
LogManager.Log("Database does not exist. Creating new database...");
db.Create(db_name);
}
@@ -203,6 +174,7 @@ namespace Tango.PPC.Common.MachineSetup
LogManager.Log("Initializing database manager...");
db = DbManager.FromAddressAndName(localAddress, db_name);
+ UpdateProgress("Updating Database", "Clearing current database...");
LogManager.Log("Clearing database...");
db.ClearDb();
@@ -211,6 +183,8 @@ namespace Tango.PPC.Common.MachineSetup
LogManager.Log($"Initializing {nameof(ExaminerSequenceConfigurationRunner)}...");
+ UpdateProgress("Updating Database", "Initializing provisioning sequence...");
+
ExaminerSequenceConfigurationRunner runner = new ExaminerSequenceConfigurationRunner(
Path.Combine(_newPackageTempFolder, "Provision Scripts", "config.xml"),
Path.Combine(_newPackageTempFolder, "Provision Scripts"),
@@ -238,34 +212,87 @@ namespace Tango.PPC.Common.MachineSetup
runner.ScriptExecuting += (x, item) =>
{
LogManager.Log($"Executing script {item.ToString()}...");
-
- if (item.Type == ExaminerSequenceItemType.Data && item.RequiresSerialNumber)
- {
- CurrentStep = MachineSetupSteps.SynchronizingMachineConfiguration;
- }
- else if (item.Type == ExaminerSequenceItemType.Data)
- {
- CurrentStep = MachineSetupSteps.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 MachineSetupResult()
+ //Updating firmware
+ UpdateProgress("Updating Firmware", "Connecting to firmware device...");
+ LogManager.Log("");
+ LogManager.Log("-------------------------------------------------------------------------");
+ LogManager.Log("Updating Firmware...");
+ var op = await DefaultMachineProvider.CreateMinimalMachineOperator();
+
+ 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 MachineSetupResult()
+ {
+ UpdatePackagePath = _newPackageTempFolder,
+ });
+ };
+ handler.Canceled += (_, __) =>
{
- UpdatePackagePath = _newPackageTempFolder,
+ 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 setup.");
+ result.SetException(ex);
+ }
+
+ return await result.Task;
+ }
+
+ #endregion
+
+ #region Protected Methods
+
+ protected virtual void UpdateProgress(String name, String message = "", bool isIntermediate = true, double progress = 0, double total = 0)
+ {
+ InvokeUINow(() =>
+ {
+ Status = new MachineSetupProgress()
+ {
+ Name = name,
+ Message = message,
+ IsIntermediate = isIntermediate,
+ Progress = progress,
+ Total = total,
+ };
+
+ Progress?.Invoke(this, Status);
});
+
+ UIHelper.DoEvents();
}
#endregion
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupProgress.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupProgress.cs
new file mode 100644
index 000000000..bd279b05c
--- /dev/null
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupProgress.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.PPC.Common.MachineSetup
+{
+ public class MachineSetupProgress : EventArgs
+ {
+ public double Progress { get; set; }
+ public double Total { get; set; }
+ public bool IsIntermediate { get; set; }
+ public String Name { get; set; }
+ public String Message { get; set; }
+ }
+}
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupResult.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupResult.cs
index ad24ad62d..f9887ea38 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupResult.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupResult.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Tango.Integration.Upgrade;
namespace Tango.PPC.Common.MachineSetup
{
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupSteps.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupSteps.cs
deleted file mode 100644
index ec1db4826..000000000
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupSteps.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Tango.PPC.Common.MachineSetup
-{
- /// <summary>
- /// Represents the various <see cref="IMachineSetupManager"/> steps.
- /// </summary>
- public enum MachineSetupSteps
- {
- [Description("Downloading Package")]
- DownloadingPackage,
- [Description("Synchronizing Schema")]
- SynchronizingSchema,
- [Description("Synchronizing Data")]
- SynchronizingData,
- [Description("Configuring Machine")]
- SynchronizingMachineConfiguration
- }
-}