diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-16 12:06:36 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-16 12:06:36 +0300 |
| commit | 9dbb8f8eb3d07ee07cf7ce1beab72df056e157c6 (patch) | |
| tree | cd5ace2339d8f3f4a4b92381843497d4f6b21301 /Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity | |
| parent | a3aedf6ba1e0f9cc65877c88f66af6f330484086 (diff) | |
| download | Tango-9dbb8f8eb3d07ee07cf7ce1beab72df056e157c6.tar.gz Tango-9dbb8f8eb3d07ee07cf7ce1beab72df056e157c6.zip | |
New Implementation of PPC WiFI networks management!
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity')
4 files changed, 368 insertions, 0 deletions
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; + + /// <summary> + /// Occurs when the connectivity provider state has changed (e.g network connected/disconnected). + /// </summary> + public event EventHandler<ConnectionStateEventArgs> ConnectionStateChanged; + + private bool _isConnected; + /// <summary> + /// Gets a value indicating whether there is any Internet connection available. + /// </summary> + public bool IsConnected + { + get { return _isConnected; } + set { _isConnected = value; RaisePropertyChangedAuto(); } + } + + /// <summary> + /// Gets the available WiFi networks. + /// </summary> + public ObservableCollection<WiFiNetwork> AvailableWiFiNetworks { get; } + + /// <summary> + /// Gets the available WiFi networks as a sorted view source by signal strength. + /// </summary> + public ICollectionView AvailableWiFiNetworksViewSource { get; set; } + + /// <summary> + /// Gets the connect to WiFi command. + /// </summary> + public RelayCommand<WiFiNetwork> ConnectToWiFiCommand { get; private set; } + + /// <summary> + /// Gets the disconnect from WiFi command. + /// </summary> + public RelayCommand<WiFiNetwork> DisconnectFromWiFiCommand { get; private set; } + + /// <summary> + /// + /// </summary> + public RelayCommand RefreshAvailableWiFiNetworksCommand { get; set; } + + /// <summary> + /// Initializes a new instance of the <see cref="DefaultConnectivityProvider"/> class. + /// </summary> + public DefaultConnectivityProvider(IPPCApplicationManager applicationManager, INotificationProvider notificationProvider) + { + _notification = notificationProvider; + + AvailableWiFiNetworks = new ObservableCollection<WiFiNetwork>(); + 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<WiFiNetwork>(async (x) => + { + await Connect(x); + }); + + DisconnectFromWiFiCommand = new RelayCommand<WiFiNetwork>((x) => + { + Disconnect(x); + }); + + RefreshAvailableWiFiNetworksCommand = new RelayCommand(() => + { + RefreshAvailableWiFiNetworks(); + }); + } + + /// <summary> + /// Handles the application manager ApplicationReady event. + /// </summary> + /// <param name="sender"></param> + /// <param name="e"></param> + private void ApplicationManager_Ready(object sender, EventArgs e) + { + Task.Factory.StartNew(async () => + { + Thread.Sleep(2000); + + await RefreshAvailableWiFiNetworks(); + var settings = SettingsManager.Default.GetOrCreate<PPCSettings>(); + + 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); + } + }); + } + + /// <summary> + /// Gets the available WiFi networks. + /// </summary> + /// <returns></returns> + public Task<List<WiFiNetwork>> GetAvailableWiFiNetworks() + { + return Task.Factory.StartNew<List<WiFiNetwork>>(() => + { + 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(); + }); + } + + /// <summary> + /// Resets the available WiFi networks collection. + /// </summary> + public Task RefreshAvailableWiFiNetworks() + { + return Task.Factory.StartNew(() => + { + AvailableWiFiNetworks.Clear(); + GetAvailableWiFiNetworks().Wait(); + }); + } + + /// <summary> + /// Checks the Internet connection. + /// </summary> + /// <returns></returns> + public Task<bool> CheckInternetConnection() + { + return Task.Factory.StartNew<bool>(() => + { + try + { + using (var client = new WebClient()) + using (client.OpenRead("http://clients3.google.com/generate_204")) + { + return true; + } + } + catch + { + return false; + } + }); + } + + /// <summary> + /// Connects the specified network. + /// </summary> + /// <param name="network">The network.</param> + /// <returns></returns> + public async Task<bool> Connect(WiFiNetwork network) + { + var auth = network.CreateAuthenticationRequest(); + + bool result = false; + + if ((auth.IsPasswordRequired || auth.IsUserNameRequired) && !network.HasProfile) + { + var vm = await _notification.ShowDialog<WiFiAuthenticationViewVM>(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<PPCSettings>(); + settings.AutoConnectWiFiName = network.AutoConnect ? network.Name : null; + settings.Save(); + } + + return result; + } + + /// <summary> + /// Disconnects the specified network. + /// </summary> + /// <param name="network">The network.</param> + /// <returns></returns> + public void Disconnect(WiFiNetwork network) + { + Task.Factory.StartNew(() => + { + var settings = SettingsManager.Default.GetOrCreate<PPCSettings>(); + settings.AutoConnectWiFiName = null; + settings.Save(); + network.AutoConnect = false; + network.Disconnect().Wait(); + RefreshAvailableWiFiNetworks().Wait(); + OnConnectionStateChanged(false); + }); + } + + /// <summary> + /// Called when the connection state has been changed + /// </summary> + /// <param name="connected">if set to <c>true</c> [connected].</param> + 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 @@ +<UserControl x:Class="Tango.PPC.UI.Connectivity.WiFiAuthenticationView" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:touch="clr-namespace:Tango.Touch.Controls;assembly=Tango.Touch" + xmlns:local="clr-namespace:Tango.PPC.UI.Connectivity" + mc:Ignorable="d" + Background="{StaticResource TangoPrimaryBackgroundBrush}" MinWidth="500" Height="570" d:DataContext="{d:DesignInstance Type=local:WiFiAuthenticationViewVM, IsDesignTimeCreatable=False}"> + <Grid Margin="10"> + <Grid.RowDefinitions> + <RowDefinition Height="200"/> + <RowDefinition Height="1*"/> + <RowDefinition Height="50"/> + </Grid.RowDefinitions> + + <StackPanel> + <touch:TouchIcon Icon="WifiStrength3Lock" Width="100" Height="100" Foreground="{StaticResource TangoPrimaryAccentBrush}" Margin="10" /> + <TextBlock HorizontalAlignment="Center" FontSize="{StaticResource TangoTitleFontSize}"> + <Run>Connect to </Run> + <Run Foreground="{StaticResource TangoPrimaryAccentBrush}" FontStyle="Italic" Text="{Binding WiFiNetwork.Name,Mode=OneWay}"></Run> + </TextBlock> + </StackPanel> + + <Grid Grid.Row="1"> + <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300" TextElement.FontSize="{StaticResource TangoDefaultFontSize}"> + <StackPanel Margin="0 0 0 40" Visibility="{Binding WiFiAuthentication.IsUserNameRequired,Converter={StaticResource BooleanToVisibilityConverter}}"> + <TextBlock TextAlignment="Center">Enter user name</TextBlock> + <touch:TouchTextBox Margin="0 10 0 0" Background="{StaticResource TangoPrimaryBackgroundBrush}" Text="{Binding WiFiAuthentication.UserName}"></touch:TouchTextBox> + </StackPanel> + <StackPanel Margin="0 0 0 0" Visibility="{Binding WiFiAuthentication.IsPasswordRequired,Converter={StaticResource BooleanToVisibilityConverter}}"> + <TextBlock x:Name="txtPassword" TextAlignment="Center">Enter the network security key</TextBlock> + <touch:TouchTextBox Margin="0 10 0 0" IsPassword="True" Background="{StaticResource TangoPrimaryBackgroundBrush}" Text="{Binding WiFiAuthentication.Password}"></touch:TouchTextBox> + </StackPanel> + </StackPanel> + </Grid> + + <Grid Grid.Row="2"> + <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> + <touch:TouchButton Command="{Binding CloseCommand}" CornerRadius="25" Width="170" Height="50" Style="{StaticResource TangoHollowButton}">CANCEL</touch:TouchButton> + <touch:TouchButton CornerRadius="25" Width="170" Height="50" Margin="10 0 0 0" Command="{Binding OKCommand}">CONNECT</touch:TouchButton> + </StackPanel> + </Grid> + </Grid> +</UserControl> 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 +{ + /// <summary> + /// Interaction logic for WiFiAuthenticationView.xaml + /// </summary> + 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; + } + } +} |
