aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-02-21 21:30:45 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-02-21 21:30:45 +0200
commitf991f37a95a854e9b2aada52982d83ddaa0ca1b0 (patch)
tree02ea1f3b73cd5a500023a5d0daa905fc6978112a /Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs
parent956f9ea033553136ebf199fff30f288dc26fbeb0 (diff)
downloadTango-f991f37a95a854e9b2aada52982d83ddaa0ca1b0.tar.gz
Tango-f991f37a95a854e9b2aada52982d83ddaa0ca1b0.zip
Working on macine studio update center..
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs')
-rw-r--r--Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs189
1 files changed, 166 insertions, 23 deletions
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();