page9.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. var cbdLogLevel = "";
  2. function setCBDLogLevel() {
  3. var level = logLevelP9.value;
  4. fetch("/api/cbd/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. cbdLogLevel = level;
  16. })
  17. .catch(function (e) {
  18. logLevelP9.value = cbdLogLevel;
  19. });
  20. }
  21. function getCBDLogLevel() {
  22. fetch("/api/cbd/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. cbdLogLevel = d.log_level;
  33. logLevelP9.value = d.log_level;
  34. })
  35. .catch(function (e) {
  36. cbdLogLevel = "";
  37. logLevelP9.value = "";
  38. });
  39. }
  40. function downloadCBDLogFile(event) {
  41. var btn = event.currentTarget;
  42. btn.disabled = true;
  43. fetch("/api/cbd/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 = "cbd.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 restartCBDService(event) {
  74. if (!confirm("确定重启用户应用吗?")) {
  75. return;
  76. }
  77. var btn = event.currentTarget;
  78. btn.disabled = true;
  79. fetch("/api/user-app/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. function disableP9CtrlCmdButtons() {
  99. for (var i = 1; i <= 12; i++) {
  100. setBtnStatus("p9btn" + (i < 10 ? "0" : "") + i, false, "page9");
  101. }
  102. }
  103. function enableP9CtrlCmdButtons() {
  104. var password = prompt("请输入操作密码:");
  105. if (password === null) {
  106. return;
  107. }
  108. fetch("/api/cbd/unlock", {
  109. method: "POST",
  110. headers: {
  111. "Content-Type": "application/json",
  112. "X-Operation-Password": password
  113. }
  114. })
  115. .then(function (r) {
  116. if (!r.ok) {
  117. throw new Error(r.status === 401 ? "操作密码错误!" : "HTTP " + r.status);
  118. }
  119. for (var i = 1; i <= 12; i++) {
  120. setBtnStatus("p9btn" + (i < 10 ? "0" : "") + i, true, "page9");
  121. }
  122. setBtnStatus("p9AuthBtn", false, "page9");
  123. })
  124. .catch(function (e) {
  125. alert(e.message);
  126. });
  127. }
  128. function viewCBDServerConfig() {
  129. var password = prompt("请输入操作密码:");
  130. if (password === null) {
  131. return;
  132. }
  133. fetch("/api/cbd/serverCfg", {
  134. headers: {
  135. "X-Operation-Password": password
  136. }
  137. })
  138. .then(function (r) {
  139. if (!r.ok) {
  140. throw new Error(r.status === 401 ? "操作密码错误!" : "HTTP " + r.status);
  141. }
  142. return r.json();
  143. })
  144. .then(function (data) {
  145. showJsonView("服务器配置", data);
  146. })
  147. .catch(function (e) {
  148. console.error("viewCBDServerConfig() failed:", e);
  149. alert("获取服务器配置失败:" + e.message);
  150. });
  151. }
  152. var cbdWorkStatusTimer = null;
  153. function getCBDWorkStatus() {
  154. fetch("/api/cbd/getWorkStatus")
  155. .then(function (r) {
  156. if (!r.ok) {
  157. throw new Error("HTTP " + r.status);
  158. }
  159. return r.json();
  160. })
  161. .then(function (data) {
  162. setText("text1", data.mcuLastActiveTime, "page9");
  163. setText("text2", data.dataReportOKTime, "page9");
  164. setText("text3", data.photoUploadOKTime, "page9");
  165. })
  166. .catch(function (e) {
  167. console.error("getCBDWorkStatus() failed:", e);
  168. setText("text1", null, "page9");
  169. setText("text2", null, "page9");
  170. setText("text3", null, "page9");
  171. });
  172. getCBDLogLevel();
  173. }
  174. function startCBDWorkStatusRefresh() {
  175. if (cbdWorkStatusTimer) {
  176. return;
  177. }
  178. getCBDWorkStatus();
  179. cbdWorkStatusTimer = setInterval(function () {
  180. getCBDWorkStatus();
  181. }, 1000);
  182. }
  183. function stopCBDWorkStatusRefresh() {
  184. if (cbdWorkStatusTimer) {
  185. clearInterval(cbdWorkStatusTimer);
  186. cbdWorkStatusTimer = null;
  187. }
  188. }
  189. function initPage9() {
  190. disableP9CtrlCmdButtons();
  191. startCBDWorkStatusRefresh();
  192. connectLog("yfkj-user-app.service", "text5");
  193. }
  194. function exitPage9() {
  195. stopCBDWorkStatusRefresh();
  196. disconnectLog();
  197. }