UNPKG

agora-edu-core

Version:

Core APIs for building an online classroom

109 lines (107 loc) 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LogReporter = void 0; var _agoraRteSdk = require("agora-rte-sdk"); var _ = require("."); var _class, _LogReporter; function _applyDecoratedDescriptor(i, e, r, n, l) { var a = {}; return Object.keys(n).forEach(function (i) { a[i] = n[i]; }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = !0), a = r.slice().reverse().reduce(function (r, n) { return n(i, e, r) || r; }, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a; } let LogReporter = exports.LogReporter = (_class = (_LogReporter = class LogReporter { constructor(store) { this._task = void 0; this._lastUploadTs = 0; this._isProcessing = false; this._store = void 0; this._store = store; } start() { this._task = _agoraRteSdk.Scheduler.shared.addIntervalTask(this._execute.bind(this), _agoraRteSdk.Scheduler.Duration.second(1)); } stop() { var _this$_task; (_this$_task = this._task) === null || _this$_task === void 0 ? void 0 : _this$_task.stop(); } get _isTimeToUpload() { return Date.now() - this._lastUploadTs > LogReporter.UPLOAD_PERIOD_IN_SECONDS; } get _engine() { return this._store.connectionStore.engine; } async _execute() { if (!this._isTimeToUpload) { return; } this._uploadLog(); } async _uploadLog() { if (this._isProcessing) { return; } try { this._isProcessing = true; const { roomUuid, roomName, roomType, userUuid, userName, role } = _.EduClassroomConfig.shared.sessionInfo; const engine = this._engine; if (!engine) { _agoraRteSdk.Logger.warn('Agora RTE Engine not ready, cannot upload logs'); return; } // upload await engine.uploadSDKLogToAgoraService({ roomUuid, roomName, roomType, userUuid, userName, role }); this._lastUploadTs = Date.now(); } catch (e) { _agoraRteSdk.Logger.info('failed to collect logs', e); } finally { this._isProcessing = false; } } enableLogReport() { _.EduEventCenter.shared.onClassroomEvents(this._classroomEventHandler); _.EduErrorCenter.shared.onError(this._handleError); } disableLogReport() { this.stop(); _.EduEventCenter.shared.offClassroomEvents(this._classroomEventHandler); _.EduErrorCenter.shared.offError(this._handleError); } uploadLog() { this._uploadLog(); } _handleError(code) { if (code === _.AGEduErrorCode.EDU_ERR_JOIN_CLASSROOM_FAIL) { this.stop(); this.disableLogReport(); this._uploadLog(); } } _classroomEventHandler(event, ...args) { //1.进入房间失败 //2.进入房间后每分钟 //3.离开房间 //4.房间结束 const isClassClosed = event === _.AgoraEduClassroomEvent.ClassStateChanged && args[0] === _.ClassState.close; const isKickedOut = event === _.AgoraEduClassroomEvent.KickOut; const isJoined = event === _.AgoraEduClassroomEvent.Ready; if (isJoined) { this.start(); } else if (isClassClosed || isKickedOut) { this.stop(); this.disableLogReport(); this._uploadLog(); } } }, _LogReporter.UPLOAD_PERIOD_IN_SECONDS = 1000 * 60 * 5, _LogReporter), _applyDecoratedDescriptor(_class.prototype, "_handleError", [_agoraRteSdk.bound], Object.getOwnPropertyDescriptor(_class.prototype, "_handleError"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "_classroomEventHandler", [_agoraRteSdk.bound], Object.getOwnPropertyDescriptor(_class.prototype, "_classroomEventHandler"), _class.prototype), _class);