aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.PPCConsole/ViewModels/ConsoleViewVM.cs
blob: 21cd7cd82c8851ca1e829c23e41e23e6ea76a74f (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
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(); }
        }

        /// <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();
        }

        /// <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,
                });

                _lastSuggestions = response.Suggestions;
                e.Complete(new ConsoleCommandExecutionResult()
                {
                    Output = response.Output,
                    Suggestions = response.Suggestions,
                    WorkingFolder = response.WorkingFolder
                });
            }
            catch (TransporterDisconnectedException ex)
            {
                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
                });
            }
        }
    }
}