aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/ViewModels/LayoutViewVM.cs
blob: 00adb72b07fcf12cc05e876d05473e0061f0e33b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Console.Network
{
    public class ConsoleCommandRequest
    {
        public String WorkingFolder { get; set; }
        public String Command { get; set; }
        public bool RunAsAdministrator { get; set; }
    }
}
id='n255' href='#n255'>255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.Core.Threading;
using Tango.FSE.Common;
using Tango.FSE.Common.Navigation;
using Tango.FSE.Common.Notifications;
using Tango.FSE.UI.Panes;
using Tango.FSE.UI.Views;
using Tango.Integration.ExternalBridge;
using Tango.SharedUI.Helpers;
using Tango.Transport;

namespace Tango.FSE.UI.ViewModels
{
    /// <summary>
    /// Represents the application main layout view model.
    /// </summary>
    /// <seealso cref="Tango.FSE.Common.FSEViewModel" />
    public class LayoutViewVM : FSEViewModel
    {
        private ActionTimer _transportOutActionTimer;
        private ActionTimer _transportInActionTimer;

        #region Properties

        private bool _isConnectionPaneOpened;
        /// <summary>
        /// Gets or sets a value indicating whether the machine connection pane is opened.
        /// </summary>
        public bool IsConnectionPaneOpened
        {
            get { return _isConnectionPaneOpened; }
            set { _isConnectionPaneOpened = value; RaisePropertyChangedAuto(); OnConnectionPaneToggleChanged(); }
        }

        private MachineConnectionPaneVM _connectionPaneVM;
        /// <summary>
        /// Gets or sets the machine connection pane view model.
        /// </summary>
        public MachineConnectionPaneVM ConnectionPaneVM
        {
            get { return _connectionPaneVM; }
            set { _connectionPaneVM = value; RaisePropertyChangedAuto(); }
        }

        private ConnectedMachinePaneVM _connectedMachinePaneVM;
        /// <summary>
        /// Gets or sets the connected machine pane view model.
        /// </summary>
        public ConnectedMachinePaneVM ConnectedMachinePaneVM
        {
            get { return _connectedMachinePaneVM; }
            set { _connectedMachinePaneVM = value; RaisePropertyChangedAuto(); }
        }

        private LogViewerPaneVM _logViewPaneVM;
        /// <summary>
        /// Gets or sets the log viewer pane view model.
        /// </summary>
        public LogViewerPaneVM LogViewerPaneVM
        {
            get { return _logViewPaneVM; }
            set { _logViewPaneVM = value; RaisePropertyChangedAuto(); }
        }

        private bool _isMenuOpened;
        /// <summary>
        /// Gets or sets a value indicating whether the main menu is opened.
        /// </summary>
        public bool IsMenuOpened
        {
            get { return _isMenuOpened; }
            set { _isMenuOpened = value; RaisePropertyChangedAuto(); }
        }

        private bool _isLogsOpened;
        /// <summary>
        /// Gets or sets a value indicating whether the log viewer is opened.
        /// </summary>
        public bool IsLogsOpened
        {
            get { return _isLogsOpened; }
            set { _isLogsOpened = value; RaisePropertyChangedAuto(); }
        }

        private bool _transportOut;
        /// <summary>
        /// Transport out on/off.
        /// </summary>
        public bool TransportOut
        {
            get { return _transportOut; }
            set { _transportOut = value; RaisePropertyChanged(nameof(TransportOut)); }
        }

        private bool _transportIn;
        /// <summary>
        /// Transport in on/off.
        /// </summary>
        public bool TransportIn
        {
            get { return _transportIn; }
            set { _transportIn = value; RaisePropertyChanged(nameof(TransportIn)); }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Toggles the connection pane visibility.
        /// </summary>
        public RelayCommand ToggleConnectionPaneCommand { get; set; }

        /// <summary>
        /// Logs outs the current user.
        /// </summary>
        public RelayCommand LogoutCommand { get; set; }

        /// <summary>
        /// Opens the bug report dialog.
        /// </summary>
        public RelayCommand ReportIssueCommand { get; set; }

        /// <summary>
        /// Navigates to the events view.
        /// </summary>
        public RelayCommand NavigateToEventsCommand { get; set; }

        /// <summary>
        /// Navigates to the home view.
        /// </summary>
        public RelayCommand NavigateHomeCommand { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="LayoutViewVM"/> class.
        /// </summary>
        public LayoutViewVM()
        {
            ToggleConnectionPaneCommand = new RelayCommand(() => IsConnectionPaneOpened = !IsConnectionPaneOpened);
            ConnectionPaneVM = new MachineConnectionPaneVM();
            ConnectionPaneVM.MachineSelected += ConnectionPaneVM_MachineSelected;

            ConnectedMachinePaneVM = new ConnectedMachinePaneVM();
            ConnectedMachinePaneVM.DisconnectedFromMachine += ConnectedMachinePaneVM_DisconnectedFromMachine;

            LogViewerPaneVM = new LogViewerPaneVM();

            LogoutCommand = new RelayCommand(Logout);

            _transportOutActionTimer = new ActionTimer(TimeSpan.FromMilliseconds(50));
            _transportInActionTimer = new ActionTimer(TimeSpan.FromMilliseconds(50));

            ReportIssueCommand = new RelayCommand(OpenBugReportDialog);

            NavigateToEventsCommand = new RelayCommand(NavigateToEvents);
            NavigateHomeCommand = new RelayCommand(NavigateHome);
        }

        #endregion

        #region Machine Operator Event Handlers

        private void MachineProvider_MachineDisconnected(object sender, Common.Connection.MachineDisconnectedEventArgs e)
        {
            if (e.MachineOperator.Adapter != null)
            {
                e.MachineOperator.Adapter.PropertyChanged -= Adapter_PropertyChanged;
            }
        }

        private void MachineProvider_MachineConnected(object sender, Common.Connection.MachineConnectedEventArgs e)
        {
            e.MachineOperator.Adapter.PropertyChanged -= Adapter_PropertyChanged;
            e.MachineOperator.Adapter.PropertyChanged += Adapter_PropertyChanged;
        }

        private void Adapter_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(ITransportAdapter.TotalBytesSent))
            {
                TransportOut = true;
                _transportOutActionTimer.ResetReplace(() => TransportOut = false);
            }
            else if (e.PropertyName == nameof(ITransportAdapter.TotalBytesReceived))
            {
                TransportIn = true;
                _transportInActionTimer.ResetReplace(() => TransportIn = false);
            }
        }

        #endregion

        #region Connected Machine Pane Event Handlers

        private async void ConnectedMachinePaneVM_DisconnectedFromMachine(object sender, EventArgs e)
        {
            IsConnectionPaneOpened = false;

            try
            {
                await MachineProvider.DisconnectMachine();
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error disconnecting from machine.");
                await NotificationProvider.ShowError("Error occurred while trying to disconnect from the machine.");
            }
        }

        #endregion

        #region Machine Connection Pane Event Handlers

        private async void ConnectionPaneVM_MachineSelected(object sender, IExternalBridgeClient machine)
        {
            IsConnectionPaneOpened = false;

            LogManager.Log($"Machine '{machine.SerialNumber}' selected for connection by user.");

            try
            {
                if (await MachineProvider.ConnectToMachine(machine))
                {
                    NotificationProvider.PushSnackbarItem(MessageType.Success, "Machine successfully connected", true, $"{MachineProvider.Machine.SerialNumber}, {MachineProvider.Machine.Name}", TimeSpan.FromSeconds(5));
                }
            }
            catch (Exception ex)
            {
                await NotificationProvider.ShowError($"Error occurred while trying to connect to the specified machine.\n{ex.FlattenMessage()}");
                LogManager.Log(ex, $"Error connecting to machine '{machine.SerialNumber}'.");
            }
        }

        #endregion

        #region Override Methods

        public override void OnApplicationStarted()
        {
            MachineProvider.MachineDisconnected += MachineProvider_MachineDisconnected;
            MachineProvider.MachineConnected += MachineProvider_MachineConnected;
        }

        public override void OnNavigatedTo()
        {
            base.OnNavigatedTo();
            this.SetFocus(nameof(ToggleConnectionPaneCommand));
        }

        public override async Task<bool> OnApplicationLogout()
        {
            if (MachineProvider.IsConnected)
            {
                if (await NotificationProvider.ShowWarningQuestion("The current machine connection will be automatically closed.\nAre you sure you want to log out?"))
                {
                    await MachineProvider.DisconnectMachine();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }

        #endregion

        #region Private Methods

        private void OnConnectionPaneToggleChanged()
        {
            if (!MachineProvider.IsConnected)
            {
                if (IsConnectionPaneOpened)
                {
                    try
                    {
                        ConnectionPaneVM.Start();
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error while trying to initialize machine scanning.");
                        NotificationProvider.ShowError($"Error occurred while trying to initialize machine scanning.\n{ex.FlattenMessage()}");
                        _isConnectionPaneOpened = false;
                        RaisePropertyChanged(nameof(IsConnectionPaneOpened));
                    }
                }
                else
                {
                    try
                    {
                        ConnectionPaneVM.Stop();
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error while stopping machine scanning.");
                    }
                }
            }
        }

        private void Logout()
        {
            LogManager.Log("Logout command pressed by user.");
            AuthenticationProvider.LogOut();
        }

        private void OpenBugReportDialog()
        {
            BugReporter.ShowBugReportDialog();
        }

        private void NavigateToEvents()
        {
            NavigationManager.NavigateTo<InternalModule>(nameof(EventsView));
        }

        private void NavigateHome()
        {
            NavigationManager.NavigateTo(NavigationView.Home);
        }

        #endregion
    }
}