DataModel.cs 18 KB

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