GPSTrackerLocationStore.cs 6.2 KB

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