public void start(string args) { lock (_startLock) { while (!IsConfigured && IsConfiguring) { // Wait for configure() to complete... } if (!IsConfigured || !BackgroundGeoLocationOptions.ParsingSucceeded) { DispatchCommandResult(new PluginResult(PluginResult.Status.INVALID_ACTION, "Cannot start: Run configure() with proper values!")); stop(args); return; } if (Geolocator != null && Geolocator.IsActive) { DispatchCommandResult(new PluginResult(PluginResult.Status.INVALID_ACTION, "Already started!")); return; } Geolocator = new GeolocatorWrapper(BackgroundGeoLocationOptions.DesiredAccuracyInMeters, BackgroundGeoLocationOptions.LocationTimeoutInSeconds * 1000, BackgroundGeoLocationOptions.DistanceFilterInMeters, BackgroundGeoLocationOptions.StationaryRadius); Geolocator.PositionChanged += OnGeolocatorOnPositionChanged; Geolocator.Start(); RunningInBackground = true; } }
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); } }
public void setConfig(string setConfigArgs) { if (Geolocator == null) { return; } if (Geolocator.IsActive) { Geolocator.PositionChanged -= OnGeolocatorOnPositionChanged; Geolocator.Stop(); } if (string.IsNullOrWhiteSpace(setConfigArgs)) { DispatchCommandResult(new PluginResult(PluginResult.Status.INVALID_ACTION, "Cannot set config because of an empty input")); return; } var parsingSucceeded = true; var options = JsonHelper.Deserialize <string[]>(setConfigArgs); double stationaryRadius, distanceFilter; UInt32 locationTimeout, desiredAccuracy; if (!double.TryParse(options[0], out stationaryRadius)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for stationaryRadius:{0}", options[2]))); parsingSucceeded = false; } if (!double.TryParse(options[1], out distanceFilter)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for distanceFilter:{0}", options[3]))); parsingSucceeded = false; } if (!UInt32.TryParse(options[2], out locationTimeout)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for locationTimeout:{0}", options[4]))); parsingSucceeded = false; } if (!UInt32.TryParse(options[3], out desiredAccuracy)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for desiredAccuracy:{0}", options[5]))); parsingSucceeded = false; } if (!parsingSucceeded) { return; } BackgroundGeoLocationOptions.StationaryRadius = stationaryRadius; BackgroundGeoLocationOptions.DistanceFilterInMeters = distanceFilter; BackgroundGeoLocationOptions.LocationTimeoutInSeconds = locationTimeout * 1000; BackgroundGeoLocationOptions.DesiredAccuracyInMeters = desiredAccuracy; Geolocator = new GeolocatorWrapper(desiredAccuracy, locationTimeout * 1000, distanceFilter, stationaryRadius); Geolocator.PositionChanged += OnGeolocatorOnPositionChanged; Geolocator.Start(); DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); }
public void setConfig(string setConfigArgs) { if (Geolocator == null) return; if (Geolocator.IsActive) { Geolocator.PositionChanged -= OnGeolocatorOnPositionChanged; Geolocator.Stop(); } if (string.IsNullOrWhiteSpace(setConfigArgs)) { DispatchCommandResult(new PluginResult(PluginResult.Status.INVALID_ACTION, "Cannot set config because of an empty input")); return; } var parsingSucceeded = true; var options = JsonHelper.Deserialize<string[]>(setConfigArgs); double stationaryRadius, distanceFilter; UInt32 locationTimeout, desiredAccuracy; if (!double.TryParse(options[0], out stationaryRadius)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for stationaryRadius:{0}", options[2]))); parsingSucceeded = false; } if (!double.TryParse(options[1], out distanceFilter)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for distanceFilter:{0}", options[3]))); parsingSucceeded = false; } if (!UInt32.TryParse(options[2], out locationTimeout)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for locationTimeout:{0}", options[4]))); parsingSucceeded = false; } if (!UInt32.TryParse(options[3], out desiredAccuracy)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for desiredAccuracy:{0}", options[5]))); parsingSucceeded = false; } if (!parsingSucceeded) return; BackgroundGeoLocationOptions.StationaryRadius = stationaryRadius; BackgroundGeoLocationOptions.DistanceFilterInMeters = distanceFilter; BackgroundGeoLocationOptions.LocationTimeoutInSeconds = locationTimeout * 1000; BackgroundGeoLocationOptions.DesiredAccuracyInMeters = desiredAccuracy; Geolocator = new GeolocatorWrapper(desiredAccuracy, locationTimeout * 1000, distanceFilter, stationaryRadius); Geolocator.PositionChanged += OnGeolocatorOnPositionChanged; Geolocator.Start(); DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); }
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); }
public void setConfig(string setConfigArgs) { if (Geolocator == null) return; if (Geolocator.IsActive) { Geolocator.PositionChanged -= OnGeolocatorOnPositionChanged; Geolocator.Stop(); } if (string.IsNullOrWhiteSpace(setConfigArgs)) { DispatchCommandResult(new PluginResult(PluginResult.Status.INVALID_ACTION, "Cannot set config because of an empty input")); return; } var parsingSucceeded = true; var options = JsonHelper.Deserialize<string[]>(setConfigArgs); double stationaryRadius, distanceFilter; UInt32 locationTimeout, desiredAccuracy; bool useFixedTimeInterval; UInt32 intervalReportSeconds; UInt32 intervalReportMeters; bool reportTotalTime; bool reportTotalDistance; bool reportAveragePace; bool reportCurrentPace; bool reportAverageSpeed; bool reportCurrentSpeed; bool reportInMiles; List<Notification> notifications; if (!double.TryParse(options[0], out stationaryRadius)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for stationaryRadius: {0}", options[2]))); parsingSucceeded = false; } if (!double.TryParse(options[1], out distanceFilter)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for distanceFilter: {0}", options[3]))); parsingSucceeded = false; } if (!UInt32.TryParse(options[2], out locationTimeout)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for locationTimeout: {0}", options[4]))); parsingSucceeded = false; } if (!UInt32.TryParse(options[3], out desiredAccuracy)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for desiredAccuracy: {0}", options[5]))); parsingSucceeded = false; } if (!bool.TryParse(options[15], out useFixedTimeInterval)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for useFixedTimeInterval: {0}", options[15]))); parsingSucceeded = false; } if (!UInt32.TryParse(options[16], out intervalReportSeconds)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for intervalReportSeconds: {0}", options[16]))); parsingSucceeded = false; } if (!UInt32.TryParse(options[17], out intervalReportMeters)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for intervalReportMeters: {0}", options[17]))); parsingSucceeded = false; } if (!bool.TryParse(options[18], out reportTotalTime)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for reportTotalTime: {0}", options[18]))); parsingSucceeded = false; } if (!bool.TryParse(options[19], out reportTotalDistance)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for reportTotalDistance: {0}", options[19]))); parsingSucceeded = false; } if (!bool.TryParse(options[20], out reportAveragePace)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for reportAveragePace: {0}", options[20]))); parsingSucceeded = false; } if (!bool.TryParse(options[21], out reportCurrentPace)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for reportCurrentPace: {0}", options[21]))); parsingSucceeded = false; } if (!bool.TryParse(options[22], out reportAverageSpeed)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for reportAverageSpeed: {0}", options[22]))); parsingSucceeded = false; } if (!bool.TryParse(options[23], out reportCurrentSpeed)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for reportCurrentSpeed: {0}", options[23]))); parsingSucceeded = false; } if (!bool.TryParse(options[24], out reportInMiles)) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for reportInMiles: {0}", options[24]))); parsingSucceeded = false; } if (options.Length > 24) { notifications = JsonHelper.Deserialize<Notification[]>(options[25]).ToList(); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, string.Format("Invalid value for notifications: {0}", options[25]))); parsingSucceeded = false; notifications = new List<Notification>(); } if (!parsingSucceeded) return; _reportInMiles = reportInMiles; _intervalReportSeconds = intervalReportSeconds; _intervalReportMeters = intervalReportMeters; _reportTotalTime = reportTotalTime; _reportTotalDistance = reportTotalDistance; _reportAveragePace = reportAveragePace; _reportCurrentPace = reportCurrentPace; _reportAverageSpeed = reportAverageSpeed; _reportCurrentSpeed = reportCurrentSpeed; _notifications = notifications; BackgroundGeoLocationOptions.StationaryRadius = stationaryRadius; BackgroundGeoLocationOptions.DistanceFilterInMeters = distanceFilter; BackgroundGeoLocationOptions.LocationTimeoutInSeconds = locationTimeout * 1000; BackgroundGeoLocationOptions.DesiredAccuracyInMeters = desiredAccuracy; BackgroundGeoLocationOptions.UseFixedTimeInterval = useFixedTimeInterval; BackgroundGeoLocationOptions.IntervalReportSeconds = intervalReportSeconds; BackgroundGeoLocationOptions.IntervalReportMeters = intervalReportMeters; BackgroundGeoLocationOptions.ReportInMiles = reportInMiles; BackgroundGeoLocationOptions.ReportTotalTime = reportTotalTime; BackgroundGeoLocationOptions.ReportTotalDistance = reportTotalDistance; BackgroundGeoLocationOptions.ReportAveragePace = reportAveragePace; BackgroundGeoLocationOptions.ReportCurrentPace = reportCurrentPace; BackgroundGeoLocationOptions.ReportAverageSpeed = reportAverageSpeed; BackgroundGeoLocationOptions.ReportCurrentSpeed = reportCurrentSpeed; BackgroundGeoLocationOptions.Notifications = notifications; Geolocator = new GeolocatorWrapper(desiredAccuracy, locationTimeout * 1000, distanceFilter, stationaryRadius, useFixedTimeInterval, intervalReportSeconds, intervalReportMeters, reportTotalTime, reportTotalDistance, reportAveragePace, reportCurrentPace, reportAverageSpeed, reportCurrentSpeed, reportInMiles, notifications); Geolocator.PositionChanged += OnGeolocatorOnPositionChanged; Geolocator.Start(); DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); }
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(); } } }