diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-11-26 10:45:17 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-11-26 10:45:17 +0200 |
| commit | a70419a35bac0e053278002856499430edbbb598 (patch) | |
| tree | 0d397d07e4343f9f4db46185ead9f1679ee4301d /Software/Visual_Studio/PPC/Tango.PPC.Common | |
| parent | 78c93e2ee1eddff67554edec9f956536a0b61482 (diff) | |
| download | Tango-a70419a35bac0e053278002856499430edbbb598.tar.gz Tango-a70419a35bac0e053278002856499430edbbb598.zip | |
Working on backup/restore...
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Common/BackupRestore/DefaultBackupManager.cs | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/BackupRestore/DefaultBackupManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/BackupRestore/DefaultBackupManager.cs new file mode 100644 index 000000000..1724dfa73 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/BackupRestore/DefaultBackupManager.cs @@ -0,0 +1,181 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.BackupRestore; +using Tango.Core.ExtensionMethods; +using Tango.Core.DI; +using Tango.PPC.Common.Connection; +using System.IO; +using Tango.Core.Helpers; +using Tango.BL; +using Tango.Settings; +using Tango.Core.DB; +using System.Data.SqlClient; + +namespace Tango.PPC.UI.BackupRestore +{ + public class DefaultBackupManager : ExtendedObject, IBackupManager + { + private const string BACKUP_FILE_NAME = "Backup.json"; + private const string DATABASE_FILE_NAME = "Tango.bak"; + + [TangoInject(TangoInjectMode.WhenAvailable)] + private IPPCApplicationManager _applicationManager; + + [TangoInject(TangoInjectMode.WhenAvailable)] + private IMachineProvider _machineProvider; + + public DefaultBackupManager() + { + TangoIOC.Default.Inject(this); + } + + public event EventHandler<BackupRestoreProgressEventArgs> Progress; + + public Task CreateBackup(string filePath, String name, BackupSettings settings) + { + return Task.Factory.StartNew(() => + { + try + { + LogManager.Log($"Starting backup operation to file {filePath}."); + LogManager.Log($"Backup settings:\n{settings.ToJsonString()}"); + OnProgress(BackupRestoreStage.Initializing); + + var tempFolder = TemporaryManager.CreateFolder(); + LogManager.Log($"Temporary folder created on {tempFolder.Path}."); + + BackupFile backupFile = new BackupFile(); + backupFile.Date = DateTime.Now; + backupFile.Settings = settings; + backupFile.Name = name; + backupFile.MachineSerialNumber = _machineProvider.Machine.SerialNumber; + backupFile.ApplicationVersion = _applicationManager.Version.ToString(); + + try + { + LogManager.Log("Extracting firmware version from local tfp package..."); + using (var st = File.OpenRead(Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "firmware_package.tfp"))) + { + backupFile.FirmwareVersion = _machineProvider.MachineOperator.GetFirmwarePackageInfo(st).Result.FileDescriptors.SingleOrDefault(x => x.Destination == PMR.FirmwareUpgrade.VersionFileDestination.Mcu).Version; + } + } + catch (Exception ex) + { + throw new FileLoadException("Could extract the firmware version from the TFP package.", ex); + } + + LogManager.Log($"Backup file generated:\n{backupFile.ToJsonString()}"); + + if (settings.Mode == BackupMode.Jobs) + { + LogManager.Log("Starting jobs backup..."); + OnProgress(BackupRestoreStage.BackingupJobs); + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { + var jobs = db.Jobs.ToList(); + + foreach (var job in jobs) + { + try + { + LogManager.Log($"Backing up job '{job.Name}'..."); + var jobFile = job.ToJobFile().Result; + backupFile.JobFiles.Add(jobFile); + + OnProgress(BackupRestoreStage.BackingupJobs, jobs.IndexOf(job) + 1, jobs.Count, false); + } + catch (Exception ex) + { + throw new InvalidOperationException($"Error extracting job {job.Name}.", ex); + } + } + } + LogManager.Log("Jobs backup completed."); + } + else + { + LogManager.Log("Backing up application settings..."); + OnProgress(BackupRestoreStage.BackingupSettings); + backupFile.SettingsFile = File.ReadAllText(SettingsManager.Default.FilePath); + + LogManager.Log("Backing up application files..."); + OnProgress(BackupRestoreStage.BackingupApplication); + var files = Directory.GetFiles(AssemblyHelper.GetCurrentAssemblyFolder()).Where(x => Path.GetFileName(x) != BACKUP_FILE_NAME).ToList(); + foreach (var file in files) + { + try + { + LogManager.Log($"Copying file '{Path.GetFileName(file)}'..."); + File.Copy(file, Path.Combine(tempFolder, Path.GetFileName(file))); + OnProgress(BackupRestoreStage.BackingupApplication, files.IndexOf(file) + 1, files.Count, false); + } + catch (Exception ex) + { + throw new IOException($"Could not copy file '{file}'.", ex); + } + } + + LogManager.Log("Backing up database..."); + OnProgress(BackupRestoreStage.BackingupDatabase); + try + { + var dataSource = ObservablesContext.GetActualDataSource(); + using (var dbManager = DbManager.FromDataSource(dataSource)) + { + dbManager.Backup(dataSource.Catalog, Path.Combine(tempFolder, DATABASE_FILE_NAME)); + } + } + catch (Exception ex) + { + throw new IOException("Error creating database backup", ex); + } + + LogManager.Log("Database backup completed."); + } + + try + { + OnProgress(BackupRestoreStage.WritingSettings); + var backupFilePath = Path.Combine(tempFolder, BACKUP_FILE_NAME); + LogManager.Log($"Writing backup configuration file '{backupFilePath}'..."); + File.WriteAllText(backupFilePath, backupFile.ToJsonString()); + } + catch (Exception ex) + { + throw new IOException("Error writing backup configuration file.", ex); + } + + //using (ZipFile zip = new ZipFile()) + //{ + //} + } + catch (Exception ex) + { + LogManager.Log(ex, "Could not complete the backup operation."); + throw ex; + } + }); + } + + public Task Restore(string filePath) + { + throw new NotImplementedException(); + } + + protected virtual void OnProgress(BackupRestoreStage stage, double progress = 0, double maxProgress = 100, bool isIntermediate = true) + { + Progress?.Invoke(this, new BackupRestoreProgressEventArgs() + { + Stage = stage, + Progress = progress, + MaxProgress = maxProgress, + IsIntermediate = isIntermediate, + }); + } + } +} |
