page9.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 formatWorkTime(value) {
  154. if (value == null || value === "") {
  155. return "--";
  156. }
  157. value = String(value);
  158. if (/^\d{1,2}:\d{1,2}$/.test(value)) {
  159. var parts = value.split(":");
  160. return ("0" + parts[0]).slice(-2) + ":" +
  161. ("0" + parts[1]).slice(-2);
  162. }
  163. if (/^\d{1,2}$/.test(value)) {
  164. return ("0" + value).slice(-2) + ":00";
  165. }
  166. if (/^\d{3,4}$/.test(value)) {
  167. value = ("0000" + value).slice(-4);
  168. return value.substring(0, 2) + ":" + value.substring(2, 4);
  169. }
  170. return "--";
  171. }
  172. function getCBDWorkStatus() {
  173. fetch("/api/cbd/getWorkStatus")
  174. .then(function (r) {
  175. if (!r.ok) {
  176. throw new Error("HTTP " + r.status);
  177. }
  178. return r.json();
  179. })
  180. .then(function (data) {
  181. setText("text1", data.mcuLastActiveTime, "page9");
  182. setText("text2", data.dataReportOKTime, "page9");
  183. setText("text3", data.photoUploadOKTime, "page9");
  184. setText("runtimeStamp", data.stamp, "page9");
  185. setText("runtimeFirmwareVersion", data.firmwareVersion, "page9");
  186. setText("runtimeWorkMode", data.workMode, "page9");
  187. setText("runtimeUpds", data.upds, "page9");
  188. setText("runtimeDnds", data.dnds, "page9");
  189. setText("runtimeHs", data.hs, "page9");
  190. setText("runtimeTps", data.tps, "page9");
  191. setText("runtimeRps", data.rps, "page9");
  192. setText("runtimeLps", data.lps, "page9");
  193. setText("runtimeBatStatus", data.batStatus, "page9");
  194. setText("runtimeWs", data.ws, "page9");
  195. setText("runtimeWt", data.st && data.et ? formatWorkTime(data.st) + " - " + formatWorkTime(data.et) : "--", "page9");
  196. })
  197. .catch(function (e) {
  198. console.error("getCBDWorkStatus() failed:", e);
  199. setText("text1", null, "page9");
  200. setText("text2", null, "page9");
  201. setText("text3", null, "page9");
  202. setText("runtimeStamp", null, "page9");
  203. setText("runtimeFirmwareVersion", null, "page9");
  204. setText("runtimeWorkMode", null, "page9");
  205. setText("runtimeUpds", null, "page9");
  206. setText("runtimeDnds", null, "page9");
  207. setText("runtimeHs", null, "page9");
  208. setText("runtimeTps", null, "page9");
  209. setText("runtimeRps", null, "page9");
  210. setText("runtimeLps", null, "page9");
  211. setText("runtimeBatStatus", null, "page9");
  212. setText("runtimeWs", null, "page9");
  213. setText("runtimeWt", null, "page9");
  214. });
  215. getCBDLogLevel();
  216. }
  217. function startCBDWorkStatusRefresh() {
  218. if (cbdWorkStatusTimer) {
  219. return;
  220. }
  221. getCBDWorkStatus();
  222. cbdWorkStatusTimer = setInterval(function () {
  223. getCBDWorkStatus();
  224. }, 1000);
  225. }
  226. function stopCBDWorkStatusRefresh() {
  227. if (cbdWorkStatusTimer) {
  228. clearInterval(cbdWorkStatusTimer);
  229. cbdWorkStatusTimer = null;
  230. }
  231. }
  232. function writeMCUData(data) {
  233. return fetch("/api/cbd/writeMCUData", {
  234. method: "POST",
  235. headers: { "Content-Type": "application/json" },
  236. body: JSON.stringify(data),
  237. }).then(function (r) {
  238. if (!r.ok) {
  239. return r.text().then(function (t) {
  240. throw new Error(t);
  241. });
  242. }
  243. return r.json();
  244. });
  245. }
  246. function sendP9CtrlCommand(type, name) {
  247. writeMCUData({
  248. cmd: "ctrl",
  249. ext: { type: type },
  250. })
  251. .then(function () {
  252. alert(name + "指令已发送");
  253. })
  254. .catch(function (err) {
  255. alert(name + "指令发送失败:" + err.message);
  256. });
  257. }
  258. function onP9Btn01Click() {
  259. sendP9CtrlCommand("lamp_on", "打开灯管");
  260. }
  261. function onP9Btn02Click() {
  262. sendP9CtrlCommand("lamp_off", "关闭灯管");
  263. }
  264. function onP9Btn03Click() {
  265. sendP9CtrlCommand("led_on", "打开补光");
  266. }
  267. function onP9Btn04Click() {
  268. sendP9CtrlCommand("led_off", "关闭补光");
  269. }
  270. function onP9Btn05Click() {
  271. sendP9CtrlCommand("up_door_open", "打开上仓");
  272. }
  273. function onP9Btn06Click() {
  274. sendP9CtrlCommand("up_door_close", "关闭上仓");
  275. }
  276. function onP9Btn07Click() {
  277. sendP9CtrlCommand("down_door_open", "打开下仓");
  278. }
  279. function onP9Btn08Click() {
  280. sendP9CtrlCommand("down_door_close", "关闭下仓");
  281. }
  282. function onP9Btn09Click() {
  283. sendP9CtrlCommand("heat_on", "开始加热");
  284. }
  285. function onP9Btn10Click() {
  286. sendP9CtrlCommand("heat_off", "停止加热");
  287. }
  288. function onP9Btn11Click() {
  289. sendP9CtrlCommand("clear_on", "执行清扫");
  290. }
  291. function onP9Btn12Click() {
  292. writeMCUData({ cmd: "takephoto" })
  293. .then(function () {
  294. alert("测试拍照指令已发送");
  295. })
  296. .catch(function (err) {
  297. alert("测试拍照指令发送失败:" + err.message);
  298. });
  299. }
  300. function refreshCBDRunStatus() {
  301. writeMCUData({ cmd: "read" })
  302. .catch(function (err) {
  303. console.error("refreshCBDRunStatus() failed:", err);
  304. alert("刷新运行状态失败:" + err.message);
  305. });
  306. }
  307. function disablePUpgradeButtons() {
  308. document.getElementById("hmiFile").disabled = true;
  309. document.getElementById("hmiUploadBtn").disabled = true;
  310. document.getElementById("mcuFile").disabled = true;
  311. document.getElementById("mcuUploadBtn").disabled = true;
  312. }
  313. function restorePUpgradeButtons() {
  314. document.getElementById("hmiFile").disabled = false;
  315. document.getElementById("hmiUploadBtn").disabled = false;
  316. document.getElementById("mcuFile").disabled = false;
  317. document.getElementById("mcuUploadBtn").disabled = false;
  318. }
  319. function enablePUpgradeButtons() {
  320. var password = prompt("请输入操作密码:");
  321. if (password === null) {
  322. return;
  323. }
  324. fetch("/api/cbd/unlock", {
  325. method: "POST",
  326. headers: {
  327. "X-Operation-Password": password
  328. }
  329. })
  330. .then(function (r) {
  331. if (!r.ok) {
  332. throw new Error(r.status === 401 ? "操作密码错误!" : "HTTP " + r.status);
  333. }
  334. restorePUpgradeButtons();
  335. document.getElementById("p9UpgradeAuthBtn").disabled = true;
  336. })
  337. .catch(function (e) {
  338. alert(e.message);
  339. });
  340. }
  341. function onFirmwareFileChange(type) {
  342. var file = document.getElementById(type + "File").files[0];
  343. var status = document.getElementById(type + "Status");
  344. status.textContent = file ? "已选择:" + file.name : "等待选择文件";
  345. }
  346. function uploadFirmware(type) {
  347. if (type !== "hmi" && type !== "mcu") {
  348. return;
  349. }
  350. var fileInput = document.getElementById(type + "File");
  351. var status = document.getElementById(type + "Status");
  352. var file = fileInput.files[0];
  353. if (!file) {
  354. alert("请先选择固件文件");
  355. return;
  356. }
  357. var suffix = type === "hmi" ? ".zip" : ".rbl";
  358. if (file.name.toLowerCase().slice(-suffix.length) !== suffix) {
  359. alert("固件文件类型错误,只允许上传 " + suffix + " 文件");
  360. return;
  361. }
  362. if (file.size > 100 * 1024 * 1024) {
  363. alert("固件文件不能超过 100MB");
  364. return;
  365. }
  366. var name = type === "hmi" ? "串口屏" : "控制板";
  367. if (!confirm("确定升级" + name + "吗?")) {
  368. return;
  369. }
  370. disablePUpgradeButtons();
  371. var formData = new FormData();
  372. formData.append("file", file);
  373. var xhr = new XMLHttpRequest();
  374. xhr.open("POST", "/api/cbd/uploadFirmware?type=" + type, true);
  375. xhr.timeout = 600000; // 10分钟
  376. status.textContent = "正在上传:0%";
  377. xhr.upload.onprogress = function (e) {
  378. if (e.lengthComputable) {
  379. var percent = Math.round(e.loaded / e.total * 100);
  380. status.textContent = "正在上传:" + percent + "%";
  381. }
  382. };
  383. xhr.upload.onload = function () {
  384. status.textContent = "文件上传完成,正在执行固件升级...";
  385. };
  386. xhr.onload = function () {
  387. if (xhr.status >= 200 && xhr.status < 300) {
  388. try {
  389. var data = JSON.parse(xhr.responseText);
  390. if (data.result !== "success") {
  391. throw new Error("返回结果异常");
  392. }
  393. status.textContent = "升级指令已发送,请等待升级完成";
  394. } catch (e) {
  395. status.textContent = "升级请求失败:" + e.message;
  396. }
  397. } else {
  398. status.textContent = "升级请求失败:" + (xhr.responseText || "HTTP " + xhr.status);
  399. }
  400. };
  401. xhr.onerror = function () {
  402. status.textContent = "升级请求失败,请检查网络连接";
  403. };
  404. xhr.ontimeout = function () {
  405. status.textContent = "升级请求超时,请稍后重新尝试";
  406. };
  407. xhr.onloadend = function () {
  408. restorePUpgradeButtons();
  409. };
  410. xhr.send(formData);
  411. }
  412. function initPage9() {
  413. disableP9CtrlCmdButtons();
  414. disablePUpgradeButtons();
  415. startCBDWorkStatusRefresh();
  416. connectLog("yfkj-user-app.service", "text4");
  417. }
  418. function exitPage9() {
  419. stopCBDWorkStatusRefresh();
  420. disconnectLog();
  421. }