diff options
| author | Shlomo Hecht <shlomo@twine-s.com> | 2018-08-13 15:47:59 +0300 |
|---|---|---|
| committer | Shlomo Hecht <shlomo@twine-s.com> | 2018-08-13 15:47:59 +0300 |
| commit | c6864d03caa002d590dbe2abc43a931b26fc7b27 (patch) | |
| tree | b066b7307aab9af7fd76de4bf26795434fd0ee5e /Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs | |
| parent | ba3d7e34700be27d5eed1c2a58f6cd146296fd95 (diff) | |
| parent | fe8111ccce920b73b47b6af657f1ee4bcac11120 (diff) | |
| download | Tango-c6864d03caa002d590dbe2abc43a931b26fc7b27.tar.gz Tango-c6864d03caa002d590dbe2abc43a931b26fc7b27.zip | |
Merge branch 'master' of https://twinetfs.visualstudio.com/Tango/_git/Tango
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs | 184 |
1 files changed, 65 insertions, 119 deletions
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 dc5164465..01f828c3f 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs @@ -11,8 +11,10 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Tango.Core; +using Tango.Core.DB; using Tango.Core.IO; using Tango.PMR.Synchronization; +using Tango.PPC.Common.Application; using Tango.Settings; using Tango.SQLExaminer; using Tango.Transport.Web; @@ -21,6 +23,8 @@ namespace Tango.PPC.Common.MachineSetup { public class MachineSetupManager : ExtendedObject, IMachineSetupManager { + private IPPCApplicationManager _applicationManager; + public event EventHandler<string> ProgressLog; public event EventHandler<MachineSetupSteps> ProgressStep; @@ -30,35 +34,43 @@ namespace Tango.PPC.Common.MachineSetup get { return _currentStep; } set { - _currentStep = value; - RaisePropertyChangedAuto(); - ProgressStep?.Invoke(this, _currentStep); + if (_currentStep != value) + { + _currentStep = value; + RaisePropertyChangedAuto(); + ProgressStep?.Invoke(this, _currentStep); + } } } private double _downloadProgress; - public double UpdatingPackagesProgress + public double DownloadingPackagesProgress { get { return _downloadProgress; } private set { _downloadProgress = value; RaisePropertyChangedAuto(); } } private String _updatingPackagesStatus; - public String UpdatingPackagesStatus + public String DownloadingPackagesStatus { get { return _updatingPackagesStatus; } set { _updatingPackagesStatus = value; RaisePropertyChangedAuto(); } } - public Task Setup(string serialNumber, string machineServiceAddress) + public MachineSetupManager(IPPCApplicationManager applicationManager) { - return Task.Factory.StartNew(() => + _applicationManager = applicationManager; + } + + public Task<MachineSetupResult> Setup(string serialNumber, string machineServiceAddress) + { + return Task.Factory.StartNew<MachineSetupResult>(() => { //Connect to machine service and get matching packages for this machine. - CurrentStep = MachineSetupSteps.UpdatingPackages; - UpdatingPackagesProgress = 0; - UpdatingPackagesStatus = "Connecting to machine service..."; + CurrentStep = MachineSetupSteps.DownloadingPackage; + DownloadingPackagesProgress = 0; + DownloadingPackagesStatus = "Connecting to machine service..."; MachineSetupRequest request = new MachineSetupRequest(); request.SerialNumber = serialNumber; @@ -74,164 +86,98 @@ namespace Tango.PPC.Common.MachineSetup var _newPackageTempFolder = TemporaryManager.CreateFolder(); _newPackageTempFolder.Persist = true; - var software_package_folder = _newPackageTempFolder.CreateFolder(); - var embedded_package_folder = _newPackageTempFolder.CreateFolder(); - - //Download software package. var tempFile = TemporaryManager.CreateFile(".zip"); - UpdatingPackagesStatus = "Downloading application package..."; + DownloadingPackagesStatus = "Downloading software package..."; int fileSize = 0; - UpdatingPackagesProgress = 0; + DownloadingPackagesProgress = 0; using (FileStreamWrapper fs = new FileStreamWrapper(tempFile.Path, FileMode.Create, (current) => { InvokeUINow(() => { Thread.Sleep(10); - UpdatingPackagesProgress = ((double)current / (double)fileSize) * 100d; + DownloadingPackagesProgress = ((double)current / (double)fileSize) * 100d; }); })) { - using (FtpClient ftp = new FtpClient(setup_response.FtpAddress, setup_response.UserName, setup_response.Password)) + using (FtpClient ftp = new FtpClient(setup_response.FtpAddress, setup_response.FtpUserName, setup_response.FtpPassword)) { LogManager.Log("Connecting to FTP site: " + setup_response.FtpAddress); ftp.ConnectAsync().Wait(); LogManager.Log("Retrieving download size..."); - fileSize = (int)ftp.GetFileSize(setup_response.ApplicationFtpFilePath); + fileSize = (int)ftp.GetFileSize(setup_response.FtpFilePath); LogManager.Log("Download size: " + fileSize + " bytes."); LogManager.Log("Starting download..."); - ftp.DownloadAsync(fs, setup_response.ApplicationFtpFilePath).Wait(); + ftp.DownloadAsync(fs, setup_response.FtpFilePath).Wait(); } } //Extract software package. - ZipFile.ExtractToDirectory(tempFile, software_package_folder); - - //Download embedded package. - tempFile = TemporaryManager.CreateFile(".zip"); - - UpdatingPackagesStatus = "Downloading embedded package..."; - - fileSize = 0; - UpdatingPackagesProgress = 0; - - using (FileStreamWrapper fs = new FileStreamWrapper(tempFile.Path, FileMode.Create, (current) => - { - InvokeUINow(() => - { - Thread.Sleep(10); - UpdatingPackagesProgress = ((double)current / (double)fileSize) * 100d; - }); - })) - { - using (FtpClient ftp = new FtpClient(setup_response.FtpAddress, setup_response.UserName, setup_response.Password)) - { - LogManager.Log("Connecting to FTP site: " + setup_response.FtpAddress); - ftp.ConnectAsync().Wait(); - LogManager.Log("Retrieving download size..."); - fileSize = (int)ftp.GetFileSize(setup_response.EmbeddedFtpFilePath); - LogManager.Log("Download size: " + fileSize + " bytes."); - LogManager.Log("Starting download..."); - ftp.DownloadAsync(fs, setup_response.EmbeddedFtpFilePath).Wait(); - } - } + ZipFile.ExtractToDirectory(tempFile, _newPackageTempFolder); - //Extract embedded package. - ZipFile.ExtractToDirectory(tempFile, embedded_package_folder); //Synchronize database - String remote_address = setup_response.DbAddress; - CurrentStep = MachineSetupSteps.SynchronizingSchema; String db_name = "Tango"; - String localAddress = SettingsManager.Default.GetOrCreate<CoreSettings>().DataBaseSource; + String remote_address = setup_response.DbAddress; - var tempFolder = TemporaryManager.Default.CreateFolder("Machine Setup"); - String report_file = tempFolder.CreateImaginaryFile(".xml"); - String config_file = tempFolder.CreateImaginaryFile(".xml"); - - DbManager db = new DbManager(new SqlConnection(String.Format("Server={0};Integrated security=SSPI", localAddress))); + DbManager db = DbManager.FromAddressAndName(localAddress, db_name); - if (!db.Exists("Tango")) + if (!db.Exists(db_name)) { throw new InvalidProgramException("Database tango does not exists."); } - //Create schema configuration - ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(ExaminerConfigurationType.Schema); + db.ClearDb(); - builder. - SetSourceServer(remote_address, db_name). - SetTargetServer(localAddress, db_name). - Synchronize(); + db.Dispose(); - //Synchronize Source schema with Target schema + ExaminerSequenceConfigurationRunner runner = new ExaminerSequenceConfigurationRunner( + Path.Combine(_newPackageTempFolder, "Synchronization Scripts", "config.xml"), + Path.Combine(_newPackageTempFolder, "Synchronization Scripts"), + new ExaminerSequenceDataSource() + { + Address = remote_address, + DataBaseName = db_name, + IntegratedSecurity = false, + UserName = setup_response.DbUserName, + Password = setup_response.DbPassword, + }, + new ExaminerSequenceDataSource() + { + Address = localAddress, + DataBaseName = db_name, + IntegratedSecurity = true, + }, serialNumber); - var process = new ExaminerProcess(builder.Build(), ExaminerProcessType.Schema); - process.Progress += (x, msg) => + runner.Log += (x, msg) => { - ProgressLog?.Invoke(this, msg); + ProgressLog.Invoke(this, msg); }; - var result = process.Execute().Result; - - //Synchronization was successful - if (result.ExitCode != ExaminerProcessExitCode.Success) - { - throw new InvalidProgramException("Error while trying to synchronize database schema."); - } - - CurrentStep = MachineSetupSteps.SynchronizingData; - - //Create override data configuration - builder = new ExaminerConfigurationBuilder(ExaminerConfigurationType.OverrideData); - - builder. - SetSourceServer(remote_address, db_name). - SetTargetServer(localAddress, db_name). - Synchronize(); - process = new ExaminerProcess(builder.Build(), ExaminerProcessType.Data); - process.Progress += (x, msg) => + runner.ScriptExecuting += (x, item) => { - ProgressLog?.Invoke(this, msg); + if (item.Type == ExaminerSequenceItemType.Data && item.RequiresSerialNumber) + { + CurrentStep = MachineSetupSteps.SynchronizingMachineConfiguration; + } + else if (item.Type == ExaminerSequenceItemType.Data) + { + CurrentStep = MachineSetupSteps.SynchronizingData; + } }; - result = process.Execute().Result; - - //Synchronization was successful - if (result.ExitCode != ExaminerProcessExitCode.Success) - { - throw new InvalidProgramException("Error while trying to synchronize data."); - } - CurrentStep = MachineSetupSteps.SynchronizingMachineConfiguration; + runner.Run().Wait(); - //Provision Target - builder = new ExaminerConfigurationBuilder(ExaminerConfigurationType.ProvisionMachine); - - builder. - SetSourceServer(remote_address, db_name). - SetTargetServer(localAddress, db_name). - SetMachineSerialNumber(serialNumber). - Synchronize(); - - - process = new ExaminerProcess(builder.Build(), ExaminerProcessType.Data); - process.Progress += (x, msg) => + return new MachineSetupResult() { - ProgressLog?.Invoke(this, msg); + UpdatePackagePath = _newPackageTempFolder, }; - result = process.Execute().Result; - - //Synchronization was successful - if (result.ExitCode != ExaminerProcessExitCode.Success) - { - throw new InvalidProgramException("Error while trying to synchronize database schema."); - } }); } } |
