Improve display of messages in desktop sample app

This commit is contained in:
Morten Nielsen 2019-02-19 22:55:08 -08:00
parent d2c1d2e11f
commit a576649114
8 changed files with 84 additions and 43 deletions

View file

@ -1,4 +1,4 @@
using NmeaParser.Nmea.Gps;
using NmeaParser.Nmea;
using System;
using System.Collections.Generic;
using System.Linq;
@ -26,13 +26,13 @@ namespace SampleApp.WinDesktop
InitializeComponent();
}
public Gpgga Message
public Gga Message
{
get { return (Gpgga)GetValue(GpggaProperty); }
get { return (Gga)GetValue(GpggaProperty); }
set { SetValue(GpggaProperty, value); }
}
public static readonly DependencyProperty GpggaProperty =
DependencyProperty.Register("Message", typeof(Gpgga), typeof(GpggaControl), new PropertyMetadata(null));
DependencyProperty.Register("Message", typeof(Gga), typeof(GpggaControl), new PropertyMetadata(null));
}
}

View file

@ -1,4 +1,4 @@
using NmeaParser.Nmea.Gps;
using NmeaParser.Nmea;
using System;
using System.Collections.Generic;
using System.Linq;
@ -26,13 +26,13 @@ namespace SampleApp.WinDesktop
InitializeComponent();
}
public Gpgll Message
public Gll Message
{
get { return (Gpgll)GetValue(MessageProperty); }
get { return (Gll)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(Gpgll), typeof(GpgllControl), new PropertyMetadata(null));
DependencyProperty.Register("Message", typeof(Gll), typeof(GpgllControl), new PropertyMetadata(null));
}
}

View file

@ -1,4 +1,4 @@
using NmeaParser.Nmea.Gps;
using NmeaParser.Nmea;
using System;
using System.Collections.Generic;
using System.Linq;
@ -26,13 +26,13 @@ namespace SampleApp.WinDesktop
InitializeComponent();
}
public Gpgsa Message
public Gsa Message
{
get { return (Gpgsa)GetValue(GpgsaProperty); }
get { return (Gsa)GetValue(GpgsaProperty); }
set { SetValue(GpgsaProperty, value); }
}
public static readonly DependencyProperty GpgsaProperty =
DependencyProperty.Register("Message", typeof(Gpgsa), typeof(GpgsaControl), new PropertyMetadata(null));
DependencyProperty.Register("Message", typeof(Gsa), typeof(GpgsaControl), new PropertyMetadata(null));
}
}

View file

@ -1,4 +1,4 @@
using NmeaParser.Nmea.Gps;
using NmeaParser.Nmea;
using System;
using System.Collections.Generic;
using System.Linq;
@ -26,13 +26,13 @@ namespace SampleApp.WinDesktop
InitializeComponent();
}
public Gprmc Message
public Rmc Message
{
get { return (Gprmc)GetValue(MessageProperty); }
get { return (Rmc)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(Gprmc), typeof(GprmcControl), new PropertyMetadata(null));
DependencyProperty.Register("Message", typeof(Rmc), typeof(GprmcControl), new PropertyMetadata(null));
}
}

View file

@ -52,12 +52,14 @@ namespace SampleApp.WinDesktop
/// Unloads the current device, and opens the next device
/// </summary>
/// <param name="device"></param>
private void StartDevice(NmeaParser.NmeaDevice device)
private async void StartDevice(NmeaParser.NmeaDevice device)
{
//Clean up old device
if (currentDevice != null)
{
currentDevice.MessageReceived -= device_MessageReceived;
if(currentDevice.IsOpen)
await currentDevice.CloseAsync();
currentDevice.Dispose();
}
output.Text = "";
@ -100,17 +102,17 @@ namespace SampleApp.WinDesktop
satView.GsvMessages = gsvMessages.SelectMany(m=>m.Value);
}
}
if (args.Message is NmeaParser.Nmea.Gps.Gprmc)
gprmcView.Message = args.Message as NmeaParser.Nmea.Gps.Gprmc;
else if (args.Message is NmeaParser.Nmea.Gps.Gpgga)
gpggaView.Message = args.Message as NmeaParser.Nmea.Gps.Gpgga;
else if (args.Message is NmeaParser.Nmea.Gps.Gpgsa)
gpgsaView.Message = args.Message as NmeaParser.Nmea.Gps.Gpgsa;
else if (args.Message is NmeaParser.Nmea.Gps.Gpgll)
gpgllView.Message = args.Message as NmeaParser.Nmea.Gps.Gpgll;
else if (args.Message is NmeaParser.Nmea.Rmc)
gprmcView.Message = args.Message as NmeaParser.Nmea.Rmc;
else if (args.Message is NmeaParser.Nmea.Gga)
gpggaView.Message = args.Message as NmeaParser.Nmea.Gga;
else if (args.Message is NmeaParser.Nmea.Gsa)
gpgsaView.Message = args.Message as NmeaParser.Nmea.Gsa;
else if (args.Message is NmeaParser.Nmea.Gll)
gpgllView.Message = args.Message as NmeaParser.Nmea.Gll;
else if (args.Message is NmeaParser.Nmea.Gps.Garmin.Pgrme)
pgrmeView.Message = args.Message as NmeaParser.Nmea.Gps.Garmin.Pgrme;
else if (args.Message is NmeaParser.Nmea.UnknownMessage)
else
{
var ctrl = MessagePanel.Children.OfType<UnknownMessageControl>().Where(c => c.Message.MessageType == args.Message.MessageType).FirstOrDefault();
if(ctrl == null)
@ -121,11 +123,7 @@ namespace SampleApp.WinDesktop
};
MessagePanel.Children.Add(ctrl);
}
ctrl.Message = args.Message as NmeaParser.Nmea.UnknownMessage;
}
else
{
//
ctrl.Message = args.Message;
}
});
}
@ -145,10 +143,17 @@ namespace SampleApp.WinDesktop
//Creates a serial port device from the selected settings
private void ConnectToSerialButton_Click(object sender, RoutedEventArgs e)
{
var portName = serialPorts.Text as string;
var baudRate = int.Parse(baudRates.Text);
var device = new NmeaParser.SerialPortDevice(new System.IO.Ports.SerialPort(portName, baudRate));
StartDevice(device);
try
{
var portName = serialPorts.Text as string;
var baudRate = int.Parse(baudRates.Text);
var device = new NmeaParser.SerialPortDevice(new System.IO.Ports.SerialPort(portName, baudRate));
StartDevice(device);
}
catch(System.Exception ex)
{
MessageBox.Show("Error connecting: " + ex.Message);
}
}
//Attempts to perform an auto discovery of serial ports

View file

@ -25,8 +25,6 @@ namespace SampleApp.WinDesktop
InitializeComponent();
}
public IEnumerable<NmeaParser.Nmea.Gsv> GsvMessages
{
get { return (IEnumerable<NmeaParser.Nmea.Gsv>)GetValue(GsvMessagesProperty); }

View file

@ -7,10 +7,10 @@
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Message}">
<Border Background="LightGray" Padding="10" Margin="-10,-10,-10,5">
<Border Background="LightGray" Padding="10" Margin="-10,-10,-10,5" x:Name="HeaderPanel">
<TextBlock Text="{Binding MessageType}" x:Name="typeName" FontSize="20" FontWeight="Bold" Foreground="White" />
</Border>
<ItemsControl x:Name="Values" ItemsSource="{Binding Values}">
<ItemsControl x:Name="Values">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">

View file

@ -28,13 +28,51 @@ namespace SampleApp.WinDesktop
InitializeComponent();
}
public UnknownMessage Message
public NmeaMessage Message
{
get { return (UnknownMessage)GetValue(MessageProperty); }
get { return (NmeaMessage)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(NmeaParser.Nmea.UnknownMessage), typeof(UnknownMessageControl), new PropertyMetadata(null));
}
DependencyProperty.Register("Message", typeof(NmeaMessage), typeof(UnknownMessageControl), new PropertyMetadata(null, OnMessagePropertyChanged));
private static void OnMessagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = (UnknownMessageControl)d;
if (e.NewValue == null || e.NewValue is UnknownMessage && !(e.OldValue is UnknownMessage))
{
ctrl.HeaderPanel.Background = new SolidColorBrush(Colors.LightGray);
}
else if ((e.OldValue == null || e.OldValue is UnknownMessage) && !(e.NewValue is UnknownMessage))
{
ctrl.HeaderPanel.Background = new SolidColorBrush(Colors.CornflowerBlue);
}
if (e.NewValue == null)
ctrl.Values.ItemsSource = null;
else if (e.NewValue is UnknownMessage unk)
ctrl.Values.ItemsSource = unk.Values;
else
{
var props = e.NewValue.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public).Where(p => !p.CanWrite);
List<string> values = new List<string>();
foreach (var prop in props.OrderBy(t => t.Name))
{
if (prop.Name == nameof(NmeaMessage.MessageType) || prop.Name == nameof(NmeaMessage.Checksum))
continue;
var value = prop.GetValue(e.NewValue);
if (!(value is string) && value is System.Collections.IEnumerable arr)
{
var str = "[" + string.Join(",", arr.OfType<object>().ToArray()) + "]";
if (str.Length == 2)
str = "[ ]";
value = str;
}
values.Add($"{prop.Name}: {value}");
}
ctrl.Values.ItemsSource = values;
}
}
}
}