DataModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.Mobile;
  9. using InABox.Rpc;
  10. namespace PRS.Mobile
  11. {
  12. public class GPSEventArgs : EventArgs
  13. {
  14. }
  15. public delegate void GPSLocationUpdatedEvent(GPSEventArgs args);
  16. public class BluetoothEventArgs : EventArgs
  17. {
  18. }
  19. public delegate void BluetoothScanFinishedEvent(BluetoothEventArgs args);
  20. public class DataModel : IModelHost
  21. {
  22. public event GPSLocationUpdatedEvent GPSLocationUpdated;
  23. public event BluetoothScanFinishedEvent BluetoothScanFinished;
  24. /// <summary>
  25. /// All Active Employees of the Company
  26. /// </summary>
  27. public EmployeeDetailModel CurrentEmployee { get; private set; }
  28. private readonly object _employeelock = new object();
  29. private EmployeeDetailShell _me;
  30. /// <summary>
  31. /// EmployeeDetails for Currently Logged in User
  32. /// </summary>
  33. public EmployeeDetailShell Me
  34. {
  35. get
  36. {
  37. lock (_employeelock)
  38. {
  39. if (_me == null)
  40. {
  41. CurrentEmployee.Refresh(false);
  42. _me = CurrentEmployee.FirstOrDefault(x => x.UserID == ClientFactory.UserGuid);
  43. }
  44. }
  45. return _me;
  46. }
  47. }
  48. /// <summary>
  49. /// All Active Employees of the Company
  50. /// </summary>
  51. public EmployeeModel Employees { get; private set; }
  52. /// <summary>
  53. /// All Employee Teams for the Company
  54. /// Should this be just teams I am a part of, or all available teams
  55. /// Need to implement a Security Token to differentiate
  56. /// </summary>
  57. public EmployeeTeamModel EmployeeTeams { get; private set; }
  58. /// <summary>
  59. /// Current Employee Form Instances for this employee
  60. /// </summary>
  61. public EmployeeFormModel EmployeeForms { get; private set; }
  62. public EmployeeQualificationModel EmployeeQualifications { get; private set; }
  63. /// <summary>
  64. /// Available Activity Codes for this Employee
  65. /// </summary>
  66. public ActivityModel Activities { get; private set; }
  67. /// <summary>
  68. /// All Available Contacts - used in Deliveries
  69. /// </summary>
  70. public ContactModel Contacts { get; private set; }
  71. /// <summary>
  72. /// All Defined Bluetooth-enabled Gates
  73. /// </summary>
  74. public BluetoothGateModel BluetoothGates { get; private set; }
  75. /// <summary>
  76. /// Unprocessed Timesheets for Currently Logged in User
  77. /// </summary>
  78. public TimeSheetModel TimeSheets { get; private set; }
  79. /// <summary>
  80. /// List of Jobs that Currently Logged in User has access To
  81. /// </summary>
  82. public JobModel Jobs { get; private set; }
  83. /// <summary>
  84. /// List of Assignments for ??? need to specify this.
  85. /// Can I see other people's assignments? (Security Token)
  86. /// How far back / forward can I look.
  87. /// This smells Transient, or maybe split into lookups (for all)
  88. /// and shells (for mine)
  89. /// Possibly need a back/forward window as well?
  90. /// </summary>
  91. public AssignmentModel Assignments { get; private set; }
  92. /// <summary>
  93. /// Master Product Catalogue for the company
  94. /// This should be split into Lookups and Shells
  95. /// </summary>
  96. public ProductModel Products { get; private set; }
  97. /// <summary>
  98. /// Master Product Groups List (Lookup?)
  99. /// </summary>
  100. public ProductGroupModel ProductGroups { get; private set; }
  101. /// <summary>
  102. /// All GPS Tracker Devices registered by the company
  103. /// </summary>
  104. public GPSTrackerModel GPSTrackers { get; private set; }
  105. /// <summary>
  106. /// Master Equipment Register
  107. /// </summary>
  108. public EquipmentModel Equipment { get; private set; }
  109. /// <summary>
  110. /// Master List of Equipment Groups
  111. /// </summary>
  112. public EquipmentGroupModel EquipmentGroups { get; private set; }
  113. /// <summary>
  114. /// List of all Deliveries made in the last 90 days
  115. /// hmmm.. should be configurable?
  116. /// </summary>
  117. public DeliveryModel Deliveries { get; private set; }
  118. /// <summary>
  119. /// List of all available Delivery Barcodes
  120. /// hmmm.. should be configurable?
  121. /// </summary>
  122. public DeliveryItemModel DeliveryItems { get; private set; }
  123. /// <summary>
  124. /// My Notifications (open only?)
  125. /// </summary>
  126. public NotificationModel Notifications { get; private set; }
  127. /// <summary>
  128. /// All Tasks I am subscribed to
  129. /// </summary>
  130. public KanbanModel Kanbans { get; private set; }
  131. /// <summary>
  132. /// All Kanban Types
  133. /// </summary>
  134. public KanbanTypeModel KanbanTypes { get; private set; }
  135. /// <summary>
  136. /// Available Form Library - this includes all "Applies To" values
  137. /// You can filter to a specific type by using the "Search" function
  138. /// </summary>
  139. public DigitalFormModel DigitalForms { get; private set; }
  140. // The list of open task-kased forms for the current user
  141. // Specifically for us in the "Forms" module
  142. // This overlaps the "Task" module a fair amount, but it provides
  143. // an alternative paradigm for tasks
  144. public KanbanFormModel KanbanForms { get; private set; }
  145. /// <summary>
  146. /// List of Employees, along with Clock on / off status
  147. /// </summary>
  148. public InOutModel InOut { get; private set; }
  149. /// <summary>
  150. /// List of Open Purchase Orders
  151. /// </summary>
  152. public PurchaseOrderModel PurchaseOrders { get; private set; }
  153. public ManufacturingFactoryModel ManufacturingFactories { get; private set; }
  154. public ManufacturingPacketModel ManufacturingPackets { get; private set; }
  155. public DataModel()
  156. {
  157. Reset();
  158. }
  159. public void Reset()
  160. {
  161. _me = null;
  162. CurrentEmployee = new EmployeeDetailModel(this,
  163. () => new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)
  164. );
  165. Employees = new EmployeeModel(this,
  166. LookupFactory.DefineFilter<Employee>);
  167. EmployeeTeams = new EmployeeTeamModel(this,
  168. () => new Filter<EmployeeTeam>(x => x.EmployeeLink.ID)
  169. .InQuery(LookupFactory.DefineFilter<Employee>(), x => x.ID)
  170. );
  171. EmployeeForms = new EmployeeFormModel(this,
  172. () => new Filter<EmployeeForm>(x => x.Parent.ID).IsEqualTo(App.Data.Me.ID)
  173. );
  174. EmployeeQualifications = new EmployeeQualificationModel(this,
  175. () => new Filter<EmployeeQualification>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  176. );
  177. Activities = new ActivityModel(this,
  178. () => new Filter<EmployeeActivity>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  179. );
  180. InOut = new InOutModel(this,
  181. () => new Filters<Employee>()
  182. .Add(LookupFactory.DefineFilter<Employee>())
  183. .Add(new Filter<Employee>(x=>x.ID).IsNotEqualTo(App.Data.Me.ID).And(x=>x.ShowOnInOutBoard).IsEqualTo(true))
  184. .Combine()
  185. );
  186. Jobs = new JobModel(this, MyJobsFilter) { FileName = "job.index" };
  187. Products = new ProductModel(this,
  188. LookupFactory.DefineFilter<Product>);
  189. ProductGroups = new ProductGroupModel(this,
  190. LookupFactory.DefineFilter<ProductGroup>);
  191. GPSTrackers = new GPSTrackerModel(this,
  192. LookupFactory.DefineFilter<GPSTracker>);
  193. Equipment = new EquipmentModel(this,
  194. () =>
  195. {
  196. var filters = new Filters<Equipment>();
  197. filters.Add(LookupFactory.DefineFilter<Equipment>());
  198. if (!Security.IsAllowed<CanViewPrivateEquipment>())
  199. filters.Add(new Filter<Equipment>(x => x.Private).IsEqualTo(false));
  200. return filters.Combine();
  201. }
  202. ) { FileName = "Equipment.index"};
  203. EquipmentGroups = new EquipmentGroupModel(this,
  204. LookupFactory.DefineFilter<EquipmentGroup>);
  205. Deliveries = new DeliveryModel(this, () => null);
  206. DeliveryItems = new DeliveryItemModel(this,
  207. LookupFactory.DefineFilter<DeliveryItem>
  208. );
  209. Notifications = new NotificationModel(this,
  210. () => new Filter<Notification>(x=>x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  211. .And(new Filter<Notification>(x=>x.Closed).IsEqualTo(DateTime.MinValue)
  212. .Or(x=>x.Created).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-90)))
  213. );
  214. TimeSheets = new TimeSheetModel(this,
  215. () => new Filter<TimeSheet>(x=>x.Processed).IsEqualTo(DateTime.MinValue)
  216. .And(x=>x.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID));
  217. Assignments = new AssignmentModel(this,
  218. () => new Filter<Assignment>(x => x.Date).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-90))
  219. .And(x => x.Date).IsLessThanOrEqualTo(DateTime.Today.AddDays(90))
  220. );
  221. Contacts = new ContactModel(this,
  222. () => LookupFactory.DefineFilter<Contact>());
  223. Kanbans = new KanbanModel(this,
  224. () => new Filter<Kanban>(x => x.ID)
  225. .InQuery(new Filter<KanbanSubscriber>(x=>x.Employee.ID).IsEqualTo(App.Data.Me.ID), x=>x.Kanban.ID)
  226. .And(
  227. new Filter<Kanban>(x=>x.Category).IsNotEqualTo(Kanban.COMPLETE)
  228. .Or(x=>x.Completed).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)
  229. )
  230. )
  231. ) { FileName = "kanbans.index" };
  232. KanbanTypes = new KanbanTypeModel(this, () => null);
  233. BluetoothGates = new BluetoothGateModel(this,
  234. () => new Filter<JobTracker>(x => x.Active).IsEqualTo(true)
  235. );
  236. DigitalForms = new DigitalFormModel(this,
  237. () => new Filter<EmployeeDigitalForm>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  238. .And(x => x.Form.Active).IsEqualTo(true)
  239. );
  240. KanbanForms = new KanbanFormModel(this,
  241. () => new Filter<KanbanForm>(x=>x.Parent.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID)
  242. .And(new Filter<KanbanForm>(x=>x.FormCompleted).IsEqualTo(DateTime.MinValue)
  243. .Or(x=>x.FormCompleted).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7))
  244. )
  245. );
  246. PurchaseOrders = new PurchaseOrderModel(this,
  247. () => new Filter<PurchaseOrder>(x => x.ClosedDate).IsEqualTo(DateTime.MinValue)
  248. );
  249. ManufacturingPackets = new ManufacturingPacketModel(this,
  250. () => new Filter<ManufacturingPacket>(x => x.Archived).IsEqualTo(DateTime.MinValue)
  251. );
  252. ManufacturingFactories = new ManufacturingFactoryModel(this,
  253. () => null
  254. );
  255. }
  256. private static Filter<Job> MyJobsFilter()
  257. {
  258. return Security.IsAllowed<CanViewAllJobs>()
  259. ? new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue)
  260. .And(x => x.JobStatus.Active).IsEqualTo(true)
  261. : new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue)
  262. .And(x => x.JobStatus.Active).IsEqualTo(true)
  263. .And(x => x.ID).InQuery(
  264. new Filter<JobEmployee>(x => x.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID),
  265. x => x.JobLink.ID
  266. );
  267. }
  268. public void Setup()
  269. {
  270. // Clear All Transient Caches
  271. Reset();
  272. // Always load the current Employee and Security Tokens
  273. Task[] init = new Task[]
  274. {
  275. Task.Run(() => { CurrentEmployee.Refresh(true); }),
  276. Task.Run(() => { Security.CheckTokens(); })
  277. };
  278. Task.WaitAll(init);
  279. App.Bluetooth.OnScanFinished += OnBluetoothScanFinished;
  280. App.GPS.OnLocationFound += OnGPSLocationFound;
  281. App.Transport.OnOpen += OnTransportConnected;
  282. App.Transport.OnClose += OnTransportDisconnected;
  283. }
  284. public bool IsConnected() => App.Transport?.IsConnected() == true;
  285. public event TransportDisconnectedEvent TransportDisconnected;
  286. public event TransportConnectedEvent TransportConnected;
  287. private void OnTransportDisconnected(IRpcTransport transport, RpcTransportCloseArgs e)
  288. {
  289. TransportDisconnected?.Invoke(new TransportDisconnectedEventArgs());
  290. Task.Run(() =>
  291. {
  292. while (!IsConnected())
  293. App.Transport.Connect();
  294. ClientFactory.Validate(ClientFactory.SessionID);
  295. });
  296. }
  297. private void OnTransportConnected(IRpcTransport transport, RpcTransportOpenArgs e)
  298. {
  299. TransportConnected?.Invoke(new TransportConnectedEventArgs());
  300. }
  301. private Location _lastgpslocation = new Location();
  302. private GPSTrackerLocation GetGPSTrackerUpdate(string deviceid, Location location,
  303. TimeSpan threshold, double distance)
  304. {
  305. GPSTrackerShell tracker = GPSTrackers.FirstOrDefault(x => x.DeviceID.Equals(deviceid));
  306. if (tracker != null)
  307. {
  308. if ((tracker.Timestamp < location.Timestamp.Subtract(threshold)) || (tracker.Location.DistanceTo(location, UnitOfLength.Kilometers) > distance))
  309. {
  310. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  311. gpsTrackerLocation.DeviceID = tracker.DeviceID;
  312. gpsTrackerLocation.Tracker.ID = tracker.ID;
  313. gpsTrackerLocation.Location = location;
  314. return gpsTrackerLocation;
  315. }
  316. }
  317. return null;
  318. }
  319. private void OnGPSLocationFound(LocationServices sender)
  320. {
  321. if (_lastgpslocation.Timestamp < DateTime.Now.Subtract(TimeSpan.FromMinutes(2)))
  322. {
  323. var devicelocation = new InABox.Core.Location()
  324. {
  325. Latitude = App.GPS.Latitude,
  326. Longitude = App.GPS.Longitude,
  327. Timestamp = App.GPS.TimeStamp,
  328. Address = App.GPS.Address
  329. };
  330. GPSTrackers.Refresh(false);
  331. var update = GetGPSTrackerUpdate(MobileUtils.GetDeviceID(), devicelocation, TimeSpan.FromMinutes(2),
  332. 0.1);
  333. if (update != null)
  334. new Client<GPSTrackerLocation>().Save(update, "Updated via Mobile Device", (o, e) => { });
  335. }
  336. GPSLocationUpdated?.Invoke(new GPSEventArgs());
  337. }
  338. private void OnBluetoothScanFinished(Bluetooth sender)
  339. {
  340. UploadTiles();
  341. BluetoothScanFinished?.Invoke(new BluetoothEventArgs());
  342. }
  343. private void UploadTiles()
  344. {
  345. try
  346. {
  347. if (App.GPS.Latitude.Equals(0.0F) && App.GPS.Longitude.Equals(0.0F))
  348. return;
  349. if (App.Bluetooth.DetectedBlueToothMACAddresses.Count == 0)
  350. return;
  351. var devicelocation = new InABox.Core.Location()
  352. {
  353. Latitude = App.GPS.Latitude,
  354. Longitude = App.GPS.Longitude,
  355. Timestamp = App.GPS.TimeStamp,
  356. Address = App.GPS.Address
  357. };
  358. GPSTrackers.Refresh(true);
  359. List<GPSTrackerLocation> updates = new List<GPSTrackerLocation>();
  360. foreach (String deviceid in App.Bluetooth.DetectedBlueToothMACAddresses)
  361. {
  362. var update = GetGPSTrackerUpdate(deviceid, devicelocation, TimeSpan.FromMinutes(2), 0.1);
  363. if (update != null)
  364. updates.Add(update);
  365. }
  366. if (updates.Any())
  367. new Client<GPSTrackerLocation>().Save(updates, $"Updated by Mobile {MobileUtils.GetDeviceID()}",
  368. (o, e) => { });
  369. }
  370. catch (Exception ex)
  371. {
  372. MobileLogging.Log($"UploadTiles() {ex.Message} \n {ex.StackTrace}");
  373. }
  374. }
  375. }
  376. }