public ListeningSpeedProbe()
        {
            _positionChangedHandler = async(o, e) =>
            {
                if (e.Position == null)
                {
                    return;
                }

                Datum datum = null;

                lock (_locker)
                {
                    SensusServiceHelper.Get().Logger.Log("Received position change notification.", LoggingLevel.Verbose, GetType());

                    if (_previousPosition == null)
                    {
                        _previousPosition = e.Position;
                    }
                    else if (e.Position.Timestamp > _previousPosition.Timestamp)  // it has happened (rarely) that positions come in out of order...drop any such positions.
                    {
                        datum             = new SpeedDatum(e.Position.Timestamp, _previousPosition, e.Position);
                        _previousPosition = e.Position;
                    }
                }

                if (datum != null)
                {
                    await StoreDatumAsync(datum);
                }
            };
        }
Exemple #2
0
        public SpeedDatum(DateTimeOffset timestamp, Position previousPosition, Position currentPosition)
            : base(timestamp, 0)
        {
            double distanceKM;
            double timeHours;

            _kph = SpeedDatum.CalculateSpeedKPH(previousPosition, currentPosition, out distanceKM, out timeHours);

            if (previousPosition.Accuracy >= 0 && currentPosition.Accuracy >= 0)
            {
                double maximumDistanceKM = distanceKM + previousPosition.Accuracy / 1000f + currentPosition.Accuracy / 1000f;
                double maximumSpeedKPH   = maximumDistanceKM / timeHours;
                Accuracy = (float)(maximumSpeedKPH - _kph);
            }
        }
        protected override IEnumerable <Datum> Poll(CancellationToken cancellationToken)
        {
            SpeedDatum datum = null;

            Position currentPosition = GpsReceiver.Get().GetReading(cancellationToken);

            if (currentPosition == null)
            {
                throw new Exception("Failed to get GPS reading.");
            }
            else
            {
                lock (_previousPositionLocker)
                {
                    if (_previousPosition == null)
                    {
                        _previousPosition = currentPosition;
                    }
                    else if (currentPosition.Timestamp > _previousPosition.Timestamp)  // it has happened (rarely) that positions come in out of order...drop any such positions.
                    {
                        datum             = new SpeedDatum(currentPosition.Timestamp, _previousPosition, currentPosition);
                        _previousPosition = currentPosition;
                    }
                }
            }

            if (datum == null)
            {
                return new Datum[] { }
            }
            ;                            // datum will be null on the first poll and where polls return locations out of order (rare). these should count toward participation.
            else
            {
                return new Datum[] { datum }
            };
        }