Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Proxiwash.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // @flow
  2. import type {ProxiwashMachineType} from '../screens/Proxiwash/ProxiwashScreen';
  3. /**
  4. * Gets the machine end Date object.
  5. * If the end time is at least 12 hours before the current time,
  6. * it will be considered as happening the day after.
  7. * If it is before but less than 12 hours, it will be considered invalid (to fix proxiwash delay)
  8. *
  9. * @param machine The machine to get the date from
  10. * @returns {Date} The date object representing the end time.
  11. */
  12. export function getMachineEndDate(machine: ProxiwashMachineType): Date | null {
  13. const array = machine.endTime.split(':');
  14. let endDate = new Date(Date.now());
  15. endDate.setHours(parseInt(array[0], 10), parseInt(array[1], 10));
  16. const limit = new Date(Date.now());
  17. if (endDate < limit) {
  18. if (limit.getHours() > 12) {
  19. limit.setHours(limit.getHours() - 12);
  20. if (endDate < limit) endDate.setDate(endDate.getDate() + 1);
  21. else endDate = null;
  22. } else endDate = null;
  23. }
  24. return endDate;
  25. }
  26. /**
  27. * Checks whether the machine of the given ID has scheduled notifications
  28. *
  29. * @param machine The machine to check
  30. * @param machinesWatched The machine list
  31. * @returns {boolean}
  32. */
  33. export function isMachineWatched(
  34. machine: ProxiwashMachineType,
  35. machinesWatched: Array<ProxiwashMachineType>,
  36. ): boolean {
  37. let watched = false;
  38. machinesWatched.forEach((watchedMachine: ProxiwashMachineType) => {
  39. if (
  40. watchedMachine.number === machine.number &&
  41. watchedMachine.endTime === machine.endTime
  42. )
  43. watched = true;
  44. });
  45. return watched;
  46. }
  47. /**
  48. * Gets the machine of the given id
  49. *
  50. * @param id The machine's ID
  51. * @param allMachines The machine list
  52. * @returns {null|ProxiwashMachineType} The machine or null if not found
  53. */
  54. export function getMachineOfId(
  55. id: string,
  56. allMachines: Array<ProxiwashMachineType>,
  57. ): ProxiwashMachineType | null {
  58. let machineFound = null;
  59. allMachines.forEach((machine: ProxiwashMachineType) => {
  60. if (machine.number === id) machineFound = machine;
  61. });
  62. return machineFound;
  63. }
  64. /**
  65. * Gets a cleaned machine watched list by removing invalid entries.
  66. * An entry is considered invalid if the end time in the watched list
  67. * and in the full list does not match (a new machine cycle started)
  68. *
  69. * @param machineWatchedList The current machine watch list
  70. * @param allMachines The current full machine list
  71. * @returns {Array<ProxiwashMachineType>}
  72. */
  73. export function getCleanedMachineWatched(
  74. machineWatchedList: Array<ProxiwashMachineType>,
  75. allMachines: Array<ProxiwashMachineType>,
  76. ): Array<ProxiwashMachineType> {
  77. const newList = [];
  78. machineWatchedList.forEach((watchedMachine: ProxiwashMachineType) => {
  79. const machine = getMachineOfId(watchedMachine.number, allMachines);
  80. if (
  81. machine != null &&
  82. watchedMachine.number === machine.number &&
  83. watchedMachine.endTime === machine.endTime
  84. ) {
  85. newList.push(machine);
  86. }
  87. });
  88. return newList;
  89. }