123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System.Collections.Concurrent;
- using Comal.Classes;
- using InABox.Core;
- using InABox.Database;
- using System.Drawing;
- using Guid = System.Guid;
- namespace Comal.Stores
- {
- public class GPSTrackerLocationStore : BaseStore<GPSTrackerLocation>
- {
- private static ConcurrentDictionary<string, Guid> _cache;
- private static ConcurrentBag<Tuple<double, double, GeoFenceDefinition, Guid, string>> _addresses;
- static GPSTrackerLocationStore()
- {
- _cache = new ConcurrentDictionary<string, Guid>();
- _addresses = new ConcurrentBag<Tuple<double, double, GeoFenceDefinition, Guid, string>>();
- RegisterListener<GPSTracker>(ReloadTrackers);
- RegisterListener<CompanyInformation>(ReloadAddresses);
- RegisterListener<Job>(ReloadAddresses);
- RegisterListener<Supplier>(ReloadAddresses);
- RegisterListener<Employee>(ReloadAddresses);
- }
- private static void ReloadTrackers(Guid[] ids)
- {
- _cache = new ConcurrentDictionary<string, Guid>(
- DbFactory.NewProvider(Logger.Main).Query(
- new Filter<GPSTracker>(x => x.DeviceID).IsNotEqualTo(Guid.Empty),
- Columns.None<GPSTracker>().Add(x => x.DeviceID).Add(x => x.ID)
- ).Rows
- .Select(r => new KeyValuePair<string, Guid>(
- r.Get<GPSTracker, String>(c => c.DeviceID),
- r.Get<GPSTracker, Guid>(c => c.ID))
- )
- );
- }
- private static void ReloadAddresses(Guid[] ids)
- {
- var locations = DbFactory.NewProvider(Logger.Main).Query<Classes.GeoFence>().ToObjects<Classes.GeoFence>();
- _addresses = new ConcurrentBag<Tuple<double, double, GeoFenceDefinition, Guid, string>>(
- DbFactory.NewProvider(Logger.Main).Query<Classes.GeoFence>()
- .Rows
- .Select( r => new Tuple<double, double, GeoFenceDefinition, Guid, string>(
- r.Get<Classes.GeoFence,double>(c=>c.Latitude),
- r.Get<Classes.GeoFence,double>(c=>c.Longitude),
- Serialization.Deserialize<GeoFenceDefinition>(r.Get<Classes.GeoFence,string>(c=>c.Geofence)) ?? new GeoFenceDefinition(),
- r.Get<Classes.GeoFence,Guid>(x=>x.ID),
- $"{r.Get<Classes.GeoFence,string>(c=>c.Code)}: {r.Get<Classes.GeoFence,string>(c=>c.Name)}"))
- );
- }
-
- public override void Init()
- {
- Logger.Send(LogType.Information, "", "Initializing GPS Tracker Cache");
- ReloadTrackers([]);
-
- Logger.Send(LogType.Information, "", "Initializing GPS Location Cache");
- ReloadAddresses([]);
-
- }
- public string ReverseGeocode(double latitude, double longitude)
- {
- var geopoint = new GeoPoint(latitude, longitude);
- var address = _addresses.FirstOrDefault(a => a.Item3.Contains(geopoint));
- return address?.Item5 ?? "";
- // var tuple = _addresses.FirstOrDefault(x => Equals(x.Item1, latitude) && Equals(x.Item2, longitude));
- // if (tuple == null)
- // {
- // try
- // {
- // var address = StoreUtils.ReverseGeocode(latitude, longitude);
- // if (!string.IsNullOrWhiteSpace(address))
- // {
- // tuple = new Tuple<double, double, string>(latitude, longitude, address);
- // _addresses.Add(tuple);
- // }
- // }
- // catch (Exception e)
- // {
- // Logger.Send(LogType.Error, "", e.ToString());
- // }
- // }
- //
- // return tuple != null ? tuple.Item3 : "";
- }
- protected override void BeforeSave(GPSTrackerLocation entity)
- {
- base.BeforeSave(entity);
-
- if (Equals(entity.Tracker.ID, Guid.Empty))
- {
- if (!_cache.ContainsKey(entity.DeviceID))
- {
- Logger.Send(LogType.Information, "", string.Format("- Tracker Cache Update Required: {0}", entity.DeviceID));
- var row = Provider.Query(
- new Filter<GPSTracker>(x => x.DeviceID).IsEqualTo(entity.DeviceID),
- Columns.None<GPSTracker>().Add(x => x.ID)
- ).Rows.FirstOrDefault();
- if (row != null)
- {
- entity.Tracker.ID = row.Get<GPSTracker, Guid>(x => x.ID);
- _cache[entity.DeviceID] = entity.Tracker.ID;
- Logger.Send(LogType.Information, "",
- string.Format("- Adding Tracker to Cache: {0} => {1}", entity.DeviceID, entity.Tracker.ID));
- }
- }
- else
- {
- entity.Tracker.ID = _cache[entity.DeviceID];
- }
- }
- if (!Equals(entity.Tracker.ID, Guid.Empty)
- && string.IsNullOrWhiteSpace(entity.Location.Address)
- && !Equals(entity.Location.Latitude, 0.0D)
- && !Equals(entity.Location.Longitude, 0.0D)
- )
- entity.Location.Address = ReverseGeocode(entity.Location.Latitude, entity.Location.Longitude);
- }
- private void UpdateTrackers(IEnumerable<GPSTrackerLocation> entities, ref string auditnote)
- {
- var updates = new List<GPSTracker>();
- foreach (var entity in entities)
- {
- if (entity.Tracker.IsValid())
- {
- var tracker = new GPSTracker();
- tracker.SetID(entity.Tracker.ID);
- tracker.Location.Longitude = entity.Location.Longitude;
- tracker.Location.Latitude = entity.Location.Latitude;
- tracker.Location.Timestamp = entity.Location.Timestamp;
- tracker.Location.Address = entity.Location.Address;
- tracker.BatteryLevel = entity.BatteryLevel;
- tracker.Hours = entity.Hours;
- tracker.Distance = entity.Distance;
- tracker.Counter1 = entity.Counter1;
- tracker.Counter2 = entity.Counter2;
- tracker.Counter3 = entity.Counter3;
- tracker.Counter4 = entity.Counter4;
- updates.Add(tracker);
- }
- else
- {
- Logger.Send(LogType.Error, "", string.Format("Skipping GPS Tracker Update (Cache Size={0})", _cache.Count));
- }
- }
- if (updates.Any())
- {
- Provider.Save(updates);
- //AuditTrail(updates, new[] { auditnote });
- }
- auditnote = null;
- }
- protected override void OnSave(GPSTrackerLocation[] entities, ref string auditnote)
- {
- var updates = entities.Where(x => !Equals(x.Tracker.ID, Guid.Empty)).ToArray();
- if (updates.Any())
- {
- UpdateTrackers(updates, ref auditnote);
- base.OnSave(updates, ref auditnote);
- }
- }
- protected override void OnSave(GPSTrackerLocation entity, ref string auditnote)
- {
- if (Equals(entity.Tracker.ID, Guid.Empty))
- return;
- UpdateTrackers(new[] { entity }, ref auditnote);
- base.OnSave(entity, ref auditnote);
- }
- }
- }
|