aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/HotSpot/DefaultHotSpotProvider.cs
blob: 835a7fc4a3889054bab0850b93f543366f2a3702 (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.Core;
using Tango.Core.Components;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Connection;
using Tango.Settings;

namespace Tango.PPC.Common.HotSpot
{
    /// <summary>
    /// Represents the default hot spot provider.
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    /// <seealso cref="Tango.PPC.Common.HotSpot.IHotSpotProvider" />
    public class DefaultHotSpotProvider : ExtendedObject, IHotSpotProvider
    {
        private IMachineProvider _machineProvider;

        private bool _isEnabled;
        /// <summary>
        /// Gets a value indicating whether the hot spot network is active.
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            private set { _isEnabled = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultHotSpotProvider"/> class.
        /// </summary>
        /// <param name="applicationManager">The application manager.</param>
        /// <param name="machineProvider">The machine provider.</param>
        public DefaultHotSpotProvider(IPPCApplicationManager applicationManager, IMachineProvider machineProvider)
        {
            _machineProvider = machineProvider;
            applicationManager.ApplicationReady += ApplicationManager_ApplicationReady;
        }

        /// <summary>
        /// Handles the ApplicationReady event of the ApplicationManager.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ApplicationManager_ApplicationReady(object sender, EventArgs e)
        {
            Task.Factory.StartNew(async () =>
            {
                var settings = SettingsManager.Default.GetOrCreate<PPCSettings>();

                if (settings.EnableHotSpot)
                {
                    try
                    {
                        await EnableHotSpot(settings.HotSpotPassword);
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error starting on application startup.");
                    }
                }
            });
        }

        /// <summary>
        /// Enables the hot spot.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        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;
                }
            }
        }

        /// <summary>
        /// Disables the hot spot.
        /// </summary>
        /// <returns></returns>
        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;
                }
            }
        }
    }
}