You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.9 KiB

1 year ago
  1. using GMCabsDriverAssistant.Messages;
  2. using GMCabsDriverAssistant.Models;
  3. using GMCabsDriverAssistant.Utils;
  4. using System;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Microsoft.Maui;
  8. using Microsoft.Maui.Controls;
  9. namespace GMCabsDriverAssistant.Services
  10. {
  11. public class LocationService
  12. {
  13. private const double LOCATION_DISTANCE = 0.015; // 15 meters
  14. readonly bool stopping = false;
  15. private double lastLatitude = 0;
  16. private double lastLongitude = 0;
  17. public LocationService()
  18. {
  19. }
  20. public async Task Run(CancellationToken token)
  21. {
  22. await Task.Run(async () =>
  23. {
  24. while (!stopping)
  25. {
  26. token.ThrowIfCancellationRequested();
  27. try
  28. {
  29. await Task.Delay(10000);
  30. var request = new GeolocationRequest(GeolocationAccuracy.Best);
  31. var location = await Geolocation.GetLocationAsync(request);
  32. if (location != null)
  33. {
  34. var message = new LocationMessage
  35. {
  36. Latitude = location.Latitude,
  37. Longitude = location.Longitude
  38. };
  39. await UpdateLocationOnServer(location.Latitude, location.Longitude);
  40. Device.BeginInvokeOnMainThread(() =>
  41. {
  42. MessagingCenter.Send<LocationMessage>(message, "Location");
  43. });
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. Device.BeginInvokeOnMainThread(() =>
  49. {
  50. var errormessage = new LocationErrorMessage();
  51. MessagingCenter.Send<LocationErrorMessage>(errormessage, "LocationError");
  52. });
  53. }
  54. }
  55. return;
  56. }, token);
  57. }
  58. private async Task UpdateLocationOnServer(double currentLatitude, double currentLongitude)
  59. {
  60. var token = Preferences.Get(SecureStorageData.Token,"");
  61. if (token != null)
  62. {
  63. GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
  64. LocationUpdateRequestDto locationUpdateRequestDto = new LocationUpdateRequestDto();
  65. if (lastLatitude == 0.00 && lastLongitude == 0.00)
  66. {
  67. lastLatitude = currentLatitude;
  68. lastLongitude = currentLongitude;
  69. locationUpdateRequestDto.Latitude = currentLatitude;
  70. locationUpdateRequestDto.Longitude = currentLongitude;
  71. locationUpdateRequestDto.Imei = Preferences.Get("imeiNumber", null);
  72. if (!string.IsNullOrEmpty(locationUpdateRequestDto.Imei))
  73. {
  74. await gmCabsDriverService.SendDriverLocationTablet(locationUpdateRequestDto);
  75. }
  76. else
  77. {
  78. await gmCabsDriverService.SendDriverLocation(token, locationUpdateRequestDto);
  79. }
  80. Preferences.Set("lastLat", currentLatitude);
  81. Preferences.Set("lastLng", currentLongitude);
  82. }
  83. else
  84. {
  85. Location lastLocation = new Location(lastLatitude, lastLongitude);
  86. Location currentLocation = new Location(currentLatitude, currentLongitude);
  87. double kilometers = Location.CalculateDistance(lastLocation, currentLocation, DistanceUnits.Kilometers);
  88. if (kilometers > LOCATION_DISTANCE)
  89. {
  90. lastLatitude = currentLatitude;
  91. lastLongitude = currentLongitude;
  92. locationUpdateRequestDto.Latitude = currentLatitude;
  93. locationUpdateRequestDto.Longitude = currentLongitude;
  94. locationUpdateRequestDto.Imei = Preferences.Get("imeiNumber", null);
  95. if (!string.IsNullOrEmpty(locationUpdateRequestDto.Imei))
  96. {
  97. await gmCabsDriverService.SendDriverLocationTablet(locationUpdateRequestDto);
  98. }
  99. else
  100. {
  101. await gmCabsDriverService.SendDriverLocation(token, locationUpdateRequestDto);
  102. }
  103. Preferences.Set("lastLat", currentLatitude);
  104. Preferences.Set("lastLng", currentLongitude);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }