"use strict"; const { DeviceConnection } = require("./device-connection"); class DeviceManager { constructor({ config, dataCache, logger, uploadManager }) { this.config = config; this.dataCache = dataCache; this.logger = logger; this.uploadManager = uploadManager; this._connections = new Map(); } startAll() { const enabled = this.config.devices.filter((d) => d.enabled !== false); const disabled = this.config.devices.length - enabled.length; for (const device of this.config.devices) { this.dataCache.update(device.ip, { serialNumber: device.serialNumber, status: device.enabled === false ? "disabled" : "pending", lastPollAt: null, lastDataAt: null, rawFrame: null, parsed: null, errorMessage: null, reconnectCount: 0, }); } this.logger.sys(`启动 ${enabled.length} 台设备` + (disabled > 0 ? ` (${disabled} 台已禁用)` : "")); for (const device of enabled) { const conn = new DeviceConnection({ ip: device.ip, port: device.port, serialNumber: device.serialNumber, config: this.config, dataCache: this.dataCache, logger: this.logger, uploadManager: this.uploadManager, }); this._connections.set(device.ip, conn); conn.start(); } } stopAll() { this.logger.sys(`停止 ${this._connections.size} 台设备连接`); for (const [, conn] of this._connections) { conn.stop(); } this._connections.clear(); } restart(ip) { const conn = this._connections.get(ip); if (conn) { this.logger.sys(`${ip} 手动重启`); conn.stop(); conn.start(); } } } module.exports = { DeviceManager };