helper.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //此文件实现将控制命令封装成协议,具体协议内容请看:
  2. //链接:http://note.youdao.com/noteshare?id=dabda6c613adef7a416bd2625cd770a1
  3. //bcc校验码计算
  4. //arry: 要计算的数组
  5. //返回计算协议中校验位的校验码
  6. function calBcc(arry) {
  7. var bcc = 0;
  8. for (i = 0; i < arry.length; i++) {
  9. bcc ^= arry[i];
  10. }
  11. return bcc;
  12. }
  13. //数组打印,调试用
  14. function PrintArry(data) {
  15. var str = "";
  16. for (i = 0; i < data.length; i++) {
  17. str = str + data[i].toString(16).padStart(2, '0');
  18. }
  19. str = str.toUpperCase();
  20. return str;
  21. }
  22. //sn:板卡sn号,stirng
  23. //type:数据类型,数字
  24. //jsonCmd: json命令
  25. //返回值:生成一个Uint8Array,通过websocket发送给板卡
  26. function makeFrame(sn, dataType, jsonCmd) {
  27. var index = 0;
  28. var dataLen = jsonCmd.length;
  29. var frameLen = dataLen + 26;
  30. var outPut = new Uint8Array(frameLen);
  31. outPut[index++] = 0x68;
  32. outPut[index++] = (dataLen & 0xff000000) >> 24;
  33. outPut[index++] = (dataLen & 0x00ff0000) >> 16;
  34. outPut[index++] = (dataLen & 0x0000ff00) >> 8;
  35. outPut[index++] = dataLen & 0x000000ff;
  36. outPut[index++] = 0; //类型为client
  37. //sn号赋值,string转ascii
  38. for (i = 0; i < sn.length; i++) {
  39. outPut[index++] = sn[i].charCodeAt();
  40. }
  41. outPut[index++] = dataType; //指定数据类型为json
  42. //json string转ascii
  43. for (i = 0; i < jsonCmd.length; i++) {
  44. outPut[index++] = jsonCmd[i].charCodeAt();
  45. }
  46. var bccBuffer = outPut.slice(1, frameLen - 3 + 1); //忽略协议头和协议尾
  47. outPut[index++] = calBcc(bccBuffer);
  48. outPut[index++] = 0x16;
  49. // console.log("打印数组:%s", PrintArry(outPut));
  50. console.log("数组长度:%d", outPut.length);
  51. //return PrintArry(outPut);
  52. return outPut;
  53. }
  54. //触发键盘事件, code表示键盘值
  55. function ExexuteKeyDown(code, sn) {
  56. var jsonObj = {
  57. "data": {
  58. "keyCode": code,
  59. "event": "keyDown"
  60. }
  61. };
  62. var json = JSON.stringify(jsonObj);
  63. console.log("json==================", json);
  64. // var sn = "RK3923C1201900139";
  65. return makeFrame(sn, 0, json);
  66. }
  67. //触发鼠标按下事件,x:x坐标, y:y坐标
  68. function ExexuteMouseDown(x, y, sn) {
  69. var jsonObj = {
  70. "data": {
  71. "action": 0,
  72. "count": 1,
  73. "pointerId": 0,
  74. "x": x,
  75. "y": y
  76. },
  77. "event": "0"
  78. };
  79. var json = JSON.stringify(jsonObj);
  80. console.log("json==================", json);
  81. // var sn = "RK3923C1201900139";
  82. return makeFrame(sn, 0, json);
  83. }
  84. //触发鼠标移动事件,x:x坐标, y:y坐标
  85. function ExexuteMouseMove(x, y, sn) {
  86. var jsonObj = {
  87. "data": {
  88. "action": 2,
  89. "count": 1,
  90. "pointerId": 0,
  91. "x": x,
  92. "y": y
  93. },
  94. "event": "2"
  95. };
  96. var json = JSON.stringify(jsonObj);
  97. // var sn = "RK3923C1201900139";
  98. return makeFrame(sn, 0, json);
  99. }
  100. function ExexuteKeyBoard(keycode, sn) {
  101. var jsonObj = {
  102. "data": {
  103. "keyCode": keycode.toString()
  104. },
  105. "event": "keyCode"
  106. };
  107. var json = JSON.stringify(jsonObj);
  108. // var sn = "RK3923C1201900139";
  109. return makeFrame(sn, 0, json);
  110. }
  111. //触发鼠标抬起事件,x:x坐标, y:y坐标
  112. function ExexuteMouseUp(x, y, sn) {
  113. var jsonObj = {
  114. "data": {
  115. "action": 1,
  116. "count": 1,
  117. "pointerId": 0,
  118. "x": x,
  119. "y": y
  120. },
  121. "event": "1"
  122. };
  123. var json = JSON.stringify(jsonObj);
  124. // var sn = "RK3923C1201900139";
  125. return makeFrame(sn, 0, json);
  126. }
  127. //触发滑动事件
  128. function ExexuteMove(data, sn) {
  129. // var jsonObj = {"data":{"action":1, "count":1, "pointerId":0,"x":x, "y":y}, "event":"1"};
  130. // var json = JSON.stringify(jsonObj);
  131. // var sn = "RK3923C1201900139";
  132. return makeFrame(sn, 0, data);
  133. }
  134. //示例:
  135. //var sn = "RK3923C1201900139";
  136. //var json = "{\"data\":{\"keyCode\":24},\"type\":\"keyDown\"}";
  137. // makeFrame(sn, 0, json);
  138. //ExexuteKeyDown()
  139. // 加个请求I帧的报文
  140. function RequestIFrame(sn) {
  141. console.log('加个请求I帧的报文')
  142. var outPut = new Uint8Array([0x20])
  143. return makeFrameExtend(sn, 6, outPut)
  144. }
  145. function makeFrameExtend(sn, dataType, body) {
  146. var index = 0
  147. var dataLen = body.length
  148. var frameLen = dataLen + 26
  149. var outPut = new Uint8Array(frameLen)
  150. outPut[index++] = 0x68
  151. outPut[index++] = (dataLen & 0xff000000) >> 24
  152. outPut[index++] = (dataLen & 0x00ff0000) >> 16
  153. outPut[index++] = (dataLen & 0x0000ff00) >> 8
  154. outPut[index++] = dataLen & 0x000000ff
  155. outPut[index++] = 0 // 类型为client
  156. // sn号赋值,string转ascii
  157. for (let i = 0; i < sn.length; i++) {
  158. outPut[index++] = sn[i].charCodeAt()
  159. }
  160. outPut[index++] = dataType // 指定数据类型为json
  161. // json string转ascii
  162. for (let i = 0; i < body.length; i++) {
  163. outPut[index++] = body[i]
  164. }
  165. var bccBuffer = outPut.slice(1, frameLen - 3 + 1) // 忽略协议头和协议尾
  166. outPut[index++] = calBcc(bccBuffer)
  167. outPut[index++] = 0x16
  168. return outPut
  169. }
  170. function ConfigChannel(sn) {
  171. var outPut = new Uint8Array([0x07])
  172. return makeFrameExtend(sn, 6, outPut)
  173. }