page9.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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("p9CtrlCmdAuthBtn", 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. showJsonDlg("服务器配置", 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 writeMCUData(data) {
  190. return fetch("/api/cbd/writeMCUData", {
  191. method: "POST",
  192. headers: { "Content-Type": "application/json" },
  193. body: JSON.stringify(data),
  194. }).then(function (r) {
  195. if (!r.ok) {
  196. return r.text().then(function (t) {
  197. throw new Error(t);
  198. });
  199. }
  200. return r.json();
  201. });
  202. }
  203. function sendP9CtrlCommand(type, name) {
  204. writeMCUData({
  205. cmd: "ctrl",
  206. ext: { type: type },
  207. })
  208. .then(function () {
  209. alert(name + "指令已发送");
  210. })
  211. .catch(function (err) {
  212. alert(name + "指令发送失败:" + err.message);
  213. });
  214. }
  215. function onP9Btn01Click() {
  216. sendP9CtrlCommand("lamp_on", "打开灯管");
  217. }
  218. function onP9Btn02Click() {
  219. sendP9CtrlCommand("lamp_off", "关闭灯管");
  220. }
  221. function onP9Btn03Click() {
  222. sendP9CtrlCommand("led_on", "打开补光");
  223. }
  224. function onP9Btn04Click() {
  225. sendP9CtrlCommand("led_off", "关闭补光");
  226. }
  227. function onP9Btn05Click() {
  228. sendP9CtrlCommand("up_door_open", "打开上仓");
  229. }
  230. function onP9Btn06Click() {
  231. sendP9CtrlCommand("up_door_close", "关闭上仓");
  232. }
  233. function onP9Btn07Click() {
  234. sendP9CtrlCommand("down_door_open", "打开下仓");
  235. }
  236. function onP9Btn08Click() {
  237. sendP9CtrlCommand("down_door_close", "关闭下仓");
  238. }
  239. function onP9Btn09Click() {
  240. sendP9CtrlCommand("heat_on", "开始加热");
  241. }
  242. function onP9Btn10Click() {
  243. sendP9CtrlCommand("heat_off", "停止加热");
  244. }
  245. function onP9Btn11Click() {
  246. sendP9CtrlCommand("clear_on", "执行清扫");
  247. }
  248. function onP9Btn12Click() {
  249. writeMCUData({ cmd: "takephoto" })
  250. .then(function () {
  251. alert("测试拍照指令已发送");
  252. })
  253. .catch(function (err) {
  254. alert("测试拍照指令发送失败:" + err.message);
  255. });
  256. }
  257. function initPage9() {
  258. disableP9CtrlCmdButtons();
  259. startCBDWorkStatusRefresh();
  260. connectLog("yfkj-user-app.service", "text5");
  261. }
  262. function exitPage9() {
  263. stopCBDWorkStatusRefresh();
  264. disconnectLog();
  265. }