From d1510118065fb0255fe9c4ed596c1187426aa877 Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Wed, 18 Jun 2025 13:59:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=AE=BE=E5=A4=87=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=9C=B0=E5=9B=BE=E6=98=BE=E7=A4=BA=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E4=BD=8D=E7=BD=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../react-web/src/main/frontend/package.json | 1 + .../src/pages/device-ops/monitor/index.tsx | 225 +++++++++++++++++- 2 files changed, 224 insertions(+), 2 deletions(-) diff --git a/zlt-web/react-web/src/main/frontend/package.json b/zlt-web/react-web/src/main/frontend/package.json index 713bb279..fa3cfafb 100644 --- a/zlt-web/react-web/src/main/frontend/package.json +++ b/zlt-web/react-web/src/main/frontend/package.json @@ -45,6 +45,7 @@ "not ie <= 10" ], "dependencies": { + "@amap/amap-react": "^0.1.5", "@ant-design/charts": "^1.4.2", "@ant-design/icons": "^4.7.0", "@ant-design/pro-components": "1.1.1", 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 22359b07..0a8f8974 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 @@ -1,11 +1,12 @@ import React, { useState, useEffect, useRef } from 'react'; import { PageContainer } from '@ant-design/pro-layout'; -import { Card, Row, Col, DatePicker, Select, Space, Table, Tag, Statistic, message } from 'antd'; +import { Card, Row, Col, DatePicker, Select, Space, Table, Tag, Statistic, message, Modal } from 'antd'; import { Line } from '@ant-design/plots'; import moment from 'moment'; -import { CheckCircleOutlined, CloseCircleOutlined } from '@ant-design/icons'; +import { CheckCircleOutlined, CloseCircleOutlined, EnvironmentOutlined } from '@ant-design/icons'; import { device } from '@/services/device/api'; import { request } from 'umi'; +import { Amap } from '@amap/amap-react'; const { RangePicker } = DatePicker; @@ -16,6 +17,14 @@ interface Device { boundStatus: number; createTime: string; loginTime: string; + // 添加位置信息 + location?: { + latitude: number; + longitude: number; + height?: number; + heading?: number; + updateTime: string; + }; metrics: { temperature: number; humidity: number; @@ -175,6 +184,12 @@ const DeviceMonitor: React.FC = () => { const [currentSubscribedDevice, setCurrentSubscribedDevice] = useState('all'); // 添加订阅ID跟踪 const [currentSubscriptions, setCurrentSubscriptions] = useState([]); + + // 地图相关状态 + const [mapVisible, setMapVisible] = useState(false); + const [selectedMapDevice, setSelectedMapDevice] = useState(null); + const [mapCenter, setMapCenter] = useState<[number, number]>([116.397428, 39.90923]); // 默认北京 + const [mapInitialized, setMapInitialized] = useState(false); // token自动续期相关 const tokenCache = { @@ -577,6 +592,14 @@ const DeviceMonitor: React.FC = () => { boundStatus: 1, createTime: moment().format('YYYY-MM-DD HH:mm:ss'), loginTime: moment(payload.timestamp).format('YYYY-MM-DD HH:mm:ss'), + // 添加位置信息 + location: deviceData.latitude && deviceData.longitude ? { + latitude: deviceData.latitude, + longitude: deviceData.longitude, + height: deviceData.height, + heading: deviceData.heading, + updateTime: moment(payload.timestamp).format('YYYY-MM-DD HH:mm:ss'), + } : undefined, metrics: { temperature: deviceData.temperature ?? 0, humidity: deviceData.humidity ?? 0, @@ -628,10 +651,21 @@ const DeviceMonitor: React.FC = () => { batteryState: deviceData.drone_charge_state?.state ?? d.metrics.batteryState, }; console.log('更新后的设备指标:', JSON.stringify(updatedMetrics, null, 2)); + + // 更新位置信息 + const updatedLocation = deviceData.latitude && deviceData.longitude ? { + latitude: deviceData.latitude, + longitude: deviceData.longitude, + height: deviceData.height, + heading: deviceData.heading, + updateTime: moment(payload.timestamp).format('YYYY-MM-DD HH:mm:ss'), + } : d.location; + const updatedDevice = { ...d, boundStatus: 1, loginTime: moment(payload.timestamp).format('YYYY-MM-DD HH:mm:ss'), + location: updatedLocation, metrics: updatedMetrics }; console.log('更新后的设备数据:', JSON.stringify(updatedDevice, null, 2)); @@ -787,6 +821,73 @@ const DeviceMonitor: React.FC = () => { console.log('- 设备列表:', deviceList.map(d => ({ deviceSn: d.deviceSn, deviceName: d.deviceName }))); }; + // 地图相关功能 + const handleMapDeviceClick = (device: Device) => { + setSelectedMapDevice(device); + }; + + const closeMapDeviceInfo = () => { + setSelectedMapDevice(null); + }; + + // 获取有位置信息的设备 + const getDevicesWithLocation = () => { + return deviceList.filter(device => device.location && device.location.latitude && device.location.longitude); + }; + + // 计算地图中心点 + const calculateMapCenter = () => { + const devicesWithLocation = getDevicesWithLocation(); + if (devicesWithLocation.length === 0) { + return [116.397428, 39.90923]; // 默认北京 + } + + const totalLat = devicesWithLocation.reduce((sum, device) => sum + device.location!.latitude, 0); + const totalLng = devicesWithLocation.reduce((sum, device) => sum + device.location!.longitude, 0); + + return [totalLng / devicesWithLocation.length, totalLat / devicesWithLocation.length]; + }; + + // 渲染地图组件 + const renderDeviceMap = () => { + const devicesWithLocation = getDevicesWithLocation(); + const center = calculateMapCenter(); + + return ( +
+ + {/* 设备标记 */} + {devicesWithLocation.map((device) => ( +
handleMapDeviceClick(device)} + title={`${device.deviceName} (${device.boundStatus === 1 ? '在线' : '离线'})`} + > + +
+ ))} +
+ ); + }; + // 订阅设备消息 const subscribeDeviceMessages = (deviceSn?: string) => { if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) { @@ -1033,6 +1134,22 @@ const DeviceMonitor: React.FC = () => { } }, [selectedDevice]); // 只监听selectedDevice的变化 + // 初始化高德地图 + useEffect(() => { + // 设置高德地图API密钥 + if (typeof window !== 'undefined' && !window.AMap) { + const script = document.createElement('script'); + script.src = `https://webapi.amap.com/maps?v=1.4.15&key=7df3685709e341daed76e877e417e5be`; + script.async = true; + script.onload = () => { + setMapInitialized(true); + }; + document.head.appendChild(script); + } else { + setMapInitialized(true); + } + }, []); + const alertColumns = [ { title: '设备序列号', @@ -1260,6 +1377,23 @@ const DeviceMonitor: React.FC = () => { }} disabled={loading} /> + @@ -1319,7 +1453,94 @@ const DeviceMonitor: React.FC = () => { /> + + {/* 地图组件 */} + {mapVisible && ( + + + {mapInitialized ? ( + renderDeviceMap() + ) : ( +
+
地图加载中...
+
+ )} +
+ + )} + + {/* 设备详情弹窗 */} + + {selectedMapDevice && ( +
+ + + + + + + + + + + + + + + + + + + + + + {selectedMapDevice.location && ( +
+

位置信息

+

纬度: {selectedMapDevice.location.latitude}

+

经度: {selectedMapDevice.location.longitude}

+ {selectedMapDevice.location.height && ( +

高度: {selectedMapDevice.location.height}m

+ )} + {selectedMapDevice.location.heading && ( +

航向: {selectedMapDevice.location.heading}°

+ )} +

更新时间: {selectedMapDevice.location.updateTime}

+
+ )} +
+ )} +
); };