using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.Components;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Connection;
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();
command = new CmdCommand("netsh", "wlan start hosted network");
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;
}
}
}
}
}