page4.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(event) {
  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(event) {
  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. ["var1", "var2", "var3"].forEach(id => {
  111. setText(id, data[id], "page4");
  112. });
  113. if (data.var1 == "--") {
  114. setText("var4", "--");
  115. } else {
  116. const located = data.var2 !== "" && data.var3 !== "" && data.var2 !== "--" && data.var3 !== "--";
  117. setText("var4", located ? "已定位" : "定位中", "page4");
  118. }
  119. })
  120. .catch(e => {
  121. console.error("getGnssStatus() failed:", e);
  122. ["var1", "var2", "var3", "var4"].forEach(id => {
  123. setText(id, null, "page4");
  124. });
  125. });
  126. getGnssLogLevel();
  127. }
  128. function startGnssStatusRefresh() {
  129. if (gnssStatusTimer) {
  130. return;
  131. }
  132. getGnssStatus();
  133. gnssStatusTimer = setInterval(() => {
  134. getGnssStatus();
  135. }, 5000);
  136. }
  137. function stopGnssStatusRefresh() {
  138. if (gnssStatusTimer) {
  139. clearInterval(gnssStatusTimer);
  140. gnssStatusTimer = null;
  141. }
  142. }
  143. function initPage4() {
  144. startGnssStatusRefresh()
  145. connectLog("yfkj-gnss.service", "text1");
  146. }
  147. function exitPage4() {
  148. stopGnssStatusRefresh()
  149. disconnectLog();
  150. }