DataModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 Supplier List for Lookups, etc
  100. /// </summary>
  101. public SupplierModel Suppliers { get; private set; }
  102. public ConsignmentTypeModel ConsignmentTypes { get; private set; }
  103. /// <summary>
  104. /// Master List of Dimension Units for the company
  105. /// </summary>
  106. public ProductDimensionUnitModel ProductDimensionUnits { get; private set; }
  107. /// <summary>
  108. /// Master Product Groups List (Lookup?)
  109. /// </summary>
  110. public ProductGroupModel ProductGroups { get; private set; }
  111. /// <summary>
  112. /// Master Product Styles List (Lookup?)
  113. /// </summary>
  114. public ProductStyleModel ProductStyles { get; private set; }
  115. /// <summary>
  116. /// All GPS Tracker Devices registered by the company
  117. /// </summary>
  118. public GPSTrackerModel GPSTrackers { get; private set; }
  119. /// <summary>
  120. /// Master Equipment Register
  121. /// </summary>
  122. public EquipmentModel Equipment { get; private set; }
  123. /// <summary>
  124. /// Master List of Equipment Groups
  125. /// </summary>
  126. public EquipmentGroupModel EquipmentGroups { get; private set; }
  127. /// <summary>
  128. /// Master List of Available Delivery Types
  129. /// </summary>
  130. public DeliveryTypeModel DeliveryTypes { get; private set; }
  131. /// <summary>
  132. /// List of all Deliveries
  133. /// hmmm.. should be configurable?
  134. /// </summary>
  135. public DeliveryModel Deliveries { get; private set; }
  136. /// <summary>
  137. /// List of all available Delivery Barcodes
  138. /// hmmm.. should be configurable?
  139. /// </summary>
  140. public DeliveryItemModel DeliveryItems { get; private set; }
  141. public RequisitionDestinationModel RequisitionDestinations { get; private set; }
  142. /// <summary>
  143. /// My Notifications (open only?)
  144. /// </summary>
  145. public NotificationModel Notifications { get; private set; }
  146. /// <summary>
  147. /// All Tasks I am subscribed to
  148. /// </summary>
  149. public KanbanModel Kanbans { get; private set; }
  150. /// <summary>
  151. /// All Kanban Types
  152. /// </summary>
  153. public KanbanTypeModel KanbanTypes { get; private set; }
  154. /// <summary>
  155. /// Available Form Library - this includes all "Applies To" values
  156. /// You can filter to a specific type by using the "Search" function
  157. /// </summary>
  158. public DigitalFormModel DigitalForms { get; private set; }
  159. // The list of open task-kased forms for the current user
  160. // Specifically for us in the "Forms" module
  161. // This overlaps the "Task" module a fair amount, but it provides
  162. // an alternative paradigm for tasks
  163. public KanbanFormModel KanbanForms { get; private set; }
  164. /// <summary>
  165. /// List of Employees, along with Clock on / off status
  166. /// </summary>
  167. public InOutModel InOut { get; private set; }
  168. /// <summary>
  169. /// List of Open Purchase Orders
  170. /// </summary>
  171. public PurchaseOrderModel PurchaseOrders { get; private set; }
  172. public ManufacturingFactoryModel ManufacturingFactories { get; private set; }
  173. public StockLocationModel StockLocations { get; set; }
  174. public StockAreaModel StockAreas { get; set; }
  175. public StockWarehouseModel StockWarehouses { get; set; }
  176. public DataModel()
  177. {
  178. Reset();
  179. }
  180. public void Reset()
  181. {
  182. _me = null;
  183. CurrentEmployee = new EmployeeDetailModel(this,
  184. () => new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)
  185. );
  186. Employees = new EmployeeModel(this,
  187. LookupFactory.DefineFilter<Employee>
  188. );
  189. EmployeeTeams = new EmployeeTeamModel(this,
  190. () => new Filter<EmployeeTeam>(x => x.EmployeeLink.ID)
  191. .InQuery(LookupFactory.DefineFilter<Employee>(), x => x.ID)
  192. );
  193. EmployeeForms = new EmployeeFormModel(this,
  194. () => new Filter<EmployeeForm>(x => x.Parent.ID).IsEqualTo(App.Data.Me.ID)
  195. );
  196. EmployeeQualifications = new EmployeeQualificationModel(this,
  197. () => new Filter<EmployeeQualification>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  198. );
  199. Activities = new ActivityModel(this,
  200. () => new Filter<EmployeeActivity>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  201. );
  202. InOut = new InOutModel(this,
  203. () => new Filters<Employee>()
  204. .Add(LookupFactory.DefineFilter<Employee>())
  205. .Add(new Filter<Employee>(x=>x.ID).IsNotEqualTo(App.Data.Me.ID).And(x=>x.ShowOnInOutBoard).IsEqualTo(true))
  206. .Combine()!
  207. );
  208. Jobs = new JobModel(this,
  209. MyJobsFilter,
  210. CoreRepository.CacheFileName<Job>
  211. );
  212. Products = new ProductModel(this,
  213. LookupFactory.DefineFilter<Product>,
  214. CoreRepository.CacheFileName<Product>
  215. );
  216. Suppliers = new SupplierModel(this,
  217. LookupFactory.DefineFilter<Supplier>,
  218. CoreRepository.CacheFileName<Supplier>
  219. );
  220. ConsignmentTypes = new ConsignmentTypeModel(this,
  221. LookupFactory.DefineFilter<ConsignmentType>,
  222. CoreRepository.CacheFileName<ConsignmentType>
  223. );
  224. ProductDimensionUnits = new ProductDimensionUnitModel(this,
  225. LookupFactory.DefineFilter<ProductDimensionUnit>,
  226. () => null);
  227. ProductGroups = new ProductGroupModel(this,
  228. LookupFactory.DefineFilter<ProductGroup>,
  229. CoreRepository.CacheFileName<ProductGroup>
  230. );
  231. ProductStyles = new ProductStyleModel(this,
  232. LookupFactory.DefineFilter<ProductStyle>
  233. );
  234. GPSTrackers = new GPSTrackerModel(this,
  235. LookupFactory.DefineFilter<GPSTracker>
  236. );
  237. Equipment = new EquipmentModel(this,
  238. () =>
  239. {
  240. var filters = new Filters<Equipment>();
  241. filters.Add(LookupFactory.DefineFilter<Equipment>());
  242. if (!Security.IsAllowed<CanViewPrivateEquipment>())
  243. filters.Add(new Filter<Equipment>(x => x.Private).IsEqualTo(false));
  244. return filters!.Combine();
  245. },
  246. CoreRepository.CacheFileName<Equipment>
  247. );
  248. EquipmentGroups = new EquipmentGroupModel(this,
  249. LookupFactory.DefineFilter<EquipmentGroup>
  250. );
  251. DeliveryTypes= new DeliveryTypeModel(this,
  252. null,
  253. CoreRepository.CacheFileName<DeliveryType>);
  254. Deliveries = new DeliveryModel(this);
  255. DeliveryItems = new DeliveryItemModel(this,
  256. LookupFactory.DefineFilter<DeliveryItem>
  257. );
  258. RequisitionDestinations = new RequisitionDestinationModel(this,
  259. null,
  260. CoreRepository.CacheFileName<RequisitionDestination>
  261. );
  262. Notifications = new NotificationModel(this,
  263. () => new Filter<Notification>(x=>x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  264. .And(new Filter<Notification>(x=>x.Closed).IsEqualTo(DateTime.MinValue)
  265. .Or(x=>x.Created).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-90)))
  266. );
  267. TimeSheets = new TimeSheetModel(this,
  268. () => new Filter<TimeSheet>(x=>x.Processed).IsEqualTo(DateTime.MinValue)
  269. .And(x=>x.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID)
  270. );
  271. Assignments = new AssignmentModel(this,
  272. () => new Filter<Assignment>(x => x.Date).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-90))
  273. .And(x => x.Date).IsLessThanOrEqualTo(DateTime.Today.AddDays(90))
  274. );
  275. Contacts = new ContactModel(this,
  276. () => LookupFactory.DefineFilter<Contact>()
  277. );
  278. Kanbans = new KanbanModel(this,
  279. () => new Filter<Kanban>(x => x.ID)
  280. .InQuery(new Filter<KanbanSubscriber>(x=>x.Employee.ID).IsEqualTo(App.Data.Me.ID), x=>x.Kanban.ID)
  281. .And(
  282. new Filter<Kanban>(x=>x.Status).IsNotEqualTo(KanbanStatus.Complete)
  283. .Or(x=>x.Completed).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)
  284. )
  285. ),
  286. CoreRepository.CacheFileName<Kanban>
  287. );
  288. KanbanTypes = new KanbanTypeModel(this);
  289. BluetoothGates = new BluetoothGateModel(this,
  290. () => new Filter<JobTracker>(x => x.Active).IsEqualTo(true)
  291. );
  292. DigitalForms = new DigitalFormModel(this,
  293. () => new Filter<EmployeeDigitalForm>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  294. .And(x => x.Form.Active).IsEqualTo(true)
  295. );
  296. KanbanForms = new KanbanFormModel(this,
  297. () => new Filter<KanbanForm>(x=>x.Parent.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID)
  298. .And(new Filter<KanbanForm>(x=>x.FormCompleted).IsEqualTo(DateTime.MinValue)
  299. .Or(x=>x.FormCompleted).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)))
  300. );
  301. PurchaseOrders = new PurchaseOrderModel(this,
  302. () => new Filter<PurchaseOrder>(x => x.ClosedDate).IsEqualTo(DateTime.MinValue)
  303. );
  304. ManufacturingFactories = new ManufacturingFactoryModel(this);
  305. StockLocations = new StockLocationModel(this,
  306. null,
  307. CoreRepository.CacheFileName<StockLocation>
  308. );
  309. StockAreas = new StockAreaModel(this,
  310. null,
  311. CoreRepository.CacheFileName<StockArea>
  312. );
  313. StockWarehouses = new StockWarehouseModel(this,
  314. null,
  315. CoreRepository.CacheFileName<StockWarehouse>
  316. );
  317. }
  318. private static Filter<Job> MyJobsFilter()
  319. {
  320. return Security.IsAllowed<CanViewAllJobs>()
  321. ? new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue)
  322. .And(x => x.JobStatus.Active).IsEqualTo(true)
  323. : new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue)
  324. .And(x => x.JobStatus.Active).IsEqualTo(true)
  325. .And(x => x.ID).InQuery(
  326. new Filter<JobEmployee>(x => x.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID),
  327. x => x.JobLink.ID
  328. );
  329. }
  330. public void Setup()
  331. {
  332. // Clear All Transient Caches
  333. Reset();
  334. // Always load the current Employee and Security Tokens
  335. Task[] init = new Task[]
  336. {
  337. Task.Run(() => { CurrentEmployee.Refresh(true); }),
  338. Task.Run(() => { Security.CheckTokens(); })
  339. };
  340. Task.WaitAll(init);
  341. App.Bluetooth.OnScanFinished += OnBluetoothScanFinished;
  342. App.GPS.OnLocationFound += OnGPSLocationFound;
  343. App.Transport.OnOpen += OnTransportConnected;
  344. App.Transport.OnClose += (transport, args) => Reconnect();
  345. App.Transport.OnException += (transport, args) => Reconnect();
  346. DigitalFormDocumentFactory.Init(
  347. new MobileDigitalFormDocumentHandler(
  348. b =>
  349. {
  350. IsBackgroundUpdateStatusActive = b;
  351. BackgroundUpdateStatusChanged?.Invoke(this, EventArgs.Empty);
  352. })
  353. );
  354. DigitalFormDocumentFactory.Run();
  355. StartUpdateQueue();
  356. }
  357. public bool IsBackgroundUpdateStatusActive { get; private set; }
  358. public event BackgroundUpdateStatusEvent BackgroundUpdateStatusChanged;
  359. public bool IsConnected() => App.Transport?.IsConnected() == true;
  360. public event TransportDisconnectedEvent TransportDisconnected;
  361. public event TransportConnectedEvent TransportConnected;
  362. private void Reconnect()
  363. {
  364. DigitalFormDocumentFactory.Stop();
  365. TransportDisconnected?.Invoke(new TransportDisconnectedEventArgs());
  366. Task.Run(() =>
  367. {
  368. ValidationStatus status = ValidationStatus.INVALID;
  369. while (status != ValidationStatus.VALID)
  370. {
  371. try
  372. {
  373. while (!IsConnected())
  374. {
  375. try
  376. {
  377. App.Transport.Connect();
  378. }
  379. catch (Exception eConnect)
  380. {
  381. MobileLogging.Log($"Reconnect() {eConnect.Message} \n {eConnect.StackTrace}");
  382. Task.Delay(TimeSpan.FromMilliseconds(1000));
  383. }
  384. }
  385. status = ClientFactory.Validate(ClientFactory.SessionID);
  386. }
  387. catch (Exception ex)
  388. {
  389. MobileLogging.Log($"Revalidate() {ex.Message} \n {ex.StackTrace}");
  390. Task.Delay(TimeSpan.FromMilliseconds(1000));
  391. }
  392. }
  393. DigitalFormDocumentFactory.Run();
  394. });
  395. }
  396. private void OnTransportConnected(IRpcTransport transport, RpcTransportOpenArgs e)
  397. {
  398. TransportConnected?.Invoke(new TransportConnectedEventArgs());
  399. }
  400. #region QueueUpdaterSupport
  401. private CancellationTokenSource? _cancelQueueUpdate;
  402. private List<IQueueUpdater> _updateQueues = new();
  403. private void StartUpdateQueue()
  404. {
  405. StopUpdateQueue();
  406. _cancelQueueUpdate = new CancellationTokenSource();
  407. Task.Run(
  408. () =>
  409. {
  410. while (_cancelQueueUpdate?.IsCancellationRequested != true)
  411. {
  412. foreach (var queue in _updateQueues.ToArray())
  413. queue.ProcessQueue();
  414. Thread.Sleep(100);
  415. }
  416. },
  417. _cancelQueueUpdate.Token
  418. );
  419. }
  420. private void StopUpdateQueue()
  421. {
  422. if (_cancelQueueUpdate != null)
  423. {
  424. _cancelQueueUpdate.Cancel();
  425. _cancelQueueUpdate.Dispose();
  426. _cancelQueueUpdate = null;
  427. }
  428. }
  429. public void AddUpdateQueue<TEntity>(QueueUpdater<TEntity> queue)
  430. where TEntity : Entity, IPersistent, IRemotable, new()
  431. {
  432. _updateQueues.Add(queue);
  433. }
  434. public void RemoveUpdateQueue<TEntity>(QueueUpdater<TEntity> queue)
  435. where TEntity : Entity, IPersistent, IRemotable, new()
  436. {
  437. _updateQueues.Remove(queue);
  438. }
  439. #endregion
  440. private Location _lastgpslocation = new Location();
  441. private GPSTrackerLocation GetGPSTrackerUpdate(string deviceid, Location location,
  442. TimeSpan threshold, double distance)
  443. {
  444. GPSTrackerShell tracker = GPSTrackers.FirstOrDefault(x => x.DeviceID.Equals(deviceid));
  445. if (tracker != null)
  446. {
  447. if ((tracker.Timestamp < location.Timestamp.Subtract(threshold)) || (tracker.Location.DistanceTo(location, UnitOfLength.Kilometers) > distance))
  448. {
  449. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  450. gpsTrackerLocation.DeviceID = tracker.DeviceID;
  451. gpsTrackerLocation.Tracker.ID = tracker.ID;
  452. gpsTrackerLocation.Location = location;
  453. return gpsTrackerLocation;
  454. }
  455. }
  456. return null;
  457. }
  458. private void OnGPSLocationFound(LocationServices sender)
  459. {
  460. if (_lastgpslocation.Timestamp < DateTime.Now.Subtract(TimeSpan.FromMinutes(2)))
  461. {
  462. var devicelocation = new InABox.Core.Location()
  463. {
  464. Latitude = App.GPS.Latitude,
  465. Longitude = App.GPS.Longitude,
  466. Timestamp = App.GPS.TimeStamp,
  467. Address = App.GPS.Address
  468. };
  469. GPSTrackers.Refresh(false);
  470. var update = GetGPSTrackerUpdate(MobileUtils.GetDeviceID(), devicelocation, TimeSpan.FromMinutes(2),
  471. 0.1);
  472. if (update != null)
  473. new Client<GPSTrackerLocation>().Save(update, "Updated via Mobile Device", (o, e) => { });
  474. }
  475. GPSLocationUpdated?.Invoke(new GPSEventArgs());
  476. }
  477. private void OnBluetoothScanFinished(Bluetooth sender)
  478. {
  479. UploadTiles();
  480. BluetoothScanFinished?.Invoke(new BluetoothEventArgs());
  481. }
  482. private void UploadTiles()
  483. {
  484. try
  485. {
  486. if (App.GPS.Latitude.Equals(0.0F) && App.GPS.Longitude.Equals(0.0F))
  487. return;
  488. if (App.Bluetooth.DetectedBlueToothMACAddresses.Count == 0)
  489. return;
  490. var devicelocation = new InABox.Core.Location()
  491. {
  492. Latitude = App.GPS.Latitude,
  493. Longitude = App.GPS.Longitude,
  494. Timestamp = App.GPS.TimeStamp,
  495. Address = App.GPS.Address
  496. };
  497. GPSTrackers.Refresh(true);
  498. List<GPSTrackerLocation> updates = new List<GPSTrackerLocation>();
  499. foreach (String deviceid in App.Bluetooth.DetectedBlueToothMACAddresses)
  500. {
  501. var update = GetGPSTrackerUpdate(deviceid, devicelocation, TimeSpan.FromMinutes(2), 0.1);
  502. if (update != null)
  503. updates.Add(update);
  504. }
  505. if (updates.Any())
  506. new Client<GPSTrackerLocation>().Save(updates, $"Updated by Mobile {MobileUtils.GetDeviceID()}",
  507. (o, e) => { });
  508. }
  509. catch (Exception ex)
  510. {
  511. MobileLogging.Log($"UploadTiles() {ex.Message} \n {ex.StackTrace}");
  512. }
  513. }
  514. }
  515. }