page4.js 3.4 KB

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