debugger.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. let socket = null, socketHandler = {},
  2. frontTimer = null, backTimer = null, Timeout = 500,
  3. DevicesAll = {}, DevicesShow = {},
  4. DeviceNow = {
  5. seq: "", online: false, location: "", vpp1: 0, vpp2: 0, vpp3: 0, vpp4: 0
  6. };
  7. const strBase = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789", strLen = 62;
  8. const randStr = len => {
  9. let res = "";
  10. for (let i = 0; i < len; i++) res += strBase[Math.floor(Math.random() * strLen)];
  11. return res;
  12. }, createDeviceNode = (seq, online) => {
  13. let $device = document.createElement("div"),
  14. $seq = document.createElement("span"),
  15. $status = document.createElement("span");
  16. $device.setAttribute("rel", seq);
  17. $device.className = "device " + (online ? "online" : "offline");
  18. $seq.className = "seq";
  19. $seq.innerText = seq;
  20. $status.className = "status";
  21. $status.innerText = online ? "在线" : "离线";
  22. $device.appendChild($seq);
  23. $device.appendChild($status);
  24. return $device;
  25. }, connectSocket = () => {
  26. const did = randStr(16);
  27. socket = new WebSocket(`${Url}/${did}`);
  28. socket.onopen = () => {
  29. setInterval(() => sendEvent("pin", null), 50 * 1000);
  30. sendEvent("listDevices", null);
  31. }
  32. socket.onmessage = data => {
  33. let body = JSON.parse(data.data);
  34. socketHandler[body.event] && socketHandler[body.event](body.data);
  35. }
  36. socket.onclose = e => {
  37. socket = null;
  38. if (e.code === 1006) connectSocket();
  39. }
  40. }, sendEvent = (event, data) => {
  41. if (socket !== null) socket.send(JSON.stringify({event, data}));
  42. else setTimeout(() => sendEvent(event, data), 1000);
  43. };
  44. window.onload = () => {
  45. const $searchIcon = document.getElementById("search-icon"),
  46. $backIcon = document.getElementById("back-icon"),
  47. $detailPage = document.getElementById("detail-page"),
  48. $seqInput = document.getElementById("seq-input"),
  49. $devices = document.getElementById("devices"),
  50. $detailTitle = document.getElementById("seq-text"),
  51. $updateLocBtn = document.getElementById("update-loc"),
  52. $locationInput = document.getElementById("location"),
  53. $vpps = [
  54. [document.getElementById("vpp1"), document.getElementById("update-vpp1")],
  55. [document.getElementById("vpp2"), document.getElementById("update-vpp2")],
  56. [document.getElementById("vpp3"), document.getElementById("update-vpp3")],
  57. [document.getElementById("vpp4"), document.getElementById("update-vpp4")]
  58. ],
  59. $openFront = document.getElementById("open-front"),
  60. $openBack = document.getElementById("open-back"),
  61. $openDebug = document.getElementById("open-debug"),
  62. $stopDebug = document.getElementById("stop-debug"),
  63. $msgBody = document.getElementById("msg-body");
  64. const filterAndShow = seq => {
  65. $devices.innerHTML = "";
  66. for (let key in DevicesAll) if (key.includes(seq)) DevicesShow[key] = DevicesAll[key];
  67. for (let key in DevicesShow) {
  68. let $device = createDeviceNode(key, DevicesShow[key].online);
  69. $device.onclick = function () {
  70. DeviceNow = DevicesShow[this.getAttribute("rel")];
  71. $detailTitle.innerText = DeviceNow.seq;
  72. $locationInput.value = DeviceNow.location;
  73. $vpps[0][0].value = DeviceNow.vpp1;
  74. $vpps[1][0].value = DeviceNow.vpp2;
  75. $vpps[2][0].value = DeviceNow.vpp3;
  76. $vpps[3][0].value = DeviceNow.vpp4;
  77. $detailPage.className = "slide-in";
  78. }
  79. $devices.appendChild($device);
  80. }
  81. };
  82. $searchIcon.setAttribute("src", `${Prefix}/icon/search.svg`);
  83. $backIcon.setAttribute("src", `${Prefix}/icon/back.svg`);
  84. $backIcon.onclick = () => $detailPage.className = "slide-out";
  85. $seqInput.onchange = () => filterAndShow($seqInput.value.trim());
  86. $updateLocBtn.onclick = () => {
  87. let loc = $locationInput.value.trim();
  88. if (loc === "") return alert("location is blank");
  89. sendEvent("setLocation", {seq: DeviceNow.seq, loc: loc});
  90. }
  91. $vpps.forEach((pair, index) => {
  92. pair[1].onclick = function () {
  93. let val = +pair[0].value;
  94. if (val === undefined || val < 1) return alert("require: vpp > 0");
  95. console.log({seq: DeviceNow.seq, index: index, value: val});
  96. sendEvent("setVpp", {seq: DeviceNow.seq, index: index, value: val});
  97. }
  98. });
  99. $openFront.onclick = () => {
  100. if (frontTimer !== null) return;
  101. frontTimer = setTimeout(() => {
  102. clearTimeout(frontTimer);
  103. frontTimer = null;
  104. }, Timeout);
  105. sendEvent("openGate", {seq: DeviceNow.seq, kind: "Changing"});
  106. }
  107. $openBack.onclick = () => {
  108. if (backTimer !== null) return;
  109. backTimer = setTimeout(() => {
  110. clearTimeout(backTimer);
  111. backTimer = null;
  112. }, Timeout);
  113. sendEvent("openGate", {seq: DeviceNow.seq, kind: "Fixing"});
  114. }
  115. $openDebug.onclick = () => sendEvent("setDebug", {seq: DeviceNow.seq, debug: true});
  116. $stopDebug.onclick = () => sendEvent("setDebug", {seq: DeviceNow.seq, debug: false});
  117. socketHandler.__Error_Event__ = msg => alert(msg);
  118. socketHandler.devices = data => {
  119. DevicesAll = data;
  120. filterAndShow("");
  121. }
  122. socketHandler.newDevice = device => {
  123. DevicesAll[device.seq] = device;
  124. filterAndShow($seqInput.value.trim());
  125. }
  126. socketHandler.deviceStatusChange = data => {
  127. DevicesAll[data.seq].online = data.online;
  128. filterAndShow($seqInput.value.trim());
  129. }
  130. socketHandler.authCode = event => {
  131. if (event.status) $msgBody.innerText = `授权码:${event.data}`;
  132. else alert(event.msg);
  133. }
  134. socketHandler.openResult = data => $msgBody.innerText = data.type + (data.result ? "-成功" : "-失败");
  135. socketHandler.workFinished = data => $msgBody.innerText = data;
  136. connectSocket();
  137. }