From 6182be28072f40511de38510b7018915f147c796 Mon Sep 17 00:00:00 2001 From: luoyifan Date: Sat, 5 Jul 2025 19:16:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=8A=B6=E6=80=81=E7=9B=91?= =?UTF-8?q?=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/manager/BackendMessageReceiver.ts | 38 +++++--- src/core/script/LCCScript.ts | 2 +- src/editor/widgets/monitor/MonitorMeta.ts | 4 +- src/editor/widgets/monitor/MonitorView.vue | 141 +++++++++++++---------------- src/types/LCC.d.ts | 4 +- 5 files changed, 94 insertions(+), 95 deletions(-) diff --git a/src/core/manager/BackendMessageReceiver.ts b/src/core/manager/BackendMessageReceiver.ts index 0e08e19..c8e974d 100644 --- a/src/core/manager/BackendMessageReceiver.ts +++ b/src/core/manager/BackendMessageReceiver.ts @@ -44,6 +44,9 @@ export default class BackendMessageReceiver { // 启动MQTT连接 public async start(projectUuid: string, envId: number, config: MqttConfig): Promise { + this.projectUuid = projectUuid + this.envId = envId + // 如果已经连接,先断开 if (this.client?.connected) { await this.dispose() @@ -66,6 +69,7 @@ export default class BackendMessageReceiver { return new Promise((resolve) => { try { + console.log('Connecting to MQTT broker:', config.websocket) this.client = mqtt.connect(config.websocket, options) // 连接成功 @@ -135,30 +139,30 @@ export default class BackendMessageReceiver { const envId = this.envId switch (type) { case 'ServerState': - return [`lcc/${projId}/${envId}/server`, this.handleServerState] + return [`/lcc/${projId}/${envId}/server`, this.handleServerState] case 'ClientState': - return [`lcc/${projId}/${envId}/client`, this.handleClientState] + return [`/lcc/${projId}/${envId}/client`, this.handleClientState] case 'TaskUpdate': - return [`lcc/${projId}/${envId}/task`, this.handleTaskMonitor] + return [`/lcc/${projId}/${envId}/task`, this.handleTaskMonitor] case 'InvUpdate': - return [`lcc/${projId}/${envId}/inv/#`, this.handleInventoryMonitor] + return [`/lcc/${projId}/${envId}/inv/#`, this.handleInventoryMonitor] case 'DeviceStatus': - return [`lcc/${projId}/${envId}/device/+/status`, this.handleDeviceStatus] + return [`/lcc/${projId}/${envId}/device/+/status`, this.handleDeviceStatus] case 'DeviceAlive': - return [`lcc/${projId}/${envId}/device/+/alive`, this.handleDeviceAlive] + return [`/lcc/${projId}/${envId}/device/+/alive`, this.handleDeviceAlive] case 'Logs': - return [`lcc/${projId}/${envId}/log/#`, this.handleLogMonitor] + return [`/lcc/${projId}/${envId}/log/#`, this.handleLogMonitor] case 'Alarm': - return [`lcc/${projId}/${envId}/alarm`, this.handleAlarmMonitor] + return [`/lcc/${projId}/${envId}/alarm`, this.handleAlarmMonitor] case 'ScriptUpdate': - return [`lcc/${projId}/script`, this.handleScriptSystem] + return [`/lcc/${projId}/script`, this.handleScriptSystem] } throw new Error(`Invalid topic for type ${type}`) } // 订阅主题 - public subscribe(type: BackendTopicType, handler: BackendMessageHandler): void { + public subscribe(type: BackendTopicType, handler: BackendMessageHandler): StopSubscribe { if (!this.client?.connected) { throw new Error('Cannot subscribe - MQTT not connected') } @@ -175,15 +179,18 @@ export default class BackendMessageReceiver { if (!this.state.subscribedTopics.includes(topic) && this.client) { const options: IClientSubscribeOptions = { qos: 1 } + console.log(`client.subscribe(${topic},`, options) this.client.subscribe(topic, options, (err) => { if (err) { console.error(`Failed to subscribe to ${topic}:`, err) - } else { - this.state.subscribedTopics = [...this.state.subscribedTopics, topic] - console.log(`Subscribed to ${topic}`) } }) } + + return () => { + // 取消订阅 + this.unsubscribe(type, handler) + } } // 取消订阅 @@ -213,7 +220,10 @@ export default class BackendMessageReceiver { // 如果没有处理器了,取消订阅 if (!this.handlers.has(topic) && this.client) { - this.client.unsubscribe(topic, {}, (err) => { + + const options = {} + console.log(`client.unsubscribe(${topic},`, options) + this.client.unsubscribe(topic, options, (err) => { if (err) { console.error(`Failed to unsubscribe from ${topic}:`, err) } else { diff --git a/src/core/script/LCCScript.ts b/src/core/script/LCCScript.ts index e631e03..b6ddb54 100644 --- a/src/core/script/LCCScript.ts +++ b/src/core/script/LCCScript.ts @@ -121,7 +121,7 @@ export default class LCCScript implements LCC { } subscribe(topicType: BackendTopicType, eventHandler: BackendMessageHandler) { - worldModel.backendMessageReceiver.subscribe(topicType, eventHandler) + return worldModel.backendMessageReceiver.subscribe(topicType, eventHandler) } unsubscribe(topicType: BackendTopicType, eventHandler: BackendMessageHandler) { diff --git a/src/editor/widgets/monitor/MonitorMeta.ts b/src/editor/widgets/monitor/MonitorMeta.ts index 07cddde..6d581a2 100644 --- a/src/editor/widgets/monitor/MonitorMeta.ts +++ b/src/editor/widgets/monitor/MonitorMeta.ts @@ -4,9 +4,9 @@ import MonitorView from './MonitorView.vue' export default defineWidget({ name: 'monitor', - title: '监控', + title: '设备监控', icon: renderIcon('antd DashboardOutlined'), side: 'left', order: 2, component: MonitorView -}) \ No newline at end of file +}) diff --git a/src/editor/widgets/monitor/MonitorView.vue b/src/editor/widgets/monitor/MonitorView.vue index 4bbc398..b6d1c15 100644 --- a/src/editor/widgets/monitor/MonitorView.vue +++ b/src/editor/widgets/monitor/MonitorView.vue @@ -1,6 +1,6 @@