Пример #1
0
        protected override IEnumerable <Datum> Poll()
        {
            lock (_locker)
            {
                Position currentPosition = GpsReceiver.Get().GetReading();

                Datum[] data = null;

                if (_previousPosition == null || currentPosition == null || currentPosition.Timestamp == _previousPosition.Timestamp)
                {
                    data = new Datum[] { }
                }
                ;
                else
                {
                    // http://www.movable-type.co.uk/scripts/latlong.html

                    double φ1 = DegreesToRadians(_previousPosition.Latitude);
                    double φ2 = DegreesToRadians(currentPosition.Latitude);
                    double Δφ = DegreesToRadians(currentPosition.Latitude - _previousPosition.Latitude);
                    double Δλ = DegreesToRadians(currentPosition.Longitude - _previousPosition.Longitude);

                    double a = Math.Pow(Math.Sin(Δφ / 2), 2) +
                               Math.Cos(φ1) *
                               Math.Cos(φ2) *
                               Math.Pow(Math.Sin(Δλ / 2), 2);

                    var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

                    var reportedDistKM = 6371 * c;

                    double elapsedHours     = new TimeSpan(currentPosition.Timestamp.Ticks - _previousPosition.Timestamp.Ticks).TotalHours;
                    double reportedSpeedKPH = reportedDistKM / elapsedHours;

                    float accuracy = 0;
                    if (_previousPosition.Accuracy >= 0 && currentPosition.Accuracy >= 0)
                    {
                        double maxDistKM   = reportedDistKM + _previousPosition.Accuracy / 1000f + currentPosition.Accuracy / 1000f;
                        double maxSpeedKPH = maxDistKM / elapsedHours;
                        accuracy = (float)(maxSpeedKPH - reportedSpeedKPH);
                    }

                    data = new SpeedDatum[] { new SpeedDatum(currentPosition.Timestamp, accuracy, (float)reportedSpeedKPH) };
                }

                if (currentPosition != null)
                {
                    _previousPosition = currentPosition;
                }

                return(data);
            }
        }
Пример #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);
            }
        }
Пример #3
0
        protected override IEnumerable <Datum> Poll(CancellationToken cancellationToken)
        {
            lock (_locker)
            {
                Position currentPosition = null;

                try
                {
                    currentPosition = GpsReceiver.Get().GetReading(cancellationToken);
                }
                catch (Exception ex)
                {
                    SensusServiceHelper.Get().Logger.Log("Failed to get GPS reading:  " + ex.Message, LoggingLevel.Normal, GetType());
                }

                SpeedDatum datum = null;

                if (currentPosition != null)
                {
                    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[] { }
                }
                ;
                else
                {
                    return new Datum[] { datum }
                };
            }
        }
Пример #4
0
        protected override IEnumerable<Datum> Poll(CancellationToken cancellationToken)
        {
            lock (_locker)
            {
                SpeedDatum datum = null;

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

                if (currentPosition == null)
                    throw new Exception("Failed to get GPS reading.");
                else
                {
                    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 };
            }
        }
Пример #5
0
        protected override IEnumerable<Datum> Poll(CancellationToken cancellationToken)
        {
            lock (_locker)
            {
                Position currentPosition = GpsReceiver.Get().GetReading(cancellationToken);

                Datum[] data = null;

                if (_previousPosition == null || currentPosition == null || currentPosition.Timestamp == _previousPosition.Timestamp)
                    data = new Datum[] { };
                else
                    data = new SpeedDatum[] { new SpeedDatum(currentPosition.Timestamp, _previousPosition, currentPosition) };

                if (currentPosition != null)
                    _previousPosition = currentPosition;

                return data;
            }
        }
Пример #6
0
        protected override IEnumerable<Datum> Poll(CancellationToken cancellationToken)
        {
            lock (_locker)
            {
                Position currentPosition = null;

                try
                {
                    currentPosition = GpsReceiver.Get().GetReading(cancellationToken);
                }
                catch (Exception ex)
                {
                    SensusServiceHelper.Get().Logger.Log("Failed to get GPS reading:  " + ex.Message, LoggingLevel.Normal, GetType());
                }

                SpeedDatum datum = null;

                if (currentPosition != null)
                {
                    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[] { };
                else
                    return new Datum[] { datum };
            }
        }