helper.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. return outPut;
  50. }
  51. //触发键盘事件, code表示键盘值
  52. function ExexuteKeyDown(code) {
  53. var jsonObj = { "data": { "keyCode": code, "event": "keyDown" } };
  54. var json = JSON.stringify(jsonObj);
  55. var sn = "RK3923C1201900139";
  56. return json;
  57. }
  58. //触发鼠标按下事件,x:x坐标, y:y坐标
  59. function ExexuteMouseDown(x, y) {
  60. var jsonObj = { "data": { "action": 0, "count": 1, "pointerId": 0, "x": x, "y": y }, "event": "0" };
  61. var json = JSON.stringify(jsonObj);
  62. var sn = "RK3923C1201900139";
  63. return json;
  64. }
  65. //触发鼠标移动事件,x:x坐标, y:y坐标
  66. function ExexuteMouseMove(x, y) {
  67. var jsonObj = { "data": { "action": 2, "count": 1, "pointerId": 0, "x": x, "y": y }, "event": "2" };
  68. var json = JSON.stringify(jsonObj);
  69. var sn = "RK3923C1201900139";
  70. return json;
  71. }
  72. function ExexuteKeyBoard(keycode) {
  73. var jsonObj = { "data": { "keyCode": keycode.toString() }, "event": "keyCode" };
  74. var json = JSON.stringify(jsonObj);
  75. var sn = "RK3923C1201900139";
  76. return json;
  77. }
  78. //触发鼠标抬起事件,x:x坐标, y:y坐标
  79. function ExexuteMouseUp(x, y) {
  80. var jsonObj = { "data": { "action": 1, "count": 1, "pointerId": 0, "x": x, "y": y }, "event": "1" };
  81. var json = JSON.stringify(jsonObj);
  82. var sn = "RK3923C1201900139";
  83. return json;
  84. }
  85. //触发滑动事件
  86. function ExexuteMove(data, sn) {
  87. return makeFrame(sn, 0, data);
  88. }
  89. function makeFrameExtend(sn, dataType, body) {
  90. var index = 0;
  91. var dataLen = body.length;
  92. var frameLen = dataLen + 26;
  93. var outPut = new Uint8Array(frameLen);
  94. outPut[index++] = 0x68;
  95. outPut[index++] = (dataLen & 0xff000000) >> 24;
  96. outPut[index++] = (dataLen & 0x00ff0000) >> 16;
  97. outPut[index++] = (dataLen & 0x0000ff00) >> 8;
  98. outPut[index++] = dataLen & 0x000000ff;
  99. outPut[index++] = 0; // 类型为client
  100. //sn号赋值,string转ascii
  101. for (i = 0; i < sn.length; i++) {
  102. outPut[index++] = sn[i].charCodeAt();
  103. }
  104. outPut[index++] = dataType; // 指定数据类型为json
  105. //json string转ascii
  106. for (i = 0; i < body.length; i++) {
  107. outPut[index++] = body[i];
  108. }
  109. var bccBuffer = outPut.slice(1, frameLen - 3 + 1);//忽略协议头和协议尾
  110. outPut[index++] = calBcc(bccBuffer);
  111. outPut[index++] = 0x16;
  112. return outPut;
  113. }
  114. //根据报文识别屏幕方向, 0横屏,1竖屏
  115. function CheckScreenDirection(data) {
  116. if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1) {
  117. if (data[4] == 1 && data[5] == 1) {
  118. if (data[6] == 1) {
  119. screen = data[7];
  120. return screen;
  121. }
  122. }
  123. }
  124. }
  125. var emptyCount = 0;
  126. function GetEmptyFrame() {
  127. var emptyFrame = new Uint8Array([0xFF, 0xF1, 0x50, 0x80, 0x12, 0x5F, 0xFC, 0x21, 0x1A, 0xC8, 0x01, 0x27, 0xFC, 0xC0, 0x00, 0x7E, 0x03, 0x10, 0x40, 0x63, 0x3D, 0x77, 0xE2, 0xB6, 0xE3, 0x6E, 0x00, 0x37, 0x56, 0x78, 0xEB, 0x70, 0xAB, 0xC5, 0x58, 0x08, 0x59, 0x76, 0xF0, 0x47, 0x3D, 0x23, 0x6C, 0xA6, 0x2B, 0x59, 0x4E, 0x9C, 0xE0, 0x23, 0x1C, 0x2D, 0x74, 0xCB, 0xE2, 0xFC, 0x77, 0x7D, 0x26, 0x13, 0xC3, 0x04, 0x40, 0x02, 0x60, 0xF6, 0x03, 0x20, 0x80, 0xC7, 0x9A, 0x11, 0x0E, 0x9B, 0xDA, 0xA0, 0x84, 0x00, 0x2A, 0x95, 0x4A, 0x1E, 0x74, 0xA5, 0x40, 0x2A, 0xCA, 0xA8, 0xCA, 0xF0, 0xF2, 0x1E, 0xA8, 0x77, 0x86, 0xA0, 0x62, 0x8C, 0xB8, 0x5F, 0xA6, 0x67, 0xBF, 0x0D, 0x27, 0x8B, 0xF9, 0x58, 0xBD, 0xE3, 0x2D, 0x0C, 0xBF, 0x48, 0x3C, 0xFD, 0x70, 0x78, 0x5E, 0xA9, 0x0B, 0x24, 0x9C, 0x13, 0x98, 0xA4, 0xA0, 0x6E, 0xCA, 0xAA, 0x7A, 0x88, 0xA5, 0x0C, 0x2E, 0x83, 0x59, 0x02, 0x24, 0x01, 0x41, 0x03, 0x92, 0x10, 0x40, 0x07])
  128. return emptyFrame;
  129. }
  130. //查询屏幕方向
  131. function GetScreenState() {
  132. var sn = "RK3923C1201900139";
  133. var outPut = new Uint8Array([0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x02]);
  134. return makeFrameExtend(sn, 5, outPut);
  135. }
  136. //通道配置
  137. function ConfigChannel(sn) {
  138. var outPut = new Uint8Array([0x07])
  139. return makeFrameExtend(sn, 6, outPut)
  140. }
  141. function OpenFileLog(sn) {
  142. var outPut = new Uint8Array([0x01]);
  143. return makeFrameExtend(sn, 7, outPut);
  144. }
  145. //I 帧请求报文生成
  146. function RequestIFrame() {
  147. var sn = "RK3923C1201900139";
  148. var outPut = new Uint8Array([0x20]);
  149. return makeFrameExtend(sn, 6, outPut);
  150. }
  151. //示例:
  152. //var sn = "RK3923C1201900139";
  153. //var json = "{\"data\":{\"keyCode\":24},\"type\":\"keyDown\"}";
  154. // makeFrame(sn, 0, json);
  155. //ExexuteKeyDown()