From d9a89773f2f283fbf5596799dd4d50d231817203 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 12 Dec 2018 18:23:24 +0200 Subject: IHotSpot Provider & IRemoteAssistance Provider. --- .../Connectivity/IConnectivityProvider.cs | 18 ---- .../HotSpot/DefaultHotSpotProvider.cs | 114 ++++++++++++++++++++ .../Tango.PPC.Common/HotSpot/IHotSpotProvider.cs | 32 ++++++ .../MachineSetup/MachineSetupManager.cs | 88 ++++++++++------ .../PPC/Tango.PPC.Common/PPCSettings.cs | 5 + .../PPC/Tango.PPC.Common/PPCViewModel.cs | 14 +++ .../DefaultRemoteAssistanceProvider.cs | 115 +++++++++++++++++++++ .../RemoteAssistance/IRemoteAssistanceProvider.cs | 39 +++++++ .../PPC/Tango.PPC.Common/Scripting/CmdCommand.cs | 1 + .../PPC/Tango.PPC.Common/Tango.PPC.Common.csproj | 6 +- 10 files changed, 380 insertions(+), 52 deletions(-) create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/DefaultHotSpotProvider.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/IHotSpotProvider.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/DefaultRemoteAssistanceProvider.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/IRemoteAssistanceProvider.cs (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Connectivity/IConnectivityProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Connectivity/IConnectivityProvider.cs index 077f05110..85c25128a 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Connectivity/IConnectivityProvider.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Connectivity/IConnectivityProvider.cs @@ -79,23 +79,5 @@ namespace Tango.PPC.Common.Connectivity /// The network. /// void Disconnect(WiFiNetwork network); - - /// - /// Gets a value indicating whether the hot spot network is active. - /// - bool IsHotspotActive { get; } - - /// - /// Enables the hot spot. - /// - /// The password. - /// - Task EnableHotSpot(String password); - - /// - /// Disables the hot spot. - /// - /// - Task DisableHotSpot(); } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/DefaultHotSpotProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/DefaultHotSpotProvider.cs new file mode 100644 index 000000000..1126a84bc --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/DefaultHotSpotProvider.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Scripting; +using Tango.Settings; + +namespace Tango.PPC.Common.HotSpot +{ + /// + /// Represents the default hot spot provider. + /// + /// + /// + public class DefaultHotSpotProvider : ExtendedObject, IHotSpotProvider + { + private IMachineProvider _machineProvider; + + private bool _isEnabled; + /// + /// Gets a value indicating whether the hot spot network is active. + /// + public bool IsEnabled + { + get { return _isEnabled; } + private set { _isEnabled = value; RaisePropertyChangedAuto(); } + } + + /// + /// Initializes a new instance of the class. + /// + /// The application manager. + /// The machine provider. + public DefaultHotSpotProvider(IPPCApplicationManager applicationManager, IMachineProvider machineProvider) + { + _machineProvider = machineProvider; + applicationManager.ApplicationReady += ApplicationManager_ApplicationReady; + } + + /// + /// Handles the ApplicationReady event of the ApplicationManager. + /// + /// The source of the event. + /// The instance containing the event data. + private void ApplicationManager_ApplicationReady(object sender, EventArgs e) + { + Task.Factory.StartNew(async () => + { + var settings = SettingsManager.Default.GetOrCreate(); + + if (settings.EnableHotSpot) + { + try + { + await EnableHotSpot(settings.HotSpotPassword); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error starting on application startup."); + } + } + }); + } + + /// + /// Enables the hot spot. + /// + /// The password. + /// + public async Task EnableHotSpot(string password) + { + if (!IsEnabled) + { + try + { + CmdCommand command = new CmdCommand("netsh", $"wlan set hostednetwork mode=allow ssid='{"Tango_" + _machineProvider.Machine.SerialNumber}' key='{password}'"); + await command.Run(); + IsEnabled = true; + } + catch (Exception ex) + { + LogManager.Log(ex, "Error activating hot spot."); + throw; + } + } + } + + /// + /// Disables the hot spot. + /// + /// + public async Task DisableHotSpot() + { + if (IsEnabled) + { + try + { + CmdCommand command = new CmdCommand("netsh", "wlan stop hosted network"); + await command.Run(); + IsEnabled = false; + } + catch (Exception ex) + { + LogManager.Log(ex, "Error deactivating hot spot."); + throw; + } + } + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/IHotSpotProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/IHotSpotProvider.cs new file mode 100644 index 000000000..1a4dabae3 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/IHotSpotProvider.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.HotSpot +{ + /// + /// Represents a hot-spot network provider. + /// + public interface IHotSpotProvider + { + /// + /// Gets a value indicating whether the hot spot network is active. + /// + bool IsEnabled { get; } + + /// + /// Enables the hot spot. + /// + /// The password. + /// + Task EnableHotSpot(String password); + + /// + /// Disables the hot spot. + /// + /// + Task DisableHotSpot(); + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs index 3c70e0744..3d816c89a 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs @@ -74,14 +74,25 @@ namespace Tango.PPC.Common.MachineSetup { LogManager.Log($"Starting machine setup for serial number {serialNumber}..."); - //Connecting to machine... - LogManager.Log("Initiating machine connection..."); + IMachineOperator op = null; - UpdateProgress("Connecting to machine", "Connecting..."); - IMachineOperator op = await DefaultMachineProvider.CreateMinimalMachineOperator((msg) => + var demoMode = SettingsManager.Default.GetOrCreate().DemoMode; + + if (!demoMode) { - UpdateProgress("Connecting to machine", msg); - }); + //Connecting to machine... + LogManager.Log("Initiating machine connection..."); + + UpdateProgress("Connecting to machine", "Connecting..."); + op = await DefaultMachineProvider.CreateMinimalMachineOperator((msg) => + { + UpdateProgress("Connecting to machine", msg); + }); + } + else + { + LogManager.Log("Application in demo mode. Skipping machine connection..."); + } //Connect to machine service and get matching packages for this machine. UpdateProgress("Downloading software package", "Connecting to machine service..."); @@ -238,39 +249,50 @@ namespace Tango.PPC.Common.MachineSetup throw LogManager.Log(ex, "Setup manager error while trying to synchronize database."); } - //Updating firmware - UpdateProgress("Updating Firmware", "Connecting to firmware device..."); - LogManager.Log(""); - LogManager.Log("-------------------------------------------------------------------------"); - LogManager.Log("Updating Firmware..."); - - UpdateProgress("Updating Firmware", "Loading firmware package..."); - var tfpPath = Path.Combine(_newPackageTempFolder, "firmware_package.tfp"); - var stream = new FileStream(tfpPath, FileMode.Open); - var handler = await op.UpgradeFirmware(stream); - handler.Failed += (_, ex) => + if (!demoMode) { - stream.Dispose(); - result.SetException(ex); - }; - handler.Completed += (_, __) => + //Updating firmware + UpdateProgress("Updating Firmware", "Connecting to firmware device..."); + LogManager.Log(""); + LogManager.Log("-------------------------------------------------------------------------"); + LogManager.Log("Updating Firmware..."); + + UpdateProgress("Updating Firmware", "Loading firmware package..."); + var tfpPath = Path.Combine(_newPackageTempFolder, "firmware_package.tfp"); + var stream = new FileStream(tfpPath, FileMode.Open); + var handler = await op.UpgradeFirmware(stream); + handler.Failed += (_, ex) => + { + stream.Dispose(); + result.SetException(ex); + }; + handler.Completed += (_, __) => + { + UpdateProgress("Updating Firmware", "Firmware update completed successfully."); + stream.Dispose(); + result.SetResult(new MachineSetupResult() + { + UpdatePackagePath = _newPackageTempFolder, + }); + }; + handler.Canceled += (_, __) => + { + stream.Dispose(); + result.SetException(new Exception("The operation has been canceled.")); + }; + handler.Progress += (_, e) => + { + UpdateProgress("Updating Firmware", e.Message, false, e.Current, e.Total); + }; + } + else { - UpdateProgress("Updating Firmware", "Firmware update completed successfully."); - stream.Dispose(); + LogManager.Log("Application in demo mode. Skipping firmware upgrade..."); result.SetResult(new MachineSetupResult() { UpdatePackagePath = _newPackageTempFolder, }); - }; - handler.Canceled += (_, __) => - { - stream.Dispose(); - result.SetException(new Exception("The operation has been canceled.")); - }; - handler.Progress += (_, e) => - { - UpdateProgress("Updating Firmware", e.Message, false, e.Current, e.Total); - }; + } } catch (Exception ex) { diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs index b6ab9c163..142c3afe9 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs @@ -80,6 +80,11 @@ namespace Tango.PPC.Common /// public String HotSpotPassword { get; set; } + /// + /// Gets or sets a value indicating whether to enable team viewer service. + /// + public bool EnableRemoteAssistance { get; set; } + /// /// Initializes a new instance of the class. /// diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs index dc67d2137..289683855 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs @@ -11,9 +11,11 @@ using Tango.PPC.Common.Connection; using Tango.PPC.Common.Connectivity; using Tango.PPC.Common.EventLogging; using Tango.PPC.Common.ExternalBridge; +using Tango.PPC.Common.HotSpot; using Tango.PPC.Common.Navigation; using Tango.PPC.Common.Notifications; using Tango.PPC.Common.Printing; +using Tango.PPC.Common.RemoteAssistance; using Tango.PPC.Common.Storage; using Tango.Settings; using Tango.SharedUI; @@ -83,6 +85,18 @@ namespace Tango.PPC.Common [TangoInject] public IConnectivityProvider ConnectivityProvider { get; set; } + /// + /// Gets or sets the hot spot provider. + /// + [TangoInject] + public IHotSpotProvider HotSpotProvider { get; set; } + + /// + /// Gets or sets the remote assistance provider. + /// + [TangoInject] + public IRemoteAssistanceProvider RemoteAssistanceProvider { get; set; } + /// /// Gets or sets the storage provider. /// diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/DefaultRemoteAssistanceProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/DefaultRemoteAssistanceProvider.cs new file mode 100644 index 000000000..53596544a --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/DefaultRemoteAssistanceProvider.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Scripting; + +namespace Tango.PPC.Common.RemoteAssistance +{ + /// + /// Represents the default remote assistance provider. + /// + /// + /// + public class DefaultRemoteAssistanceProvider : ExtendedObject, IRemoteAssistanceProvider + { + private IMachineProvider _machineProvider; + + private bool _isEnabled; + /// + /// Gets a value indicating whether the remote assistance service is enabled. + /// + public bool IsEnabled + { + get { return _isEnabled; } + private set { _isEnabled = value; RaisePropertyChangedAuto(); } + } + + /// + /// Initializes a new instance of the class. + /// + /// The machine provider. + public DefaultRemoteAssistanceProvider(IMachineProvider machineProvider) + { + _machineProvider = machineProvider; + } + + /// + /// Enables the remote assistance. + /// + /// + public async Task EnableRemoteAssistance() + { + if (!IsEnabled) + { + try + { + CmdCommand command = new CmdCommand("sc.exe", "config TeamViewer start=auto"); + command.Timeout = TimeSpan.FromSeconds(10); + await command.Run(); + + command = new CmdCommand("net", "start TeamViewer"); + command.Timeout = TimeSpan.FromSeconds(10); + await command.Run(); + + IsEnabled = true; + } + catch (Exception ex) + { + LogManager.Log(ex, "Error enabling remote assistance."); + throw; + } + } + } + + /// + /// Disables the remote assistance. + /// + /// + public async Task DisableRemoteAssistance() + { + if (IsEnabled) + { + try + { + CmdCommand command = new CmdCommand("sc.exe", "config TeamViewer start=disabled"); + command.Timeout = TimeSpan.FromSeconds(10); + await command.Run(); + + command = new CmdCommand("net", "stop TeamViewer"); + command.Timeout = TimeSpan.FromSeconds(10); + await command.Run(); + IsEnabled = false; + } + catch (Exception ex) + { + LogManager.Log(ex, "Error disabling remote assistance."); + throw; + } + } + } + + /// + /// Installs the remote assistance. + /// + /// Name of the group. + /// Name of the computer. + /// + public async Task InstallRemoteAssistance(string groupName, string computerName) + { + try + { + CmdCommand command = new CmdCommand("msiexec.exe", $"/i \"C:\\Program Files(x86)\\TeamViewer\\TeamViewer_Host.msi\" /qn CUSTOMCONFIGID=ke43ann APITOKEN=4765529-gon1LwO1N1TTrlLI21ji ASSIGNMENTOPTIONS=\" --reassign --alias {"TANGO-" + _machineProvider.Machine.SerialNumber} --grant-easy-access\""); + await command.Run(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error installing remote assistance."); + throw; + } + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/IRemoteAssistanceProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/IRemoteAssistanceProvider.cs new file mode 100644 index 000000000..288b5c652 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/IRemoteAssistanceProvider.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.RemoteAssistance +{ + /// + /// Represents a remote assistance provider. + /// + public interface IRemoteAssistanceProvider + { + /// + /// Gets a value indicating whether the remote assistance service is enabled. + /// + bool IsEnabled { get; } + + /// + /// Enables the remote assistance. + /// + /// + Task EnableRemoteAssistance(); + + /// + /// Disables the remote assistance. + /// + /// + Task DisableRemoteAssistance(); + + /// + /// Installs the remote assistance. + /// + /// Name of the group. + /// Name of the computer. + /// + Task InstallRemoteAssistance(String groupName, String computerName); + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Scripting/CmdCommand.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Scripting/CmdCommand.cs index 2ee0d83d9..abae98f06 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Scripting/CmdCommand.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Scripting/CmdCommand.cs @@ -29,6 +29,7 @@ namespace Tango.PPC.Common.Scripting _process.StartInfo.RedirectStandardError = true; _process.StartInfo.RedirectStandardOutput = true; _process.StartInfo.Arguments = arguments; + _process.StartInfo.Verb = "runas"; Arguments = arguments; } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj index b9d9c7620..2f95d5060 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj @@ -128,6 +128,8 @@ + + @@ -158,6 +160,8 @@ + + @@ -326,7 +330,7 @@ - + \ No newline at end of file -- cgit v1.3.1