page3.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. var timeLogLevel = "";
  2. function setTimeLogLevel() {
  3. var level = logLevel.value;
  4. fetch("/api/time/loglevel", {
  5. method: "POST",
  6. headers: { "Content-Type": "application/json" },
  7. body: JSON.stringify({ level: level }),
  8. })
  9. .then(function (r) {
  10. if (!r.ok) {
  11. return r.text().then(function (t) {
  12. throw new Error(t);
  13. });
  14. }
  15. timeLogLevel = level;
  16. })
  17. .catch(function (e) {
  18. logLevel.value = timeLogLevel;
  19. });
  20. }
  21. function getTimeLogLevel() {
  22. fetch("/api/time/loglevel")
  23. .then(function (r) {
  24. if (!r.ok) {
  25. return r.text().then(function (t) {
  26. throw new Error(t);
  27. });
  28. }
  29. return r.json();
  30. })
  31. .then(function (d) {
  32. timeLogLevel = d.log_level;
  33. logLevel.value = d.log_level;
  34. })
  35. .catch(function (e) {
  36. timeLogLevel = "";
  37. logLevel.value = "";
  38. });
  39. }
  40. function downloadTimeLogFile(event) {
  41. var btn = event.currentTarget;
  42. btn.disabled = true;
  43. fetch("/api/time/logfile")
  44. .then(function (r) {
  45. if (!r.ok) {
  46. throw new Error("HTTP " + r.status);
  47. }
  48. var disposition = r.headers.get("Content-Disposition");
  49. var filename = "time.log.tar.gz";
  50. if (disposition) {
  51. var match = disposition.match(/filename="?([^";]+)"?/);
  52. if (match) {
  53. filename = match[1];
  54. }
  55. }
  56. return r.blob().then(function (blob) {
  57. return { blob: blob, filename: filename };
  58. });
  59. })
  60. .then(function (res) {
  61. downloadBlobFile(res.blob, res.filename);
  62. alert("下载成功");
  63. })
  64. .catch(function (e) {
  65. console.error("download failed:", e);
  66. alert("下载失败: " + e.message);
  67. })
  68. .finally(function () {
  69. btn.disabled = false;
  70. btn.textContent = "下载";
  71. });
  72. }
  73. function restartTimeService(event) {
  74. if (!confirm("确定重启时间服务吗?")) {
  75. return;
  76. }
  77. var btn = event.currentTarget;
  78. btn.disabled = true;
  79. fetch("/api/time/restart", {
  80. method: "POST",
  81. })
  82. .then(function (r) {
  83. if (!r.ok) {
  84. return r.text().then(function (t) {
  85. throw new Error(t);
  86. });
  87. }
  88. alert("重启成功");
  89. })
  90. .catch(function (e) {
  91. console.error("restart failed:", e);
  92. alert("重启失败: " + e.message);
  93. })
  94. .finally(function () {
  95. btn.disabled = false;
  96. });
  97. }
  98. var timeStatusTimer = null;
  99. function getTimeStatus() {
  100. fetch("/api/time/status")
  101. .then(function (r) {
  102. if (!r.ok) {
  103. throw new Error("HTTP " + r.status);
  104. }
  105. return r.json();
  106. })
  107. .then(function (data) {
  108. ["var1", "var2", "var3", "var4", "var5", "var6"].forEach(function (id) {
  109. return setText(id, data[id], "page3");
  110. });
  111. })
  112. .catch(function (e) {
  113. console.error("getTimeStatus() failed:", e);
  114. ["var1", "var2", "var3", "var4", "var5", "var6"].forEach(function (id) {
  115. return setText(id, null, "page3");
  116. });
  117. });
  118. getTimeLogLevel();
  119. }
  120. function startTimeStatusRefresh() {
  121. if (timeStatusTimer) {
  122. return;
  123. }
  124. getTimeStatus();
  125. timeStatusTimer = setInterval(function () {
  126. getTimeStatus();
  127. }, 1000);
  128. }
  129. function stopTimeStatusRefresh() {
  130. if (timeStatusTimer) {
  131. clearInterval(timeStatusTimer);
  132. timeStatusTimer = null;
  133. }
  134. }
  135. function initPage3() {
  136. startTimeStatusRefresh();
  137. connectLog("yfkj-timesyncd.service", "text1");
  138. }
  139. function exitPage3() {
  140. stopTimeStatusRefresh();
  141. disconnectLog();
  142. }