123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /**
- * 此文件实现将控制命令封装成协议,具体协议内容请看:
- * 链接:http://note.youdao.com/noteshare?id=dabda6c613adef7a416bd2625cd770a1
- */
- /**
- * bcc校验码计算
- * arry: 要计算的数组
- * 返回计算协议中校验位的校验码
- */
- export function calBcc(arry) {
- var bcc = 0
- for (let i = 0; i < arry.length; i++) {
- bcc ^= arry[i]
- }
- return bcc
- }
- // 数组打印,调试用
- export function PrintArry(data) {
- var str = ''
- for (let i = 0; i < data.length; i++) {
- str = str + data[i].toString(16).padStart(2, '0')
- }
- str = str.toUpperCase()
- return str
- }
- /**
- * @param {*} sn: 板卡sn号,stirng
- * @param {*} dataType:数据类型,数字
- * @param {*} jsonCmd: json命令
- * 返回值:生成一个Uint8Array,通过websocket发送给板卡
- */
- export function makeFrame(sn, dataType, jsonCmd) {
- var index = 0
- var dataLen = jsonCmd.length
- var frameLen = dataLen + 26
- var outPut = new Uint8Array(frameLen)
- outPut[index++] = 0x68
- outPut[index++] = (dataLen & 0xff000000) >> 24
- outPut[index++] = (dataLen & 0x00ff0000) >> 16
- outPut[index++] = (dataLen & 0x0000ff00) >> 8
- outPut[index++] = dataLen & 0x000000ff
- outPut[index++] = 0 // 类型为client
- // sn号赋值,string转ascii
- for (let i = 0; i < sn.length; i++) {
- outPut[index++] = sn[i].charCodeAt()
- }
- outPut[index++] = dataType // 指定数据类型为json
- // json string转ascii
- for (let i = 0; i < jsonCmd.length; i++) {
- outPut[index++] = jsonCmd[i].charCodeAt()
- }
- var bccBuffer = outPut.slice(1, frameLen - 3 + 1) // 忽略协议头和协议尾
- outPut[index++] = calBcc(bccBuffer)
- outPut[index++] = 0x16
- return outPut
- }
- /**
- * 触发键盘事件, code表示键盘值
- * 音量加减、home键、back事件(keyCode的值分别表示为25减音量,24加音量,4为返回键,3为home键,187为切换菜单)
- */
- export function ExexuteKeyDown(code, sn) {
- var jsonObj = { 'data': { 'keyCode': code.toString() }, 'type': 'keyDown' }
- var json = JSON.stringify(jsonObj)
- return makeFrame(sn, 0, json)
- }
- // 触发鼠标按下事件,x:x坐标, y:y坐标
- export function ExexuteMouseDown(x, y, sn) {
- var jsonObj = { 'data': { 'action': 0, 'count': 1, 'pointerId': 0, 'x': x, 'y': y }, 'type': '0' }
- var json = JSON.stringify(jsonObj)
- return makeFrame(sn, 0, json)
- }
- // 触发鼠标移动事件,x:x坐标, y:y坐标
- export function ExexuteMouseMove(x, y, sn) {
- var jsonObj = { 'data': { 'action': 2, 'count': 1, 'pointerId': 0, 'x': x, 'y': y }, 'type': '2' }
- var json = JSON.stringify(jsonObj)
- return makeFrame(sn, 0, json)
- }
- // 触发鼠标抬起事件,x:x坐标, y:y坐标
- export function ExexuteMouseUp(x, y, sn) {
- var jsonObj = { 'data': { 'action': 1, 'count': 1, 'pointerId': 0, 'x': x, 'y': y }, 'type': '1' }
- var json = JSON.stringify(jsonObj)
- return makeFrame(sn, 0, json)
- }
- // 设置像素(200:自动/400000:极速/2243000:标清/3072000:高清)
- export function ExexutePixel(bitRate, sn) {
- const jsonObj = { 'data': { 'bitRate': Number(bitRate) }, 'type': 'setBitRate' }
- const json = JSON.stringify(jsonObj)
- return makeFrame(sn, 0, json)
- }
- // 关闭连接事件
- export function ExexuteCloseServer(sn) {
- const jsonObj = { 'data': { 'x': '0', 'y': '0' }, 'type': 'closeServer' }
- const json = JSON.stringify(jsonObj)
- return makeFrame(sn, 0, json)
- }
- // 根据报文识别屏幕方向, 0横屏,1竖屏
- export function CheckScreenDirection(data) {
- let screen
- if (data[0] === 0 && data[1] === 0 && data[2] === 0 && data[3] === 1) {
- if (data[4] === 1 && data[5] === 1) {
- if (data[6] === 1) {
- screen = data[7]
- return screen
- }
- }
- }
- }
- // 加个请求I帧的报文
- export function RequestIFrame(sn) {
- var outPut = new Uint8Array([0x20])
- return makeFrameExtend(sn, 6, outPut)
- }
- function makeFrameExtend(sn, dataType, body) {
- var index = 0
- var dataLen = body.length
- var frameLen = dataLen + 26
- var outPut = new Uint8Array(frameLen)
- outPut[index++] = 0x68
- outPut[index++] = (dataLen & 0xff000000) >> 24
- outPut[index++] = (dataLen & 0x00ff0000) >> 16
- outPut[index++] = (dataLen & 0x0000ff00) >> 8
- outPut[index++] = dataLen & 0x000000ff
- outPut[index++] = 0 // 类型为client
- // sn号赋值,string转ascii
- for (let i = 0; i < sn.length; i++) {
- outPut[index++] = sn[i].charCodeAt()
- }
- outPut[index++] = dataType // 指定数据类型为json
- // json string转ascii
- for (let i = 0; i < body.length; i++) {
- outPut[index++] = body[i]
- }
- var bccBuffer = outPut.slice(1, frameLen - 3 + 1) // 忽略协议头和协议尾
- outPut[index++] = calBcc(bccBuffer)
- outPut[index++] = 0x16
- return outPut
- }
- export function ConfigChannel(sn) {
- var outPut = new Uint8Array([0x07])
- return makeFrameExtend(sn, 6, outPut)
- }
|