GPSTrackerLocationGrid.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Controls;
  5. using Comal.Classes;
  6. using Comal.Stores;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. namespace PRSDesktop;
  12. internal class GPSTrackerLocationGrid : DynamicOneToManyGrid<GPSTracker, GPSTrackerLocation>
  13. {
  14. private static List<GPSTrackerLocation> _cache;
  15. protected override void Init()
  16. {
  17. base.Init();
  18. Criteria.Add(
  19. new Filter<GPSTrackerLocation>(x => x.Location.Timestamp).IsGreaterThanOrEqualTo(DateTime.Now.AddDays(-1))
  20. );
  21. HiddenColumns.Add(x => x.Location.Longitude);
  22. HiddenColumns.Add(x => x.Location.Latitude);
  23. HiddenColumns.Add(x => x.Location.Address);
  24. ActionColumns.Add(new DynamicMapColumn<GPSTrackerLocation>(this, x => x.Location));
  25. AddButton("Get Addresses", null, GetAddressClick);
  26. }
  27. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  28. {
  29. base.DoReconfigure(options);
  30. options.AddRange(
  31. DynamicGridOption.SelectColumns,
  32. DynamicGridOption.RecordCount,
  33. DynamicGridOption.FilterRows
  34. );
  35. }
  36. public static string ReverseGeocode(double latitude, double longitude)
  37. {
  38. _cache ??= Client.Query(
  39. new Filter<GPSTrackerLocation>(x => x.Location.Address).IsNotEqualTo(""),
  40. new Columns<GPSTrackerLocation>(
  41. x => x.Location.Address,
  42. x => x.Location.Longitude,
  43. x => x.Location.Latitude))
  44. .ToList<GPSTrackerLocation>();
  45. var tuple = _cache.FirstOrDefault(x => Equals(x.Location.Latitude, latitude) && Equals(x.Location.Longitude, longitude));
  46. if (tuple == null)
  47. {
  48. var address = StoreUtils.ReverseGeocode(latitude, longitude);
  49. if (!string.IsNullOrWhiteSpace(address))
  50. {
  51. tuple = new GPSTrackerLocation();
  52. tuple.Location.Latitude = latitude;
  53. tuple.Location.Longitude = longitude;
  54. tuple.Location.Address = address;
  55. _cache.Add(tuple);
  56. }
  57. }
  58. return tuple != null ? tuple.Location.Address : "";
  59. }
  60. private bool GetAddressClick(Button arg1, CoreRow[] arg2)
  61. {
  62. var result = false;
  63. var rows = Data.Rows.Where(r =>
  64. string.IsNullOrWhiteSpace(r.Get<GPSTrackerLocation, string>(c => c.Location.Address))
  65. && !Equals(r.Get<GPSTrackerLocation, double>(c => c.Location.Latitude), 0.0F)
  66. && !Equals(r.Get<GPSTrackerLocation, double>(c => c.Location.Longitude), 0.0F)
  67. );
  68. Progress.ShowModal("Updating Addresses", progress =>
  69. {
  70. foreach (var row in rows)
  71. {
  72. var item = LoadItem(row);
  73. var address = ReverseGeocode(
  74. row.Get<GPSTrackerLocation, double>(x => x.Location.Latitude),
  75. row.Get<GPSTrackerLocation, double>(x => x.Location.Longitude)
  76. );
  77. if (!string.IsNullOrWhiteSpace(address))
  78. {
  79. item.Location.Address = address;
  80. UpdateRow<GPSTrackerLocation, string>(row, x => x.Location.Address, address, false);
  81. result = true;
  82. }
  83. }
  84. });
  85. return result;
  86. }
  87. }