Нема описа
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const FEST_AGENT_STATUS_URL = 'http://localhost:38473/status';
  2. const FEST_AGENT_POLL_MS = 120000;
  3. const FEST_AGENT_TIMEOUT_MS = 3000;
  4. function renderFestAgentStatus(el, status) {
  5. if (!el) {
  6. return;
  7. }
  8. const online = !!(status && status.online);
  9. const endpointId = status && status.endpointId != null ? status.endpointId : null;
  10. if (online && endpointId != null) {
  11. el.innerHTML =
  12. '<span class="badge bg-label-success ms-2 text-nowrap">' +
  13. '<i class="bx bx-check-circle me-1"></i>FestAgent · #' + endpointId +
  14. '</span>';
  15. return;
  16. }
  17. if (online) {
  18. el.innerHTML =
  19. '<span class="badge bg-label-success ms-2 text-nowrap">' +
  20. '<i class="bx bx-check-circle me-1"></i>FestAgent attivo</span>';
  21. return;
  22. }
  23. el.innerHTML =
  24. '<span class="badge bg-label-danger ms-2 text-nowrap">' +
  25. '<i class="bx bx-x-circle me-1"></i>FestAgent non raggiungibile</span>';
  26. }
  27. function fetchFestAgentStatus() {
  28. const controller = new AbortController();
  29. const timeoutId = setTimeout(function () {
  30. controller.abort();
  31. }, FEST_AGENT_TIMEOUT_MS);
  32. return fetch(FEST_AGENT_STATUS_URL, {
  33. method: 'GET',
  34. headers: {
  35. Accept: 'application/json',
  36. },
  37. signal: controller.signal,
  38. })
  39. .then(function (response) {
  40. return response.json();
  41. })
  42. .then(function (data) {
  43. return {
  44. online: !!(data && data.ok),
  45. endpointId: data && data.endpoint ? data.endpoint.endpoint_id : null,
  46. };
  47. })
  48. .finally(function () {
  49. clearTimeout(timeoutId);
  50. });
  51. }
  52. function updateLocalFestAgentStatusFooter() {
  53. const el = document.getElementById('status_local_festagent');
  54. if (!el) {
  55. return Promise.resolve(false);
  56. }
  57. return fetchFestAgentStatus()
  58. .then(function (status) {
  59. renderFestAgentStatus(el, status);
  60. return status.online;
  61. })
  62. .catch(function () {
  63. renderFestAgentStatus(el, { online: false, endpointId: null });
  64. return false;
  65. });
  66. }
  67. function startFestAgentStatusWatcher() {
  68. if (!document.getElementById('status_local_festagent')) {
  69. return;
  70. }
  71. updateLocalFestAgentStatusFooter();
  72. if (window.__festAgentStatusInterval) {
  73. clearInterval(window.__festAgentStatusInterval);
  74. }
  75. window.__festAgentStatusInterval = setInterval(updateLocalFestAgentStatusFooter, FEST_AGENT_POLL_MS);
  76. }
  77. function getLocalFestAgentStatus(showNotification) {
  78. return updateLocalFestAgentStatusFooter().then(function (online) {
  79. if (!showNotification || typeof Notyf === 'undefined') {
  80. return online;
  81. }
  82. const notyf = new Notyf({
  83. duration: 3000,
  84. position: { x: 'right', y: 'top' },
  85. });
  86. if (online) {
  87. notyf.success('FestAgent è in esecuzione');
  88. } else {
  89. notyf.error('FestAgent non è in esecuzione su questo PC');
  90. }
  91. return online;
  92. });
  93. }
  94. document.addEventListener('DOMContentLoaded', startFestAgentStatusWatcher);
  95. window.getLocalFestAgentStatus = getLocalFestAgentStatus;
  96. window.updateLocalFestAgentStatusFooter = updateLocalFestAgentStatusFooter;
  97. window.fetchFestAgentStatus = fetchFestAgentStatus;
  98. window.renderFestAgentStatus = renderFestAgentStatus;