Skip to content

Commit

Permalink
- Added a raw data viewer component to replace MessageBox
Browse files Browse the repository at this point in the history
- Cached credentials to refresh tokens when they expire
  • Loading branch information
jhurder-cvet committed Oct 4, 2022
1 parent 6bf52d6 commit 716e508
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 23 deletions.
15 changes: 11 additions & 4 deletions GatewayWatchdog/Authentication.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,26 @@ 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;

}

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();

}
Expand Down
30 changes: 30 additions & 0 deletions GatewayWatchdog/Credentials.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
57 changes: 41 additions & 16 deletions GatewayWatchdog/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}
Expand All @@ -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);
}
}
Expand Down
15 changes: 13 additions & 2 deletions GatewayWatchdog/RawDataViewer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ScrollViewer Grid.ColumnSpan="2">
<TextBlock FontFamily="Consolas" x:Name="ResultsText"/>
</ScrollViewer>
<Button Grid.Row="1" Grid.Column="1" Margin="5" Padding="5" Content="Close" Click="Button_Click"></Button>
</Grid>
</Window>
10 changes: 10 additions & 0 deletions GatewayWatchdog/RawDataViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,15 @@ public RawDataViewer()
{
InitializeComponent();
}
public RawDataViewer(string message)
{
InitializeComponent();
ResultsText.Text = message;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}
8 changes: 7 additions & 1 deletion TMobileAPI/AuthenticationEngine.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GatewayWatchdog.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace TMobileAPI
{
Expand Down Expand Up @@ -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
};
}
}


}
2 changes: 2 additions & 0 deletions TMobileAPI/SessionInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ public class SessionInformation
{
public string GatewayUrl { get; set; }
public string Token { get; set; }

public DateTime ExpireDateTime { get; set; }
}
}

0 comments on commit 716e508

Please sign in to comment.