diff --git a/app/GNSSStatus/Parsing/GNSSData.cs b/app/GNSSStatus/Parsing/GNSSData.cs index 300c617..36f949a 100644 --- a/app/GNSSStatus/Parsing/GNSSData.cs +++ b/app/GNSSStatus/Parsing/GNSSData.cs @@ -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; @@ -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( @@ -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 @@ -180,7 +151,7 @@ public string GetPayloadJson() } - private GGAData.FixType GetWorstFixType() + private GGAData.FixType GetWorstFixTypeAndClear() { // Determine the worst fix type. GGAData.FixType worstFixType; @@ -195,6 +166,7 @@ private GGAData.FixType GetWorstFixType() } else worstFixType = GGAData.FixType.NoFix; + _fixTypesCache.Clear(); return worstFixType; } diff --git a/app/GNSSStatus/Program.cs b/app/GNSSStatus/Program.cs index bf86b82..4794b88 100644 --- a/app/GNSSStatus/Program.cs +++ b/app/GNSSStatus/Program.cs @@ -1,4 +1,4 @@ -#define MQTT_ENABLE +//#define MQTT_ENABLE using System.Globalization; using System.Text; diff --git a/app/GNSSStatus/Utils/MathUtils.cs b/app/GNSSStatus/Utils/MathUtils.cs index 9bdac71..9786acd 100644 --- a/app/GNSSStatus/Utils/MathUtils.cs +++ b/app/GNSSStatus/Utils/MathUtils.cs @@ -2,6 +2,30 @@ public static class MathUtils { + public static int GetMedianAndClear(this List source) + { + int val = MedianInPlace(source.ToArray()); + source.Clear(); + return val; + } + + + public static float GetMedianAndClear(this List source) + { + float val = MedianInPlace(source.ToArray()); + source.Clear(); + return val; + } + + + public static double GetMedianAndClear(this List source) + { + double val = MedianInPlace(source.ToArray()); + source.Clear(); + return val; + } + + public static int Median(this IEnumerable source) => MedianInPlace(source.ToArray()); public static float Median(this IEnumerable source) => MedianInPlace(source.ToArray()); public static double Median(this IEnumerable source) => MedianInPlace(source.ToArray()); @@ -9,6 +33,9 @@ public static class MathUtils private static int MedianInPlace(int[] source) { + if (source.Length == 0) + return 0; + Array.Sort(source); int n = source.Length; @@ -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; @@ -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;