aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.PPCConsole/ViewModels/ConsoleViewVM.cs
blob: 818159b49560240a695380a55054355c59d19c45 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Console;
using Tango.Console.Network;
using Tango.Core.DI;
using Tango.FSE.Common;
using Tango.FSE.Common.Console;
using Tango.Transport;

namespace Tango.FSE.PPCConsole.ViewModels
{
    /// <summary>
    /// Represents the command prompt console view model.
    /// </summary>
    /// <seealso cref="Tango.FSE.Common.FSEViewModel" />
    public class ConsoleViewVM : FSEViewModel
    {
        private List<ConsoleSuggestion> _lastSuggestions;

        /// <summary>
        /// Gets or sets the console provider.
        /// </summary>
        [TangoInject]
        public IConsolProvider ConsoleProvider { get; set; }

        private ConsoleControlVM _consoleVM;
        /// <summary>
        /// Gets or sets the console control view model.
        /// </summary>
        public ConsoleControlVM ConsoleVM
        {
            get { return _consoleVM; }
            set { _consoleVM = value; RaisePropertyChangedAuto(); }
        }

        private int _timeout;
        /// <summary>
        /// Gets or sets the command timeout.
        /// </summary>
        public int Timeout
        {
            get { return _timeout; }
            set { _timeout = value; RaisePropertyChangedAuto(); }
        }

        private bool _runAsAdministrator;
        /// <summary>
        /// Gets or sets a value indicating whether to run the next command as an administrator.
        /// </summary>
        public bool RunAsAdministrator
        {
            get { return _runAsAdministrator; }
            set { _runAsAdministrator = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleViewVM"/> class.
        /// </summary>
        public ConsoleViewVM()
        {
            _lastSuggestions = new List<ConsoleSuggestion>();
            ConsoleVM = new ConsoleControlVM();
            ConsoleVM.CommandExecuting += ConsoleVM_CommandExecuting;
            ConsoleVM.Clear();
            Timeout = 30;
        }

        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public override void OnApplicationStarted()
        {
            ConsoleProvider.Initialized += ConsoleService_Initialized;
        }

        private void ConsoleService_Initialized(object sender, GetCurrentDirectoryResponse e)
        {
            ConsoleVM.Clear();
            ConsoleVM.CurrentCommand.WorkingFolder = e.CurrentDirectory;
            ConsoleVM.AppendSuggestions(e.Suggestions);
        }

        private async void ConsoleVM_CommandExecuting(object sender, ConsoleCommandExecutingEventArgs e)
        {
            try
            {
                var response = await ConsoleProvider.ExecuteCommand(new ConsoleCommandRequest()
                {
                    Command = e.Command.CommandText,
                    WorkingFolder = e.Command.WorkingFolder,
                    RunAsAdministrator = RunAsAdministrator
                }, TimeSpan.FromSeconds(Timeout));

                _lastSuggestions = response.Suggestions;
                e.Complete(new ConsoleCommandExecutionResult()
                {
                    Output = response.Output,
                    Suggestions = response.Suggestions,
                    WorkingFolder = response.WorkingFolder
                });
            }
            catch (TransporterDisconnectedException)
            {
                e.Complete(new ConsoleCommandExecutionResult()
                {
                    Output = "Please connect to a machine before attempting to execute a command.",
                    WorkingFolder = e.Command.WorkingFolder,
                    Suggestions = _lastSuggestions
                });
            }
            catch (Exception ex)
            {
                e.Complete(new ConsoleCommandExecutionResult()
                {
                    Output = ex.FlattenMessage(),
                    WorkingFolder = e.Command.WorkingFolder,
                    Suggestions = _lastSuggestions
                });
            }
        }
    }
}