GPSTrackerLocationStore.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Comal.Classes;
  6. using InABox.Core;
  7. using InABox.Database;
  8. using Syncfusion.UI.Xaml.CellGrid.Styles;
  9. namespace Comal.Stores
  10. {
  11. public class GPSTrackerLocationStore : BaseStore<GPSTrackerLocation>
  12. {
  13. private static ConcurrentDictionary<string, Guid> _cache;
  14. private static ConcurrentBag<Tuple<double, double, string>> _addresses;
  15. public override void Init()
  16. {
  17. Logger.Send(LogType.Information, "", "Initializing GPS Tracker Caches");
  18. _addresses = new ConcurrentBag<Tuple<double, double, string>>(
  19. Provider.Query(
  20. new Filter<GPSTrackerLocation>(x => x.Location.Address).IsNotEqualTo(""),
  21. new Columns<GPSTrackerLocation>(
  22. x => x.Location.Address,
  23. x => x.Location.Longitude,
  24. x => x.Location.Latitude
  25. ),
  26. null,
  27. int.MaxValue,
  28. true,
  29. true
  30. ).Rows.Select(r => new Tuple<double, double, string>(
  31. r.Get<GPSTrackerLocation, double>(c => c.Location.Latitude),
  32. r.Get<GPSTrackerLocation, double>(c => c.Location.Longitude),
  33. r.Get<GPSTrackerLocation, string>(c => c.Location.Address)
  34. )
  35. ));
  36. _cache = new ConcurrentDictionary<string, Guid>(
  37. Provider.Query(
  38. new Filter<GPSTracker>(x => x.DeviceID).IsNotEqualTo(Guid.Empty),
  39. new Columns<GPSTracker>(x => x.DeviceID).Add(x => x.ID)
  40. ).Rows.Select(r => new KeyValuePair<String, Guid>(r.Get<GPSTracker, String>(c => c.DeviceID), r.Get<GPSTracker, Guid>(c => c.ID)))
  41. );
  42. }
  43. public string ReverseGeocode(double latitude, double longitude)
  44. {
  45. var tuple = _addresses.FirstOrDefault(x => Equals(x.Item1, latitude) && Equals(x.Item2, longitude));
  46. if (tuple == null)
  47. {
  48. var address = StoreUtils.ReverseGeocode(latitude, longitude);
  49. if (!string.IsNullOrWhiteSpace(address))
  50. {
  51. tuple = new Tuple<double, double, string>(latitude, longitude, address);
  52. _addresses.Add(tuple);
  53. }
  54. }
  55. return tuple != null ? tuple.Item3 : "";
  56. }
  57. protected override void BeforeSave(GPSTrackerLocation entity)
  58. {
  59. base.BeforeSave(entity);
  60. if (Equals(entity.Tracker.ID, Guid.Empty))
  61. {
  62. if (!_cache.ContainsKey(entity.DeviceID))
  63. {
  64. Logger.Send(LogType.Information, "", string.Format("- Tracker Cache Update Required: {0}", entity.DeviceID));
  65. var row = Provider.Query(
  66. new Filter<GPSTracker>(x => x.DeviceID).IsEqualTo(entity.DeviceID),
  67. new Columns<GPSTracker>(x => x.ID)
  68. ).Rows.FirstOrDefault();
  69. if (row != null)
  70. {
  71. entity.Tracker.ID = row.Get<GPSTracker, Guid>(x => x.ID);
  72. _cache[entity.DeviceID] = entity.Tracker.ID;
  73. Logger.Send(LogType.Information, "",
  74. string.Format("- Adding Tracker to Cache: {0} => {1}", entity.DeviceID, entity.Tracker.ID));
  75. }
  76. }
  77. else
  78. {
  79. entity.Tracker.ID = _cache[entity.DeviceID];
  80. }
  81. }
  82. if (!Equals(entity.Tracker.ID, Guid.Empty)
  83. && string.IsNullOrWhiteSpace(entity.Location.Address)
  84. && !Equals(entity.Location.Latitude, 0.0D)
  85. && !Equals(entity.Location.Longitude, 0.0D)
  86. )
  87. entity.Location.Address = ReverseGeocode(entity.Location.Latitude, entity.Location.Longitude);
  88. }
  89. private void UpdateTrackers(IEnumerable<GPSTrackerLocation> entities, ref string auditnote)
  90. {
  91. var updates = new List<GPSTracker>();
  92. foreach (var entity in entities)
  93. {
  94. if (entity.Tracker.IsValid())
  95. {
  96. var tracker = new GPSTracker();
  97. tracker.ID = entity.Tracker.ID;
  98. tracker.Location.Longitude = entity.Location.Longitude;
  99. tracker.Location.Latitude = entity.Location.Latitude;
  100. tracker.Location.Timestamp = entity.Location.Timestamp;
  101. tracker.Location.Address = entity.Location.Address;
  102. tracker.BatteryLevel = entity.BatteryLevel;
  103. tracker.Hours = entity.Hours;
  104. tracker.Distance = entity.Distance;
  105. tracker.Counter1 = entity.Counter1;
  106. tracker.Counter2 = entity.Counter2;
  107. tracker.Counter3 = entity.Counter3;
  108. tracker.Counter4 = entity.Counter4;
  109. updates.Add(tracker);
  110. }
  111. else
  112. {
  113. Logger.Send(LogType.Error, "", string.Format("Skipping GPS Tracker Update (Cache Size={0})", _cache.Count));
  114. }
  115. }
  116. if (updates.Any())
  117. {
  118. Provider.Save(updates);
  119. AuditTrail(updates, new[] { auditnote });
  120. }
  121. auditnote = null;
  122. }
  123. protected override void OnSave(IEnumerable<GPSTrackerLocation> entities, ref string auditnote)
  124. {
  125. var updates = entities.Where(x => !Equals(x.Tracker.ID, Guid.Empty)).ToArray();
  126. if (updates.Any())
  127. {
  128. UpdateTrackers(updates, ref auditnote);
  129. base.OnSave(updates, ref auditnote);
  130. }
  131. }
  132. protected override void OnSave(GPSTrackerLocation entity, ref string auditnote)
  133. {
  134. if (Equals(entity.Tracker.ID, Guid.Empty))
  135. return;
  136. UpdateTrackers(new[] { entity }, ref auditnote);
  137. base.OnSave(entity, ref auditnote);
  138. }
  139. }
  140. }