WhiteBoardApe.js 20.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
// //////////////////////////////////////////////////////////////////////////////
//
//  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';
import EngineUtils from 'EngineUtils';

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

//let itemIdx = 0;//table插入新数据的计数id,目前用时间戳
const TYPE_BIGHT = 0;//任意线段
const TYPE_LINE = 1;//直线
const TYPE_RECT = 2;//矩形
const TYPE_CIRCLE = 3;//圆形
const TYPE_TEXT = 4;//文本

class WhiteBoardApe extends Ape {
    constructor() {
        super(
            ApeConsts.WHITEBOARD_SESSION_ID,
            ApeConsts.WHITEBOARD_SESSION_NAME,
            ApeConsts.WHITEBOARD_SESSION_TAG
        );
        // properties
        this.annoInfos = {};//储存所有的标注数据
        this.insertHistory = [];//添加的白板记录,用于撤回操作
        // 白板延迟
        //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_SESSION_JOIN_RESPONSE, this._joinSessionHandler.bind(this));
        //this.on(pdu.RCPDU_CONFERENCE_SEND_DATA_REQUEST, this.whiteboardMsgComingHandler.bind(this));//这个是课堂消息类型,flash里在使用这里不再使用,各个模块的消息由模块自己来处理
    }

    _joinSessionHandler(_data) {
        loger.log("RCPDU_SESSION_JOIN_RESPONSE");
        this.insertHistory = [];
    }

    //清除当前模块的数据
    clearData(){
        loger.log("clearData->")
        this.annoInfos = {};//储存所有的标注数据
        this.insertHistory = [];//添加的白板记录,用于撤回操作
    }
    //更新文档模块的录制信息,每次开启录制的时候需要把当前文档的信息更新一次
    updaterRecordApeStatus(_param){
       /* if(GlobalConfig.isHost&&!GlobalConfig.isRecordPlayBack){
            let curPageAnnos={};
            for (let key in this.annoInfos) {
                let item = this.annoInfos[key];
                if (item && item.parentId == GlobalConfig.activeDocId && item.curPageNo == GlobalConfig.activeDocCurPage) {
                    curPageAnnos[key]=item;
                }
            }
            if(Object.keys(curPageAnnos).length>0){
                loger.log("开启录制成功->更新当前的标注数据->docId:", GlobalConfig.activeDocId, 'page:',GlobalConfig.activeDocCurPage);
                this.sendUpdaterAnnotaion({"itemIdxArr":curPageAnnos});
            }else {
                loger.log("开启录制成功->当前没有标注数据需要更新->docId:", GlobalConfig.activeDocId, 'page:',GlobalConfig.activeDocCurPage);
            }
        }*/
        let curPageAnnos={};
        for (let key in this.annoInfos) {
            let item = this.annoInfos[key];
            if (item && item.parentId == GlobalConfig.activeDocId && item.curPageNo == GlobalConfig.activeDocCurPage) {
                curPageAnnos[key]=item;
            }
        }
        if(Object.keys(curPageAnnos).length>0){
            loger.warn("录制状态发送改变->更新当前的标注数据->docId:", GlobalConfig.activeDocId, 'page:',GlobalConfig.activeDocCurPage);
            this.sendUpdaterAnnotaion({"itemIdxArr":curPageAnnos});
        }else {
            loger.warn("录制状态发送改变->当前没有标注数据需要更新->docId:", GlobalConfig.activeDocId, 'page:',GlobalConfig.activeDocCurPage);
        }
    }

    /////////////发送数据操作//////////////////////////////////////////////////////
    // 添加标注,发送信息
    sendInsetAnnotaion(_param) {
        if (_param == null || EngineUtils.isEmptyObject(_param)) {
            loger.warn('添加标注失败->参数错误->',_param);
            this._emit(MessageTypes.MCU_ERROR, MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
            return;
        }

        let itemIdx = EngineUtils.creatSoleNumberFromTimestamp();//创建时间戳,保证每条数据的唯一
        let whiteBoardModelPdu = this.packPdu(_param, itemIdx);
        if (whiteBoardModelPdu == null) {
            loger.warn('添加标注失败-->参数错误->',_param);
            this._emit(MessageTypes.MCU_ERROR, MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
            return;
        }
        //储存记录,用于返回上一步操作
        this.insertHistory.push(whiteBoardModelPdu);

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

        //insert
        let tableInsertItemPdu = new pdu['RCRegistryTableInsertItemPdu'];
        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);

        loger.log("添加标注->itemIdx=" + tableItemPdu.itemIdx);
        this.sendUniform(adapterPdu, true);
    }

    //撤销上一步
    sendGotoPrev() {
        loger.log("撤销上一步");
        if (this.insertHistory == null || this.insertHistory.length < 1) {
            loger.warn("无法继续上一步操作,已经没有可以撤销的数据");
            return;
        }
        let annoItem=this.insertHistory.pop();
        if(annoItem){
            let temAnnos={};
            temAnnos[annoItem.itemIdx]=annoItem;
            this.sendDeleteAnnotaion({"itemIdxArr":temAnnos});
        }
    }

    //删除当前页码的所有标注
    sendDeleteCurPageAnnotation(_param) {
        this.insertHistory=[];
        let curPageAnnos={};
        for (let key in this.annoInfos) {
            let item = this.annoInfos[key];
            if (item && item.parentId == GlobalConfig.activeDocId && item.curPageNo == GlobalConfig.activeDocCurPage) {
                //loger.log("sendDeleteCurPageAnnotation 删除当前页面上的标注", key);
                //this.sendDeleteAnnotaion({"itemIdx": key});
                curPageAnnos[key]=item;
            }
        }

        this.sendDeleteAnnotaion({"itemIdxArr":curPageAnnos});
    }

    //删除所有标注
    sendDeleteAllAnnotation(_param) {
        this.insertHistory=[];
       /* for (let key in this.annoInfos) {
            this.sendDeleteAnnotaion({"itemIdx": key});
        }*/
        this.sendDeleteAnnotaion({"itemIdxArr":this.annoInfos});
    }

    //删除标注,发送信息 {"itemIdxArr":{}}
    sendDeleteAnnotaion(_param) {
        if (_param == null||_param.itemIdxArr==null) {
            loger.warn("要删除的数据不存在->",_param);
            return;
        }
        let adapterPdu = new pdu['RCAdapterPdu'];
        adapterPdu.type = pdu.RCPDU_REG_ADAPTER;

        for (let key in _param.itemIdxArr) {
            let tableDeleteItemPdu = new pdu['RCRegistryTableDeleteItemPdu'];
            tableDeleteItemPdu.type = pdu.RCPDU_REG_TABLE_DELETE_PDU;//
            tableDeleteItemPdu.itemIdx = parseInt(key);//这里需要设置要删除的数据的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();
            adapterPdu.item.push(adapterItemPdu);
        }
        loger.log("发送删除白板标注数据->长度:" + adapterPdu.item.length);
        this.sendUniform(adapterPdu, true);
    }
    /*sendDeleteAnnotaion(_param) {
        if (_param == null) {
            loger.warn("要删除的数据不存在");
            return;
        }
        //{"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(_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);

        loger.log("白板发送删除数据->" + tableDeleteItemPdu.itemIdx);
        this.sendUniform(adapterPdu, true);
    }*/




    //更新标注
    sendUpdaterAnnotaion(_param) {
        if (_param == null ||_param.itemIdxArr==null) {
            loger.log('更新标注数据->失败->参数错误');
            this._emit(MessageTypes.MCU_ERROR, MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
            return;
        }
        let adapterPdu = new pdu['RCAdapterPdu'];
        adapterPdu.type = pdu.RCPDU_REG_ADAPTER;
        for (let key in _param.itemIdxArr) {
            let itemIdx =parseInt(key);
            let item= _param.itemIdxArr[key];
            let whiteBoardModelPdu = this.packPdu(item, itemIdx);
            if (whiteBoardModelPdu == null) {
                loger.log('更新标注数据->失败->数据无效');
                this._emit(MessageTypes.MCU_ERROR, MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
                continue;
            }
            let tableItemPdu = new pdu['RCRegistryTableItemPdu'];
            tableItemPdu.itemIdx = itemIdx;
            tableItemPdu.owner = 0;//收到flash的是这个值,不清楚先写固定
            tableItemPdu.registerObjId = ApeConsts.WHITEBOARD_OBJ_TABLE_ID;
            tableItemPdu.itemData = whiteBoardModelPdu.toArrayBuffer();

            //updater
            let whiteBoardUpdateItem = new pdu['RCRegistryTableUpdateItemPdu'];
            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();
            adapterPdu.item.push(adapterItemPdu);
        }
        let len=adapterPdu.item.length
        if(len>0){
            loger.log("更新标注数据->发送数量->",adapterPdu.item.length);
            this.sendUniform(adapterPdu, true);
        }else {
            loger.log("更新标注数据->没有可更新的数据");
        }
    }

    /////收到消息处理/////////////////////////////////////////////////////////////////////////////////
    tableInsertHandler(owner, itemIdx, itemData) {
        let whiteBoardModel = this.unPackPdu(owner, itemIdx, itemData);
        loger.log('tableInsertHandler',"activeDocId->",GlobalConfig.activeDocId ,"parentId->", whiteBoardModel.parentId);
        loger.log(whiteBoardModel);
        if (whiteBoardModel) {
            if (GlobalConfig.activeDocId == whiteBoardModel.parentId && GlobalConfig.activeDocCurPage == whiteBoardModel.curPageNo) {
                //loger.log('显示新增的标注->');
                this.insertAandShowAnnotaion(whiteBoardModel);
            }
        }
    }

    tableUpdateHandler(owner, itemIdx, itemData) {
        //白板标注数据不在这个函数中处理,在tableUpdateApeHandler中处理
       /*let whiteBoardModel = this.unPackPdu(owner, itemIdx, itemData);
        loger.log('tableUpdateHandler',"activeDocId->",GlobalConfig.activeDocId ,"parentId->", whiteBoardModel.parentId);
        loger.log(whiteBoardModel);
        if (whiteBoardModel && whiteBoardModel.parentId == GlobalConfig.activeDocId && whiteBoardModel.curPageNo == GlobalConfig.activeDocCurPage) {
            this.updateAandShowAnnotaion();
        }*/
    }

    tableUpdateApeHandler(_tableUpdateItems,_seekTime){
        let tableUpdateItemsLen = _tableUpdateItems.length;
        loger.log('白板标注更新->',"activeDocId->",GlobalConfig.activeDocId ,"数量->", tableUpdateItemsLen);
        for (let i = 0; i < tableUpdateItemsLen; ++i) {
            let tableItem = _tableUpdateItems[i];
            let whiteBoardModel = this.unPackPdu(tableItem.owner, tableItem.itemIdx, tableItem.itemData);
        }
        this.updateAandShowAnnotaion();
    }

    tableDeleteHandler(object_id, tableDeleteData) {
        // console.log("白板收到数据,tableDeleteHandler  object_id="+object_id);//((18<< 16) + 1)=1179649
        loger.log('删除白板数据->', 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.annoInfos[itemIdxs[i]]) {
                    loger.log("删除白板数据:", itemIdxs[i]);
                    delete this.annoInfos[itemIdxs[i]];
                }
            }
        }
        this.updateAandShowAnnotaion();
    }

    //文档更新,白板也要更新
    docUpdateHandler(_data) {
        loger.log("白板收到文档更新的消息");
        //loger.log(_data);

        //如果切换了文档或翻页,清除之前的添加步骤记录
        if (_data.action == ApeConsts.DOC_ACTION_SWITCH_DOC || _data.action == ApeConsts.DOC_ACTION_SWITCH_PAGE) {
            this.insertHistory = [];
        }
        this.updateAandShowAnnotaion();
    }

    //删除白板数据
    docDeleteHandler(_parentId) {
        loger.log("白板收到文档删除的消息->", _parentId);
        for (let key in this.annoInfos) {
            let item = this.annoInfos[key];
            if (item && item.parentId == _parentId) {
                loger.log("文档删除->白板数据也删除,itemIdx:" + key, "_parentId:", _parentId);
                this.sendDeleteAnnotaion({"itemIdx": key});
            }
        }

        //判断当前是否有文档在显示,如果文档没有显示就显示默认的白板数据
        if(GlobalConfig.activeDocId==0){
            this.insertHistory=[];
            this.updateAandShowAnnotaion();
        }
    }

    //增量添加标注
    insertAandShowAnnotaion(_item) {
        let annotaionItems = [_item];
        var updateObj = {
            "isFresh": false,
            "annotaionItems": annotaionItems
        };
        loger.log("新增一条标注数据->显示到界面");
        this._emit(MessageTypes.WHITEBOARD_ANNOTATION_UPDATE, updateObj);
    }

    //整体更新并且显示标注
    updateAandShowAnnotaion() {
        let annotaionItems = [];
        for (let key in this.annoInfos) {
            let item = this.annoInfos[key];
            if (item && item.parentId == GlobalConfig.activeDocId && item.curPageNo == GlobalConfig.activeDocCurPage) {
                annotaionItems.push(item);
                //loger.log("显示和文档对应的白板数据->:", item.itemIdx, "doc->itemIdx:", GlobalConfig.activeDocId, "curPageNo:", GlobalConfig.activeDocCurPage);
            } else {
                //loger.log("不显示白板数据docUpdateHandler->",item.itemIdx);
            }
        }
        var updateObj = {
            "isFresh": true,//清除现有显示的数据
            "annotaionItems": annotaionItems //最新需要显示的数据
        };
        loger.log("清除当前显示的标注---->重绘标注数量--->", annotaionItems.length,"当前文档信息--->DocId->",GlobalConfig.activeDocId,"curPageNo->",GlobalConfig.activeDocCurPage);
        this._emit(MessageTypes.WHITEBOARD_ANNOTATION_UPDATE, updateObj);
    }

    ///////白板数据的封包和解包/////////////////////////////////////////
    packPdu(_param, _itemIdx) {
        //验证坐标点集合数组是否合法
        if (_param.pointGroup == null || _param.pointGroup.length < 1) {
            this._emit(MessageTypes.MCU_ERROR, MessageTypes.ERR_APE_INTERFACE_PARAM_WRONG);
            return null;
        }
        //判断type类型,根据type设置不同的参数
        let whiteBoardModelPdu = new pdu['RCWhiteBoardDataModelPdu'];
        switch (_param.type) {
            case TYPE_BIGHT:
                break;
            case TYPE_LINE:
                break;
            case TYPE_RECT:
                break;
            case TYPE_CIRCLE:
                whiteBoardModelPdu.radius = parseInt(_param.radius);
                break;
            case TYPE_TEXT:
                whiteBoardModelPdu.fontSize = parseInt(_param.fontSize);
                whiteBoardModelPdu.fontName = _param.fontName || null;
                whiteBoardModelPdu.text = _param.text || null;
                break;
            default:
                //其它类型不做处理
                return null;
                break;
        }
        //下面4个是必须的参数
        whiteBoardModelPdu.type = _param.type;
        whiteBoardModelPdu.itemIdx = _itemIdx;
        whiteBoardModelPdu.initiator = GlobalConfig.nodeId;

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

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

        return whiteBoardModelPdu;
    }

    unPackPdu(owner, itemIdx, itemData) {
        try {
            loger.log("白板标注数据->unPackPdu");
            let whiteBoardModelPdu = pdu['RCWhiteBoardDataModelPdu'].decode(itemData);
            let _pointGroup = EngineUtils.arrayFromJsonString(whiteBoardModelPdu.pointGroup);
            whiteBoardModelPdu.pointGroup = _pointGroup;
            this.annoInfos[itemIdx] = whiteBoardModelPdu;
            return whiteBoardModelPdu;
        } catch (err) {
            loger.log("白板标注数据->unPackPdu->Pdu解析错误,itemIdx=" + itemIdx + "  err:" + err.message);
        }
        return null;
    }

}

export default WhiteBoardApe;