Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
japsuu committed Oct 21, 2024
1 parent fc45f6c commit 29b5265
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 53 deletions.
76 changes: 24 additions & 52 deletions app/GNSSStatus/Parsing/GNSSData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,50 +77,7 @@ public void SetIonoPercentage(double value)
public string GetPayloadJson()
{
JsonPayloadBuilder builder = new();

// Median properties
double deltaX = _deltaXCache.Count > 0 ? _deltaXCache.Median() : 0;
_deltaXCache.Clear();

double deltaY = _deltaYCache.Count > 0 ? _deltaYCache.Median() : 0;
_deltaYCache.Clear();

double deltaZAverage = _deltaZCache.Count > 0 ? _deltaZCache.Median() : 0;
_deltaZCache.Clear();

double roverXAverage = _roverXCache.Count > 0 ? _roverXCache.Median() : 0;
_roverXCache.Clear();

double roverYAverage = _roverYCache.Count > 0 ? _roverYCache.Median() : 0;
_roverYCache.Clear();

double roverZAverage = _roverZCache.Count > 0 ? _roverZCache.Median() : 0;
_roverZCache.Clear();

int satellitesInUse = _satellitesInUseCache.Count > 0 ? _satellitesInUseCache.Median() : 0;
_satellitesInUseCache.Clear();

float pDop = _pDopCache.Count > 0 ? _pDopCache.Median() : 0;
_pDopCache.Clear();

float hDop = _hDopCache.Count > 0 ? _hDopCache.Median() : 0;
_hDopCache.Clear();

float vDop = _vDopCache.Count > 0 ? _vDopCache.Median() : 0;
_vDopCache.Clear();

float latitudeError = _latitudeErrorCache.Count > 0 ? _latitudeErrorCache.Median() : 0;
_latitudeErrorCache.Clear();

float longitudeError = _longitudeErrorCache.Count > 0 ? _longitudeErrorCache.Median() : 0;
_longitudeErrorCache.Clear();

float altitudeError = _altitudeErrorCache.Count > 0 ? _altitudeErrorCache.Median() : 0;
_altitudeErrorCache.Clear();

double baseDistance = _baseDistanceCache.Count > 0 ? _baseDistanceCache.Median() : 0;
_baseDistanceCache.Clear();


// Static properties
string roverIdentifier = ConfigManager.CurrentConfiguration.RoverIdentifier;

Expand All @@ -130,11 +87,25 @@ public string GetPayloadJson()
float differentialDataAge = _latestDifferentialDataAge;
double ionoPercentage = _latestIonoPercentage;

// Median properties
double deltaX = _deltaXCache.GetMedianAndClear();
double deltaY = _deltaYCache.GetMedianAndClear();
double deltaZ = _deltaZCache.GetMedianAndClear();
double roverX = _roverXCache.GetMedianAndClear();
double roverY = _roverYCache.GetMedianAndClear();
double roverZ = _roverZCache.GetMedianAndClear();
int satellitesInUse = _satellitesInUseCache.GetMedianAndClear();
float pDop = _pDopCache.GetMedianAndClear();
float hDop = _hDopCache.GetMedianAndClear();
float vDop = _vDopCache.GetMedianAndClear();
float latitudeError = _latitudeErrorCache.GetMedianAndClear();
float longitudeError = _longitudeErrorCache.GetMedianAndClear();
float altitudeError = _altitudeErrorCache.GetMedianAndClear();
double baseDistance = _baseDistanceCache.GetMedianAndClear();

// Calculated properties
double deltaXY = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);

GGAData.FixType worstFixType = GetWorstFixType();
_fixTypesCache.Clear();
GGAData.FixType worstFixType = GetWorstFixTypeAndClear();

// Manually serialize relevant properties.
builder.AddPayload(
Expand All @@ -143,16 +114,16 @@ public string GetPayloadJson()
TimeUtc = utcTime,
FixType = worstFixType,
SatellitesInUse = satellitesInUse,
RoverX = roverXAverage,
RoverY = roverYAverage,
RoverZ = roverZAverage
RoverX = roverX,
RoverY = roverY,
RoverZ = roverZ
});

builder.AddPayload(
new
{
DeltaXY = deltaXY,
DeltaZ = deltaZAverage,
DeltaZ = deltaZ,
PDop = pDop,
HDop = hDop,
VDop = vDop
Expand Down Expand Up @@ -180,7 +151,7 @@ public string GetPayloadJson()
}


private GGAData.FixType GetWorstFixType()
private GGAData.FixType GetWorstFixTypeAndClear()
{
// Determine the worst fix type.
GGAData.FixType worstFixType;
Expand All @@ -195,6 +166,7 @@ private GGAData.FixType GetWorstFixType()
}
else
worstFixType = GGAData.FixType.NoFix;
_fixTypesCache.Clear();

return worstFixType;
}
Expand Down
2 changes: 1 addition & 1 deletion app/GNSSStatus/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define MQTT_ENABLE
//#define MQTT_ENABLE

using System.Globalization;
using System.Text;
Expand Down
33 changes: 33 additions & 0 deletions app/GNSSStatus/Utils/MathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@

public static class MathUtils
{
public static int GetMedianAndClear(this List<int> source)
{
int val = MedianInPlace(source.ToArray());
source.Clear();
return val;
}


public static float GetMedianAndClear(this List<float> source)
{
float val = MedianInPlace(source.ToArray());
source.Clear();
return val;
}


public static double GetMedianAndClear(this List<double> source)
{
double val = MedianInPlace(source.ToArray());
source.Clear();
return val;
}


public static int Median(this IEnumerable<int> source) => MedianInPlace(source.ToArray());
public static float Median(this IEnumerable<float> source) => MedianInPlace(source.ToArray());
public static double Median(this IEnumerable<double> source) => MedianInPlace(source.ToArray());


private static int MedianInPlace(int[] source)
{
if (source.Length == 0)
return 0;

Array.Sort(source);
int n = source.Length;

Expand All @@ -23,6 +50,9 @@ private static int MedianInPlace(int[] source)

private static float MedianInPlace(float[] source)
{
if (source.Length == 0)
return 0;

Array.Sort(source);
int n = source.Length;

Expand All @@ -37,6 +67,9 @@ private static float MedianInPlace(float[] source)

private static double MedianInPlace(double[] source)
{
if (source.Length == 0)
return 0;

Array.Sort(source);
int n = source.Length;

Expand Down

0 comments on commit 29b5265

Please sign in to comment.