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.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import type {ProxiwashMachineType} from '../screens/Proxiwash/ProxiwashScreen';
  20. /**
  21. * Gets the machine end Date object.
  22. * If the end time is at least 12 hours before the current time,
  23. * it will be considered as happening the day after.
  24. * If it is before but less than 12 hours, it will be considered invalid (to fix proxiwash delay)
  25. *
  26. * @param machine The machine to get the date from
  27. * @returns {Date} The date object representing the end time.
  28. */
  29. export function getMachineEndDate(machine: ProxiwashMachineType): Date | null {
  30. const array = machine.endTime.split(':');
  31. let endDate: Date | null = new Date(Date.now());
  32. endDate.setHours(parseInt(array[0], 10), parseInt(array[1], 10));
  33. const limit = new Date(Date.now());
  34. if (endDate < limit) {
  35. if (limit.getHours() > 12) {
  36. limit.setHours(limit.getHours() - 12);
  37. if (endDate < limit) {
  38. endDate.setDate(endDate.getDate() + 1);
  39. } else {
  40. endDate = null;
  41. }
  42. } else {
  43. endDate = null;
  44. }
  45. }
  46. return endDate;
  47. }
  48. /**
  49. * Checks whether the machine of the given ID has scheduled notifications
  50. *
  51. * @param machine The machine to check
  52. * @param machinesWatched The machine list
  53. * @returns {boolean}
  54. */
  55. export function isMachineWatched(
  56. machine: ProxiwashMachineType,
  57. machinesWatched: Array<ProxiwashMachineType>,
  58. ): boolean {
  59. let watched = false;
  60. machinesWatched.forEach((watchedMachine: ProxiwashMachineType) => {
  61. if (
  62. watchedMachine.number === machine.number &&
  63. watchedMachine.endTime === machine.endTime
  64. ) {
  65. watched = true;
  66. }
  67. });
  68. return watched;
  69. }
  70. /**
  71. * Gets the machine of the given id
  72. *
  73. * @param id The machine's ID
  74. * @param allMachines The machine list
  75. * @returns {null|ProxiwashMachineType} The machine or null if not found
  76. */
  77. export function getMachineOfId(
  78. id: string,
  79. allMachines: Array<ProxiwashMachineType>,
  80. ): ProxiwashMachineType | null {
  81. let machineFound = null;
  82. allMachines.forEach((machine: ProxiwashMachineType) => {
  83. if (machine.number === id) {
  84. machineFound = machine;
  85. }
  86. });
  87. return machineFound;
  88. }
  89. /**
  90. * Gets a cleaned machine watched list by removing invalid entries.
  91. * An entry is considered invalid if the end time in the watched list
  92. * and in the full list does not match (a new machine cycle started)
  93. *
  94. * @param machineWatchedList The current machine watch list
  95. * @param allMachines The current full machine list
  96. * @returns {Array<ProxiwashMachineType>}
  97. */
  98. export function getCleanedMachineWatched(
  99. machineWatchedList: Array<ProxiwashMachineType>,
  100. allMachines: Array<ProxiwashMachineType>,
  101. ): Array<ProxiwashMachineType> {
  102. const newList: Array<ProxiwashMachineType> = [];
  103. machineWatchedList.forEach((watchedMachine: ProxiwashMachineType) => {
  104. const machine = getMachineOfId(watchedMachine.number, allMachines);
  105. if (
  106. machine != null &&
  107. watchedMachine.number === machine.number &&
  108. watchedMachine.endTime === machine.endTime
  109. ) {
  110. newList.push(machine);
  111. }
  112. });
  113. return newList;
  114. }