From 9dbb8f8eb3d07ee07cf7ce1beab72df056e157c6 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Thu, 16 Aug 2018 12:06:36 +0300 Subject: New Implementation of PPC WiFI networks management! --- .../Connectivity/DefaultConnectivityProvider.cs | 265 +++++++++++++++++++++ .../Connectivity/WiFiAuthenticationView.xaml | 45 ++++ .../Connectivity/WiFiAuthenticationView.xaml.cs | 35 +++ .../Connectivity/WiFiAuthenticationViewVM.cs | 23 ++ 4 files changed, 368 insertions(+) create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/DefaultConnectivityProvider.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationViewVM.cs (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/DefaultConnectivityProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/DefaultConnectivityProvider.cs new file mode 100644 index 000000000..ec51b9a98 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/DefaultConnectivityProvider.cs @@ -0,0 +1,265 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Data; +using Tango.BL; +using Tango.Core; +using Tango.Core.Commands; +using Tango.Core.Cryptography; +using Tango.PPC.Common; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Connectivity; +using Tango.PPC.Common.Notifications; +using Tango.Settings; +using Tango.WiFi; + +namespace Tango.PPC.UI.Connectivity +{ + public class DefaultConnectivityProvider : ExtendedObject, IConnectivityProvider + { + private Wifi _wifi; + private INotificationProvider _notification; + + /// + /// Occurs when the connectivity provider state has changed (e.g network connected/disconnected). + /// + public event EventHandler ConnectionStateChanged; + + private bool _isConnected; + /// + /// Gets a value indicating whether there is any Internet connection available. + /// + public bool IsConnected + { + get { return _isConnected; } + set { _isConnected = value; RaisePropertyChangedAuto(); } + } + + /// + /// Gets the available WiFi networks. + /// + public ObservableCollection AvailableWiFiNetworks { get; } + + /// + /// Gets the available WiFi networks as a sorted view source by signal strength. + /// + public ICollectionView AvailableWiFiNetworksViewSource { get; set; } + + /// + /// Gets the connect to WiFi command. + /// + public RelayCommand ConnectToWiFiCommand { get; private set; } + + /// + /// Gets the disconnect from WiFi command. + /// + public RelayCommand DisconnectFromWiFiCommand { get; private set; } + + /// + /// + /// + public RelayCommand RefreshAvailableWiFiNetworksCommand { get; set; } + + /// + /// Initializes a new instance of the class. + /// + public DefaultConnectivityProvider(IPPCApplicationManager applicationManager, INotificationProvider notificationProvider) + { + _notification = notificationProvider; + + AvailableWiFiNetworks = new ObservableCollection(); + AvailableWiFiNetworks.EnableCrossThreadOperations(); + AvailableWiFiNetworksViewSource = CollectionViewSource.GetDefaultView(AvailableWiFiNetworks); + + AvailableWiFiNetworksViewSource.SortDescriptions.Add(new SortDescription(nameof(WiFiNetwork.IsConnected), ListSortDirection.Descending)); + AvailableWiFiNetworksViewSource.SortDescriptions.Add(new SortDescription(nameof(WiFiNetwork.SignalStrength), ListSortDirection.Descending)); + + _wifi = new Wifi(); + + applicationManager.Ready += ApplicationManager_Ready; + + ConnectToWiFiCommand = new RelayCommand(async (x) => + { + await Connect(x); + }); + + DisconnectFromWiFiCommand = new RelayCommand((x) => + { + Disconnect(x); + }); + + RefreshAvailableWiFiNetworksCommand = new RelayCommand(() => + { + RefreshAvailableWiFiNetworks(); + }); + } + + /// + /// Handles the application manager ApplicationReady event. + /// + /// + /// + private void ApplicationManager_Ready(object sender, EventArgs e) + { + Task.Factory.StartNew(async () => + { + Thread.Sleep(2000); + + await RefreshAvailableWiFiNetworks(); + var settings = SettingsManager.Default.GetOrCreate(); + + var auto_connect_network = AvailableWiFiNetworks.ToList().FirstOrDefault(x => x.Name == settings.AutoConnectWiFiName); + + if (auto_connect_network != null && !auto_connect_network.IsConnected) + { + await Connect(auto_connect_network); + } + }); + } + + /// + /// Gets the available WiFi networks. + /// + /// + public Task> GetAvailableWiFiNetworks() + { + return Task.Factory.StartNew>(() => + { + var accessPoints = _wifi.GetAccessPoints(); + + AvailableWiFiNetworks.Where(x => !accessPoints.ToList().Exists(y => y.Name == x.Name)).ToList().ForEach(x => AvailableWiFiNetworks.Remove(x)); + + foreach (var ap in accessPoints.ToList()) + { + var network = AvailableWiFiNetworks.FirstOrDefault(x => x.Name == ap.Name); + + if (network == null) + { + network = new WiFiNetwork(ap, _wifi); + AvailableWiFiNetworks.Add(network); + } + else + { + network.AccessPoint = ap; + } + } + + return AvailableWiFiNetworks.ToList(); + }); + } + + /// + /// Resets the available WiFi networks collection. + /// + public Task RefreshAvailableWiFiNetworks() + { + return Task.Factory.StartNew(() => + { + AvailableWiFiNetworks.Clear(); + GetAvailableWiFiNetworks().Wait(); + }); + } + + /// + /// Checks the Internet connection. + /// + /// + public Task CheckInternetConnection() + { + return Task.Factory.StartNew(() => + { + try + { + using (var client = new WebClient()) + using (client.OpenRead("http://clients3.google.com/generate_204")) + { + return true; + } + } + catch + { + return false; + } + }); + } + + /// + /// Connects the specified network. + /// + /// The network. + /// + public async Task Connect(WiFiNetwork network) + { + var auth = network.CreateAuthenticationRequest(); + + bool result = false; + + if ((auth.IsPasswordRequired || auth.IsUserNameRequired) && !network.HasProfile) + { + var vm = await _notification.ShowDialog(new WiFiAuthenticationViewVM(auth, network)); + + if (vm.DialogResult) + { + result = await network.Connect(auth); + await RefreshAvailableWiFiNetworks(); + } + else + { + return false; + } + } + else + { + result = await network.Connect(auth); + await RefreshAvailableWiFiNetworks(); + return result; + } + + if (result) + { + OnConnectionStateChanged(true); + var settings = SettingsManager.Default.GetOrCreate(); + settings.AutoConnectWiFiName = network.AutoConnect ? network.Name : null; + settings.Save(); + } + + return result; + } + + /// + /// Disconnects the specified network. + /// + /// The network. + /// + public void Disconnect(WiFiNetwork network) + { + Task.Factory.StartNew(() => + { + var settings = SettingsManager.Default.GetOrCreate(); + settings.AutoConnectWiFiName = null; + settings.Save(); + network.AutoConnect = false; + network.Disconnect().Wait(); + RefreshAvailableWiFiNetworks().Wait(); + OnConnectionStateChanged(false); + }); + } + + /// + /// Called when the connection state has been changed + /// + /// if set to true [connected]. + protected virtual void OnConnectionStateChanged(bool connected) + { + IsConnected = connected; + ConnectionStateChanged?.Invoke(this, new ConnectionStateEventArgs() { IsConnected = connected }); + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml b/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml new file mode 100644 index 000000000..311c3ce99 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + Connect to + + + + + + + + Enter user name + + + + Enter the network security key + + + + + + + + CANCEL + CONNECT + + + + diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml.cs new file mode 100644 index 000000000..690f2321f --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace Tango.PPC.UI.Connectivity +{ + /// + /// Interaction logic for WiFiAuthenticationView.xaml + /// + public partial class WiFiAuthenticationView : UserControl + { + public WiFiAuthenticationView() + { + InitializeComponent(); + + this.Loaded += WiFiAuthenticationView_Loaded; + } + + private void WiFiAuthenticationView_Loaded(object sender, RoutedEventArgs e) + { + txtPassword.Focus(); + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationViewVM.cs new file mode 100644 index 000000000..68fb2496b --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationViewVM.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.PPC.Common.Connectivity; +using Tango.SharedUI; + +namespace Tango.PPC.UI.Connectivity +{ + public class WiFiAuthenticationViewVM : DialogViewVM + { + public WiFiAuthentication WiFiAuthentication { get; set; } + + public WiFiNetwork WiFiNetwork { get; set; } + + public WiFiAuthenticationViewVM(WiFiAuthentication wifiAuthentication,WiFiNetwork wifiNetwork) : base() + { + WiFiAuthentication = wifiAuthentication; + WiFiNetwork = wifiNetwork; + } + } +} -- cgit v1.3.1