control.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * 此文件实现将控制命令封装成协议,具体协议内容请看:
  3. * 链接:http://note.youdao.com/noteshare?id=dabda6c613adef7a416bd2625cd770a1
  4. */
  5. /**
  6. * bcc校验码计算
  7. * arry: 要计算的数组
  8. * 返回计算协议中校验位的校验码
  9. */
  10. export function calBcc(arry) {
  11. var bcc = 0
  12. for (let i = 0; i < arry.length; i++) {
  13. bcc ^= arry[i]
  14. }
  15. return bcc
  16. }
  17. // 数组打印,调试用
  18. export function PrintArry(data) {
  19. var str = ''
  20. for (let i = 0; i < data.length; i++) {
  21. str = str + data[i].toString(16).padStart(2, '0')
  22. }
  23. str = str.toUpperCase()
  24. return str
  25. }
  26. /**
  27. * @param {*} sn: 板卡sn号,stirng
  28. * @param {*} dataType:数据类型,数字
  29. * @param {*} jsonCmd: json命令
  30. * 返回值:生成一个Uint8Array,通过websocket发送给板卡
  31. */
  32. export function makeFrame(sn, dataType, jsonCmd) {
  33. var index = 0
  34. var dataLen = jsonCmd.length
  35. var frameLen = dataLen + 26
  36. var outPut = new Uint8Array(frameLen)
  37. outPut[index++] = 0x68
  38. outPut[index++] = (dataLen & 0xff000000) >> 24
  39. outPut[index++] = (dataLen & 0x00ff0000) >> 16
  40. outPut[index++] = (dataLen & 0x0000ff00) >> 8
  41. outPut[index++] = dataLen & 0x000000ff
  42. outPut[index++] = 0 // 类型为client
  43. // sn号赋值,string转ascii
  44. for (let i = 0; i < sn.length; i++) {
  45. outPut[index++] = sn[i].charCodeAt()
  46. }
  47. outPut[index++] = dataType // 指定数据类型为json
  48. // json string转ascii
  49. for (let i = 0; i < jsonCmd.length; i++) {
  50. outPut[index++] = jsonCmd[i].charCodeAt()
  51. }
  52. var bccBuffer = outPut.slice(1, frameLen - 3 + 1) // 忽略协议头和协议尾
  53. outPut[index++] = calBcc(bccBuffer)
  54. outPut[index++] = 0x16
  55. return outPut
  56. }
  57. /**
  58. * 触发键盘事件, code表示键盘值
  59. * 音量加减、home键、back事件(keyCode的值分别表示为25减音量,24加音量,4为返回键,3为home键,187为切换菜单)
  60. */
  61. export function ExexuteKeyDown(code, sn) {
  62. var jsonObj = { 'data': { 'keyCode': code.toString() }, 'type': 'keyDown' }
  63. var json = JSON.stringify(jsonObj)
  64. return makeFrame(sn, 0, json)
  65. }
  66. // 触发鼠标按下事件,x:x坐标, y:y坐标
  67. export function ExexuteMouseDown(x, y, sn) {
  68. var jsonObj = { 'data': { 'action': 0, 'count': 1, 'pointerId': 0, 'x': x, 'y': y }, 'type': '0' }
  69. var json = JSON.stringify(jsonObj)
  70. return makeFrame(sn, 0, json)
  71. }
  72. // 触发鼠标移动事件,x:x坐标, y:y坐标
  73. export function ExexuteMouseMove(x, y, sn) {
  74. var jsonObj = { 'data': { 'action': 2, 'count': 1, 'pointerId': 0, 'x': x, 'y': y }, 'type': '2' }
  75. var json = JSON.stringify(jsonObj)
  76. return makeFrame(sn, 0, json)
  77. }
  78. // 触发鼠标抬起事件,x:x坐标, y:y坐标
  79. export function ExexuteMouseUp(x, y, sn) {
  80. var jsonObj = { 'data': { 'action': 1, 'count': 1, 'pointerId': 0, 'x': x, 'y': y }, 'type': '1' }
  81. var json = JSON.stringify(jsonObj)
  82. return makeFrame(sn, 0, json)
  83. }
  84. // 设置像素(200:自动/400000:极速/2243000:标清/3072000:高清)
  85. export function ExexutePixel(bitRate, sn) {
  86. const jsonObj = { 'data': { 'bitRate': Number(bitRate) }, 'type': 'setBitRate' }
  87. const json = JSON.stringify(jsonObj)
  88. return makeFrame(sn, 0, json)
  89. }
  90. // 关闭连接事件
  91. export function ExexuteCloseServer(sn) {
  92. const jsonObj = { 'data': { 'x': '0', 'y': '0' }, 'type': 'closeServer' }
  93. const json = JSON.stringify(jsonObj)
  94. return makeFrame(sn, 0, json)
  95. }
  96. // 根据报文识别屏幕方向, 0横屏,1竖屏
  97. export function CheckScreenDirection(data) {
  98. let screen
  99. if (data[0] === 0 && data[1] === 0 && data[2] === 0 && data[3] === 1) {
  100. if (data[4] === 1 && data[5] === 1) {
  101. if (data[6] === 1) {
  102. screen = data[7]
  103. return screen
  104. }
  105. }
  106. }
  107. }
  108. // 加个请求I帧的报文
  109. export function RequestIFrame(sn) {
  110. var outPut = new Uint8Array([0x20])
  111. return makeFrameExtend(sn, 6, outPut)
  112. }
  113. function makeFrameExtend(sn, dataType, body) {
  114. var index = 0
  115. var dataLen = body.length
  116. var frameLen = dataLen + 26
  117. var outPut = new Uint8Array(frameLen)
  118. outPut[index++] = 0x68
  119. outPut[index++] = (dataLen & 0xff000000) >> 24
  120. outPut[index++] = (dataLen & 0x00ff0000) >> 16
  121. outPut[index++] = (dataLen & 0x0000ff00) >> 8
  122. outPut[index++] = dataLen & 0x000000ff
  123. outPut[index++] = 0 // 类型为client
  124. // sn号赋值,string转ascii
  125. for (let i = 0; i < sn.length; i++) {
  126. outPut[index++] = sn[i].charCodeAt()
  127. }
  128. outPut[index++] = dataType // 指定数据类型为json
  129. // json string转ascii
  130. for (let i = 0; i < body.length; i++) {
  131. outPut[index++] = body[i]
  132. }
  133. var bccBuffer = outPut.slice(1, frameLen - 3 + 1) // 忽略协议头和协议尾
  134. outPut[index++] = calBcc(bccBuffer)
  135. outPut[index++] = 0x16
  136. return outPut
  137. }
  138. export function ConfigChannel(sn) {
  139. var outPut = new Uint8Array([0x07])
  140. return makeFrameExtend(sn, 6, outPut)
  141. }