CursorApe.js
4.6 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
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;