Add project files.
This commit is contained in:
85
WindowsLocationApiGpsClient.cs
Normal file
85
WindowsLocationApiGpsClient.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using GpsClient2.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Device.Location;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GpsClient2 {
|
||||
public class WindowsLocationApiGpsClient : BaseGpsClient {
|
||||
#region Private Properties
|
||||
|
||||
private GeoCoordinateWatcher _watcher;
|
||||
|
||||
private DateTimeOffset? _previousReadTime;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public WindowsLocationApiGpsClient(WindowsLocationApiInfo connectionData) : base(GpsType.WindowsLocationApi, connectionData) {
|
||||
}
|
||||
|
||||
public WindowsLocationApiGpsClient(BaseGpsInfo connectionData) : base(GpsType.WindowsLocationApi, connectionData) {
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Connect and Disconnect
|
||||
|
||||
public override bool Connect() {
|
||||
var data = (WindowsLocationApiInfo)GpsInfo;
|
||||
|
||||
IsRunning = true;
|
||||
OnGpsStatusChanged(GpsStatus.Connecting);
|
||||
|
||||
_watcher = new GeoCoordinateWatcher();
|
||||
|
||||
_watcher.PositionChanged += WatcherOnPositionChanged;
|
||||
_watcher.StatusChanged += WatcherOnStatusChanged;
|
||||
|
||||
bool result;
|
||||
try {
|
||||
result = _watcher.TryStart(false, TimeSpan.FromMilliseconds(data.Timeout));
|
||||
} catch {
|
||||
Disconnect();
|
||||
throw;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void WatcherOnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e) {
|
||||
switch (e.Status) {
|
||||
case GeoPositionStatus.Ready:
|
||||
OnGpsStatusChanged(GpsStatus.Connected);
|
||||
break;
|
||||
case GeoPositionStatus.Initializing:
|
||||
OnGpsStatusChanged(GpsStatus.Connecting);
|
||||
break;
|
||||
case GeoPositionStatus.NoData:
|
||||
OnGpsStatusChanged(GpsStatus.Connecting);
|
||||
break;
|
||||
case GeoPositionStatus.Disabled:
|
||||
OnGpsStatusChanged(GpsStatus.Disabled);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void WatcherOnPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) {
|
||||
if (_previousReadTime != null && GpsInfo.ReadFrequenty != 0 && e.Position.Timestamp.Subtract(new TimeSpan(0, 0, 0, 0, GpsInfo.ReadFrequenty)) <= _previousReadTime) return;
|
||||
OnGpsDataReceived(new GpsDataEventArgs(e.Position.Location));
|
||||
_previousReadTime = e.Position.Timestamp;
|
||||
}
|
||||
|
||||
public override bool Disconnect() {
|
||||
IsRunning = false;
|
||||
_watcher.Stop();
|
||||
_watcher.Dispose();
|
||||
OnGpsStatusChanged(GpsStatus.Disabled);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user