aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/Console/DefaultConsoleProvider.cs
blob: 09d37f617d61b8f93c63bb70162422cb63091331 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Console.Network;
using Tango.Core;
using Tango.Core.DI;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.Console;

namespace Tango.FSE.UI.Console
{
    /// <summary>
    /// Represents the <see cref="IConsolProvider"/> default implementation.
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    /// <seealso cref="Tango.FSE.Common.Console.IConsolProvider" />
    [TangoCreateWhenRegistered]
    public class DefaultConsoleProvider : ExtendedObject, IConsolProvider
    {
        private IMachineProvider MachineProvider { get; set; }

        #region Events

        /// <summary>
        /// Occurs when the provider has initialized and the available.
        /// </summary>
        public event EventHandler<GetCurrentDirectoryResponse> Initialized;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the current remote directory.
        /// </summary>
        public string CurrentDirectory { get; private set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultConsoleProvider"/> class.
        /// </summary>
        /// <param name="machineProvider">The machine provider.</param>
        public DefaultConsoleProvider(IMachineProvider machineProvider)
        {
            MachineProvider = machineProvider;
            MachineProvider.MachineConnected += MachineProvider_MachineConnected;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Executes the specified command on the remote machine PPC.
        /// </summary>
        /// <param name="request">The command request.</param>
        /// <param name="timeout">The command timeout.</param>
        /// <returns></returns>
        public async Task<ConsoleCommandResponse> ExecuteCommand(ConsoleCommandRequest request, TimeSpan? timeout = null)
        {
            LogManager.Log($"Executing remote console command: '{request.Command}'...");

            try
            {
                var response = await MachineProvider.MachineOperator.SendGenericRequest<ConsoleCommandRequest, ConsoleCommandResponse>(request, new Transport.TransportRequestConfig()
                {
                    ShouldLog = true,
                    Timeout = timeout
                });

                CurrentDirectory = response.WorkingFolder;

                return response;
            }
            catch (Exception ex)
            {
                throw LogManager.Log(ex, "Error executing remote console command.");
            }
        }

        #endregion

        #region Event Handlers

        private async void MachineProvider_MachineConnected(object sender, MachineConnectedEventArgs e)
        {
            if (MachineProvider.ConnectionType.IsRemote())
            {
                try
                {
                    if (e.DifferentFromPrevious)
                    {
                        LogManager.Log("Machine connection changed. Initializing console provider...");
                        var response = await MachineProvider.MachineOperator.SendGenericRequest<GetCurrentDirectoryRequest, GetCurrentDirectoryResponse>(new GetCurrentDirectoryRequest(), new Transport.TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(10) });
                        CurrentDirectory = response.CurrentDirectory;
                        LogManager.Log($"Default console provider initialized successfully.\nCurrent remote directory: '{CurrentDirectory}'.");
                        Initialized?.Invoke(this, response);
                    }
                    else
                    {
                        LogManager.Log("Machine connection changed but machine is the same. Skipping console provider initialization.");
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error getting remote machine current console directory. Console provider could not be initialized.");
                }
            }
        }

        #endregion
    }
}