WhiteBoardApe.js 10.0 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:36:20
//  QQ Email: 1669499355@qq.com
//  Last Modified time: 2016-09-21 14:06:12
//  Description: LiveClass-WhiteBoardApe
//
// //////////////////////////////////////////////////////////////////////////////

import Ape from './Ape';
import ApeConsts from './ApeConsts';
import pdu from 'pdus';
import Loger from 'Loger';
import MessageTypes from 'MessageTypes';
import { Zlib } from 'zlibjs/bin/zlib.min';
import UTF8 from 'utf-8';
import GlobalConfig from 'GlobalConfig';

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

let insertIdx=1;//table插入新数据的计数id
class WhiteBoardApe extends Ape {
  constructor() {
    super(
      ApeConsts.WHITEBOARD_SESSION_ID,
      ApeConsts.WHITEBOARD_SESSION_NAME,
      ApeConsts.WHITEBOARD_SESSION_TAG
    );
    // properties
    this.annoInfos = {};

    //Ape Models
    this.registerKey(this._session_id, this._session_name, this._session_tag, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_TABLE, ApeConsts.WHITEBOARD_OBJ_TABLE_ID,
      ApeConsts.WHITEBOARD_OBJ_TABLE_NAME, ApeConsts.WHITEBOARD_OBJ_TABLE_TAG, 0, new ArrayBuffer);

    // ape listeners
    this.on(pdu.RCPDU_CONFERENCE_SEND_DATA_REQUEST, this.whiteboardMsgComingHandler.bind(this));
    this.on(pdu.RCPDU_SESSION_JOIN_RESPONSE, this._joinSessionHandler.bind(this));
    // 白板延迟
     this._apeDelayed = true;
  }

  _joinSessionHandler(confInfo) {
    loger.log("RCPDU_SESSION_JOIN_RESPONSE");
  }

  /////////////发送数据操作//////////////////////////////////////////////////////
  // 添加标注,发送信息
  sendInsetAnnotaion(_param){
    insertIdx=parseInt(Date.now()/1000);

    let whiteBoardModelPdu =new pdu['RCWhiteBoardData'];
    whiteBoardModelPdu.id =insertIdx;
    whiteBoardModelPdu.initiator =GlobalConfig.nodeId;
    whiteBoardModelPdu.data ="白板数据 "+insertIdx;
    whiteBoardModelPdu.type = 3;
    //required uint32 id= 1;
    //required uint32 initiator=2;
    //optional string data= 3;
    //optional uint32 type= 4;
  //RCWhiteBoardData

    //com.seemq.wrcp.RCApe._update_table_item(obj_id:int, idx:int, item_data:ByteArray, has_owner:Boolean=true):int
    //public function add_item(idx:int, owner:int = 0, item_data:ByteArray = null):void
    //registry_table_operator.add_item(((RCConst_E.VIDEO_SESSION_ID << 16) + i), 0, channel_data); VIDEO
    //tableItemPdu.itemIdx=ApeConsts.WHITEBOARD_OBJ_TABLE_ID;//收到flash的是999这个值,不清楚先写固定WHITEBOARD_OBJ_TABLE_ID
    //tableItemPdu.itemIdx=ApeConsts.WHITEBOARD_OBJ_TABLE_ID+insertIdx;
    //insertIdx++;
    let tableItemPdu = new pdu['RCRegistryTableItemPdu'];
    tableItemPdu.itemIdx=insertIdx;//直接用时间戳作为id
    tableItemPdu.registerObjId=ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
    tableItemPdu.owner = 0;//收到flash的是这个值,不清楚先写固定
    //tableItemPdu.itemData =whiteBoardModelPdu.toArrayBuffer();
    tableItemPdu.itemData =whiteBoardModelPdu.toArrayBuffer();

    //console.log("解析");
    //console.log(pdu['RCWhiteBoardData'].decode(tableItemPdu.itemData));
/*    message RCRegistryTableItemPdu {
      required uint32 item_idx = 1;
      required uint32 owner = 2;
      required bytes item_data = 3;
    }*/

  //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.WHITEBOARD_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);

    console.log("白板insert数据======111111111111=tableItemPdu.itemIdx="+tableItemPdu.itemIdx);
    this.sendUniform(adapterPdu,true);
  }
  //删除所有标注
  sendDeleteAllAnnotation(_param){
    let len=0;
    for (let key in this.annoInfos){
      console.log("key:"+key);
      console.log("删除白板数据,itemIdx:"+this.annoInfos[key]);
      this.sendDeleteAnnotaion({"itemIdx":key});
    }
  }
  //删除标注,发送信息
  sendDeleteAnnotaion(_param){
    //delete
    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(_param.itemIdx);//这里需要设置要删除的数据的itemIdx,每条数据的这个id都不一样

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

    console.log("白板发送删除数据============="+tableDeleteItemPdu.itemIdx);
    this.sendUniform(adapterPdu,true);
  }


  //更新标注
  sendUpdaterAnnotaion(_param){
    let whiteBoardModelPdu =new pdu['RCWhiteBoardData'];
    whiteBoardModelPdu.id =ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
    whiteBoardModelPdu.initiator =GlobalConfig.nodeId;
    whiteBoardModelPdu.data ="白板数据 "+insertIdx;
    whiteBoardModelPdu.type = 3;

    let tableItemPdu = new pdu['RCRegistryTableItemPdu'];
    /*  tableItemPdu.itemIdx =ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
     tableItemPdu.owner = GlobalConfig.nodeId;
     tableItemPdu.itemData =whiteBoardModelPdu.toArrayBuffer();*/

    //com.seemq.wrcp.RCApe._update_table_item(obj_id:int, idx:int, item_data:ByteArray, has_owner:Boolean=true):int
    //public function add_item(idx:int, owner:int = 0, item_data:ByteArray = null):void
    //registry_table_operator.add_item(((RCConst_E.VIDEO_SESSION_ID << 16) + i), 0, channel_data); VIDEO
    //tableItemPdu.itemIdx=ApeConsts.WHITEBOARD_OBJ_TABLE_ID;//收到flash的是999这个值,不清楚先写固定WHITEBOARD_OBJ_TABLE_ID
    tableItemPdu.itemIdx=ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
    tableItemPdu.owner = 0;//收到flash的是这个值,不清楚先写固定
    tableItemPdu.registerObjId=ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
    tableItemPdu.itemData =whiteBoardModelPdu.toArrayBuffer();


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


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

    console.log("白板发送更新数据===============22");
    this.sendUniform(adapterPdu,true);
  }


  /////收到消息处理/////////////////////////////////////////////////////////////////////////////////

  whiteboardMsgComingHandler(pdu) {
      //loger.warn('whiteboardMsgComingHandler needs to be handled.');
    console.log("白板收到数据=============================RCPDU_CONFERENCE_SEND_DATA_REQUEST");
  }

  tableInsertHandler(owner, itemIdx,itemData) {
    this.annoInfos[itemIdx] = itemData;
    console.log("白板收到数据==========tableInsertHandler");
    let nodeData = pdu['RCWhiteBoardData'].decode(itemData);
    console.log("白板收到数据==========tableInsertHandler"+nodeData);
    console.log(nodeData);
      //this.emitDocChange(ApeConsts.DOCUMENT_DEL);
  }

  tableUpdateHandler(owner, itemIdx, itemData) {
    this.annoInfos[itemIdx] = itemData;
    console.log("白板收到数据===tableUpdateHandler itemData="+itemData);
    try{
      console.log("白板收到数据===tableUpdateHandler ");
      let whiteBoardModelPdu= pdu['RCWhiteBoardData'].decode(itemData);
      console.log(whiteBoardModelPdu);
    }catch (err){
      console.log("tableUpdateHandler Pdu解析错误,itemIdx="+itemIdx+"  err:"+err.message);
    }

    /*const recordInfo = pdu['RCWhiteboardDataRequestPdu'].decode(recordData);
    console.log("recordInfo");
    loger.log(recordInfo);

    // 目前只处理 文档标注
    if (recordInfo.type == ApeConsts.WBA_DOC_ANNOTATION) {
      const uncompressedBytes = new Zlib.Inflate(recordInfo.action.compact().view).decompress();
      const annoInfo = {
        id: recordId,
        svg: UTF8.getStringFromBytes(uncompressedBytes)
      };
      loger.log(annoInfo);
      this.annoInfos[recordId] = annoInfo;
      this._emit(MessageTypes.ANNOTATION_UPDATE, annoInfo);
    } else {
      loger.log('白板动作忽略,类型:', ApeConsts(recordInfo.type));
    }*/
  }
  tableDeleteHandler(object_id, tableDeleteData){
    console.log("白板收到数据=============================tableDeleteHandler");
  }
}

export default WhiteBoardApe;