-
Notifications
You must be signed in to change notification settings - Fork 1
/
PositioningInformation.cs
78 lines (67 loc) · 3.69 KB
/
PositioningInformation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Globalization;
using System.Text.RegularExpressions;
namespace BG96Sharp
{
public readonly struct PositioningInformation
{
public double Latitude { get; }
public double Longitude { get; }
public double HorizontalPrecision { get; }
public double Altitude { get; }
public int Fix { get; }
public double Cog { get; }
public double SpeedKM { get; }
public double SpeedKnots { get; }
public DateTime Date { get; }
public int NumSattelites { get; }
public PositionDisplayFormatMode FormatMode { get; }
public PositioningInformation(string result, PositionDisplayFormatMode mode)
{
FormatMode = mode;
switch (mode)
{
case PositionDisplayFormatMode.LongLatLong:
{
//+QGPSLOC: <UTC>,<latitude>,<longitude>,<hdop>,<altitude>,<fix>,<cog>,<spkm>,<spkn>,<date>,<nsat>
var regex = Regex.Match(result, @"\+QGPSLOC: (\d+.\d+),(\d+.\d+),(.),(\d+.\d+),(.),(\d+.\d+),(\d+.\d+),(\d),(\d+.\d+),(\d+.\d+),(\d+.\d+),(\d+),(\d+)");
if (regex.Groups.Count < 13)
throw new Exception("Invalid result from QGPSLOC: " + result);
//[1] is time (in UTC)
Latitude = double.Parse(regex.Groups[2].Value);
Longitude = double.Parse(regex.Groups[4].Value);
HorizontalPrecision = double.Parse(regex.Groups[6].Value);
Altitude = double.Parse(regex.Groups[7].Value);
Fix = int.Parse(regex.Groups[8].Value);
Cog = double.Parse(regex.Groups[9].Value);
SpeedKM = double.Parse(regex.Groups[10].Value);
SpeedKnots = double.Parse(regex.Groups[11].Value);
//[12] is date (in UTC)
NumSattelites = int.Parse(regex.Groups[13].Value);
Date = DateTime.ParseExact(regex.Groups[1].Value + " " + regex.Groups[12].Value, "HHmmss.f ddMMyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
}
break;
case PositionDisplayFormatMode.PositiveNegativeLatLong:
{
var regex = Regex.Match(result, @"\+QGPSLOC: (\d+.\d+),(-?\d+.\d+),(-?\d+.\d+),(\d+.\d+),(\d+.\d+),(\d),(\d+.\d+),(\d+.\d+),(\d+.\d+),(\d+),(\d+)");
if (regex.Groups.Count < 11)
throw new Exception("Invalid result from QGPSLOC: " + result);
//[1] is time (in UTC)
Latitude = double.Parse(regex.Groups[2].Value);
Longitude = double.Parse(regex.Groups[3].Value);
HorizontalPrecision = double.Parse(regex.Groups[4].Value);
Altitude = double.Parse(regex.Groups[5].Value);
Fix = int.Parse(regex.Groups[6].Value);
Cog = double.Parse(regex.Groups[7].Value);
SpeedKM = double.Parse(regex.Groups[8].Value);
SpeedKnots = double.Parse(regex.Groups[9].Value);
NumSattelites = int.Parse(regex.Groups[11].Value);
Date = DateTime.ParseExact(regex.Groups[1].Value + " " + regex.Groups[10].Value, "HHmmss.f ddMMyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
}
break;
default:
throw new NotImplementedException();
}
}
}
}