using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.BL.Enumerations; using Tango.Console.Network; using Tango.Core; using Tango.Core.DI; using Tango.FSE.BL; using Tango.FSE.Common.Authentication; using Tango.FSE.Common.Connection; using Tango.FSE.Common.Console; using Tango.FSE.Common.Notifications; namespace Tango.FSE.UI.Console { /// /// Represents the default implementation. /// /// /// [TangoCreateWhenRegistered] public class DefaultConsoleProvider : ExtendedObject, IConsolProvider { private IMachineProvider MachineProvider { get; set; } [TangoInject] private INotificationProvider NotificationProvider { get; set; } [TangoInject] public IAuthenticationProvider AuthenticationProvider { get; set; } #region Events /// /// Occurs when the provider has initialized and the available. /// public event EventHandler Initialized; #endregion #region Properties /// /// Gets the current remote directory. /// public string CurrentDirectory { get; private set; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The machine provider. public DefaultConsoleProvider(IMachineProvider machineProvider) { MachineProvider = machineProvider; MachineProvider.MachineConnected += MachineProvider_MachineConnected; } #endregion #region Public Methods /// /// Executes the specified command on the remote machine PPC. /// /// The command request. /// The command timeout. /// public async Task ExecuteCommand(ConsoleCommandRequest request, TimeSpan? timeout = null) { AuthenticationProvider.ThrowIfNoPermission(Permissions.FSE_ExecuteRemoteConsoleCommands); LogManager.Log($"Executing remote console command: '{request.Command}'..."); try { var response = await MachineProvider.MachineOperator.SendGenericRequest(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(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."); NotificationProvider.PushErrorReportingSnackbar(ex, "PPC Module Error", "Could not initialize the remote command prompt console."); } } } #endregion } }