diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-13 14:22:32 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-13 14:22:32 +0300 |
| commit | 4490b0a76d4188cb285d62b106e208803ceaa133 (patch) | |
| tree | a24f3d5653979ebaff86b524f452d20c74df3327 /Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup | |
| parent | d3c0c73dc2aeae2dacf4224ce7997609132426a1 (diff) | |
| download | Tango-4490b0a76d4188cb285d62b106e208803ceaa133.tar.gz Tango-4490b0a76d4188cb285d62b106e208803ceaa133.zip | |
PPC.Common Logs and comments!
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup')
4 files changed, 117 insertions, 12 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 8856bcfd4..145335e09 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs @@ -6,13 +6,42 @@ using System.Threading.Tasks; namespace Tango.PPC.Common.MachineSetup { + /// <summary> + /// Represents a machine setup manager responsible for first time usage application and embedded device provisioning. + /// </summary> public interface IMachineSetupManager { + /// <summary> + /// Gets the current setup step. + /// </summary> MachineSetupSteps CurrentStep { get; } + + /// <summary> + /// Occurs when there is a text log message available. + /// </summary> 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. + /// </summary> event EventHandler<MachineSetupSteps> ProgressStep; - Task<MachineSetupResult> Setup(String serialNumber, String hostAddress); + + /// <summary> + /// Performs a machine setup using the specified serial number and machine service address. + /// </summary> + /// <param name="serialNumber">The serial number.</param> + /// <param name="machineServiceAddress">The machine service address.</param> + /// <returns></returns> + Task<MachineSetupResult> Setup(String serialNumber, String machineServiceAddress); } } 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 01f828c3f..14cdeb3f2 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs @@ -21,14 +21,33 @@ using Tango.Transport.Web; namespace Tango.PPC.Common.MachineSetup { + /// <summary> + /// Represents the PPC machine setup manager. + /// </summary> + /// <seealso cref="Tango.Core.ExtendedObject" /> + /// <seealso cref="Tango.PPC.Common.MachineSetup.IMachineSetupManager" /> public class MachineSetupManager : ExtendedObject, IMachineSetupManager { - private IPPCApplicationManager _applicationManager; + #region Events + /// <summary> + /// Occurs when there is a text log message available. + /// </summary> public event EventHandler<string> ProgressLog; + + /// <summary> + /// Occurs when the <see cref="CurrentStep" /> has changed. + /// </summary> public event EventHandler<MachineSetupSteps> ProgressStep; + #endregion + + #region Properties + private MachineSetupSteps _currentStep; + /// <summary> + /// Gets the current setup step. + /// </summary> public MachineSetupSteps CurrentStep { get { return _currentStep; } @@ -39,11 +58,15 @@ namespace Tango.PPC.Common.MachineSetup _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; } @@ -51,27 +74,39 @@ namespace Tango.PPC.Common.MachineSetup } private String _updatingPackagesStatus; + /// <summary> + /// Gets the downloading packages step status. + /// </summary> public String DownloadingPackagesStatus { get { return _updatingPackagesStatus; } set { _updatingPackagesStatus = value; RaisePropertyChangedAuto(); } } - public MachineSetupManager(IPPCApplicationManager applicationManager) - { - _applicationManager = applicationManager; - } + #endregion + + #region Public Methods + /// <summary> + /// Performs a machine setup using the specified serial number and machine service address. + /// </summary> + /// <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) { return Task.Factory.StartNew<MachineSetupResult>(() => { + 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..."; + LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); + MachineSetupRequest request = new MachineSetupRequest(); request.SerialNumber = serialNumber; @@ -82,15 +117,23 @@ namespace Tango.PPC.Common.MachineSetup setup_response = http.Post<MachineSetupRequest, MachineSetupResponse>(machineServiceAddress + "/api/Synchronization/MachineSetup", request).Result; } + LogManager.Log($"Machine setup response received: {Environment.NewLine}{setup_response.ToJsonString()}"); + //Create temporary folders for packages. var _newPackageTempFolder = TemporaryManager.CreateFolder(); _newPackageTempFolder.Persist = true; + LogManager.Log($"Temporary package folder created: {_newPackageTempFolder}."); + //Download software package. var tempFile = TemporaryManager.CreateFile(".zip"); + LogManager.Log($"Temporary package zip file created: {tempFile}."); + DownloadingPackagesStatus = "Downloading software package..."; + LogManager.Log("Downloading software package..."); + int fileSize = 0; DownloadingPackagesProgress = 0; @@ -98,23 +141,24 @@ namespace Tango.PPC.Common.MachineSetup { InvokeUINow(() => { - Thread.Sleep(10); + Thread.Sleep(10); //TODO: this is necessary only for visibility... DownloadingPackagesProgress = ((double)current / (double)fileSize) * 100d; }); })) { using (FtpClient ftp = new FtpClient(setup_response.FtpAddress, setup_response.FtpUserName, setup_response.FtpPassword)) { - LogManager.Log("Connecting to FTP site: " + setup_response.FtpAddress); + LogManager.Log("FTP: Connecting to site: " + setup_response.FtpAddress); ftp.ConnectAsync().Wait(); - LogManager.Log("Retrieving download size..."); + LogManager.Log("FTP: Retrieving download size..."); fileSize = (int)ftp.GetFileSize(setup_response.FtpFilePath); - LogManager.Log("Download size: " + fileSize + " bytes."); - LogManager.Log("Starting download..."); + LogManager.Log("FTP: Download size: " + fileSize + " bytes."); + LogManager.Log("FTP: Starting download..."); ftp.DownloadAsync(fs, setup_response.FtpFilePath).Wait(); } } + LogManager.Log("Extracting downloaded zip file..."); //Extract software package. ZipFile.ExtractToDirectory(tempFile, _newPackageTempFolder); @@ -126,17 +170,25 @@ namespace Tango.PPC.Common.MachineSetup String localAddress = SettingsManager.Default.GetOrCreate<CoreSettings>().DataBaseSource; String remote_address = setup_response.DbAddress; + LogManager.Log($"Synchronizing database '{remote_address}\\{db_name}' => '{localAddress}\\{db_name}'..."); + + LogManager.Log("Initializing database manager..."); DbManager db = DbManager.FromAddressAndName(localAddress, db_name); + LogManager.Log("Checking Tango database exists on the local machine..."); if (!db.Exists(db_name)) { throw new InvalidProgramException("Database tango does not exists."); } + LogManager.Log("Clearing database..."); db.ClearDb(); + LogManager.Log("Disposing database manager."); db.Dispose(); + LogManager.Log($"Initializing {nameof(ExaminerSequenceConfigurationRunner)}..."); + ExaminerSequenceConfigurationRunner runner = new ExaminerSequenceConfigurationRunner( Path.Combine(_newPackageTempFolder, "Synchronization Scripts", "config.xml"), Path.Combine(_newPackageTempFolder, "Synchronization Scripts"), @@ -157,11 +209,14 @@ namespace Tango.PPC.Common.MachineSetup runner.Log += (x, msg) => { + LogManager.Log(msg); ProgressLog.Invoke(this, msg); }; runner.ScriptExecuting += (x, item) => { + LogManager.Log($"Executing script {item.ToString()}..."); + if (item.Type == ExaminerSequenceItemType.Data && item.RequiresSerialNumber) { CurrentStep = MachineSetupSteps.SynchronizingMachineConfiguration; @@ -172,7 +227,17 @@ namespace Tango.PPC.Common.MachineSetup } }; - runner.Run().Wait(); + LogManager.Log("Starting synchronization process..."); + + try + { + runner.Run().Wait(); + LogManager.Log("Synchronization completed successfully!"); + } + catch (Exception ex) + { + throw LogManager.Log(ex, "Setup manager error while trying to synchronize database."); + } return new MachineSetupResult() { @@ -180,5 +245,7 @@ namespace Tango.PPC.Common.MachineSetup }; }); } + + #endregion } } 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 c459ddf50..ad24ad62d 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupResult.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupResult.cs @@ -6,8 +6,14 @@ using System.Threading.Tasks; namespace Tango.PPC.Common.MachineSetup { + /// <summary> + /// Represents an <see cref="IMachineSetupManager"/> result. + /// </summary> public class MachineSetupResult { + /// <summary> + /// Gets or sets the temporary update package path from which to get the last downloaded software version. + /// </summary> public String UpdatePackagePath { get; set; } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupSteps.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupSteps.cs index 3db1eb4b0..ec1db4826 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupSteps.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupSteps.cs @@ -7,6 +7,9 @@ using System.Threading.Tasks; namespace Tango.PPC.Common.MachineSetup { + /// <summary> + /// Represents the various <see cref="IMachineSetupManager"/> steps. + /// </summary> public enum MachineSetupSteps { [Description("Downloading Package")] |
