aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Video/Resolution.cs
blob: 994c02edfd8ea831453d7a97734762d9ef75a6aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Video.DirectCapture
{
    /// <summary>
    /// Holds a resolution (width and height) for a DeviceCapture object.
    /// </summary>
    public class Resolution : INotifyPropertyChanged
    {
        /// <summary>
        /// Initializes a new instance of Resolution.
        /// </summary>
        public Resolution()
        {

        }

        /// <summary>
        /// Initializes a new instance of Resoultion.
        /// </summary>
        /// <param name="width">desired frame width.</param>
        /// <param name="height">desired frame height.</param>
        public Resolution(int width, int height)
            : this()
        {
            Width = width;
            Height = height;
        }

        private int _width;

        /// <summary>
        /// Gets or sets the current frame width.
        /// </summary>
        public int Width
        {
            get { return _width; }
            set { _width = value; RaisePropertyChanged("Width"); }
        }

        private int _height;

        /// <summary>
        /// Gets or sets the current frame height.
        /// </summary>
        public int Height
        {
            get { return _height; }
            set { _height = value; RaisePropertyChanged("Height"); }
        }


        /// <summary>
        /// Returns a string representation of the current resolution.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Width + "x" + Height;
        }

        private void RaisePropertyChanged(String propName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
ight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using FluentFTP;
using Ionic.Zip;
using Microsoft.Win32;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core;
using Tango.Core.Commands;
using Tango.Core.Cryptography;
using Tango.Core.Helpers;
using Tango.Core.IO;
using Tango.PPC.Common;
using Tango.PPC.Common.Update;
using Tango.Settings;
using Tango.SharedUI;
using Tango.SQLExaminer;
using Tango.Transport.Web;
using Tango.Web;

namespace Tango.PPC.Publisher
{
    public class MainWindowVM : ViewModel
    {
        private IPPCUpdateService _client;

        private BasicHashGenerator _hashGenerator;

        private List<MachineVersion> _machineVersions;
        public List<MachineVersion> MachineVersions
        {
            get { return _machineVersions; }
            set { _machineVersions = value; RaisePropertyChangedAuto(); }
        }

        private MachineVersion _selectedMachineVersion;
        public MachineVersion SelectedMachineVersion
        {
            get { return _selectedMachineVersion; }
            set { _selectedMachineVersion = value; RaisePropertyChangedAuto(); OnSelectedMachineVersionChanged(); }
        }

        private String _selectedBuildConfiguration;
        public String SelectedBuildConfiguration
        {
            get { return _selectedBuildConfiguration; }
            set { _selectedBuildConfiguration = value; RaisePropertyChangedAuto(); }
        }

        private String _localVersion;
        public String LocalVersion
        {
            get { return _localVersion; }
            set { _localVersion = value; RaisePropertyChangedAuto(); }
        }

        private String _currentVersion;
        public String CurrentVersion
        {
            get { return _currentVersion; }
            set { _currentVersion = value; RaisePropertyChangedAuto(); }
        }

        private String _firmwareUpgradeFilePath;
        public String FirmwareUpgradeFilePath
        {
            get { return _firmwareUpgradeFilePath; }
            set { _firmwareUpgradeFilePath = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<SequenceItem> _provisionSequenceItems;
        public ObservableCollection<SequenceItem> ProvisionSequenceItems
        {
            get { return _provisionSequenceItems; }
            set { _provisionSequenceItems = value; RaisePropertyChangedAuto(); }
        }

        private ICollectionView _provisionSequenceItemsView;
        public ICollectionView ProvisionSequenceItemsView
        {
            get { return _provisionSequenceItemsView; }
            set { _provisionSequenceItemsView = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<SequenceItem> _updateSequenceItems;
        public ObservableCollection<SequenceItem> UpdateSequenceItems
        {
            get { return _updateSequenceItems; }
            set { _updateSequenceItems = value; RaisePropertyChangedAuto(); }
        }

        private ICollectionView _updateSequenceItemsView;
        public ICollectionView UpdateSequenceItemsView
        {
            get { return _updateSequenceItemsView; }
            set { _updateSequenceItemsView = value; RaisePropertyChangedAuto(); }
        }


        private String _email;
        public String Email
        {
            get { return _email; }
            set { _email = value; RaisePropertyChangedAuto(); }
        }

        private String _password;
        public String Password
        {
            get { return _password; }
            set { _password = value; RaisePropertyChangedAuto(); }
        }

        private DeploymentSlot _deplotmentSlot;
        public DeploymentSlot DeploymentSlot
        {
            get { return _deplotmentSlot; }
            set { _deplotmentSlot = value; RaisePropertyChangedAuto(); OnDeploymentSlotChanged(); }
        }

        private String _comments;
        public String Comments
        {
            get { return _comments; }
            set { _comments = value; RaisePropertyChangedAuto(); }
        }

        private double _maxProgress;
        public double MaxProgress
        {
            get { return _maxProgress; }
            set { _maxProgress = value; RaisePropertyChangedAuto(); }
        }

        private double _progress;
        public double Progress
        {
            get { return _progress; }
            set { _progress = value; RaisePropertyChangedAuto(); }
        }

        private bool _isUpdating;
        public bool IsUpdating
        {
            get { return _isUpdating; }
            set { _isUpdating = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand PublishCommand { get; set; }

        public RelayCommand CreateTupCommand { get; set; }

        public RelayCommand FirmwareUpgradePackageBrowseCommand { get; set; }

        public MainWindowVM()
        {
            _client = new PPCUpdateService();

            _hashGenerator = new BasicHashGenerator();

            var settings = SettingsManager.Default.GetOrCreate<PublisherSettings>();

            SelectedBuildConfiguration = settings.BuildConfiguration;
            DeploymentSlot = settings.DeploymentSlot;

            ProvisionSequenceItems = new ObservableCollection<SequenceItem>(settings.ProvisionSequenceItems);
            ProvisionSequenceItemsView = CollectionViewSource.GetDefaultView(ProvisionSequenceItems);
            ProvisionSequenceItemsView.SortDescriptions.Add(new SortDescription(nameof(SequenceItem.Index), ListSortDirection.Ascending));

            UpdateSequenceItems = new ObservableCollection<SequenceItem>(settings.UpdateSequenceItems);
            UpdateSequenceItemsView = CollectionViewSource.GetDefaultView(UpdateSequenceItems);
            UpdateSequenceItemsView.SortDescriptions.Add(new SortDescription(nameof(SequenceItem.Index), ListSortDirection.Ascending));

            SettingsManager.Default.GetOrCreate<CoreSettings>();
            SettingsManager.Default.Save();

            LocalVersion = FileVersionInfo.GetVersionInfo(Core.Helpers.AssemblyHelper.GetCurrentAssemblyFolder() + "\\Tango.PPC.UI.exe").ProductVersion;

            PublishCommand = new RelayCommand(Publish);
            CreateTupCommand = new RelayCommand(PublishTupFile);
            FirmwareUpgradePackageBrowseCommand = new RelayCommand(BrowseFirmwareUpgradePackage);
        }

        private async void OnSelectedMachineVersionChanged()
        {
            if (SelectedMachineVersion != null)
            {
                var response = await _client.GetLatestVersion(new LatestVersionRequest()
                {
                    MachineVersionGuid = SelectedMachineVersion.Guid,
                });

                CurrentVersion = response.Version;
            }
        }

        private async void OnDeploymentSlotChanged()
        {
            SettingsManager.Default.GetOrCreate<PPCSettings>().DeploymentSlot = DeploymentSlot;
            SelectedMachineVersion = null;
            MachineVersions = (await _client.GetMachineVersions()).MachineVersions;
            SelectedMachineVersion = MachineVersions.OrderBy(x => x.Version).LastOrDefault();
        }

        private void Publish()
        {
            Task.Factory.StartNew(async () =>
                {
                    IsUpdating = true;
                    String tempFile = String.Empty;

                    var settings = SettingsManager.Default.GetOrCreate<PublisherSettings>();
                    settings.ProvisionSequenceItems = ProvisionSequenceItems.ToList();
                    settings.UpdateSequenceItems = UpdateSequenceItems.ToList();
                    settings.DeploymentSlot = DeploymentSlot;
                    settings.BuildConfiguration = SelectedBuildConfiguration;

                    settings.Save();

                    try
                    {
                        var response = await _client.UploadVersion(new UploadVersionRequest()
                        {
                            Comments = Comments,
                            Email = Email,
                            Password = _hashGenerator.Encrypt(Password),
                            Version = LocalVersion,
                            MachineVersionGuid = SelectedMachineVersion.Guid
                        });

                        tempFile = TemporaryManager.Default.CreateFile(".zip");

                        CreateTupPackage(tempFile);

                        Progress = 0;
                        MaxProgress = 100;

                        using (StorageBlobUploader uploader = new StorageBlobUploader(response.BlobAddress, tempFile))
                        {
                            uploader.Progress += (x, e) => 
                            {
                                InvokeUINow(() =>
                                {
                                    Thread.Sleep(10);
                                    Progress = ((double)e.Current / (double)e.Total) * 100d;
                                });
                            };

                            await uploader.Upload();
                        }

                        await _client.NotifyUploadCompleted(new UploadCompletedRequest()
                        {
                            AccessToken = response.Token,
                        });

                        Progress = 0;
                        OnSelectedMachineVersionChanged();
                        ShowInfo("Version published successfully!");
                    }
                    catch (Exception ex)
                    {
                        ShowError(ex.Message);
                    }
                    finally
                    {
                        IsUpdating = false;
                    }
                });
        }

        private void PublishTupFile()
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Tango Update Package|*.tup";
            dlg.DefaultExt = ".tup";
            if (dlg.ShowDialog().Value)
            {
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        IsUpdating = true;
                        CreateTupPackage(dlg.FileName);
                        Progress = 0;
                        ShowInfo("Package file created successfully!");
                    }
                    catch (Exception ex)
                    {
                        ShowError(ex.Message);
                    }
                    finally
                    {
                        IsUpdating = false;
                    }
                });
            }
        }

        private void BrowseFirmwareUpgradePackage()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select Tango Firmware Package";
            dlg.Filter = "Tango Firmware Package Files|*.tfp";
            if (dlg.ShowDialog().Value)
            {
                FirmwareUpgradeFilePath = dlg.FileName;
            }
        }

        private void ShowError(String error)
        {
            MessageBox.Show(error, "PPC Publisher", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void ShowInfo(String message)
        {
            MessageBox.Show(message, "PPC Publisher", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private bool ShowQuestion(String message)
        {
            return MessageBox.Show(message, "PPC Publisher", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes;
        }

        private void CreateTupPackage(String filePath)
        {
            String _appPath = String.Format(AppDomain.CurrentDomain.BaseDirectory + "..\\{0}", SelectedBuildConfiguration);
            var tempFile = filePath;

            using (ZipFile zip = new ZipFile())
            {
                zip.AddFile(FirmwareUpgradeFilePath, "/");

                String provision_dir = "Provision Scripts";

                zip.AddDirectoryByName(provision_dir);

                ExaminerSequenceConfiguration provision_config = new ExaminerSequenceConfiguration();

                foreach (var item in ProvisionSequenceItems)
                {
                    provision_config.Items.Add(new ExaminerSequenceItem()
                    {
                        Direction = item.Direction,
                        FileName = item.FileName,
                        Index = item.Index,
                        Name = item.Name,
                        Type = item.Type,
                        RequiresSerialNumber = item.RequiresSerialNumber
                    });

                    zip.AddFile(item.FilePath, provision_dir);
                }

                String provision_config_file = TemporaryManager.Default.CreateFile(".zip");
                provision_config.ToFile(provision_config_file);

                var cf = zip.AddFile(provision_config_file, provision_dir);
                cf.FileName = provision_dir + "\\config.xml";



                String update_dir = "Update Scripts";

                zip.AddDirectoryByName(update_dir);

                ExaminerSequenceConfiguration update_config = new ExaminerSequenceConfiguration();

                foreach (var item in UpdateSequenceItems)
                {
                    update_config.Items.Add(new ExaminerSequenceItem()
                    {
                        Direction = item.Direction,
                        FileName = item.FileName,
                        Index = item.Index,
                        Name = item.Name,
                        Type = item.Type,
                        RequiresSerialNumber = item.RequiresSerialNumber
                    });

                    zip.AddFile(item.FilePath, update_dir);
                }

                String update_config_file = TemporaryManager.Default.CreateFile(".zip");
                update_config.ToFile(update_config_file);

                var cuf = zip.AddFile(update_config_file, update_dir);
                cuf.FileName = update_dir + "\\config.xml";

                foreach (var file in Directory.GetFiles(_appPath, "*.*", SearchOption.TopDirectoryOnly))
                {
                    zip.AddFile(file, "/");
                }

                zip.SaveProgress += (x, e) =>
                {
                    MaxProgress = e.EntriesTotal;
                    Progress = e.EntriesSaved;
                };

                zip.Save(tempFile);
            }
        }

        protected override void RaisePropertyChangedAuto([CallerMemberName] string caller = null)
        {
            base.RaisePropertyChangedAuto(caller);
            InvalidateRelayCommands();
        }
    }
}