UNPKG

mira-app-server

Version:

Mira Server - standalone server application using mira-app-core

101 lines 3.76 kB
"use strict"; /** * 设备模块命令 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.registerDevices = registerDevices; const client_1 = require("../client"); const format_1 = require("../format"); function registerDevices(program) { const devices = program.command('devices').description('设备管理'); devices .command('list') .description('获取所有设备连接信息') .option('--library <id>', '按素材库 ID 筛选') .action(async (options) => { try { const { client } = (0, client_1.getClient)(); if (options.library) { const list = await client.devices().getByLibrary(options.library); const rows = list.map(d => ({ clientId: d.clientId, libraryId: d.libraryId, status: d.status, ipAddress: d.ipAddress, lastActivity: d.lastActivity, })); (0, format_1.output)(rows, () => (0, format_1.formatTable)(rows)); } else { // SDK 已提取 data:得到 Record<libraryId, Device[]> const devicesByLib = (await client.devices().getAll()); const rows = []; Object.values(devicesByLib || {}).forEach(arr => { arr.forEach((d) => { rows.push({ clientId: d.clientId, libraryId: d.libraryId, status: d.status, ipAddress: d.ipAddress, lastActivity: d.lastActivity, }); }); }); (0, format_1.output)(rows, () => (0, format_1.formatTable)(rows)); } } catch (error) { (0, format_1.fatal)(error); } }); devices .command('stats') .description('获取设备统计信息') .action(async () => { try { const { client } = (0, client_1.getClient)(); // SDK 的 HttpClient 已自动提取 data 字段,直接输出 const stats = await client.devices().getStats(); (0, format_1.output)(stats, () => (0, format_1.formatKeyValue)(stats)); } catch (error) { (0, format_1.fatal)(error); } }); devices .command('disconnect <clientId> <libraryId>') .description('断开设备连接') .action(async (clientId, libraryId) => { try { const { client } = (0, client_1.getClient)(); const res = await client.devices().disconnect(clientId, libraryId); (0, format_1.success)(`设备 ${clientId} 已断开`); (0, format_1.output)(res); } catch (error) { (0, format_1.fatal)(error); } }); devices .command('send <clientId> <libraryId> <message>') .description('向设备发送消息(message 为 JSON 字符串)') .action(async (clientId, libraryId, message) => { try { let msg; try { msg = JSON.parse(message); } catch { msg = message; // 纯文本也允许 } const { client } = (0, client_1.getClient)(); const res = await client.devices().sendMessage(clientId, libraryId, msg); (0, format_1.success)(`消息已发送到 ${clientId}`); (0, format_1.output)(res); } catch (error) { (0, format_1.fatal)(error); } }); } //# sourceMappingURL=devices.js.map