diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-02-21 21:30:45 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-02-21 21:30:45 +0200 |
| commit | f991f37a95a854e9b2aada52982d83ddaa0ca1b0 (patch) | |
| tree | 02ea1f3b73cd5a500023a5d0daa905fc6978112a /Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI | |
| parent | 956f9ea033553136ebf199fff30f288dc26fbeb0 (diff) | |
| download | Tango-f991f37a95a854e9b2aada52982d83ddaa0ca1b0.tar.gz Tango-f991f37a95a854e9b2aada52982d83ddaa0ca1b0.zip | |
Working on macine studio update center..
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI')
4 files changed, 178 insertions, 24 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.config b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.config index 0bd51a469..1a0e57043 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.config +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.config @@ -9,7 +9,7 @@ <entityFramework> - <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> + <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Tango.MachineStudio.UI.csproj b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Tango.MachineStudio.UI.csproj index 0ad97907b..61ea92566 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Tango.MachineStudio.UI.csproj +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Tango.MachineStudio.UI.csproj @@ -48,6 +48,9 @@ <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"> <HintPath>..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath> </Reference> + <Reference Include="FluentFTP, Version=19.1.2.0, Culture=neutral, PublicKeyToken=f4af092b1d8df44f, processorArchitecture=MSIL"> + <HintPath>..\..\packages\FluentFTP.19.1.2\lib\net45\FluentFTP.dll</HintPath> + </Reference> <Reference Include="FontAwesome.WPF, Version=4.7.0.37774, Culture=neutral, PublicKeyToken=0758b07a11a4f466, processorArchitecture=MSIL"> <HintPath>..\..\packages\FontAwesome.WPF.4.7.0.9\lib\net40\FontAwesome.WPF.dll</HintPath> </Reference> @@ -63,6 +66,9 @@ <Reference Include="Google.Protobuf, Version=3.4.1.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL"> <HintPath>..\..\packages\Google.Protobuf.3.4.1\lib\net45\Google.Protobuf.dll</HintPath> </Reference> + <Reference Include="Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c, processorArchitecture=MSIL"> + <HintPath>..\..\packages\Ionic.Zip.1.9.1.8\lib\Ionic.Zip.dll</HintPath> + </Reference> <Reference Include="MahApps.Metro, Version=1.5.0.23, Culture=neutral, PublicKeyToken=f4fb5a3c4d1e5b4f, processorArchitecture=MSIL"> <HintPath>..\..\packages\MahApps.Metro.1.5.0\lib\net45\MahApps.Metro.dll</HintPath> </Reference> @@ -94,6 +100,7 @@ <Private>True</Private> </Reference> <Reference Include="System.Drawing" /> + <Reference Include="System.IO.Compression.FileSystem" /> <Reference Include="System.Numerics" /> <Reference Include="System.Reactive.Core, Version=3.0.3000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL"> <HintPath>..\..\packages\System.Reactive.Core.3.1.1\lib\net46\System.Reactive.Core.dll</HintPath> @@ -110,7 +117,9 @@ <Reference Include="System.Reactive.Windows.Threading, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL"> <HintPath>..\..\packages\System.Reactive.Windows.Threading.3.1.1\lib\net45\System.Reactive.Windows.Threading.dll</HintPath> </Reference> + <Reference Include="System.Runtime.Serialization" /> <Reference Include="System.ServiceModel" /> + <Reference Include="System.Web" /> <Reference Include="System.Windows" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs index 014aecdb6..e5f3d7a46 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs @@ -1,11 +1,17 @@ -using System; +using FluentFTP; +using Ionic.Zip; +using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Runtime.CompilerServices; +using System.ServiceModel; using System.Text; using System.Threading; using System.Threading.Tasks; using Tango.Core.Commands; +using Tango.Core.Helpers; +using Tango.Logging; using Tango.MachineStudio.Common.Authentication; using Tango.MachineStudio.Common.Navigation; using Tango.MachineStudio.Common.Notifications; @@ -29,10 +35,13 @@ namespace Tango.MachineStudio.UI.ViewModels public class UpdateViewVM : ViewModel { + private String _appPath = AppDomain.CurrentDomain.BaseDirectory; + private INotificationProvider _notification; private INavigationManager _navigation; private IStudioApplicationManager _application; private IAuthenticationProvider _authentication; + private CheckForUpdatesResponse _updateInfo; private UpdateStatus _status; public UpdateStatus Status @@ -85,6 +94,11 @@ namespace Tango.MachineStudio.UI.ViewModels BackCommand = new RelayCommand(BackToApplication, () => Status != UpdateStatus.Updating); RestartCommand = new RelayCommand(RestartApplication, () => Status == UpdateStatus.UpdateCompleted); TryAgainCommand = new RelayCommand(TryAgain, () => Status == UpdateStatus.CheckingForUpdate); + + foreach (var file in Directory.GetFiles(_appPath, "*.tmp")) + { + PathHelper.TryDeleteFile(file); + } } public void OnNavigatedInto() @@ -96,22 +110,47 @@ namespace Tango.MachineStudio.UI.ViewModels { Status = UpdateStatus.CheckingForUpdate; + ChannelFactory<IMachineStudioUpdateService> service = null; + Task.Factory.StartNew(() => { - var service = UpdateServiceHelper.GetUpdateServiceChannel(); - var client = service.CreateChannel(); - - CheckForUpdatesResponse response = client.CheckForUpdates(new CheckForUpdatesRequest() + try { - Email = _authentication.CurrentUser.Email, - Password = _authentication.CurrentUser.Password, - Version = _application.Version, - }); + Thread.Sleep(2000); + + service = UpdateServiceHelper.GetUpdateServiceChannel(); + var client = service.CreateChannel(); - var a = response; + CheckForUpdatesResponse response = client.CheckForUpdates(new CheckForUpdatesRequest() + { + Email = _authentication.CurrentUser.Email, + Password = _authentication.CurrentUser.Password, + Version = _application.Version, + }); - Thread.Sleep(5000); - Status = UpdateStatus.UpdateAvailable; + if (response.IsUpdateAvailable) + { + _updateInfo = response; + Status = UpdateStatus.UpdateAvailable; + LatestVersion = response.Version; + } + else + { + Status = UpdateStatus.UpToDate; + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error while checking for version update!"); + Status = UpdateStatus.Error; + } + finally + { + if (service != null) + { + service.Close(); + } + } }); } @@ -128,7 +167,7 @@ namespace Tango.MachineStudio.UI.ViewModels _navigation.NavigateTo(NavigationView.MainView); Status = UpdateStatus.None; } - + private void StartUpdate() { DownloadProgress = 0; @@ -138,24 +177,128 @@ namespace Tango.MachineStudio.UI.ViewModels Task.Factory.StartNew(() => { - for (int i = 0; i < 100; i++) + try { - DownloadProgress++; - Thread.Sleep(30); - } + var tempFile = PathHelper.GetTempFilePath() + ".zip"; + + LogManager.Log("Creating temporary file " + tempFile); + + int fileSize = 0; + + using (FileStreamWrapper fs = new FileStreamWrapper(tempFile, FileMode.Create, (current) => + { + InvokeUINow(() => + { + Thread.Sleep(10); + DownloadProgress = ((double)current / (double)fileSize) * 100d; + }); + })) + { + using (FtpClient ftp = new FtpClient(_updateInfo.FtpHost, _updateInfo.UserName, _updateInfo.Password)) + { + LogManager.Log("Connecting to FTP site: " + _updateInfo.FtpHost); + ftp.ConnectAsync().Wait(); + LogManager.Log("Retrieving download size..."); + fileSize = (int)ftp.GetFileSize(_updateInfo.FilePath); + LogManager.Log("Download size: " + fileSize + " bytes."); + LogManager.Log("Starting download..."); + ftp.DownloadAsync(fs, _updateInfo.FilePath).Wait(); + } + } + + + Status = UpdateStatus.Updating; - Status = UpdateStatus.Updating; + int lockedCounter = 0; - for (int i = 0; i < 100; i++) + using (ZipFile zip = ZipFile.Read(tempFile)) + { + int currentEntry = 0; + + zip.ExtractProgress += (x, args) => + { + if (args.EventType == ZipProgressEventType.Extracting_AfterExtractEntry) + { + LogManager.Log("Extracting " + Path.GetFileName(args.CurrentEntry.FileName)); + UpdateProgress = ((double)(currentEntry++) / (double)zip.Entries.Count) * 100d; + } + }; + + foreach (ZipEntry entry in zip) + { + Thread.Sleep(10); + + string newPath = Path.Combine(_appPath, entry.FileName); + + try + { + if (entry.IsDirectory) + { + Directory.CreateDirectory(newPath); + } + else + { + if (!IsFileLocked(newPath)) + { + entry.Extract(_appPath, ExtractExistingFileAction.OverwriteSilently); + } + else + { + LogManager.Log("File locked: " + entry.FileName); + File.Move(newPath, newPath + ".tmp"); + entry.Extract(_appPath, ExtractExistingFileAction.OverwriteSilently); + LogManager.Log("Performed overwrite bypass... (changes will be applied on restart)."); + lockedCounter++; + } + } + } + catch + { + LogManager.Log("Could not overwrite " + entry.FileName); + } + } + } + + Status = UpdateStatus.UpdateCompleted; + } + catch (Exception ex) { - UpdateProgress++; - Thread.Sleep(30); + LogManager.Log(ex, "Error while extracting update package."); + Status = UpdateStatus.Error; } - - Status = UpdateStatus.UpdateCompleted; }); } + /// <summary> + /// Determines whether [is file locked] [the specified file]. + /// </summary> + /// <param name="file">The file.</param + private bool IsFileLocked(String file) + { + FileStream stream = null; + + try + { + stream = new FileInfo(file).Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); + } + catch (IOException) + { + //the file is unavailable because it is: + //still being written to + //or being processed by another thread + //or does not exist (has already been processed) + return true; + } + finally + { + if (stream != null) + stream.Close(); + } + + //file is not locked + return false; + } + private void TryAgain() { CheckForUpdates(); diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/packages.config b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/packages.config index a77e83a59..5dcc330a7 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/packages.config +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/packages.config @@ -3,8 +3,10 @@ <package id="CommonServiceLocator" version="1.3" targetFramework="net46" /> <package id="Dragablz" version="0.0.3.197" targetFramework="net46" /> <package id="EntityFramework" version="6.0.0" targetFramework="net46" /> + <package id="FluentFTP" version="19.1.2" targetFramework="net46" /> <package id="FontAwesome.WPF" version="4.7.0.9" targetFramework="net46" /> <package id="Google.Protobuf" version="3.4.1" targetFramework="net46" /> + <package id="Ionic.Zip" version="1.9.1.8" targetFramework="net46" /> <package id="MahApps.Metro" version="1.5.0" targetFramework="net46" /> <package id="MaterialDesignColors" version="1.1.2" targetFramework="net46" /> <package id="MaterialDesignThemes" version="2.3.1.953" targetFramework="net46" /> |
