polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
214 lines • 9.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionHandler = void 0;
const base_handler_1 = require("./base.handler");
const session_service_sdk_1 = require("../services/session.service.sdk");
const api_command_1 = require("../utils/api-command");
const SESSION_STATUS_MAP = {
'unStart': '未开始',
'live': '直播中',
'end': '已结束',
'playback': '回放中',
'expired': '已过期',
};
class SessionHandler extends base_handler_1.BaseHandler {
constructor(authConfig, serviceConfig, sessionService) {
super();
this.sessionService = sessionService ?? new session_service_sdk_1.SessionServiceSdk(authConfig, serviceConfig);
}
async listSessions(options) {
try {
const result = await this.sessionService.getSessionList(options);
this.displaySessionList(result.contents, options.channelId, options.output, result.totalItems);
}
catch (error) {
console.error(error instanceof Error ? error.message : String(error));
}
}
async getSession(options) {
try {
if (!options.channelId || options.channelId.trim() === '') {
throw new Error('channelId is required');
}
if (!options.sessionId || options.sessionId.trim() === '') {
throw new Error('sessionId is required');
}
const session = await this.sessionService.getSession(options.channelId, options.sessionId);
this.displaySessionDetail(session, options.channelId, options.output);
}
catch (error) {
const rawMessage = error instanceof Error ? error.message : String(error);
const message = /场次不存在|not exist/i.test(rawMessage)
? `${rawMessage}\n` +
`提示:\`session get\` 仅支持新版场次(v4 场次详情)。该 sessionId 可能是历史场次,` +
`请使用 \`session legacy-list -c ${options.channelId}\` 或 \`session data-list -c ${options.channelId}\` 查询历史场次的详情。`
: rawMessage;
if (options.output === 'json') {
console.error(JSON.stringify({ error: message }));
}
else {
console.error(message);
}
}
}
async listLegacyChannelSessions(options) {
return this.executeWithErrorHandling(async () => {
const result = await this.sessionService.listLegacyChannelSessions(this.compactOptions(options));
this.displayResult(result, options.output);
}, 'session.legacy-list');
}
async getSessionDataList(options) {
return this.executeWithErrorHandling(async () => {
const result = await this.sessionService.getSessionDataList(this.compactOptions(options));
this.displayResult(result, options.output);
}, 'session.data-list');
}
async getSessionExternalBySession(options) {
return this.executeWithErrorHandling(async () => {
const result = await this.sessionService.getSessionExternalBySession(options.channelId, options.sessionId);
this.displayResult(result, options.output);
}, 'session.external.get');
}
async createSession(options) {
return this.executeWithErrorHandling(async () => {
await (0, api_command_1.confirmWrite)(options.force, `Create session "${options.name}" for channel ${options.channelId}?`);
const result = await this.sessionService.createSession(this.compactOptions(options));
this.displayResult(result, options.output);
}, 'session.create');
}
async updateSession(options) {
return this.executeWithErrorHandling(async () => {
await (0, api_command_1.confirmWrite)(options.force, `Update session ${options.sessionId}?`);
await this.sessionService.updateSession(this.compactOptions(options));
this.displayResult({ channelId: options.channelId, sessionId: options.sessionId, updated: true }, options.output);
}, 'session.update');
}
async deleteSession(options) {
return this.executeWithErrorHandling(async () => {
await (0, api_command_1.confirmWrite)(options.force, `Delete session ${options.sessionId} from channel ${options.channelId}?`);
await this.sessionService.deleteSession(options.channelId, options.sessionId);
this.displayResult({ channelId: options.channelId, sessionId: options.sessionId, deleted: true }, options.output);
}, 'session.delete');
}
async getSessionByExternal(options) {
return this.executeWithErrorHandling(async () => {
const result = await this.sessionService.getSessionByExternal(options.channelId, options.externalSessionId);
this.displayResult(result, options.output);
}, 'session.external.session-list');
}
async listFileIdByExternal(options) {
return this.executeWithErrorHandling(async () => {
const result = await this.sessionService.listFileIdByExternal(options.channelId, options.externalSessionId);
this.displayResult(result, options.output);
}, 'session.external.file-ids');
}
async relevanceSession(options) {
return this.executeWithErrorHandling(async () => {
await (0, api_command_1.confirmWrite)(options.force, `Bind external session ID ${options.externalSessionId} to channel ${options.channelId}?`);
const result = await this.sessionService.relevanceSession(options.channelId, options.externalSessionId);
this.displayResult({ channelId: options.channelId, externalSessionId: options.externalSessionId, result }, options.output);
}, 'session.external.relevance');
}
displaySessionList(contents, channelId, format = 'table', totalItems) {
if (format === 'json') {
const responseData = {
contents,
pageNumber: 1,
pageSize: contents.length,
totalItems: totalItems ?? contents.length,
totalPages: 1,
};
console.log(JSON.stringify(responseData, null, 2));
return;
}
if (contents.length === 0) {
const contextInfo = channelId
? `暂无场次 - 频道: ${channelId}`
: '暂无场次';
this.displayInfo(contextInfo);
return;
}
const contextInfo = channelId
? `场次列表 - 频道: ${channelId}`
: '场次列表 - 全部频道';
this.displayInfo(contextInfo);
this.displayInfo(`共 ${totalItems ?? contents.length} 个场次`);
this.displaySessionTable(contents);
}
compactOptions(options) {
const { output, force, ...rest } = options;
void output;
void force;
return Object.fromEntries(Object.entries(rest).filter(([, value]) => value !== undefined && value !== ''));
}
displayResult(result, format = 'table') {
this.displayData(result ?? { success: true }, format);
}
displaySessionTable(contents) {
const tableData = contents.map((item) => ({
'场次ID': item.sessionId,
'频道ID': item.channelId,
'名称': item.name || '-',
'状态': this.getStatusText(item.status),
'开始时间': this.formatTime(item.startTime),
'结束时间': this.formatTimestamp(item.endTime),
}));
this.displayAsTable(tableData);
}
displaySessionDetail(session, channelId, format = 'table') {
if (format === 'json') {
console.log(JSON.stringify(session, null, 2));
return;
}
this.displayInfo(`场次详情 - 频道: ${channelId}`);
this.displaySessionDetailTable(session);
}
displaySessionDetailTable(session) {
const tableData = [{
'场次ID': session.sessionId,
'频道ID': session.channelId,
'名称': session.name || '-',
'状态': this.getStatusText(session.status),
'开始时间': this.formatTime(session.startTime),
'结束时间': this.formatTimestamp(session.endTime),
'计划开始': this.formatTimestamp(session.planStartTime),
'计划结束': this.formatTimestamp(session.planEndTime),
'推流类型': session.streamType || '-',
'推流客户端': session.pushClient || '-',
'观看地址': session.watchUrl || '-',
}];
this.displayAsTable(tableData);
}
getStatusText(status) {
return SESSION_STATUS_MAP[status] || status;
}
formatTime(timestamp) {
if (!timestamp)
return '-';
if (typeof timestamp === 'string') {
if (timestamp.includes('-') || timestamp.includes('/')) {
return timestamp;
}
const num = parseInt(timestamp, 10);
if (!isNaN(num)) {
return this.formatTimestamp(num);
}
return timestamp;
}
return this.formatTimestamp(timestamp);
}
formatTimestamp(timestamp) {
if (!timestamp)
return '-';
const date = new Date(timestamp);
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
});
}
}
exports.SessionHandler = SessionHandler;
//# sourceMappingURL=session.handler.js.map