diff --git a/GatewayWatchdog/Authentication.xaml.cs b/GatewayWatchdog/Authentication.xaml.cs index e02908b..9194a6c 100644 --- a/GatewayWatchdog/Authentication.xaml.cs +++ b/GatewayWatchdog/Authentication.xaml.cs @@ -34,11 +34,14 @@ public Authentication() } - public SessionInformation Authenticate() + public SessionInformation Authenticate(string url, string username, string password) { - AuthenticationEngine authenticationEngine = new AuthenticationEngine(GatewayUrlText.Text); - return authenticationEngine.Authenticate(AdminUsernameText.Text, AdminPasswordText.Password); + AuthenticationEngine authenticationEngine = new AuthenticationEngine(url); + + Session = authenticationEngine.Authenticate(username, password); + Credentials.Instance.IsInitialized = true; + return Session; } @@ -46,7 +49,11 @@ private void OKButton_Click(object sender, RoutedEventArgs e) { try { - Session = Authenticate(); + Credentials.Instance.Username = AdminUsernameText.Text; + Credentials.Instance.Password = AdminPasswordText.Password; + Credentials.Instance.GatewayUrl = GatewayUrlText.Text; + + Session = Authenticate(Credentials.Instance.GatewayUrl, Credentials.Instance.Username, Credentials.Instance.Password); Close(); } diff --git a/GatewayWatchdog/Credentials.cs b/GatewayWatchdog/Credentials.cs new file mode 100644 index 0000000..10db0f5 --- /dev/null +++ b/GatewayWatchdog/Credentials.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GatewayWatchdog +{ + public class Credentials + { + private static Credentials? _instance; + public static Credentials Instance + { + get + { + if (_instance == null) + _instance = new Credentials(); + + return _instance; + } + } + + public string? Username { get; set; } + public string? Password { get; set; } + public string? GatewayUrl { get; set; } + + public bool IsInitialized { get; set; } + + } +} diff --git a/GatewayWatchdog/MainWindow.xaml.cs b/GatewayWatchdog/MainWindow.xaml.cs index 92b15a2..3aad627 100644 --- a/GatewayWatchdog/MainWindow.xaml.cs +++ b/GatewayWatchdog/MainWindow.xaml.cs @@ -46,6 +46,19 @@ public partial class MainWindow : Window private PingWorker pingWorker; private SessionInformation? _sessionInformation; + private SessionInformation Session + { + get + { + if (_sessionInformation != null) + { + if (_sessionInformation.ExpireDateTime <= DateTime.Now) + _sessionInformation = null; + } + return _sessionInformation; + } + + } GatewayEngine _gatewayEngine = new GatewayEngine(); public MainWindow() @@ -222,14 +235,14 @@ private void Window_Loaded(object sender, RoutedEventArgs e) } } - private async void RestartGatewayBtn_Click(object sender, RoutedEventArgs e) + private void RestartGatewayBtn_Click(object sender, RoutedEventArgs e) { try { - if (_sessionInformation == null) + if (Session == null) Authenticate(); - if (_sessionInformation != null) + if (Session != null) { var confirmResponse = MessageBox.Show("This will reboot the gateway, your internet connection will be down for about 2 minutes. Do you wish to proceed?", "Confirm reboot", MessageBoxButton.YesNo); @@ -265,13 +278,16 @@ private async void ShowTelemetryBtn_Click(object sender, RoutedEventArgs e) { try { - if (_sessionInformation == null) + if (Session == null) Authenticate(); - if (_sessionInformation != null) + if (Session != null) { - var telemetryData = await _gatewayEngine.GetAll(_sessionInformation); - MessageBox.Show(JsonConvert.SerializeObject(telemetryData, Formatting.Indented)); + + var telemetryData = await _gatewayEngine.GetAll(_sessionInformation); + var message = JsonConvert.SerializeObject(telemetryData, Formatting.Indented); + RawDataViewer rawDataViewer = new RawDataViewer(message); + rawDataViewer.ShowDialog(); } } catch (Exception exc) @@ -285,13 +301,15 @@ private async void ShowCellBtn_Click(object sender, RoutedEventArgs e) { try { - if (_sessionInformation == null) + if (Session == null) Authenticate(); - if (_sessionInformation != null) + if (Session != null) { var telemetryData = await _gatewayEngine.GetCells(_sessionInformation); - MessageBox.Show(JsonConvert.SerializeObject(telemetryData, Formatting.Indented)); + var message = JsonConvert.SerializeObject(telemetryData, Formatting.Indented); + RawDataViewer rawDataViewer = new RawDataViewer(message); + rawDataViewer.ShowDialog(); } } catch (Exception exc) @@ -306,13 +324,15 @@ private async void ShowClientsBtn_Click(object sender, RoutedEventArgs e) { - if (_sessionInformation == null) + if (Session == null) Authenticate(); - if (_sessionInformation != null) + if (Session != null) { var telemetryData = await _gatewayEngine.GetDevices(_sessionInformation); - MessageBox.Show(JsonConvert.SerializeObject(telemetryData, Formatting.Indented)); + var message = JsonConvert.SerializeObject(telemetryData, Formatting.Indented); + RawDataViewer rawDataViewer = new RawDataViewer(message); + rawDataViewer.ShowDialog(); } } catch (Exception exc) @@ -327,7 +347,12 @@ private void Authenticate() try { Authentication authenticate = new Authentication(); - authenticate.ShowDialog(); + if (Credentials.Instance.IsInitialized == false) + { + authenticate.ShowDialog(); + } + else + authenticate.Authenticate(Credentials.Instance.GatewayUrl, Credentials.Instance.Username, Credentials.Instance.Password); _sessionInformation = authenticate.Session; } @@ -339,10 +364,10 @@ private void Authenticate() private void TabItem_GotFocus(object sender, RoutedEventArgs e) { - if (_sessionInformation == null) + if (Session == null) Authenticate(); - if (_sessionInformation != null) + if (Session != null) SIMInformationControl.Initialize(_sessionInformation); } } diff --git a/GatewayWatchdog/RawDataViewer.xaml b/GatewayWatchdog/RawDataViewer.xaml index b6b5c32..4d30da4 100644 --- a/GatewayWatchdog/RawDataViewer.xaml +++ b/GatewayWatchdog/RawDataViewer.xaml @@ -5,8 +5,19 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:GatewayWatchdog" mc:Ignorable="d" - Title="RawDataViewer" Height="450" Width="800"> + Title="Data Viewer" Height="450" Width="300"> - + + + + + + + + + + + + diff --git a/GatewayWatchdog/RawDataViewer.xaml.cs b/GatewayWatchdog/RawDataViewer.xaml.cs index 0fbc7ea..4944551 100644 --- a/GatewayWatchdog/RawDataViewer.xaml.cs +++ b/GatewayWatchdog/RawDataViewer.xaml.cs @@ -23,5 +23,15 @@ public RawDataViewer() { InitializeComponent(); } + public RawDataViewer(string message) + { + InitializeComponent(); + ResultsText.Text = message; + } + + private void Button_Click(object sender, RoutedEventArgs e) + { + Close(); + } } } diff --git a/TMobileAPI/AuthenticationEngine.cs b/TMobileAPI/AuthenticationEngine.cs index 4859d2b..7bfaa0b 100644 --- a/TMobileAPI/AuthenticationEngine.cs +++ b/TMobileAPI/AuthenticationEngine.cs @@ -1,5 +1,6 @@ using GatewayWatchdog.Models; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace TMobileAPI { @@ -39,11 +40,16 @@ public SessionInformation Authenticate(string username, string password) throw new Exception(authResult.result.message); } } + + return new SessionInformation { GatewayUrl = GatewayUrl, - Token = authResult.auth.Token + Token = authResult.auth.Token, + ExpireDateTime = DateTimeOffset.FromUnixTimeSeconds(authResult.auth.Expiration).LocalDateTime }; } } + + } \ No newline at end of file diff --git a/TMobileAPI/SessionInformation.cs b/TMobileAPI/SessionInformation.cs index 111093e..59aed47 100644 --- a/TMobileAPI/SessionInformation.cs +++ b/TMobileAPI/SessionInformation.cs @@ -4,5 +4,7 @@ public class SessionInformation { public string GatewayUrl { get; set; } public string Token { get; set; } + + public DateTime ExpireDateTime { get; set; } } } \ No newline at end of file