GPSTrackerLocationStore.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Collections.Concurrent;
  2. using Comal.Classes;
  3. using InABox.Core;
  4. using InABox.Database;
  5. using System.Drawing;
  6. using Guid = System.Guid;
  7. namespace Comal.Stores
  8. {
  9. public class GPSTrackerLocationStore : BaseStore<GPSTrackerLocation>
  10. {
  11. private static ConcurrentDictionary<string, Guid> _cache;
  12. private static ConcurrentBag<Tuple<double, double, GeoFenceDefinition, Guid, string>> _addresses;
  13. static GPSTrackerLocationStore()
  14. {
  15. _cache = new ConcurrentDictionary<string, Guid>();
  16. _addresses = new ConcurrentBag<Tuple<double, double, GeoFenceDefinition, Guid, string>>();
  17. RegisterListener<GPSTracker>(ReloadTrackers);
  18. RegisterListener<CompanyInformation>(ReloadAddresses);
  19. RegisterListener<Job>(ReloadAddresses);
  20. RegisterListener<Supplier>(ReloadAddresses);
  21. RegisterListener<Employee>(ReloadAddresses);
  22. }
  23. private static void ReloadTrackers(Guid[] ids)
  24. {
  25. _cache = new ConcurrentDictionary<string, Guid>(
  26. DbFactory.NewProvider(Logger.Main).Query(
  27. new Filter<GPSTracker>(x => x.DeviceID).IsNotEqualTo(Guid.Empty),
  28. Columns.None<GPSTracker>().Add(x => x.DeviceID).Add(x => x.ID)
  29. ).Rows
  30. .Select(r => new KeyValuePair<string, Guid>(
  31. r.Get<GPSTracker, String>(c => c.DeviceID),
  32. r.Get<GPSTracker, Guid>(c => c.ID))
  33. )
  34. );
  35. }
  36. private static void ReloadAddresses(Guid[] ids)
  37. {
  38. var locations = DbFactory.NewProvider(Logger.Main).Query<Classes.GeoFence>().ToObjects<Classes.GeoFence>();
  39. _addresses = new ConcurrentBag<Tuple<double, double, GeoFenceDefinition, Guid, string>>(
  40. DbFactory.NewProvider(Logger.Main).Query<Classes.GeoFence>()
  41. .Rows
  42. .Select( r => new Tuple<double, double, GeoFenceDefinition, Guid, string>(
  43. r.Get<Classes.GeoFence,double>(c=>c.Latitude),
  44. r.Get<Classes.GeoFence,double>(c=>c.Longitude),
  45. Serialization.Deserialize<GeoFenceDefinition>(r.Get<Classes.GeoFence,string>(c=>c.Geofence)) ?? new GeoFenceDefinition(),
  46. r.Get<Classes.GeoFence,Guid>(x=>x.ID),
  47. $"{r.Get<Classes.GeoFence,string>(c=>c.Code)}: {r.Get<Classes.GeoFence,string>(c=>c.Name)}"))
  48. );
  49. }
  50. public override void Init()
  51. {
  52. Logger.Send(LogType.Information, "", "Initializing GPS Tracker Cache");
  53. ReloadTrackers([]);
  54. Logger.Send(LogType.Information, "", "Initializing GPS Location Cache");
  55. ReloadAddresses([]);
  56. }
  57. public string ReverseGeocode(double latitude, double longitude)
  58. {
  59. var geopoint = new GeoPoint(latitude, longitude);
  60. var address = _addresses.FirstOrDefault(a => a.Item3.Contains(geopoint));
  61. return address?.Item5 ?? "";
  62. // var tuple = _addresses.FirstOrDefault(x => Equals(x.Item1, latitude) && Equals(x.Item2, longitude));
  63. // if (tuple == null)
  64. // {
  65. // try
  66. // {
  67. // var address = StoreUtils.ReverseGeocode(latitude, longitude);
  68. // if (!string.IsNullOrWhiteSpace(address))
  69. // {
  70. // tuple = new Tuple<double, double, string>(latitude, longitude, address);
  71. // _addresses.Add(tuple);
  72. // }
  73. // }
  74. // catch (Exception e)
  75. // {
  76. // Logger.Send(LogType.Error, "", e.ToString());
  77. // }
  78. // }
  79. //
  80. // return tuple != null ? tuple.Item3 : "";
  81. }
  82. protected override void BeforeSave(GPSTrackerLocation entity)
  83. {
  84. base.BeforeSave(entity);
  85. if (Equals(entity.Tracker.ID, Guid.Empty))
  86. {
  87. if (!_cache.ContainsKey(entity.DeviceID))
  88. {
  89. Logger.Send(LogType.Information, "", string.Format("- Tracker Cache Update Required: {0}", entity.DeviceID));
  90. var row = Provider.Query(
  91. new Filter<GPSTracker>(x => x.DeviceID).IsEqualTo(entity.DeviceID),
  92. Columns.None<GPSTracker>().Add(x => x.ID)
  93. ).Rows.FirstOrDefault();
  94. if (row != null)
  95. {
  96. entity.Tracker.ID = row.Get<GPSTracker, Guid>(x => x.ID);
  97. _cache[entity.DeviceID] = entity.Tracker.ID;
  98. Logger.Send(LogType.Information, "",
  99. string.Format("- Adding Tracker to Cache: {0} => {1}", entity.DeviceID, entity.Tracker.ID));
  100. }
  101. }
  102. else
  103. {
  104. entity.Tracker.ID = _cache[entity.DeviceID];
  105. }
  106. }
  107. if (!Equals(entity.Tracker.ID, Guid.Empty)
  108. && string.IsNullOrWhiteSpace(entity.Location.Address)
  109. && !Equals(entity.Location.Latitude, 0.0D)
  110. && !Equals(entity.Location.Longitude, 0.0D)
  111. )
  112. entity.Location.Address = ReverseGeocode(entity.Location.Latitude, entity.Location.Longitude);
  113. }
  114. private void UpdateTrackers(IEnumerable<GPSTrackerLocation> entities, ref string auditnote)
  115. {
  116. var updates = new List<GPSTracker>();
  117. foreach (var entity in entities)
  118. {
  119. if (entity.Tracker.IsValid())
  120. {
  121. var tracker = new GPSTracker();
  122. tracker.SetID(entity.Tracker.ID);
  123. tracker.Location.Longitude = entity.Location.Longitude;
  124. tracker.Location.Latitude = entity.Location.Latitude;
  125. tracker.Location.Timestamp = entity.Location.Timestamp;
  126. tracker.Location.Address = entity.Location.Address;
  127. tracker.BatteryLevel = entity.BatteryLevel;
  128. tracker.Hours = entity.Hours;
  129. tracker.Distance = entity.Distance;
  130. tracker.Counter1 = entity.Counter1;
  131. tracker.Counter2 = entity.Counter2;
  132. tracker.Counter3 = entity.Counter3;
  133. tracker.Counter4 = entity.Counter4;
  134. updates.Add(tracker);
  135. }
  136. else
  137. {
  138. Logger.Send(LogType.Error, "", string.Format("Skipping GPS Tracker Update (Cache Size={0})", _cache.Count));
  139. }
  140. }
  141. if (updates.Any())
  142. {
  143. Provider.Save(updates);
  144. //AuditTrail(updates, new[] { auditnote });
  145. }
  146. auditnote = null;
  147. }
  148. protected override void OnSave(GPSTrackerLocation[] entities, ref string auditnote)
  149. {
  150. var updates = entities.Where(x => !Equals(x.Tracker.ID, Guid.Empty)).ToArray();
  151. if (updates.Any())
  152. {
  153. UpdateTrackers(updates, ref auditnote);
  154. base.OnSave(updates, ref auditnote);
  155. }
  156. }
  157. protected override void OnSave(GPSTrackerLocation entity, ref string auditnote)
  158. {
  159. if (Equals(entity.Tracker.ID, Guid.Empty))
  160. return;
  161. UpdateTrackers(new[] { entity }, ref auditnote);
  162. base.OnSave(entity, ref auditnote);
  163. }
  164. }
  165. }