printServer/services/printerComponent.js

45 lines
1.3 KiB
JavaScript

import service from '../core/BaseService.js';
import { isRecordingMode } from '../utils/runtime.js';
class PrinterComponentService extends service {
constructor() {
super();
this.getAll = this.getAll.bind(this); // 确保上下文绑定
this.getStatus = this.getStatus.bind(this);
}
_guardRecordingMode() {
if (isRecordingMode(this.config)) {
throw new Error('当前运行在录制模式,不支持打印组件操作');
}
}
// 获取所有打印组件
getAll() {
this._guardRecordingMode();
const { app } = this;
if (app && app.printerComponents) {
const components = app.printerComponents.getAllPorts();
const status = app.printerComponents.getAllStatus();
return { components, status };
}
// 默认返回静态数据
return { components: this.components, status: this.components.map(c => ({ name: c.name, status: c.status })) };
}
// 获取指定组件状态
getStatus(name) {
this._guardRecordingMode();
const { app } = this;
if (app && app.printerComponents) {
return app.printerComponents.getStatus(name);
}
const comp = this.components.find(c => c.name === name);
return comp ? comp.status : '未知';
}
// 可扩展:添加、删除、更新组件等
}
export default new PrinterComponentService();