aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-08-16 12:06:36 +0300
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-08-16 12:06:36 +0300
commit9dbb8f8eb3d07ee07cf7ce1beab72df056e157c6 (patch)
treecd5ace2339d8f3f4a4b92381843497d4f6b21301 /Software/Visual_Studio/PPC/Tango.PPC.UI
parenta3aedf6ba1e0f9cc65877c88f66af6f330484086 (diff)
downloadTango-9dbb8f8eb3d07ee07cf7ce1beab72df056e157c6.tar.gz
Tango-9dbb8f8eb3d07ee07cf7ce1beab72df056e157c6.zip
New Implementation of PPC WiFI networks management!
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/DefaultConnectivityProvider.cs265
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml45
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationView.xaml.cs35
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/Connectivity/WiFiAuthenticationViewVM.cs23
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs15
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj13
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs4
7 files changed, 395 insertions, 5 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;
+ }
+ }
+}
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs
index db9ee3863..2d0ac96fa 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs
@@ -108,14 +108,19 @@ namespace Tango.PPC.UI.PPCApplication
/// <summary>
/// Called when the main window content has been rendered
/// </summary>
- private async void OnMainWindowContentRendered()
+ private void OnMainWindowContentRendered()
{
LogManager.Log("Main window content rendered.");
- PPCSettings settings = null;
-
ContentRendered?.Invoke(this, new EventArgs());
+ StartApplication();
+ }
+
+ private async void StartApplication()
+ {
+ PPCSettings settings = null;
+
await Task.Factory.StartNew(() =>
{
LogManager.Log("Reading PPC settings...");
@@ -154,14 +159,14 @@ namespace Tango.PPC.UI.PPCApplication
}
else
{
- OnDbInitialized();
+ PostDbInitialize();
}
}
/// <summary>
/// Called when the database has been initialized
/// </summary>
- private void OnDbInitialized()
+ private void PostDbInitialize()
{
LogManager.Log($"Raising {nameof(ApplicationStarted)} event...");
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj b/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj
index d1772d0a0..d91bee884 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj
@@ -112,6 +112,11 @@
<Link>GlobalVersionInfo.cs</Link>
</Compile>
<Compile Include="Authentication\DefaultAuthenticationProvider.cs" />
+ <Compile Include="Connectivity\DefaultConnectivityProvider.cs" />
+ <Compile Include="Connectivity\WiFiAuthenticationView.xaml.cs">
+ <DependentUpon>WiFiAuthenticationView.xaml</DependentUpon>
+ </Compile>
+ <Compile Include="Connectivity\WiFiAuthenticationViewVM.cs" />
<Compile Include="Converters\AppBarItemConverter.cs" />
<Compile Include="Converters\NotificationItemConverter.cs" />
<Compile Include="Modules\DefaultStudioModuleLoader.cs" />
@@ -149,6 +154,10 @@
<Compile Include="Views\MainView.xaml.cs">
<DependentUpon>MainView.xaml</DependentUpon>
</Compile>
+ <Page Include="Connectivity\WiFiAuthenticationView.xaml">
+ <SubType>Designer</SubType>
+ <Generator>MSBuild:Compile</Generator>
+ </Page>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -285,6 +294,10 @@
<Project>{74e700b0-1156-4126-be40-ee450d3c3026}</Project>
<Name>Tango.Transport</Name>
</ProjectReference>
+ <ProjectReference Include="..\..\Tango.WiFi\Tango.WiFi.csproj">
+ <Project>{6aa425c9-ea6a-4b01-aaed-5ff122e8b663}</Project>
+ <Name>Tango.WiFi</Name>
+ </ProjectReference>
<ProjectReference Include="..\Modules\Tango.PPC.Jobs\Tango.PPC.Jobs.csproj">
<Project>{096f16c8-6d06-4b5f-9496-b9d2df2d94a3}</Project>
<Name>Tango.PPC.Jobs</Name>
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs
index 61c2ca7c4..5c416a289 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs
@@ -6,6 +6,7 @@ using Tango.Logging;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Authentication;
using Tango.PPC.Common.Connection;
+using Tango.PPC.Common.Connectivity;
using Tango.PPC.Common.Diagnostics;
using Tango.PPC.Common.EventLogging;
using Tango.PPC.Common.ExternalBridge;
@@ -16,6 +17,7 @@ using Tango.PPC.Common.Notifications;
using Tango.PPC.Common.Printing;
using Tango.PPC.Common.Threading;
using Tango.PPC.UI.Authentication;
+using Tango.PPC.UI.Connectivity;
using Tango.PPC.UI.Modules;
using Tango.PPC.UI.Navigation;
using Tango.PPC.UI.Notifications;
@@ -54,6 +56,7 @@ namespace Tango.PPC.UI
TangoIOC.Default.Unregister<IPPCExternalBridgeService>();
TangoIOC.Default.Unregister<IMachineSetupManager>();
TangoIOC.Default.Unregister<IPrintingManager>();
+ TangoIOC.Default.Unregister<IConnectivityProvider>();
TangoIOC.Default.Register<IDispatcherProvider, DefaultDispetcherProvider>(new DefaultDispetcherProvider(Application.Current.Dispatcher));
TangoIOC.Default.Register<INotificationProvider, DefaultNotificationProvider>();
@@ -68,6 +71,7 @@ namespace Tango.PPC.UI
TangoIOC.Default.Register<IPPCExternalBridgeService, PPCExternalBridgeService>();
TangoIOC.Default.Register<IMachineSetupManager, MachineSetupManager>();
TangoIOC.Default.Register<IPrintingManager, DefaultPrintingManager>();
+ TangoIOC.Default.Register<IConnectivityProvider, DefaultConnectivityProvider>();
//TangoIOC.Default.Register<TeamFoundationServiceExtendedClient>(new TeamFoundationServiceExtendedClient("https://twinetfs.visualstudio.com", String.Empty, "szzfokrceo4rhd4eqi5qpmxn3pa5iwl3q7tlqd36l2m7smz2ynoa"));