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

89 lines
2.9 KiB
C#
Raw 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;
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();
}
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();
}
catch(System.Exception ex)
{
MessageBox.Show("Failed to connect: " + ex.Message);
return;
}
sourceList.ItemsSource = sources.OrderBy(s=>s.CountryCode);
}
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)
{
Debugger.Break();
}
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
}
}
}