CursorApe.js 4.6 KB
import Ape from './Ape';
import ApeConsts from './ApeConsts';
import pdu from 'pdus';
import Loger from 'Loger';
import MessageTypes from 'MessageTypes';
import GlobalConfig from 'GlobalConfig';
import EngineUtils from 'EngineUtils';

let loger = Loger.getLoger('CursorApe');

const DEFAULT_TYPE = 0; //鼠标

class CursorApe extends Ape {
  constructor() {
    super(
      ApeConsts.CURSOR_SESSION_ID,
      ApeConsts.CURSOR_SESSION_NAME,
      ApeConsts.CURSOR_SESSION_TAG
    );

    //Ape Models
    this.registerKey(this._session_id, this._session_name, this._session_tag, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_TABLE, ApeConsts.CURSOR_OBJ_TABLE_ID,
      ApeConsts.CURSOR_OBJ_TABLE_NAME, ApeConsts.CURSOR_OBJ_TABLE_TAG, 0, new ArrayBuffer);

    // ape listeners
    this.on(pdu.RCPDU_SESSION_JOIN_RESPONSE, this._joinSessionHandler.bind(this));
  }

  _joinSessionHandler(_data) {
    //loger.log("RCPDU_SESSION_JOIN_RESPONSE");
  }

  // 添加鼠标
  sendInsertCursor(_param) {
    if (_param == null || EngineUtils.isEmptyObject(_param)) {
      loger.warn('添加激光笔失败->参数错误->', _param);
      this._emit(MessageTypes.MCU_ERROR, MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return;
    }

    let itemIdx = ApeConsts.CURSOR_OBJ_TABLE_ID;
    let cursorModelPdu = this.packPdu(_param, itemIdx);
    if (cursorModelPdu == null) {
      loger.warn('添加激光笔失败-->参数错误->', _param);
      this._emit(MessageTypes.MCU_ERROR, MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return;
    }

    let tableItemPdu = new pdu['RCRegistryTableItemPdu'];
    tableItemPdu.itemIdx = itemIdx; //直接用时间戳作为id
    tableItemPdu.registerObjId = ApeConsts.CURSOR_OBJ_TABLE_ID;
    tableItemPdu.owner = 0; //收到flash的是这个值,不清楚先写固定
    tableItemPdu.itemData = cursorModelPdu.toArrayBuffer();

    let tableInsertItemPdu = new pdu['RCRegistryTableUpdateItemPdu'];
    //optional RCPduType_E type = 1 [default = RCPDU_REG_TABLE_UPDATE_PDU];
    //repeated RCRegistryTableItemPdu items = 2;
    tableInsertItemPdu.type = pdu.RCPDU_REG_TABLE_UPDATE_PDU; 
    tableInsertItemPdu.items.push(tableItemPdu);

    let updateObjPdu = new pdu['RCRegistryUpdateObjPdu'];
    updateObjPdu.objId = ApeConsts.CURSOR_OBJ_TABLE_ID; 
    updateObjPdu.subType = tableInsertItemPdu.type;
    updateObjPdu.userData = tableInsertItemPdu.toArrayBuffer();

    //同步
    let adapterItemPdu = new pdu['RCAdapterItemPdu'];
    adapterItemPdu.type = pdu.RCPDU_REG_UPDATE_OBJ;
    adapterItemPdu.itemData = updateObjPdu.toArrayBuffer();

    let adapterPdu = new pdu['RCAdapterPdu'];
    adapterPdu.type = pdu.RCPDU_REG_ADAPTER;
    adapterPdu.item.push(adapterItemPdu);
    this.sendUniform(adapterPdu, true);
  }

  /////鼠标数据接受/////////////////////////////////////////////////////////////////////////////////
  tableUpdateHandler(owner, itemIdx, itemData) {
    let cursorModel = this.unPackPdu(owner, itemIdx, itemData);
   // loger.log('鼠标数据->tableUpdateHandler');
    //loger.log(cursorModel);
    if (cursorModel) {
      this._emit(MessageTypes.CURSOR_UPDATE, cursorModel);
    }
  }

  ///////鼠标数据发送/////////////////////////////////////////
  packPdu(_param, _itemIdx) {
    //验证坐标点集合数组是否合法
    if (_param.pointGroup == null) {
      this._emit(MessageTypes.MCU_ERROR, MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return null;
    }

    //判断type类型,根据type设置不同的参数
    let cursorModelPdu = new pdu['RCCursorDataModelPdu'];

    //下面4个是必须的参数
    cursorModelPdu.type = DEFAULT_TYPE;
    cursorModelPdu.itemIdx = _itemIdx;
    cursorModelPdu.initiator = GlobalConfig.nodeId;

    cursorModelPdu.parentId = GlobalConfig.activeDocId; //当前激活的文档id
    cursorModelPdu.curPageNo = GlobalConfig.activeDocCurPage; //当前激活的文档页码

    cursorModelPdu.pointGroup = EngineUtils.arrayToJsonString(_param.pointGroup);
    cursorModelPdu.color = _param.color || "#000000";
    cursorModelPdu.thickness = _param.thickness || 1;
    cursorModelPdu.duration = _param.duration || 0;

    return cursorModelPdu;
  }

  unPackPdu(owner, itemIdx, itemData) {
    try {
      //loger.log("鼠标激光笔数据->unPackPdu");
      const cursorModelPdu = pdu['RCCursorDataModelPdu'].decode(itemData);
      const _pointGroup = EngineUtils.arrayFromJsonString(cursorModelPdu.pointGroup);
      cursorModelPdu.pointGroup = _pointGroup;
      return cursorModelPdu;
    } catch (err) {
      loger.log("鼠标激光笔数据->unPackPdu->Pdu解析错误,itemIdx=" + itemIdx + "  err:" + err.message);
    }
    return null;
  }
}

export default CursorApe;