page9.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 disablePUpgradeButtons() {
  258. document.getElementById("hmiFile").disabled = true;
  259. document.getElementById("hmiUploadBtn").disabled = true;
  260. document.getElementById("mcuFile").disabled = true;
  261. document.getElementById("mcuUploadBtn").disabled = true;
  262. }
  263. function restorePUpgradeButtons() {
  264. document.getElementById("hmiFile").disabled = false;
  265. document.getElementById("hmiUploadBtn").disabled = false;
  266. document.getElementById("mcuFile").disabled = false;
  267. document.getElementById("mcuUploadBtn").disabled = false;
  268. }
  269. function enablePUpgradeButtons() {
  270. var password = prompt("请输入操作密码:");
  271. if (password === null) {
  272. return;
  273. }
  274. fetch("/api/cbd/unlock", {
  275. method: "POST",
  276. headers: {
  277. "X-Operation-Password": password
  278. }
  279. })
  280. .then(function (r) {
  281. if (!r.ok) {
  282. throw new Error(r.status === 401 ? "操作密码错误!" : "HTTP " + r.status);
  283. }
  284. restorePUpgradeButtons();
  285. document.getElementById("p9UpgradeAuthBtn").disabled = true;
  286. })
  287. .catch(function (e) {
  288. alert(e.message);
  289. });
  290. }
  291. function onFirmwareFileChange(type) {
  292. var file = document.getElementById(type + "File").files[0];
  293. var status = document.getElementById(type + "Status");
  294. status.textContent = file ? "已选择:" + file.name : "等待选择文件";
  295. }
  296. function uploadFirmware(type) {
  297. if (type !== "hmi" && type !== "mcu") {
  298. return;
  299. }
  300. var fileInput = document.getElementById(type + "File");
  301. var status = document.getElementById(type + "Status");
  302. var file = fileInput.files[0];
  303. if (!file) {
  304. alert("请先选择固件文件");
  305. return;
  306. }
  307. var suffix = type === "hmi" ? ".zip" : ".rbl";
  308. if (file.name.toLowerCase().slice(-suffix.length) !== suffix) {
  309. alert("固件文件类型错误,只允许上传 " + suffix + " 文件");
  310. return;
  311. }
  312. if (file.size > 100 * 1024 * 1024) {
  313. alert("固件文件不能超过 100MB");
  314. return;
  315. }
  316. var name = type === "hmi" ? "串口屏" : "控制板";
  317. if (!confirm("确定升级" + name + "吗?")) {
  318. return;
  319. }
  320. disablePUpgradeButtons();
  321. var formData = new FormData();
  322. formData.append("file", file);
  323. var xhr = new XMLHttpRequest();
  324. xhr.open("POST", "/api/cbd/uploadFirmware?type=" + type, true);
  325. xhr.timeout = 600000; // 10分钟
  326. status.textContent = "正在上传:0%";
  327. xhr.upload.onprogress = function (e) {
  328. if (e.lengthComputable) {
  329. var percent = Math.round(e.loaded / e.total * 100);
  330. status.textContent = "正在上传:" + percent + "%";
  331. }
  332. };
  333. xhr.upload.onload = function () {
  334. status.textContent = "文件上传完成,正在执行固件升级...";
  335. };
  336. xhr.onload = function () {
  337. if (xhr.status >= 200 && xhr.status < 300) {
  338. try {
  339. var data = JSON.parse(xhr.responseText);
  340. if (data.result !== "success") {
  341. throw new Error("返回结果异常");
  342. }
  343. status.textContent = "升级指令已发送,请等待升级完成";
  344. } catch (e) {
  345. status.textContent = "升级请求失败:" + e.message;
  346. }
  347. } else {
  348. status.textContent = "升级请求失败:" + (xhr.responseText || "HTTP " + xhr.status);
  349. }
  350. };
  351. xhr.onerror = function () {
  352. status.textContent = "升级请求失败,请检查网络连接";
  353. };
  354. xhr.ontimeout = function () {
  355. status.textContent = "升级请求超时,请稍后重新尝试";
  356. };
  357. xhr.onloadend = function () {
  358. restorePUpgradeButtons();
  359. };
  360. xhr.send(formData);
  361. }
  362. function initPage9() {
  363. disableP9CtrlCmdButtons();
  364. disablePUpgradeButtons();
  365. startCBDWorkStatusRefresh();
  366. connectLog("yfkj-user-app.service", "text4");
  367. }
  368. function exitPage9() {
  369. stopCBDWorkStatusRefresh();
  370. disconnectLog();
  371. }