fix:设备监控新增地图显示设备位置功能
This commit is contained in:
@@ -45,6 +45,7 @@
|
|||||||
"not ie <= 10"
|
"not ie <= 10"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@amap/amap-react": "^0.1.5",
|
||||||
"@ant-design/charts": "^1.4.2",
|
"@ant-design/charts": "^1.4.2",
|
||||||
"@ant-design/icons": "^4.7.0",
|
"@ant-design/icons": "^4.7.0",
|
||||||
"@ant-design/pro-components": "1.1.1",
|
"@ant-design/pro-components": "1.1.1",
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import React, { useState, useEffect, useRef } from 'react';
|
import React, { useState, useEffect, useRef } from 'react';
|
||||||
import { PageContainer } from '@ant-design/pro-layout';
|
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 { Line } from '@ant-design/plots';
|
||||||
import moment from 'moment';
|
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 { device } from '@/services/device/api';
|
||||||
import { request } from 'umi';
|
import { request } from 'umi';
|
||||||
|
import { Amap } from '@amap/amap-react';
|
||||||
|
|
||||||
const { RangePicker } = DatePicker;
|
const { RangePicker } = DatePicker;
|
||||||
|
|
||||||
@@ -16,6 +17,14 @@ interface Device {
|
|||||||
boundStatus: number;
|
boundStatus: number;
|
||||||
createTime: string;
|
createTime: string;
|
||||||
loginTime: string;
|
loginTime: string;
|
||||||
|
// 添加位置信息
|
||||||
|
location?: {
|
||||||
|
latitude: number;
|
||||||
|
longitude: number;
|
||||||
|
height?: number;
|
||||||
|
heading?: number;
|
||||||
|
updateTime: string;
|
||||||
|
};
|
||||||
metrics: {
|
metrics: {
|
||||||
temperature: number;
|
temperature: number;
|
||||||
humidity: number;
|
humidity: number;
|
||||||
@@ -175,6 +184,12 @@ const DeviceMonitor: React.FC = () => {
|
|||||||
const [currentSubscribedDevice, setCurrentSubscribedDevice] = useState<string>('all');
|
const [currentSubscribedDevice, setCurrentSubscribedDevice] = useState<string>('all');
|
||||||
// 添加订阅ID跟踪
|
// 添加订阅ID跟踪
|
||||||
const [currentSubscriptions, setCurrentSubscriptions] = useState<string[]>([]);
|
const [currentSubscriptions, setCurrentSubscriptions] = useState<string[]>([]);
|
||||||
|
|
||||||
|
// 地图相关状态
|
||||||
|
const [mapVisible, setMapVisible] = useState(false);
|
||||||
|
const [selectedMapDevice, setSelectedMapDevice] = useState<Device | null>(null);
|
||||||
|
const [mapCenter, setMapCenter] = useState<[number, number]>([116.397428, 39.90923]); // 默认北京
|
||||||
|
const [mapInitialized, setMapInitialized] = useState(false);
|
||||||
|
|
||||||
// token自动续期相关
|
// token自动续期相关
|
||||||
const tokenCache = {
|
const tokenCache = {
|
||||||
@@ -577,6 +592,14 @@ const DeviceMonitor: React.FC = () => {
|
|||||||
boundStatus: 1,
|
boundStatus: 1,
|
||||||
createTime: moment().format('YYYY-MM-DD HH:mm:ss'),
|
createTime: moment().format('YYYY-MM-DD HH:mm:ss'),
|
||||||
loginTime: moment(payload.timestamp).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: {
|
metrics: {
|
||||||
temperature: deviceData.temperature ?? 0,
|
temperature: deviceData.temperature ?? 0,
|
||||||
humidity: deviceData.humidity ?? 0,
|
humidity: deviceData.humidity ?? 0,
|
||||||
@@ -628,10 +651,21 @@ const DeviceMonitor: React.FC = () => {
|
|||||||
batteryState: deviceData.drone_charge_state?.state ?? d.metrics.batteryState,
|
batteryState: deviceData.drone_charge_state?.state ?? d.metrics.batteryState,
|
||||||
};
|
};
|
||||||
console.log('更新后的设备指标:', JSON.stringify(updatedMetrics, null, 2));
|
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 = {
|
const updatedDevice = {
|
||||||
...d,
|
...d,
|
||||||
boundStatus: 1,
|
boundStatus: 1,
|
||||||
loginTime: moment(payload.timestamp).format('YYYY-MM-DD HH:mm:ss'),
|
loginTime: moment(payload.timestamp).format('YYYY-MM-DD HH:mm:ss'),
|
||||||
|
location: updatedLocation,
|
||||||
metrics: updatedMetrics
|
metrics: updatedMetrics
|
||||||
};
|
};
|
||||||
console.log('更新后的设备数据:', JSON.stringify(updatedDevice, null, 2));
|
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 })));
|
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 (
|
||||||
|
<div style={{ height: '600px', width: '100%', position: 'relative' }}>
|
||||||
|
<Amap
|
||||||
|
zoom={12}
|
||||||
|
center={center}
|
||||||
|
mapStyle="amap://styles/normal"
|
||||||
|
/>
|
||||||
|
{/* 设备标记 */}
|
||||||
|
{devicesWithLocation.map((device) => (
|
||||||
|
<div
|
||||||
|
key={device.deviceSn}
|
||||||
|
style={{
|
||||||
|
position: 'absolute',
|
||||||
|
left: '50%',
|
||||||
|
top: '50%',
|
||||||
|
transform: 'translate(-50%, -50%)',
|
||||||
|
cursor: 'pointer',
|
||||||
|
zIndex: 1000,
|
||||||
|
}}
|
||||||
|
onClick={() => handleMapDeviceClick(device)}
|
||||||
|
title={`${device.deviceName} (${device.boundStatus === 1 ? '在线' : '离线'})`}
|
||||||
|
>
|
||||||
|
<EnvironmentOutlined
|
||||||
|
style={{
|
||||||
|
fontSize: '24px',
|
||||||
|
color: device.boundStatus === 1 ? '#52c41a' : '#ff4d4f',
|
||||||
|
filter: 'drop-shadow(2px 2px 4px rgba(0,0,0,0.3))'
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
// 订阅设备消息
|
// 订阅设备消息
|
||||||
const subscribeDeviceMessages = (deviceSn?: string) => {
|
const subscribeDeviceMessages = (deviceSn?: string) => {
|
||||||
if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) {
|
if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) {
|
||||||
@@ -1033,6 +1134,22 @@ const DeviceMonitor: React.FC = () => {
|
|||||||
}
|
}
|
||||||
}, [selectedDevice]); // 只监听selectedDevice的变化
|
}, [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 = [
|
const alertColumns = [
|
||||||
{
|
{
|
||||||
title: '设备序列号',
|
title: '设备序列号',
|
||||||
@@ -1260,6 +1377,23 @@ const DeviceMonitor: React.FC = () => {
|
|||||||
}}
|
}}
|
||||||
disabled={loading}
|
disabled={loading}
|
||||||
/>
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setMapVisible(!mapVisible)}
|
||||||
|
style={{
|
||||||
|
padding: '4px 15px',
|
||||||
|
border: '1px solid #d9d9d9',
|
||||||
|
borderRadius: '6px',
|
||||||
|
background: '#fff',
|
||||||
|
cursor: 'pointer',
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
gap: '4px'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<EnvironmentOutlined />
|
||||||
|
{mapVisible ? '隐藏地图' : '显示地图'}
|
||||||
|
</button>
|
||||||
</Space>
|
</Space>
|
||||||
</Card>
|
</Card>
|
||||||
</Col>
|
</Col>
|
||||||
@@ -1319,7 +1453,94 @@ const DeviceMonitor: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
</Col>
|
</Col>
|
||||||
|
|
||||||
|
{/* 地图组件 */}
|
||||||
|
{mapVisible && (
|
||||||
|
<Col span={24}>
|
||||||
|
<Card title="设备位置地图">
|
||||||
|
{mapInitialized ? (
|
||||||
|
renderDeviceMap()
|
||||||
|
) : (
|
||||||
|
<div style={{ height: '600px', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
||||||
|
<div>地图加载中...</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</Col>
|
||||||
|
)}
|
||||||
</Row>
|
</Row>
|
||||||
|
|
||||||
|
{/* 设备详情弹窗 */}
|
||||||
|
<Modal
|
||||||
|
title="设备详情"
|
||||||
|
open={!!selectedMapDevice}
|
||||||
|
onCancel={closeMapDeviceInfo}
|
||||||
|
footer={null}
|
||||||
|
width={600}
|
||||||
|
>
|
||||||
|
{selectedMapDevice && (
|
||||||
|
<div>
|
||||||
|
<Row gutter={[16, 16]}>
|
||||||
|
<Col span={12}>
|
||||||
|
<Statistic
|
||||||
|
title="设备名称"
|
||||||
|
value={selectedMapDevice.deviceName}
|
||||||
|
/>
|
||||||
|
</Col>
|
||||||
|
<Col span={12}>
|
||||||
|
<Statistic
|
||||||
|
title="设备状态"
|
||||||
|
value={selectedMapDevice.boundStatus === 1 ? '在线' : '离线'}
|
||||||
|
valueStyle={{ color: selectedMapDevice.boundStatus === 1 ? '#3f8600' : '#cf1322' }}
|
||||||
|
/>
|
||||||
|
</Col>
|
||||||
|
<Col span={12}>
|
||||||
|
<Statistic
|
||||||
|
title="温度"
|
||||||
|
value={selectedMapDevice.metrics.temperature}
|
||||||
|
suffix="°C"
|
||||||
|
/>
|
||||||
|
</Col>
|
||||||
|
<Col span={12}>
|
||||||
|
<Statistic
|
||||||
|
title="湿度"
|
||||||
|
value={selectedMapDevice.metrics.humidity}
|
||||||
|
suffix="%"
|
||||||
|
/>
|
||||||
|
</Col>
|
||||||
|
<Col span={12}>
|
||||||
|
<Statistic
|
||||||
|
title="电池电量"
|
||||||
|
value={selectedMapDevice.metrics.batteryCapacity}
|
||||||
|
suffix="%"
|
||||||
|
/>
|
||||||
|
</Col>
|
||||||
|
<Col span={12}>
|
||||||
|
<Statistic
|
||||||
|
title="定位质量"
|
||||||
|
value={selectedMapDevice.metrics.positionQuality}
|
||||||
|
suffix="级"
|
||||||
|
/>
|
||||||
|
</Col>
|
||||||
|
</Row>
|
||||||
|
|
||||||
|
{selectedMapDevice.location && (
|
||||||
|
<div style={{ marginTop: 16 }}>
|
||||||
|
<h4>位置信息</h4>
|
||||||
|
<p>纬度: {selectedMapDevice.location.latitude}</p>
|
||||||
|
<p>经度: {selectedMapDevice.location.longitude}</p>
|
||||||
|
{selectedMapDevice.location.height && (
|
||||||
|
<p>高度: {selectedMapDevice.location.height}m</p>
|
||||||
|
)}
|
||||||
|
{selectedMapDevice.location.heading && (
|
||||||
|
<p>航向: {selectedMapDevice.location.heading}°</p>
|
||||||
|
)}
|
||||||
|
<p>更新时间: {selectedMapDevice.location.updateTime}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Modal>
|
||||||
</PageContainer>
|
</PageContainer>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user