DocApe.js 14.9 KB
// //////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2016-present  All Rights Reserved.
//  Licensed under the Apache License, Version 2.0 (the "License");
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Github Home: https://github.com/AlexWang1987
//  Author: AlexWang
//  Date: 2016-08-26 17:30:43
//  QQ Email: 1669499355@qq.com
//  Last Modified time: 2016-09-21 14:12:25
//  Description: LiveClass-DocApe
//
// //////////////////////////////////////////////////////////////////////////////

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('DocApe');
let itemIdx=0;//table插入新数据的计数id,目前用时间戳
class DocApe extends Ape {
  constructor() {
    super(
      ApeConsts.DOCSHARING_SESSION_ID,
      ApeConsts.DOCSHARING_SESSION_NAME,
      ApeConsts.DOCSHARING_SESSION_TAG
    );
    this.docList = {};//记录文档的数组this.docList[itemIdx]=itemIdx的数据


    // Ape Models
    this.registerKey(this._session_id, this._session_name, this._session_tag, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_TABLE, ApeConsts.DOCSHARING_OBJ_TABLE_ID, ApeConsts.DOCSHARING_OBJ_TABLE_NAME, ApeConsts.DOCSHARING_OBJ_TABLE_TAG, 0, new ArrayBuffer);
    //this.registerObj(pdu.RCPDU_REG_REGISTER_TABLE, ApeConsts.DOCSHARING_OBJ_TABLE_ID_H5, ApeConsts.DOCSHARING_OBJ_TABLE_NAME_H5, ApeConsts.DOCSHARING_OBJ_TABLE_TAG_H5, 0, new ArrayBuffer);

    // 延迟
      this._apeDelayed = true;
  }
  /////////////发送数据操作//////////////////////////////////////////////////////
  //上传文档
  documentUpload(paramInfo){
    if(paramInfo==null||EngineUtils.isEmptyObject(paramInfo)){
      loger.log('documentUpload失败,参数错误');
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return ;
    }
    //判断文档是否已经存在,每个文档都有唯一的docId,如果已经同步的文档中存在相同的docId就不需要再同步
    if(this.checkDocId(paramInfo.docId)){
      //文档已经存在相同的docId,不需要同步上传
      loger.warn('documentUpload  文档的docId不无效或已经存在相同的docId,不需要上传');
      return;
    }

    itemIdx=EngineUtils.creatTimestamp();
    let docDataModelPdu = this.packPdu(paramInfo,itemIdx);
    if(docDataModelPdu==null){
      loger.log('documentUpload失败,参数错误');
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return ;
    }
    //console.log(docDataModelPdu);

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

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

    let updateObjPdu = new pdu['RCRegistryUpdateObjPdu'];
    updateObjPdu.objId = ApeConsts.DOCSHARING_OBJ_TABLE_ID;// updateObjPdu.objId = ApeConsts.DOCSHARING_OBJ_TABLE_ID_H5;
    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);

    loger.log("文档upload  tableItemPdu.itemIdx="+tableItemPdu.itemIdx);
    this.sendUniform(adapterPdu,true);
  }
  updaterDoc(_docDataModel,_itemIdx){
    loger.log("文档===updaterDoc ",_itemIdx);
    //验证坐标点集合数组是否合法
    if(_docDataModel==null||_itemIdx==null){
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return null;
    }
    loger.log("文档===updaterDoc ",_docDataModel);

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

    //insert
    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.DOCSHARING_OBJ_TABLE_ID;// updateObjPdu.objId = ApeConsts.DOCSHARING_OBJ_TABLE_ID_H5;
    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);

    loger.log("发送更新文档.itemIdx="+tableItemPdu.itemIdx);
    this.sendUniform(adapterPdu,true);
  }
  //切换文档
  documentSwitch(paramInfo){
    console.log(this.docList);
    //获取已经存在的数据
    let docDataModel= this.docList[paramInfo.itemIdx];
    if(docDataModel==null){
      loger.warn('documentSwitch失败,文档不存在',paramInfo);
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return;
    }
    //更新数据的字段
    docDataModel.visible=!docDataModel.visible;
    this.updaterDoc(docDataModel,docDataModel.itemIdx);
  }

  //操作文档(翻页、缩放、滚动...)
  documentCommand(paramInfo){
    console.log(this.docList);
    //获取已经存在的数据
    let docDataModel= this.docList[paramInfo.itemIdx];
    //console.log(docDataModelPdu);
    //console.log(docDataModel);
    if(docDataModel==null){
      loger.log('documentCommand失败,文档不存在',paramInfo);
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return;
    }
    //更新数据的字段
    docDataModel.curPageNo=docDataModel.curPageNo+1;
    this.updaterDoc(docDataModel,docDataModel.itemIdx);
  }

  //删除所有文档
  documentDeleteAll(_param){
    for (let key in this.docList){
      //console.log("key:"+key);
      loger.log("删除文档数据,itemIdx:"+key);
      this.documentDelete({"itemIdx":key});
    }
  }

  //删除文档
  documentDelete(paramInfo){
    //{"itemIdx":itemIdx}
    let tableDeleteItemPdu = new pdu['RCRegistryTableDeleteItemPdu'];
    //optional RCPduType_E type = 1 [default = RCPDU_REG_TABLE_DELETE_PDU];
    // repeated uint32 item_idx = 2;
    tableDeleteItemPdu.type = pdu.RCPDU_REG_TABLE_DELETE_PDU;//
    tableDeleteItemPdu.itemIdx=parseInt(paramInfo.itemIdx);//这里需要设置要删除的数据的itemIdx,每条数据的这个id都不一样

    let updateObjPdu = new pdu['RCRegistryUpdateObjPdu'];
    updateObjPdu.objId = ApeConsts.DOCSHARING_OBJ_TABLE_ID;//    updateObjPdu.objId = ApeConsts.DOCSHARING_OBJ_TABLE_ID_H5;
    updateObjPdu.subType = tableDeleteItemPdu.type;
    updateObjPdu.userData = tableDeleteItemPdu.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);

    loger.log("文档发送删除数据============="+tableDeleteItemPdu.itemIdx);
    this.sendUniform(adapterPdu,true);
  }

  ///////白板数据的封包和解包/////////////////////////////////////////
  packPdu(_param,_itemIdx){
    loger.log("文档===packPdu ");
    //验证坐标点集合数组是否合法
    if(_param==null||_itemIdx==null){
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return null;
    }

 /*   message RCDocSendDataModelPdu {
      required uint32 item_idx=1;//唯一标识
      required uint32 owner=2;
      optional uint32 from=3;
      optional uint32 cur_page_no=4;
      optional uint32 page_num  =5;
      optional string file_type=6;
      optional string creat_user_id=7;//创建文档userid
      optional string relative_url=8;//文档相对地址
      optional string url  =9;//文档地址
      optional uint32 cur_V=10;
      optional uint32 cur_H=11;
      optional uint32 scale=12;
      optional bool visible=13;
      optional uint32 action=14;//0,无操作, 1翻页、2.显示/隐藏
      optional string doc_id=15;//文档在服务器数据库中的唯一id
      optional string file_name=16;//文档的名字
      optional string dynamic_TS=17;//"dynamicTransferStatic": "0"
      optional string md5=18;//md5
    }*/

    //判断type类型,根据type设置不同的参数
    let docModelPdu =new pdu['RCDocSendDataModelPdu'];
    docModelPdu.itemIdx=_itemIdx;
    docModelPdu.owner=GlobalConfig.nodeId;
    docModelPdu.from=GlobalConfig.nodeId;
    docModelPdu.curPageNo  = _param.curPageNo||1;
    docModelPdu.pageNum  = _param.pageNum||1;
    docModelPdu.fileType=_param.fileType||"";
    docModelPdu.creatUserId=_param.creatUserId||"0";
    docModelPdu.url  =_param.url||"";//"http://101.200.150.192/DocSharing/data/h5test/20170206-171100025/7e9c4178cac1133e0dd9d5b583439122.jpg";
    docModelPdu.relativeUrl=_param.relativeUrl||"";//"/DocSharing/data/h5test/20170206-171100025/7e9c4178cac1133e0dd9d5b583439122.jpg";
    docModelPdu.curV=_param.curV||0;
    docModelPdu.curH=_param.curH||0;
    docModelPdu.scale=_param.scale||1;
    docModelPdu.visible= _param.visible||false;
    docModelPdu.action=_param.action||0;//0,无操作, 1翻页、2.显示/隐藏
    docModelPdu.docId=_param.docId||"";//文档在服务器数据库中的唯一id,必须有
    docModelPdu.md5=_param.md5||"";//MD5
    docModelPdu.fileName=_param.fileName||"doc_"+_itemIdx;//文档的名字
    docModelPdu.dynamicTS=_param.dynamicTS||"0";//文档上传后返回值中的字段dynamicTransferStatic
    console.log(docModelPdu);
    return docModelPdu;
  }

  unPackPdu(owner, itemIdx,itemData){
    loger.log("文档===unPackPdu ");
    if(owner==null||itemIdx==null||itemData==null){
      this._emit(MessageTypes.MCU_ERROR,MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
      return null;
    }

    try{
      let docModelPdu= pdu['RCDocSendDataModelPdu'].decode(itemData);
      //console.log(whiteBoardModelPdu);
      //loger.log(docModelPdu);
      return docModelPdu;
    }catch (err){
      loger.log("文档收到数据 unPackPdu Pdu解析错误,itemIdx="+itemIdx+"  err:"+err.message);
    }
    return null;
  }

  /////收到消息处理/////////////////////////////////////////////////////////////////////////////////
  tableInsertHandler(owner, itemIdx, itemData) {
    //this.tableUpdateHandler(owner, tableId, itemData);
    //loger.log('tableInsertHandler---',itemData);
    let itemDataInfo = this.unPackPdu(owner, itemIdx, itemData);
    this.docList[itemIdx] = itemDataInfo;
    loger.log('tableInsertHandler',itemDataInfo);
  }

  tableDeleteHandler(object_id, tableDeleteData){
      /*const re={};
      re.type=ApeConsts.DOCUMENT_DEL;
      this._emit(MessageTypes.DOC_DELETE, re);*/

    loger.log('tableDeleteHandler',object_id,tableDeleteData);//["tableDeleteHandler",1179649,{"type":231,"itemIdx":[1486301768]}]
    if(tableDeleteData&&tableDeleteData.itemIdx){
      let len=tableDeleteData.itemIdx.length;
      let itemIdxs=tableDeleteData.itemIdx;
      for (let i=0;i<len;i++){
        if(this.docList[itemIdxs[i]]){
          loger.log("删除文档数据:",itemIdxs[i]);
          delete this.docList[itemIdxs[i]];
        }
      }
    }
  }
  tableUpdateHandler(owner, itemIdx, itemData) {
    //let itemDataInfo = pdu['RCDocSendDataRequestPdu'].decode(itemData);
    let itemDataInfo =  this.unPackPdu(owner, itemIdx, itemData);
    if(itemDataInfo!=null){
      this.docList[itemIdx] = itemDataInfo;
      loger.log('tableUpdateHandler',itemDataInfo);
    }else {
      loger.log('tableUpdateHandler 数据无效--> itemIdx',itemIdx);
    }


    /*try {
      const recordInfo = pdu['RCDocSendDataRequestPdu'].decode(itemData);
      recordInfo.type = ApeConsts.DOCUMENT_LOAD;

      recordInfo.ext = recordInfo.name.substr(recordInfo.name.indexOf('.') + 1);
      recordInfo.wbid = (recordInfo.id << 10) + recordInfo.curPageNo;
      recordInfo.isPicture = ~['bmp', 'png', 'gif', 'jpg', 'jpeg'].indexOf(recordInfo.ext);

      if (recordInfo.isPicture) {
        recordInfo.namePath = recordInfo.uri.substring(0, recordInfo.uri.lastIndexOf('.'));
        recordInfo.loadURL = recordInfo.namePath + '.' + recordInfo.ext;
      } else {
        recordInfo.namePath = recordInfo.uri.substring(0, recordInfo.uri.lastIndexOf('/'));
        recordInfo.loadURL = `${recordInfo.namePath}/${recordInfo.curPageNo}.jpg`;
      }
      this.docList[recordId] = recordInfo;
      this._emit(MessageTypes.DOC_UPDATE, recordInfo);
      loger.log('Doc update ->' + itemIdx);
    } catch (e) {
      loger.warn('Doc Table Update Decode包异常');
    }*/
  }
  onJoinChannelHandlerSuccess(){
    loger.log(this._session_name + ' onJoinChannelHandlerSuccess===========================');
    if (this._apeDelayed) {
      // this._apeDelayedMsgs.push(regBuffer);
      // this._apeDelayedStart();
      setTimeout(() => {
        this._emit(DocApe.DOC_JOIN_CHANNEL_SUCCESS);
      },(GlobalConfig.mcuDelay+GlobalConfig.docDelay)|| 12000+GlobalConfig.docDelay);
    }else {
      this._emit(DocApe.DOC_JOIN_CHANNEL_SUCCESS);
    }
  }

  //检查文档是否已经存在,如果存在 返回true,否则返回false
  checkDocId(_docId){
    if(_docId==null){
      loger.warn('checkDocId  _docId参数为null');
      return true;
    }
    //遍历查找
    for (let key in this.docList){
      let item=this.docList[key];
      loger.log('item.docId==============_docId',item.docId,_docId);
      if(item&&item.docId==_docId){
        return true;
      }
    }

    //储存的数据中没有查找到
    return false;
  }
}
DocApe.prototype.DOC_JOIN_CHANNEL_SUCCESS = DocApe.DOC_JOIN_CHANNEL_SUCCESS = 'doc.join.channel.success';
export default DocApe;