From 7f41ffc67843061f280136923836a8836c86b2af Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Wed, 18 Jun 2025 10:01:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=AE=BE=E5=A4=87=E8=BF=90=E7=BB=B4ops?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=A8=A1=E5=9D=97=E5=8A=9F=E8=83=BD=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=9B=91=E6=8E=A7=E5=8A=9F=E8=83=BD=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/frontend/src/locales/zh-CN/menu.ts | 7 + .../src/main/frontend/src/pages/Welcome.tsx | 16 +- .../src/pages/device-ops/device/detail.tsx | 2 +- .../src/pages/device-ops/device/index.tsx | 3 +- .../src/pages/device-ops/group/index.tsx | 3 +- .../pages/device-ops/maintenance/index.tsx | 7 +- .../src/pages/device-ops/monitor/index.tsx | 698 +++++++++++------- .../device/Wayline/components/UpdateForm.tsx | 2 +- .../src/pages/device/Wayline/index.tsx | 2 +- .../TourismFile/components/UpdateForm.tsx | 2 +- .../system/Menu/components/UpdateForm.tsx | 6 +- 11 files changed, 442 insertions(+), 306 deletions(-) diff --git a/zlt-web/react-web/src/main/frontend/src/locales/zh-CN/menu.ts b/zlt-web/react-web/src/main/frontend/src/locales/zh-CN/menu.ts index 6cf7700b..7276d851 100644 --- a/zlt-web/react-web/src/main/frontend/src/locales/zh-CN/menu.ts +++ b/zlt-web/react-web/src/main/frontend/src/locales/zh-CN/menu.ts @@ -50,4 +50,11 @@ export default { 'menu.editor.mind': '脑图编辑器', 'menu.editor.koni': '拓扑编辑器', 'menu.首页': '首页', + 'menu.设备运维': '设备运维', + 'menu.设备运维.设备管理': '设备管理', + 'menu.设备运维.分组管理': '分组管理', + 'menu.设备运维.运维管理': '运维管理', + 'menu.设备运维.设备监控': '设备监控', + 'menu.设备运维.统计分析': '统计分析', + 'menu.设备运维.设备详情': '设备详情', }; diff --git a/zlt-web/react-web/src/main/frontend/src/pages/Welcome.tsx b/zlt-web/react-web/src/main/frontend/src/pages/Welcome.tsx index b7096411..0fdfadbb 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/Welcome.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/Welcome.tsx @@ -29,24 +29,24 @@ interface Statistics { totalDevices: number; onlineDevices: number; deviceOnlineRate: number; - orderTrend: Array<{ + orderTrend: { date: string; type: string; value: number; - }>; - deviceDistribution: Array<{ + }[]; + deviceDistribution: { type: string; value: number; - }>; - incomeData: Array<{ + }[]; + incomeData: { type: string; value: number; - }>; - tourismRanking: Array<{ + }[]; + tourismRanking: { id: string; name: string; value: number; - }>; + }[]; } const Welcome: React.FC = () => { diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/device/detail.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/device/detail.tsx index efcbb90e..a69b0889 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/device/detail.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/device/detail.tsx @@ -120,7 +120,7 @@ const DeviceDetailPage: React.FC = () => { }; const getAlertLevelColor = (level: string) => { - const colorMap: { [key: string]: string } = { + const colorMap: Record = { '高': 'red', '中': 'orange', '低': 'blue', diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/device/index.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/device/index.tsx index 83198d39..38c022a3 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/device/index.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/device/index.tsx @@ -1,6 +1,7 @@ import React, { useRef, useState, useEffect } from 'react'; import { PageContainer } from '@ant-design/pro-layout'; -import { ProTable, ProColumns, ModalForm, ProFormText, ProFormSelect, ProFormUploadButton } from '@ant-design/pro-components'; +import type { ProColumns} from '@ant-design/pro-components'; +import { ProTable, ModalForm, ProFormText, ProFormSelect, ProFormUploadButton } from '@ant-design/pro-components'; import { Button, Space, message, Popconfirm, Tag, Select, Modal, Dropdown, Menu, Upload, Card, Row, Col, Statistic } from 'antd'; import { history } from 'umi'; import { DownOutlined, UploadOutlined, DownloadOutlined, FileExcelOutlined, CheckCircleOutlined, CloseCircleOutlined } from '@ant-design/icons'; diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/group/index.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/group/index.tsx index c4e703e0..b61793cf 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/group/index.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/group/index.tsx @@ -1,6 +1,7 @@ import React, { useRef, useState } from 'react'; import { PageContainer } from '@ant-design/pro-layout'; -import { ProTable, ProColumns, ModalForm, ProFormText, ProFormSelect } from '@ant-design/pro-components'; +import type { ProColumns} from '@ant-design/pro-components'; +import { ProTable, ModalForm, ProFormText, ProFormSelect } from '@ant-design/pro-components'; import { Button, Space, message, Popconfirm, Tag, Card, Row, Col, Statistic } from 'antd'; import { Pie } from '@ant-design/plots'; import { device } from '@/services/device/api'; diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/maintenance/index.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/maintenance/index.tsx index 2cf26c00..d3e6335f 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/maintenance/index.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/maintenance/index.tsx @@ -1,6 +1,7 @@ import React, { useRef, useState } from 'react'; import { PageContainer } from '@ant-design/pro-layout'; -import { ProTable, ProColumns, ModalForm, ProFormText, ProFormSelect, ProFormDatePicker } from '@ant-design/pro-components'; +import type { ProColumns} from '@ant-design/pro-components'; +import { ProTable, ModalForm, ProFormText, ProFormSelect, ProFormDatePicker } from '@ant-design/pro-components'; import { Button, Space, message, Popconfirm, Tag } from 'antd'; interface MaintenanceTask { @@ -83,7 +84,7 @@ const MaintenanceList: React.FC = () => { }; const getStatusColor = (status: string) => { - const colorMap: { [key: string]: string } = { + const colorMap: Record = { '待处理': 'orange', '进行中': 'blue', '已完成': 'green', @@ -93,7 +94,7 @@ const MaintenanceList: React.FC = () => { }; const getPriorityColor = (priority: string) => { - const colorMap: { [key: string]: string } = { + const colorMap: Record = { '高': 'red', '中': 'orange', '低': 'green', diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx index 58e411da..458e2035 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx @@ -180,17 +180,6 @@ const DeviceMonitor: React.FC = () => { timer: null as NodeJS.Timeout | null, }; - const refreshTokenAndReconnect = async () => { - // 强制获取新token - tokenCache.value = ''; - await getToken(); - // 关闭旧ws并重建 - if (wsRef.current) { - wsRef.current.close(); - } - await initWebSocket(); - }; - const getToken = async (expires: number = DEFAULT_EXPIRES) => { const now = Date.now(); if (tokenCache.value && tokenCache.expire > now) { @@ -218,18 +207,29 @@ const DeviceMonitor: React.FC = () => { return token; }; + const refreshTokenAndReconnect = async () => { + // 强制获取新token + tokenCache.value = ''; + await getToken(); + // 关闭旧ws并重建 + if (wsRef.current) { + wsRef.current.close(); + } + await initWebSocket(); + }; + // 获取设备列表 const fetchDeviceList = async () => { + setLoading(true); try { - setLoading(true); const response = await device({ page: 1, limit: 1000, }); - if (response.resp_code === 0) { - const devices = response.data || []; - // 初始化设备数据 - const initializedDevices = devices.map((item: Device) => ({ + console.log('设备列表API响应:', response); + + if (response.resp_code === 0 && response.data) { + const devices = response.data.map((item: any) => ({ ...item, metrics: { temperature: 0, @@ -256,8 +256,11 @@ const DeviceMonitor: React.FC = () => { '4gLinkQuality': 0, } })); - setDeviceList(initializedDevices); - console.log('设备列表已更新:', initializedDevices); + setDeviceList(devices); + console.log('设备列表已更新:', devices); + } else { + console.error('获取设备列表失败:', response.resp_msg); + message.error(response.resp_msg || '获取设备列表失败'); } } catch (error) { console.error('获取设备列表失败:', error); @@ -267,105 +270,6 @@ const DeviceMonitor: React.FC = () => { } }; - // 修改 WebSocket 连接初始化 - const initWebSocket = async () => { - try { - console.log('开始初始化WebSocket连接...'); - const token = await getToken(); - console.log('获取到WebSocket token:', token); - - // 如果已经存在连接,且未关闭,不重复建立 - if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { - console.log('WebSocket已连接,无需重复建立'); - return; - } - if (wsRef.current) { - console.log('关闭已存在的WebSocket连接'); - wsRef.current.close(); - } - - // 建立WebSocket连接 - const wsUrl = `ws://119.45.169.13:8848/messaging/${token}`; - console.log('正在连接WebSocket:', wsUrl); - const ws = new WebSocket(wsUrl); - wsRef.current = ws; - - ws.onopen = () => { - console.log('WebSocket连接已建立'); - setWsConnected(true); - // 启动心跳检测 - startHeartbeat(); - // 根据当前选中的设备订阅消息 - if (selectedDevice !== 'all') { - console.log('当前选中的设备:', selectedDevice); - subscribeDeviceMessages(selectedDevice); - } else { - console.log('未选择具体设备,订阅所有设备在线状态'); - subscribeDeviceMessages(); - } - }; - - ws.onmessage = (event) => { - console.log('收到WebSocket原始消息:', event.data); - try { - const data = JSON.parse(event.data); - console.log('解析后的WebSocket消息:', JSON.stringify(data, null, 2)); - // 处理心跳响应 - if (data.type === 'pong') { - console.log('收到心跳响应'); - return; - } - // 添加调试信息 - console.log('准备处理WebSocket消息...'); - console.log('消息类型:', data.type); - console.log('消息主题:', data.topic); - console.log('消息负载:', JSON.stringify(data.payload, null, 2)); - handleWebSocketMessage(data); - } catch (error) { - console.error('解析WebSocket消息失败:', error); - console.error('原始消息内容:', event.data); - } - }; - - ws.onerror = (error) => { - console.error('WebSocket错误:', error); - message.error('WebSocket连接错误'); - setWsConnected(false); - // 尝试重连 - scheduleReconnect(); - }; - - ws.onclose = (event) => { - console.log('WebSocket连接已关闭,代码:', event.code, '原因:', event.reason); - setWsConnected(false); - // 尝试重连 - scheduleReconnect(); - }; - } catch (error) { - console.error('初始化WebSocket失败:', error); - message.error('初始化WebSocket失败'); - // 尝试重连 - scheduleReconnect(); - } - }; - - // 切换设备时只发订阅,不重建ws - useEffect(() => { - console.log('选中的设备已更改:', selectedDevice); - if (wsRef.current?.readyState === WebSocket.OPEN) { - if (selectedDevice !== 'all') { - console.log('WebSocket已连接,开始订阅设备消息:', selectedDevice); - subscribeDeviceMessages(selectedDevice); - } else { - console.log('WebSocket已连接,订阅所有设备在线状态'); - subscribeDeviceMessages(); - } - } else { - console.log('WebSocket未连接,尝试重新连接...'); - initWebSocket(); - } - }, [selectedDevice]); - // 重连机制 const scheduleReconnect = () => { if (reconnectTimerRef.current) { @@ -402,67 +306,60 @@ const DeviceMonitor: React.FC = () => { } }; - // 添加调试函数 - const debugWebSocketMessage = (data: any) => { - console.log('WebSocket消息调试信息:'); - console.log('消息类型:', data.type); - console.log('消息主题:', data.topic); - console.log('消息负载:', JSON.stringify(data.payload, null, 2)); - console.log('消息ID:', data.id); - }; - - // 修改WebSocket消息处理 - const handleWebSocketMessage = (data: any) => { - console.log('开始处理WebSocket消息:', JSON.stringify(data, null, 2)); - debugWebSocketMessage(data); - - if (data.type === 'result') { - const { topic, payload } = data; - - // 正确提取 deviceSn - const topicParts = topic.split('/'); - // topic: /device/{productId}/{deviceSn}/message/event/fire_alarm - const deviceSn = topicParts[3]; - console.log('从主题中提取的设备编号:', deviceSn); - - // 处理设备在线状态 - if (topic.endsWith('/online')) { - console.log('处理设备在线状态:', deviceSn); - updateDeviceStatus(deviceSn, 1); - } - - // 处理设备离线状态 - if (topic.endsWith('/offline')) { - console.log('处理设备离线状态:', deviceSn); - updateDeviceStatus(deviceSn, 0); - } - - // 处理设备属性上报 - if (topic.includes('/message/property/report')) { - console.log('处理设备属性上报:', deviceSn, JSON.stringify(payload, null, 2)); - handlePropertyReport(deviceSn, payload); - } - - // 处理设备火警事件 - if (topic.includes('/message/event/fire_alarm')) { - console.log('处理设备火警事件:', deviceSn, JSON.stringify(payload, null, 2)); - handleDeviceEvent(deviceSn, payload); - } - } else if (data.type === 'error') { - console.error('WebSocket消息错误:', data); - message.error(data.message || '消息处理错误'); - } - }; - // 更新设备状态 const updateDeviceStatus = (deviceSn: string, status: number) => { console.log('更新设备状态:', deviceSn, status); setDeviceList(prev => { + // 检查设备是否已存在 + const existingDevice = prev.find(d => d.deviceSn === deviceSn); + + if (!existingDevice) { + console.log('设备不存在,创建新设备记录:', deviceSn); + // 如果设备不存在,创建一个新的设备记录 + const newDevice: Device = { + deviceSn: deviceSn, + deviceName: deviceSn, // 使用设备编号作为名称 + deviceType: 1, // 默认类型 + boundStatus: (status as 0 | 1), + createTime: moment().format('YYYY-MM-DD HH:mm:ss'), + loginTime: status === 1 ? moment().format('YYYY-MM-DD HH:mm:ss') : '', + metrics: { + temperature: 0, + humidity: 0, + windSpeed: 0, + rainfall: 0, + environmentTemperature: 0, + positionQuality: 0, + networkQuality: 0, + droneInDock: 0, + batteryCapacity: 0, + batteryState: 0, + workingVoltage: 0, + workingCurrent: 0, + acdcPowerInput: 0, + backupBatteryVoltage: 0, + flightTaskStep: 0, + tiltAngle: 0, + sdrUpQuality: 0, + sdrDownQuality: 0, + wirelessLinkState: 0, + wirelessLinkQuality: 0, + '4gLinkState': 0, + '4gLinkQuality': 0, + } + }; + console.log('创建的新设备:', JSON.stringify(newDevice, null, 2)); + const newList = [...prev, newDevice]; + console.log('设备状态更新后:', newList); + return newList; + } + + // 如果设备存在,更新现有设备 const newList = prev.map(device => device.deviceSn === deviceSn ? { ...device, - boundStatus: status, + boundStatus: (status as 0 | 1), loginTime: status === 1 ? moment().format('YYYY-MM-DD HH:mm:ss') : device.loginTime } : device @@ -505,28 +402,74 @@ const DeviceMonitor: React.FC = () => { } // 新增:同步更新 deviceList 里对应设备的 metrics 字段 - setDeviceList(prev => prev.map(d => - d.deviceSn === deviceSn - ? { - ...d, - metrics: { - ...d.metrics, - temperature: payload.properties.temperature ?? d.metrics.temperature, - humidity: payload.properties.humidity ?? d.metrics.humidity, - windSpeed: payload.properties.wind_speed ?? d.metrics.windSpeed, - rainfall: payload.properties.rainfall ?? d.metrics.rainfall, - environmentTemperature: payload.properties.environment_temperature ?? d.metrics.environmentTemperature, - positionQuality: payload.properties.position_state?.quality ?? d.metrics.positionQuality, - networkQuality: payload.properties.network_state?.quality ?? d.metrics.networkQuality, - droneInDock: payload.properties.drone_in_dock ?? d.metrics.droneInDock, - batteryCapacity: payload.properties.drone_charge_state?.capacity_percent ?? d.metrics.batteryCapacity, - batteryState: payload.properties.drone_charge_state?.state ?? d.metrics.batteryState, - } + setDeviceList(prev => { + // 检查设备是否已存在 + const existingDevice = prev.find(d => d.deviceSn === deviceSn); + + if (!existingDevice) { + console.log('设备不存在,创建新设备记录:', deviceSn); + // 如果设备不存在,创建一个新的设备记录 + const newDevice: Device = { + deviceSn: deviceSn, + deviceName: deviceSn, // 使用设备编号作为名称 + deviceType: 1, // 默认类型 + boundStatus: 1, + createTime: moment().format('YYYY-MM-DD HH:mm:ss'), + loginTime: moment().format('YYYY-MM-DD HH:mm:ss'), + metrics: { + temperature: payload.properties.temperature ?? 0, + humidity: payload.properties.humidity ?? 0, + windSpeed: payload.properties.wind_speed ?? 0, + rainfall: payload.properties.rainfall ?? 0, + environmentTemperature: payload.properties.environment_temperature ?? 0, + positionQuality: payload.properties.position_state?.quality ?? 0, + networkQuality: payload.properties.network_state?.quality ?? 0, + droneInDock: payload.properties.drone_in_dock ?? 0, + batteryCapacity: payload.properties.drone_charge_state?.capacity_percent ?? 0, + batteryState: payload.properties.drone_charge_state?.state ?? 0, + workingVoltage: 0, + workingCurrent: 0, + acdcPowerInput: 0, + backupBatteryVoltage: 0, + flightTaskStep: 0, + tiltAngle: 0, + sdrUpQuality: 0, + sdrDownQuality: 0, + wirelessLinkState: 0, + wirelessLinkQuality: 0, + '4gLinkState': 0, + '4gLinkQuality': 0, } - : d - )); + }; + console.log('创建的新设备:', JSON.stringify(newDevice, null, 2)); + return [...prev, newDevice]; + } + + // 如果设备存在,更新现有设备 + return prev.map(d => + d.deviceSn === deviceSn + ? { + ...d, + metrics: { + ...d.metrics, + temperature: payload.properties.temperature ?? d.metrics.temperature, + humidity: payload.properties.humidity ?? d.metrics.humidity, + windSpeed: payload.properties.wind_speed ?? d.metrics.windSpeed, + rainfall: payload.properties.rainfall ?? d.metrics.rainfall, + environmentTemperature: payload.properties.environment_temperature ?? d.metrics.environmentTemperature, + positionQuality: payload.properties.position_state?.quality ?? d.metrics.positionQuality, + networkQuality: payload.properties.network_state?.quality ?? d.metrics.networkQuality, + droneInDock: payload.properties.drone_in_dock ?? d.metrics.droneInDock, + batteryCapacity: payload.properties.drone_charge_state?.capacity_percent ?? d.metrics.batteryCapacity, + batteryState: payload.properties.drone_charge_state?.state ?? d.metrics.batteryState, + } + } + : d + ); + }); if (newMetrics.length > 0) { + console.log('新的性能指标:', JSON.stringify(newMetrics, null, 2)); setMetricsMap(prev => ({ ...prev, [deviceSn]: [...(prev[deviceSn] || []), ...newMetrics].slice(-72) @@ -540,12 +483,6 @@ const DeviceMonitor: React.FC = () => { console.log('开始处理设备事件:', deviceSn); console.log('设备列表当前状态:', JSON.stringify(deviceList, null, 2)); - const device = deviceList.find(d => d.deviceSn === deviceSn); - if (!device) { - console.log('未找到设备:', deviceSn); - return; - } - const { data } = payload; const deviceData = data.data; console.log('设备数据:', JSON.stringify(deviceData, null, 2)); @@ -553,6 +490,52 @@ const DeviceMonitor: React.FC = () => { // 更新设备状态和实时数据 setDeviceList(prev => { console.log('更新前的设备列表:', JSON.stringify(prev, null, 2)); + + // 检查设备是否已存在 + const existingDevice = prev.find(d => d.deviceSn === deviceSn); + + if (!existingDevice) { + console.log('设备不存在,创建新设备记录:', deviceSn); + // 如果设备不存在,创建一个新的设备记录 + const newDevice: Device = { + deviceSn: deviceSn, + deviceName: payload.headers.deviceName || deviceSn, + deviceType: 1, // 默认类型 + boundStatus: 1, + createTime: moment().format('YYYY-MM-DD HH:mm:ss'), + loginTime: moment(payload.timestamp).format('YYYY-MM-DD HH:mm:ss'), + metrics: { + temperature: deviceData.temperature ?? 0, + humidity: deviceData.humidity ?? 0, + windSpeed: deviceData.wind_speed ?? 0, + rainfall: deviceData.rainfall ?? 0, + environmentTemperature: deviceData.environment_temperature ?? 0, + positionQuality: deviceData.position_state?.quality ?? 0, + networkQuality: deviceData.network_state?.quality ?? 0, + droneInDock: deviceData.drone_in_dock ?? 0, + batteryCapacity: deviceData.drone_charge_state?.capacity_percent ?? 0, + batteryState: deviceData.drone_charge_state?.state ?? 0, + workingVoltage: 0, + workingCurrent: 0, + acdcPowerInput: 0, + backupBatteryVoltage: 0, + flightTaskStep: 0, + tiltAngle: 0, + sdrUpQuality: 0, + sdrDownQuality: 0, + wirelessLinkState: 0, + wirelessLinkQuality: 0, + '4gLinkState': 0, + '4gLinkQuality': 0, + } + }; + console.log('创建的新设备:', JSON.stringify(newDevice, null, 2)); + const newList = [...prev, newDevice]; + console.log('更新后的设备列表:', JSON.stringify(newList, null, 2)); + return newList; + } + + // 如果设备存在,更新现有设备 const newList = prev.map(d => { if (d.deviceSn === deviceSn) { console.log('更新设备数据:', deviceSn, JSON.stringify(deviceData, null, 2)); @@ -660,21 +643,223 @@ const DeviceMonitor: React.FC = () => { } }; + // 修改WebSocket消息处理 + const handleWebSocketMessage = (data: any) => { + console.log('开始处理WebSocket消息:', JSON.stringify(data, null, 2)); + + if (data.type === 'result') { + const { topic, payload } = data; + + // 正确提取 deviceSn + const topicParts = topic.split('/'); + // topic: /device/{productId}/{deviceSn}/message/event/fire_alarm + const deviceSn = topicParts[3]; + console.log('从主题中提取的设备编号:', deviceSn); + + // 处理设备在线状态 + if (topic.endsWith('/online')) { + console.log('处理设备在线状态:', deviceSn); + updateDeviceStatus(deviceSn, 1); + } + + // 处理设备离线状态 + if (topic.endsWith('/offline')) { + console.log('处理设备离线状态:', deviceSn); + updateDeviceStatus(deviceSn, 0); + } + + // 处理设备属性上报 + if (topic.includes('/message/property/report')) { + console.log('处理设备属性上报:', deviceSn, JSON.stringify(payload, null, 2)); + handlePropertyReport(deviceSn, payload); + } + + // 处理设备火警事件 + if (topic.includes('/message/event/fire_alarm')) { + console.log('处理设备火警事件:', deviceSn, JSON.stringify(payload, null, 2)); + handleDeviceEvent(deviceSn, payload); + } + } else if (data.type === 'error') { + console.error('WebSocket消息错误:', data); + message.error(data.message || '消息处理错误'); + } + }; + + // 订阅设备消息 + const subscribeDeviceMessages = (deviceSn?: string) => { + if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) { + console.log('WebSocket未连接,无法订阅消息'); + return; + } + + console.log('开始订阅设备消息:', deviceSn); + + // 取消所有现有订阅 + const unsubAll = { + type: 'unsub', + topic: '/device/*', + id: 'unsub-all', + }; + console.log('取消所有订阅:', JSON.stringify(unsubAll, null, 2)); + wsRef.current.send(JSON.stringify(unsubAll)); + + if (deviceSn) { + // 订阅指定设备的消息 + const subscriptions = [ + { + type: 'sub', + topic: `/device/*/${deviceSn}/online`, + id: 'online-sub', + }, + { + type: 'sub', + topic: `/device/*/${deviceSn}/offline`, + id: 'offline-sub', + }, + { + type: 'sub', + topic: `/device/*/${deviceSn}/message/property/report`, + id: 'property-sub', + }, + { + type: 'sub', + topic: `/device/*/${deviceSn}/message/event/fire_alarm`, + id: 'fire-alarm-sub', + }, + ]; + subscriptions.forEach(sub => { + console.log('发送订阅请求:', JSON.stringify(sub, null, 2)); + wsRef.current?.send(JSON.stringify(sub)); + }); + } else { + // 订阅所有设备的在线状态 + const onlineSub = { + type: 'sub', + topic: '/device/*/online', + id: 'online-sub', + }; + console.log('发送在线状态订阅请求:', JSON.stringify(onlineSub, null, 2)); + wsRef.current.send(JSON.stringify(onlineSub)); + } + console.log('设备消息订阅请求已发送'); + }; + + // 修改 WebSocket 连接初始化 + const initWebSocket = async () => { + try { + console.log('开始初始化WebSocket连接...'); + const token = await getToken(); + console.log('获取到WebSocket token:', token); + + // 如果已经存在连接,且未关闭,不重复建立 + if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { + console.log('WebSocket已连接,无需重复建立'); + return; + } + if (wsRef.current) { + console.log('关闭已存在的WebSocket连接'); + wsRef.current.close(); + } + + // 建立WebSocket连接 + const wsUrl = `ws://119.45.169.13:8848/messaging/${token}`; + console.log('正在连接WebSocket:', wsUrl); + const ws = new WebSocket(wsUrl); + wsRef.current = ws; + + ws.onopen = () => { + console.log('WebSocket连接已建立'); + setWsConnected(true); + // 启动心跳检测 + startHeartbeat(); + // 根据当前选中的设备订阅消息 + if (selectedDevice !== 'all') { + console.log('当前选中的设备:', selectedDevice); + subscribeDeviceMessages(selectedDevice); + } else { + console.log('未选择具体设备,订阅所有设备在线状态'); + subscribeDeviceMessages(); + } + }; + + ws.onmessage = (event) => { + console.log('收到WebSocket原始消息:', event.data); + try { + const data = JSON.parse(event.data); + console.log('解析后的WebSocket消息:', JSON.stringify(data, null, 2)); + // 处理心跳响应 + if (data.type === 'pong') { + console.log('收到心跳响应'); + return; + } + // 添加调试信息 + console.log('准备处理WebSocket消息...'); + console.log('消息类型:', data.type); + console.log('消息主题:', data.topic); + console.log('消息负载:', JSON.stringify(data.payload, null, 2)); + handleWebSocketMessage(data); + } catch (error) { + console.error('解析WebSocket消息失败:', error); + console.error('原始消息内容:', event.data); + } + }; + + ws.onerror = (error) => { + console.error('WebSocket错误:', error); + message.error('WebSocket连接错误'); + setWsConnected(false); + // 尝试重连 + scheduleReconnect(); + }; + + ws.onclose = (event) => { + console.log('WebSocket连接已关闭,代码:', event.code, '原因:', event.reason); + setWsConnected(false); + // 尝试重连 + scheduleReconnect(); + }; + } catch (error) { + console.error('初始化WebSocket失败:', error); + message.error('初始化WebSocket失败'); + // 尝试重连 + scheduleReconnect(); + } + }; + useEffect(() => { - console.log('组件初始化'); - (async () => { - await fetchDeviceList(); - await initWebSocket(); - })(); - return cleanup; + console.log('设备监控页面初始化'); + fetchDeviceList(); + initWebSocket(); + + return () => { + console.log('设备监控页面卸载,清理资源'); + cleanup(); + }; }, []); - // 在组件中添加调试日志 + // 切换设备时只发订阅,不重建ws useEffect(() => { - console.log('选中的设备:', selectedDevice); - console.log('当前设备列表:', JSON.stringify(deviceList, null, 2)); - console.log('当前性能指标:', JSON.stringify(metricsMap, null, 2)); - }, [selectedDevice, deviceList, metricsMap]); + console.log('选中的设备已更改:', selectedDevice); + if (wsRef.current?.readyState === WebSocket.OPEN) { + if (selectedDevice !== 'all') { + console.log('WebSocket已连接,开始订阅设备消息:', selectedDevice); + subscribeDeviceMessages(selectedDevice); + } else { + console.log('WebSocket已连接,订阅所有设备在线状态'); + subscribeDeviceMessages(); + } + } else { + console.log('WebSocket未连接,尝试重新连接...'); + initWebSocket(); + } + }, [selectedDevice]); + + useEffect(() => { + if (selectedDevice !== 'all') { + console.log(`设备选择变更: ${selectedDevice}`); + subscribeDeviceMessages(selectedDevice); + } + }, [selectedDevice]); const alertColumns = [ { @@ -759,25 +944,25 @@ const DeviceMonitor: React.FC = () => { // 修改实时数据卡片的渲染逻辑 const renderRealTimeData = () => { - const device = deviceList.find(d => d.deviceSn === selectedDevice); - console.log('渲染实时数据卡片,当前设备:', JSON.stringify(device, null, 2)); + const currentDevice = deviceList.find(d => d.deviceSn === selectedDevice); + console.log('渲染实时数据卡片,当前设备:', JSON.stringify(currentDevice, null, 2)); - if (!device) { + if (!currentDevice) { console.log('未找到选中的设备'); return null; } // 添加设备指标值的日志 - console.log('设备指标值:', { - temperature: device.metrics.temperature, - humidity: device.metrics.humidity, - environmentTemperature: device.metrics.environmentTemperature, - batteryCapacity: device.metrics.batteryCapacity, - windSpeed: device.metrics.windSpeed, - rainfall: device.metrics.rainfall, - positionQuality: device.metrics.positionQuality, - networkQuality: device.metrics.networkQuality, - }); + console.log('设备指标值:', JSON.stringify({ + temperature: currentDevice.metrics.temperature, + humidity: currentDevice.metrics.humidity, + environmentTemperature: currentDevice.metrics.environmentTemperature, + batteryCapacity: currentDevice.metrics.batteryCapacity, + windSpeed: currentDevice.metrics.windSpeed, + rainfall: currentDevice.metrics.rainfall, + positionQuality: currentDevice.metrics.positionQuality, + networkQuality: currentDevice.metrics.networkQuality, + }, null, 2)); return ( @@ -785,7 +970,7 @@ const DeviceMonitor: React.FC = () => { @@ -795,7 +980,7 @@ const DeviceMonitor: React.FC = () => { @@ -805,7 +990,7 @@ const DeviceMonitor: React.FC = () => { @@ -815,7 +1000,7 @@ const DeviceMonitor: React.FC = () => { @@ -825,7 +1010,7 @@ const DeviceMonitor: React.FC = () => { @@ -835,7 +1020,7 @@ const DeviceMonitor: React.FC = () => { @@ -845,7 +1030,7 @@ const DeviceMonitor: React.FC = () => { @@ -855,7 +1040,7 @@ const DeviceMonitor: React.FC = () => { @@ -865,65 +1050,6 @@ const DeviceMonitor: React.FC = () => { ); }; - // 订阅设备消息 - const subscribeDeviceMessages = (deviceSn?: string) => { - if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) { - console.log('WebSocket未连接,无法订阅消息'); - return; - } - - console.log('开始订阅设备消息:', deviceSn); - - // 取消所有现有订阅 - const unsubAll = { - type: 'unsub', - topic: '/device/*', - id: 'unsub-all', - }; - console.log('取消所有订阅:', JSON.stringify(unsubAll, null, 2)); - wsRef.current.send(JSON.stringify(unsubAll)); - - if (deviceSn) { - // 订阅指定设备的消息 - const subscriptions = [ - { - type: 'sub', - topic: `/device/*/${deviceSn}/online`, - id: 'online-sub', - }, - { - type: 'sub', - topic: `/device/*/${deviceSn}/offline`, - id: 'offline-sub', - }, - { - type: 'sub', - topic: `/device/*/${deviceSn}/message/property/report`, - id: 'property-sub', - }, - { - type: 'sub', - topic: `/device/*/${deviceSn}/message/event/fire_alarm`, - id: 'fire-alarm-sub', - }, - ]; - subscriptions.forEach(sub => { - console.log('发送订阅请求:', JSON.stringify(sub, null, 2)); - wsRef.current?.send(JSON.stringify(sub)); - }); - } else { - // 订阅所有设备的在线状态 - const onlineSub = { - type: 'sub', - topic: '/device/*/online', - id: 'online-sub', - }; - console.log('发送在线状态订阅请求:', JSON.stringify(onlineSub, null, 2)); - wsRef.current.send(JSON.stringify(onlineSub)); - } - console.log('设备消息订阅请求已发送'); - }; - return ( diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device/Wayline/components/UpdateForm.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device/Wayline/components/UpdateForm.tsx index e91a2dd9..183bf9e2 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device/Wayline/components/UpdateForm.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device/Wayline/components/UpdateForm.tsx @@ -7,7 +7,7 @@ import { ProFormText, } from '@ant-design/pro-components'; import { Button, Form, message, Upload } from 'antd'; -import { RcFile } from 'antd/lib/upload'; +import type { RcFile } from 'antd/lib/upload'; import { useState, useRef, useEffect } from 'react'; import styles from '../index.less'; import { TOURISM_OPTIONS, TYPE_OPTIONS, WORKSPACE_OPTIONS } from '../index'; diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device/Wayline/index.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device/Wayline/index.tsx index fb69ae1a..34c86f70 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device/Wayline/index.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device/Wayline/index.tsx @@ -17,7 +17,7 @@ import React, { useRef, useState, useCallback } from 'react'; import UpdateForm from './components/UpdateForm'; import styles from './index.less'; import SparkMD5 from 'spark-md5'; -import { DefaultOptionType } from 'antd/es/select'; +import type { DefaultOptionType } from 'antd/es/select'; const { Link } = Typography; diff --git a/zlt-web/react-web/src/main/frontend/src/pages/orders/TourismFile/components/UpdateForm.tsx b/zlt-web/react-web/src/main/frontend/src/pages/orders/TourismFile/components/UpdateForm.tsx index b66283ef..826e7cb0 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/orders/TourismFile/components/UpdateForm.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/TourismFile/components/UpdateForm.tsx @@ -9,7 +9,7 @@ import { import { Button, Upload, Image, message, Form } from 'antd'; import type { RcFile } from 'antd/lib/upload'; import React, { useState } from 'react'; -import { TYPE_OPTIONS } from '../constants'; +import type { TYPE_OPTIONS } from '../constants'; interface UpdateFormProps { onSubmit: (values: any) => Promise; diff --git a/zlt-web/react-web/src/main/frontend/src/pages/system/Menu/components/UpdateForm.tsx b/zlt-web/react-web/src/main/frontend/src/pages/system/Menu/components/UpdateForm.tsx index 0a52cd30..7614164c 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/system/Menu/components/UpdateForm.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/system/Menu/components/UpdateForm.tsx @@ -49,11 +49,11 @@ const UpdateForm: React.FC = (props) => { request={async () => { const menus = await menuOnes(); if (menus) { - let a = menus.map(item => { + const a = menus.map(item => { return { ...item, key: item.id, title: item.name, value: item.id } }) - let treeData = treeify(a, {}); - let root = { + const treeData = treeify(a, {}); + const root = { title: '顶级目录', value: -1, key: -1,