-
Notifications
You must be signed in to change notification settings - Fork 1
/
Locator.cs
174 lines (149 loc) · 5.96 KB
/
Locator.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Globalization;
using Windows.Devices.Geolocation;
using Windows.Foundation;
namespace GeolocationTCP
{
public class Locator
{
private Geolocator loc = null;
private TypedEventHandler<Geolocator, PositionChangedEventArgs> positionChangedHandler;
private TypedEventHandler<Geolocator, StatusChangedEventArgs> statusChangedHandler;
private MainWindow window;
private GeolocationProvider provider;
public Locator(MainWindow w)
{
window = w;
loc = new Geolocator();
loc.DesiredAccuracy = PositionAccuracy.High;
loc.DesiredAccuracyInMeters = 10;
loc.ReportInterval = 500;
StartTracking();
}
public void SetProvider(TcpLib.TcpServiceProvider p)
{
provider = (GeolocationProvider)p;
}
/// <SUMMARY>
/// Calculates NMEA sentence checksum.
/// Expects NMEA sentence including $.
/// </SUMMARY>
private string getChecksum(string sentence)
{
int checksum = Convert.ToByte(sentence[sentence.IndexOf('$') + 1]);
for (int i = sentence.IndexOf('$') + 2; i < sentence.Length; i++)
{
checksum ^= Convert.ToByte(sentence[i]);
}
// format as hexadecimal
return checksum.ToString("X2");
}
public String decimalToNMEA(double lat, double lon)
{
string nmea = "";
double lata = Math.Abs(lat);
double latd = Math.Truncate(lata);
double latm = (lata - latd) * 60;
string lath = lat > 0 ? "N" : "S";
double lnga = Math.Abs(lon);
double lngd = Math.Truncate(lnga);
double lngm = (lnga - lngd) * 60;
string lngh = lon > 0.0 ? "E" : "W";
nmea += latd.ToString("00") +
latm.ToString("00.00", CultureInfo.InvariantCulture) + "," +
lath + ",";
nmea += lngd.ToString("000") +
lngm.ToString("00.00", CultureInfo.InvariantCulture) + "," +
lngh;
return nmea;
}
public void Report(string nmea)
{
try
{
provider.Report(nmea);
} catch(Exception e){
//window.SetLocationLog(e.ToString());
}
}
public void UpdateUI(DateTimeOffset datetime, string nmea, double lat, double lon,
string speed, string accuracy, PositionSource source)
{
//try
//{
window.SetLocationLog(datetime.ToString() + " sending NMEA:");
window.SetLocationLog(nmea);
window.SetLatLon(lat, lon);
if (speed == "")
{
speed = "n/a";
}
else
{
speed += "kn";
}
window.SetSpeed(speed);
window.SetDatetime(datetime.ToString());
window.SetAccuracy(accuracy+"m");
window.SetSource(source.ToString());
//}
//catch (Exception ex) {
// throw ex;
//}
}
public void StartTracking()
{
window.SetStatus(loc.LocationStatus.ToString());
if (positionChangedHandler == null)
{
positionChangedHandler = (geo, e) =>
{
Geoposition pos = e.Position;
DateTimeOffset datetime = pos.Coordinate.Timestamp;
String time = pos.Coordinate.Timestamp.UtcDateTime.ToString("hhmmss");
String date = pos.Coordinate.Timestamp.UtcDateTime.ToString("dMMyy");
double lat = (double)pos.Coordinate.Point.Position.Latitude;
double lon = (double)pos.Coordinate.Point.Position.Longitude;
string accuracy = pos.Coordinate.Accuracy.ToString();
String heading = "0.0";
if (pos.Coordinate.Heading != null)
{
heading = pos.Coordinate.Heading.Value.ToString("00.00");
}
String speed = "0.0";
if (pos.Coordinate.Speed != null)
{
double kn = (double) pos.Coordinate.Speed / 0.514444;
speed = kn.ToString("00.00");
}
String coords = decimalToNMEA(lat, lon);
String sentence = String.Format("$GPRMC,{0},A,{1},{2},{3},{4},,",
time, coords, speed, heading, date);
String nmea = sentence + "*" + getChecksum(sentence);
//Console.WriteLine("Sent NMEA sentence {0}", nmea);
Report(nmea);
PositionSource source = e.Position.Coordinate.PositionSource;
UpdateUI(datetime, nmea, lat, lon, speed, accuracy, source);
};
}
if (statusChangedHandler == null)
{
statusChangedHandler = (geo, e) =>
{
PositionStatus status = e.Status;
try
{
window.SetStatus(status.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
};
}
// this implicitly starts the tracking operation
loc.PositionChanged += positionChangedHandler;
loc.StatusChanged += statusChangedHandler;
}
}
}