aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/IContext.cs
blob: d817c5d463663b29f8dd88fcf1952b1ef75f93b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Scripting.Basic
{
    public interface IContext
    {
        
    }
}
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Tango.Core.Commands;
using Tango.Integration.Operation;
using Tango.Integration.Upgrade;
using Tango.MachineStudio.Common.Notifications;
using Tango.SharedUI;
using Tango.SharedUI.Helpers;

namespace Tango.MachineStudio.UI.ViewModels
{
    public class FirmwareUpgradeViewVM : DialogViewVM
    {
        private IMachineOperator _operator;
        private INotificationProvider _notification;
        private FileStream _stream;

        private FirmwareUpgradeHandler _handler;
        public FirmwareUpgradeHandler Handler
        {
            get { return _handler; }
            set { _handler = value; RaisePropertyChangedAuto(); }
        }

        private String _selectedFile;
        public String SelectedFile
        {
            get { return _selectedFile; }
            set { _selectedFile = value; RaisePropertyChangedAuto(); }
        }

        private int _currentPage;
        public int CurrentPage
        {
            get { return _currentPage; }
            set { _currentPage = value; RaisePropertyChangedAuto(); }
        }

        private String upgradeError;
        public String UpgradeError
        {
            get { return upgradeError; }
            set { upgradeError = value; RaisePropertyChangedAuto(); }
        }

        private bool _dfu;
        public bool DFU
        {
            get { return _dfu; }
            set
            {
                _dfu = value; RaisePropertyChangedAuto();

                if (_dfu)
                {
                    UploadTFP = false;
                    DFUAndTFP = false;
                }
            }
        }

        private bool _uploadTFP;
        public bool UploadTFP
        {
            get { return _uploadTFP; }
            set
            {
                _uploadTFP = value; RaisePropertyChangedAuto();

                if (_uploadTFP)
                {
                    DFU = false;
                    DFUAndTFP = false;
                }
            }
        }

        private bool _dfuAndTFP;

        public bool DFUAndTFP
        {
            get { return _dfuAndTFP; }
            set
            {
                _dfuAndTFP = value; RaisePropertyChangedAuto();

                if (_dfuAndTFP)
                {
                    DFU = false;
                    UploadTFP = false;
                }
            }
        }


        public RelayCommand SelectCommand { get; set; }

        public RelayCommand UpgradeCommand { get; set; }

        public RelayCommand AbortCommand { get; set; }

        public FirmwareUpgradeViewVM(IMachineOperator machineOperator, INotificationProvider notificationProvider) : base()
        {
            DFU = true;
            _notification = notificationProvider;
            _operator = machineOperator;
            SelectCommand = new RelayCommand(BrowseForFile);
            UpgradeCommand = new RelayCommand(StartUpgrade, () => SelectedFile != null);
            AbortCommand = new RelayCommand(AbortUpgrade, () => Handler != null && Handler.Status != FirmwareUpgradeStatus.Validating && Handler.Status != FirmwareUpgradeStatus.Activating);
        }

        private async void BrowseForFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select tango firmware package file";
            dlg.Filter = "Tango Firmware Package|*.tfp";
            if (dlg.ShowDialog().Value)
            {
                try
                {
                    using (FileStream fs = new FileStream(dlg.FileName, FileMode.Open))
                    {
                        var info = await _operator.GetFirmwarePackageInfo(fs);
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex);
                    _notification.ShowError("The selected package seems to be invalid.");
                    return;
                }

                SelectedFile = dlg.FileName;
                InvalidateRelayCommands();
            }
        }

        private async void StartUpgrade()
        {
            CanClose = false;
            CurrentPage = 1;

            try
            {
                IsFree = false;

                if (DFU)
                {
                    _operator.FirmwareUpgradeMode = FirmwareUpgradeModes.DFU;
                }
                else if (UploadTFP)
                {
                    _operator.FirmwareUpgradeMode = FirmwareUpgradeModes.TFP_PACKAGE;
                }
                else if (DFUAndTFP)
                {
                    _operator.FirmwareUpgradeMode = FirmwareUpgradeModes.DFU | FirmwareUpgradeModes.TFP_PACKAGE;
                }

                _stream = new FileStream(SelectedFile, FileMode.Open);
                Handler = await _operator.UpgradeFirmware(_stream);
                Handler.Progress += (_, e) =>
                {
                    InvokeUI(() =>
                    {
                        AbortCommand.RaiseCanExecuteChanged();
                    });

                    UIHelper.DoEvents();
                };
                Handler.Completed += (_, __) =>
                {
                    CanClose = true;
                    _stream.Dispose();
                    CurrentPage = 2;
                    IsFree = true;
                };
                Handler.Canceled += (_, __) =>
                {
                    CanClose = true;
                    _stream.Dispose();
                    CurrentPage = 0;
                    IsFree = true;
                };
                Handler.Failed += (_, ex) =>
                {
                    UpgradeError = ex.FlattenMessage();
                    CanClose = true;
                    _stream.Dispose();
                    CurrentPage = 3;
                    IsFree = true;
                };
            }
            catch (Exception ex)
            {
                IsFree = true;
                CanClose = true;
                UpgradeError = ex.FlattenMessage();
                CurrentPage = 3;
            }
        }

        protected override bool CanOK()
        {
            return base.CanOK() && CanClose;
        }

        private async void AbortUpgrade()
        {
            CanClose = true;
            await Handler.Cancel();
            CurrentPage = 0;
        }
    }
}