Skip to content

Commit

Permalink
Merge pull request #5 from GhostwareDev/development
Browse files Browse the repository at this point in the history
Create new version
  • Loading branch information
kevingoos committed Oct 19, 2016
2 parents 851dc3d + 8e0834c commit 66532e0
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 15 deletions.
2 changes: 1 addition & 1 deletion GPSDLib/GPSDLib.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ghostware.GPSDLib", "Ghostware.GPSDLib\Ghostware.GPSDLib.csproj", "{76CA949B-C65E-497B-AA48-8651E3306436}"
EndProject
Expand Down
1 change: 1 addition & 0 deletions GPSDLib/Ghostware.GPSDLib/Ghostware.GPSDLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Models\GpsdOptions.cs" />
<Compile Include="Exceptions\UnknownTypeException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Retry.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
10 changes: 8 additions & 2 deletions GPSDLib/Ghostware.GPSDLib/Ghostware.GPSDLib.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@
<package >
<metadata>
<id>Ghostware.GPSDLib</id>
<version>1.0.1</version>
<version>1.0.2</version>
<title>Ghostware.GPSDLib</title>
<authors>Ghosttje</authors>
<owners>Ghostware</owners>
<projectUrl>https://github.com/GhostwareDev/GPSDLib</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A library for connecting to a GPSD service (GPS Deamon) in C#. (There is no official support by the developers for C#)</description>
<releaseNotes>
v1.0.2:
--------
- Added retry pattern.
- Added a raw data event.
- Some bugfixes.

v1.0.1:
--------
- Added support for securestring when using the proxy.

v1.0.0:
--------
- Bugfix: parser crashes when there is no data coming in.
- Rename of package (from Ghosttje.GPSDLib to Ghostware.GPSDLib)
- Rename of package (from Ghosttje.GPSDLib to Ghostware.GPSDLib).

v0.0.1:
--------
Expand Down
15 changes: 13 additions & 2 deletions GPSDLib/Ghostware.GPSDLib/GpsdDataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ public class GpsdDataParser
{
public object GetGpsData(string gpsData)
{
var classType = JsonConvert.DeserializeObject<DataClassType>(gpsData);
return JsonConvert.DeserializeObject(gpsData, classType.GetClassType());
try
{
var classType = JsonConvert.DeserializeObject<DataClassType>(gpsData);
return JsonConvert.DeserializeObject(gpsData, classType.GetClassType());
}
catch (JsonReaderException ex)
{
return null;
}
catch (JsonSerializationException ex)
{
return null;
}
}
}
}
15 changes: 12 additions & 3 deletions GPSDLib/Ghostware.GPSDLib/GpsdService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public class GpsdService
#region Events

public delegate void LocationEventHandler(object source, GpsLocation e);
public delegate void RawLocationEventHandler(object source, string rawLocation);
public event LocationEventHandler OnLocationChanged;
public event RawLocationEventHandler OnRawLocationChanged;

#endregion

Expand Down Expand Up @@ -77,11 +79,16 @@ public void StartService()
var streamReader = new StreamReader(networkStream);

var gpsdDataParser = new GpsdDataParser();

while (IsRunning && _client.Connected)
{
var gpsData = streamReader.ReadLine();
if (gpsData == null) continue;
OnRawLocationChanged?.Invoke(this, gpsData);
if (gpsData == null)
{
networkStream = _client.GetStream();
streamReader = new StreamReader(networkStream);
continue;
}
var message = gpsdDataParser.GetGpsData(gpsData);

var version = message as GpsdVersion;
Expand Down Expand Up @@ -190,7 +197,9 @@ private TcpClient ConnectViaHttpProxy()
webProxy.UseDefaultCredentials = true;
}

var response = request.GetResponse();
var response = Retry.Do(request.GetResponse, TimeSpan.FromSeconds(1));
//var response = request.GetResponse();

var responseStream = response.GetResponseStream();
Debug.Assert(responseStream != null);

Expand Down
4 changes: 3 additions & 1 deletion GPSDLib/Ghostware.GPSDLib/Models/GpsLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class GpsLocation
public float Track { get; set; }

[DataMember(Name = "speed")]
public float Speed { get; set; }
public float SpeedKnots { get; set; }

public double Speed => SpeedKnots * 1.852;

public override string ToString()
{
Expand Down
45 changes: 45 additions & 0 deletions GPSDLib/Ghostware.GPSDLib/Retry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Threading;

namespace Ghostware.GPSDLib
{
public static class Retry
{
public static void Do(
Action action,
TimeSpan retryInterval,
int retryCount = 3)
{
Do<object>(() =>
{
action();
return null;
}, retryInterval, retryCount);
}

public static T Do<T>(
Func<T> action,
TimeSpan retryInterval,
int retryCount = 3)
{
var exceptions = new List<Exception>();

for (int retry = 0; retry < retryCount; retry++)
{
try
{
if (retry > 0)
Thread.Sleep(retryInterval);
return action();
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}

throw new AggregateException(exceptions);
}
}
}
38 changes: 32 additions & 6 deletions GPSDLib/Ghostware.GPSDTestConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using Ghostware.GPSDLib;
using Ghostware.GPSDLib.Models;
Expand All @@ -11,7 +14,9 @@ public class Program
private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);

private delegate bool EventHandler();
static EventHandler _handler;

private static EventHandler _handler;
private static StreamWriter _writer;

private static GpsdService _gpsdService;

Expand All @@ -21,18 +26,39 @@ static void Main(string[] args)
SetConsoleCtrlHandler(_handler, true);

_gpsdService = new GpsdService("***.***.***.***", 80);
_gpsdService.SetProxy("proxy", 80);
_gpsdService.SetProxyAuthentication("*****", "*****");
//_gpsdService = new GpsdService("127.0.0.1", 80);

//_gpsdService.SetProxy("proxy", 80);
//_gpsdService.SetProxyAuthentication("*****", "*****");

_writer = new StreamWriter("testFile1.nmea");

_gpsdService.OnRawLocationChanged += GpsdServiceOnRawLocationChanged;
_gpsdService.OnLocationChanged += GpsdServiceOnLocationChanged;

_gpsdService.OnLocationChanged += GpsdServiceOnOnLocationChanged;
_gpsdService.StartService();
try
{
_gpsdService.StartService();
}
catch (AggregateException ax)
{
if (ax.InnerExceptions.Any(x => x.GetType() == typeof(WebException)))
{
Console.WriteLine("Cannot connect to the service you have given.");
}
}
}

private static void GpsdServiceOnOnLocationChanged(object source, GpsLocation e)
private static void GpsdServiceOnLocationChanged(object source, GpsLocation e)
{
Console.WriteLine(e.ToString());
}

private static void GpsdServiceOnRawLocationChanged(object source, string rawData)
{
_writer.WriteLine(rawData);
}

private static bool Handler()
{
_gpsdService?.StopService();
Expand Down

0 comments on commit 66532e0

Please sign in to comment.