From d6c7f209c71498a77ed1376bca8bd347e8816a47 Mon Sep 17 00:00:00 2001 From: BBIT-Kai <2911862937@qq.com> Date: Tue, 30 Dec 2025 17:56:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=89=8D=E7=AB=AFWebSocket?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vue2/apps/web-antd/src/api/iot/device.ts | 4 +-- vue2/apps/web-antd/src/api/iot/update.ts | 6 +++-- .../web-antd/src/views/iot/device/data.ts | 22 ++++++--------- .../web-antd/src/views/iot/device/index.vue | 27 ++++++++++++------- .../web-antd/src/views/iot/update/data.ts | 2 +- .../src/views/sentinel/record/index.vue | 8 ------ 6 files changed, 32 insertions(+), 37 deletions(-) diff --git a/vue2/apps/web-antd/src/api/iot/device.ts b/vue2/apps/web-antd/src/api/iot/device.ts index 6ca38a9..98c691b 100644 --- a/vue2/apps/web-antd/src/api/iot/device.ts +++ b/vue2/apps/web-antd/src/api/iot/device.ts @@ -78,13 +78,13 @@ async function deleteDevice(id: string) { async function iotSendCommand( id: string, command: string, - project: string, + dept_id: string, device_type: string, ) { return pyRequestClient.post('/iot/common/device/command', { id, command, - project, + dept_id, device_type, }); } diff --git a/vue2/apps/web-antd/src/api/iot/update.ts b/vue2/apps/web-antd/src/api/iot/update.ts index 1d6bf41..bf15bbf 100644 --- a/vue2/apps/web-antd/src/api/iot/update.ts +++ b/vue2/apps/web-antd/src/api/iot/update.ts @@ -45,8 +45,10 @@ async function deleteUpdate(id: string) { /** * 上传 */ -export async function getUpdateUploadUrl() { - return pyRequestClient.get('/iot/common/update/getUploadUrl'); +export async function getUpdateUploadUrl(filename: string) { + return pyRequestClient.get('/iot/common/update/getUploadUrl',{ + params: {filename} + }); } /** diff --git a/vue2/apps/web-antd/src/views/iot/device/data.ts b/vue2/apps/web-antd/src/views/iot/device/data.ts index d30b810..9a68795 100644 --- a/vue2/apps/web-antd/src/views/iot/device/data.ts +++ b/vue2/apps/web-antd/src/views/iot/device/data.ts @@ -172,12 +172,6 @@ export function useColumns( title: '当前状态', width: 100, }, - { - field: 'project', - slots: { default: 'project' }, - title: '所属IoT项目', - width: 100, - }, { field: 'device_type', slots: { default: 'deviceType' }, @@ -187,7 +181,7 @@ export function useColumns( { field: 'dept_name', minWidth: 120, - title: '所属组织', + title: '所属组织/项目', }, { cellRender: { @@ -207,11 +201,6 @@ export function useColumns( title: '管理员', width: 100, }, - { - field: 'created_at', - title: '创建时间', - width: 150, - }, { field: 'remark', title: '备注', @@ -259,8 +248,13 @@ export function useColumns( }, { field: 'last_seen', - title: '信息更新时间', - width: 100, + title: '设备信息更新时间', + width: 150, + }, + { + field: 'created_at', + title: '创建时间', + width: 150, }, { field: 'operation', diff --git a/vue2/apps/web-antd/src/views/iot/device/index.vue b/vue2/apps/web-antd/src/views/iot/device/index.vue index c8e55ba..3243751 100644 --- a/vue2/apps/web-antd/src/views/iot/device/index.vue +++ b/vue2/apps/web-antd/src/views/iot/device/index.vue @@ -24,6 +24,7 @@ import { import { useColumns, useGridFormSchema } from './data'; import Form from './form.vue'; +import { useAccessStore } from "@vben/stores"; const [FormDrawer, formDrawerApi] = useVbenDrawer({ connectedComponent: Form, @@ -164,7 +165,7 @@ function onEdit(row: SystemDeviceApi.SystemDevice) { } async function onCommand(data: SystemDeviceApi.SystemDevice, command: string) { - await iotSendCommand(data.name, command, data.project, data.device_type); + await iotSendCommand(data.name, command, data.dept_id, data.device_type); } function onDelete(row: SystemDeviceApi.SystemDevice) { @@ -197,18 +198,30 @@ function onCreate() { const wsState = reactive({ ws: null as null | WebSocket, }); +let ws: null | WebSocket = null; -onMounted(() => { - const ws = new WebSocket('wss://ai.ronsunny.cn:8090/ai/iot/ws/device-status'); +function createWs(token: string) { + ws = new WebSocket( + // `wss://ai.ronsunny.cn:8090/ai/iot/ws/device-status?token=${token}`, + `ws://127.0.0.1:13011/iot/ws/device-status?token=${token}`, + ); wsState.ws = ws; ws.onmessage = async (e) => { const msg = JSON.parse(e.data); console.log('WS 事件', msg); + if (msg.type === 'status') { + await gridApi.query(); + } // 简单策略:任何设备上下线都刷新列表 后续改进针对单行 - await gridApi.query(); }; +} +const accessStore = useAccessStore(); +onMounted(() => { + if (accessStore.accessToken) { + createWs(accessStore.accessToken); + } }); onBeforeUnmount(() => { @@ -238,12 +251,6 @@ onBeforeUnmount(() => { 其他 - diff --git a/vue2/apps/web-antd/src/views/iot/update/data.ts b/vue2/apps/web-antd/src/views/iot/update/data.ts index 97e7ed1..981aadb 100644 --- a/vue2/apps/web-antd/src/views/iot/update/data.ts +++ b/vue2/apps/web-antd/src/views/iot/update/data.ts @@ -72,7 +72,7 @@ export function useFormSchema(uploadState: { uploadState.sizeKb = Number((file.size / 1024).toFixed(2)); // 获取上传地址 - const { uploadUrl, id } = await api.getUpdateUploadUrl(); + const { uploadUrl, id } = await api.getUpdateUploadUrl(file.name); // PUT 上传 await axios.put(uploadUrl, file, { diff --git a/vue2/apps/web-antd/src/views/sentinel/record/index.vue b/vue2/apps/web-antd/src/views/sentinel/record/index.vue index 98abdaa..d1ea927 100644 --- a/vue2/apps/web-antd/src/views/sentinel/record/index.vue +++ b/vue2/apps/web-antd/src/views/sentinel/record/index.vue @@ -192,14 +192,6 @@ onMounted(() => { createWs(accessStore.accessToken); } }); -watch( - () => accessStore.accessToken, - (newToken, oldToken) => { - if (newToken && newToken !== oldToken) { - createWs(newToken); - } - }, -); onBeforeUnmount(() => { if (ws) {