NmeaParser/src/SampleApp.WinDesktop/NtripView.xaml.cs

138 lines
5 KiB
C#
Raw Permalink Normal View History

2020-07-29 06:07:53 +02:00
using NmeaParser.Gnss.Ntrip;
using System;
using System.Collections.Generic;
2020-07-29 22:24:37 +02:00
using System.Diagnostics;
2020-09-08 05:01:17 +02:00
using System.Globalization;
using System.IO;
2020-07-29 06:07:53 +02:00
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 SampleApp.WinDesktop
{
/// <summary>
/// Interaction logic for NtripView.xaml
/// </summary>
public partial class NtripView : UserControl
{
public NtripView()
{
InitializeComponent();
2020-09-24 05:46:42 +02:00
host.Text = UserSettings.Default.NTRIPServer;
port.Text = UserSettings.Default.NTRIPPort.ToString();
username.Text = UserSettings.Default.NTRIPUsername;
password.Password = UserSettings.Default.NTRIPPassword;
2020-07-29 06:07:53 +02:00
}
NmeaParser.Gnss.Ntrip.Client client;
private void Button_Click(object sender, RoutedEventArgs e)
{
sourceList.ItemsSource = null;
if (!int.TryParse(port.Text, out int portNumber))
{
MessageBox.Show("Invalid port number");
return;
}
client = new NmeaParser.Gnss.Ntrip.Client(host.Text, portNumber, username.Text, password.Password);
List<NtripStream> sources;
try
{
sources = client.GetSourceTable().OfType<NtripStream>().ToList();
2020-09-08 05:01:17 +02:00
if(sources.Count == 0)
{
MessageBox.Show($"No NTRIP data streams found at {host.Text}:{portNumber}");
return;
}
2020-07-29 06:07:53 +02:00
}
catch(System.Exception ex)
{
MessageBox.Show("Failed to connect: " + ex.Message);
return;
}
2020-09-24 05:46:42 +02:00
UserSettings.Default.NTRIPServer = host.Text;
UserSettings.Default.NTRIPPort = portNumber;
UserSettings.Default.NTRIPUsername = username.Text;
UserSettings.Default.NTRIPPassword = password.Password;
UserSettings.Default.Save();
2020-09-08 05:01:17 +02:00
if (MainWindow.monitor != null && !double.IsNaN(MainWindow.monitor.Latitude) && !double.IsNaN(MainWindow.monitor.Longitude))
{
var lat = MainWindow.monitor.Latitude;
var lon = MainWindow.monitor.Longitude;
// Order by closest source
sourceList.ItemsSource = sources.OrderBy(s => PointPlotView.Vincenty.GetDistanceVincenty(lat, lon, s.Latitude, s.Longitude));
}
else
{
sourceList.ItemsSource = sources.OrderBy(s => s.CountryCode);
}
2020-07-29 06:07:53 +02:00
}
Stream ntripStream;
private void Connect_Click(object sender, RoutedEventArgs e)
2020-07-29 06:07:53 +02:00
{
var streaminfo = ((Button)sender).DataContext as NtripStream;
if (streaminfo == null)
2020-07-29 06:07:53 +02:00
return;
ntripStream?.Dispose();
var stream = ntripStream = client.OpenStream(streaminfo.Mountpoint);
_ = Task.Run(async () =>
2020-07-29 06:07:53 +02:00
{
byte[] buffer = new byte[1024];
2020-08-01 00:23:31 +02:00
int count = 0;
while (true)
2020-07-29 08:17:41 +02:00
{
2020-08-01 00:23:31 +02:00
try
{
count = await stream.ReadAsync(buffer).ConfigureAwait(false);
}
catch (System.Exception ex)
{
2020-08-03 05:37:15 +02:00
if(!stream.CanRead)
{
// TODO: Restart stream
return;
}
2020-08-01 00:23:31 +02:00
}
var device = MainWindow.currentDevice;
if (device != null && device.CanWrite)
2020-07-29 06:07:53 +02:00
{
2020-08-01 00:23:31 +02:00
await device.WriteAsync(buffer, 0, count).ConfigureAwait(false);
2020-07-29 06:07:53 +02:00
Dispatcher.Invoke(() =>
{
ntripstatus.Text = $"Transmitted {ntripStream.Position} bytes";
2020-07-29 06:07:53 +02:00
});
}
}
});
ntripstatus.Text = $"Connected";
2020-07-29 06:07:53 +02:00
}
}
2020-09-08 05:01:17 +02:00
public class DistanceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is NtripStream s && MainWindow.monitor?.IsFixValid == true)
{
var lat = MainWindow.monitor.Latitude;
var lon = MainWindow.monitor.Longitude;
var distance = PointPlotView.Vincenty.GetDistanceVincenty(lat, lon, s.Latitude, s.Longitude);
return "Distance: " + (distance / 1000).ToString("0.##") + "km";
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
2020-07-29 06:07:53 +02:00
}