예제 #1
0
        private void OnGeolocatorOnPositionChanged(GeolocatorWrapper sender, GeolocatorWrapperPositionChangedEventArgs eventArgs)
        {
            if (eventArgs.GeolocatorLocationStatus == PositionStatus.Disabled || eventArgs.GeolocatorLocationStatus == PositionStatus.NotAvailable)
            {
                DispatchMessage(PluginResult.Status.ERROR, string.Format("Cannot start: LocationStatus/PositionStatus: {0}! {1}", eventArgs.GeolocatorLocationStatus, IsConfigured), true, ConfigureCallbackToken);
                return;
            }

            HandlePositionUpdateDebugData(eventArgs.PositionUpdateDebugData);

            if (eventArgs.Position != null)
            {
                DispatchMessage(PluginResult.Status.OK, eventArgs.Position.Coordinate.ToJson(), true, ConfigureCallbackToken);
            }
            else if (eventArgs.EnteredStationary)
            {
                DispatchMessage(PluginResult.Status.OK, string.Format("{0:0.}", BackgroundGeoLocationOptions.StationaryRadius), true, OnStationaryCallbackToken);
            }
            else
            {
                DispatchMessage(PluginResult.Status.ERROR, "Null position received", true, ConfigureCallbackToken);
            }
        }
        private void OnGeolocatorPositionChanged(Geolocator sender, PositionChangedEventArgs positionChangesEventArgs)
        {
            if (_skipNextPosition)
            {
                _skipNextPosition = false;
                return;
            }

            var newGeoCoordinate = new GeoCoordinate(positionChangesEventArgs.Position.Coordinate.Latitude, positionChangesEventArgs.Position.Coordinate.Longitude);
            var newPosition      = new Position(newGeoCoordinate, DateTime.Now, positionChangesEventArgs.Position.Coordinate.Accuracy);

            _positionPath.AddPosition(newPosition);

            var stationaryUpdateResult = StationaryUpdate(positionChangesEventArgs.Position, newPosition);
            if (stationaryUpdateResult == StationaryUpdateResult.InStationary && !_useFixedTimeInterval)
            {
                return;
            }

            var currentAvgSpeed = _positionPath.GetCurrentSpeed(TimeSpan.FromMilliseconds(_reportInterval * 5)); // avg speed of last 5 (at max) positions

            var updateScaledDistanceFilterResult = UpdateScaledDistanceFilter(currentAvgSpeed, positionChangesEventArgs.Position.Coordinate);
            if (updateScaledDistanceFilterResult.SkipPositionBecauseOfDistance && !_useFixedTimeInterval)
            {
                SkipPosition(updateScaledDistanceFilterResult.StartStationary, updateScaledDistanceFilterResult.StartStationary, updateScaledDistanceFilterResult.Distance);
                return;
            }

            if (updateScaledDistanceFilterResult.ScaledDistanceFilterChanged && !_useFixedTimeInterval)
            {
                var newReportInterval = CalculateNewReportInterval(currentAvgSpeed);
                UpdateReportInterval(newReportInterval);
            }

            _reportedPositionsCount++;
            _reportedIntervalsPositionsCount++;

            var geolocatorWrapperPositionChangedEventArgs = new GeolocatorWrapperPositionChangedEventArgs
            {
                GeolocatorLocationStatus = Geolocator.LocationStatus,
                Position                 = positionChangesEventArgs.Position,
                EnteredStationary        = false,
                PositionUpdateDebugData  = PostionUpdateDebugData.ForNewPosition(positionChangesEventArgs, currentAvgSpeed, updateScaledDistanceFilterResult, Geolocator.ReportInterval, stationaryUpdateResult == StationaryUpdateResult.ExitedFromStationary)
            };

            if (_notificationIndex < _notifications.Count)
            {
                using (IsolatedStorageFileStream file = new IsolatedStorageFileStream("geoLocatorWrapperOutput.txt", FileMode.Append, FileAccess.Write, IsolatedStorageFile.GetUserStoreForApplication()))
                {
                    using (StreamWriter writeFile = new StreamWriter(file))
                    {
                        writeFile.WriteLine("_reportedPositionsCount: " + _reportedPositionsCount);
                        writeFile.WriteLine("_notificationIndex: " + _notificationIndex);
                        writeFile.WriteLine("_notificationOffsetSeconds: " + _notificationOffsetSeconds);
                        writeFile.WriteLine("_notifications.Count: " + _notifications.Count);
                        writeFile.WriteLine("_notifications[_notificationIndex].intervalSeconds: " + _notifications[_notificationIndex].intervalSeconds);
                        writeFile.WriteLine("Current Time: " + (_reportInterval / 1000) * _reportedIntervalsPositionsCount);
                        writeFile.WriteLine("Next Interval Time: " + (_notifications[_notificationIndex].intervalSeconds + _notificationOffsetSeconds));
                        writeFile.Close();
                    }
                    file.Close();
                }

                if (_reportedPositionsCount > 1 && ((_reportInterval / 1000) * _reportedPositionsCount) >= (_notifications[_notificationIndex].intervalSeconds + _notificationOffsetSeconds))
                {
                    using (IsolatedStorageFileStream file = new IsolatedStorageFileStream("geoLocatorWrapperOutput.txt", FileMode.Append, FileAccess.Write, IsolatedStorageFile.GetUserStoreForApplication()))
                    {
                        using (StreamWriter writeFile = new StreamWriter(file))
                        {
                            writeFile.WriteLine("_notifications[_notificationIndex].text: " + _notifications[_notificationIndex].text);
                            writeFile.Close();
                        }
                        file.Close();
                    }
                    _notificationOffsetSeconds += _notifications[_notificationIndex].intervalSeconds;
                    geolocatorWrapperPositionChangedEventArgs.NotiticationText = _notifications[_notificationIndex].text;
                    _notificationIndex++;
                }
                else
                {
                    geolocatorWrapperPositionChangedEventArgs.NotiticationText = "";
                }
            }
            else
            {
                geolocatorWrapperPositionChangedEventArgs.NotiticationText = "";
            }

            if (_intervalReportSeconds > 0 && ((_reportInterval / 1000) * _reportedIntervalsPositionsCount) == _intervalReportSeconds)
            {
                _reportedIntervalsPositionsCount = 0;
                geolocatorWrapperPositionChangedEventArgs.SpeachReportReady = true;
                //Get Average speed
                geolocatorWrapperPositionChangedEventArgs.AverageSpeed =
                    _positionPath.GetCurrentSpeed(TimeSpan.FromMilliseconds(_reportInterval * _reportedPositionsCount));

                //Get Current speed
                geolocatorWrapperPositionChangedEventArgs.CurrentSpeed =
                    _positionPath.GetCurrentSpeed(TimeSpan.FromSeconds(_intervalReportSeconds));

                if (_reportInMiles)
                {
                    //Convert Average speed to MPH
                    geolocatorWrapperPositionChangedEventArgs.AverageSpeed =
                        geolocatorWrapperPositionChangedEventArgs.AverageSpeed * 2.23694;

                    //Convert Current speed to MPH
                    geolocatorWrapperPositionChangedEventArgs.CurrentSpeed =
                        geolocatorWrapperPositionChangedEventArgs.CurrentSpeed * 2.23694;

                    //Get Total Distance
                    geolocatorWrapperPositionChangedEventArgs.TotalDistance =
                        _positionPath.GetTotalDistance(TimeSpan.FromMilliseconds(_reportInterval * _reportedPositionsCount)) * 0.000621371;
                }

                //Get Average Pace
                geolocatorWrapperPositionChangedEventArgs.AveragePace =
                    60 / geolocatorWrapperPositionChangedEventArgs.AverageSpeed;

                //Get Current Pace
                geolocatorWrapperPositionChangedEventArgs.CurrentPace =
                    60 / geolocatorWrapperPositionChangedEventArgs.CurrentSpeed;

                //Get Total Time
                geolocatorWrapperPositionChangedEventArgs.TotalTime =
                    TimeSpan.FromMilliseconds(_reportInterval * _reportedPositionsCount);
            }
            PositionChanged(this, geolocatorWrapperPositionChangedEventArgs);
        }
        private string GetSpeechStringMiles(GeolocatorWrapperPositionChangedEventArgs eventArgs)
        {
            var returnValue = "";

            if (_reportTotalTime)
            {
                returnValue = string.Format("Time {0}", eventArgs.TotalTime.GetSpeechFormat());
            }

            if (_reportTotalDistance)
            {
                returnValue = returnValue == "" ? returnValue : returnValue + ", ";
                returnValue += string.Format("Total Distance {0} miles", ((double)eventArgs.TotalDistance).ToString("0.0"));
            }

            if (_reportCurrentPace)
            {
                returnValue = returnValue == "" ? returnValue : returnValue + ", ";
                returnValue += string.Format("Current Pace {0} minutes per mile", ((double)eventArgs.CurrentPace).ToString("0.0"));
            }

            if (_reportAveragePace)
            {
                returnValue = returnValue == "" ? returnValue : returnValue + ", ";
                returnValue += string.Format("Average Pace {0} minutes per mile", ((double)eventArgs.AveragePace).ToString("0.0"));
            }

            if (_reportCurrentSpeed)
            {
                returnValue = returnValue == "" ? returnValue : returnValue + ", ";
                returnValue += string.Format("Current Speed {0} miles per hour", ((double)eventArgs.CurrentSpeed).ToString("0.0"));
            }

            if (_reportAverageSpeed)
            {
                returnValue = returnValue == "" ? returnValue : returnValue + ", ";
                returnValue += string.Format("Average Speed {0} miles per hour", ((double)eventArgs.AverageSpeed).ToString("0.0"));
            }

            return returnValue;
        }
        private void OnGeolocatorOnPositionChanged(GeolocatorWrapper sender, GeolocatorWrapperPositionChangedEventArgs eventArgs)
        {
            if (eventArgs.GeolocatorLocationStatus == PositionStatus.Disabled || eventArgs.GeolocatorLocationStatus == PositionStatus.NotAvailable)
            {
                DispatchMessage(PluginResult.Status.ERROR, string.Format("Cannot start: LocationStatus/PositionStatus: {0}! {1}", eventArgs.GeolocatorLocationStatus, IsConfigured), true, ConfigureCallbackToken);
                return;
            }

            HandlePositionUpdateDebugData(eventArgs.PositionUpdateDebugData);

            if (eventArgs.Position != null)
                DispatchMessage(PluginResult.Status.OK, eventArgs.Position.Coordinate.ToJson(), true, ConfigureCallbackToken);
            else if (eventArgs.EnteredStationary)
                DispatchMessage(PluginResult.Status.OK, string.Format("{0:0.}", BackgroundGeoLocationOptions.StationaryRadius), true, OnStationaryCallbackToken);
            else
                DispatchMessage(PluginResult.Status.ERROR, "Null position received", true, ConfigureCallbackToken);

        }
        private async void OnGeolocatorOnPositionChanged(GeolocatorWrapper sender, GeolocatorWrapperPositionChangedEventArgs eventArgs)
        {
            if (eventArgs.GeolocatorLocationStatus == PositionStatus.Disabled || eventArgs.GeolocatorLocationStatus == PositionStatus.NotAvailable)
            {
                DispatchMessage(PluginResult.Status.ERROR, string.Format("Cannot start: LocationStatus/PositionStatus: {0}! {1}", eventArgs.GeolocatorLocationStatus, IsConfigured), true, ConfigureCallbackToken);
                return;
            }

            HandlePositionUpdateDebugData(eventArgs.PositionUpdateDebugData);

            if (eventArgs.Position != null)
                DispatchMessage(PluginResult.Status.OK, eventArgs.Position.Coordinate.ToJson(), true, ConfigureCallbackToken);
            else if (eventArgs.EnteredStationary)
                DispatchMessage(PluginResult.Status.OK, string.Format("{0:0.}", BackgroundGeoLocationOptions.StationaryRadius), true, OnStationaryCallbackToken);
            else
                DispatchMessage(PluginResult.Status.ERROR, "Null position received", true, ConfigureCallbackToken);

            SpeechSynthesizer synth = new SpeechSynthesizer();

            try
            {
                if (!string.IsNullOrEmpty(eventArgs.NotiticationText))
                {
                    await synth.SpeakTextAsync(eventArgs.NotiticationText);
                }

                if (eventArgs.SpeachReportReady)
                {
                    if (_reportInMiles)
                    {
                        await synth.SpeakTextAsync(GetSpeechStringMiles(eventArgs));
                    }
                }
            }
            catch (Exception ex)
            {
                using (IsolatedStorageFileStream file = new IsolatedStorageFileStream("backgroundGeoLocation.txt", FileMode.Append, FileAccess.Write, IsolatedStorageFile.GetUserStoreForApplication()))
                {
                    using (StreamWriter writeFile = new StreamWriter(file))
                    {
                        writeFile.WriteLine("Message: " + ex.Message);
                        writeFile.WriteLine("StackTrace: " + ex.StackTrace);
                        writeFile.Close();
                    }
                    file.Close();
                }
            }
        }