"use client"; import React, { useCallback, useEffect, useMemo, useState } from "react"; import { Box, Button, Chip, CircularProgress, Divider, IconButton, Stack, Tab, Tabs, Tooltip, Typography, Drawer, } from "@mui/material"; import { Refresh, ShowChart, TableChart, CleaningServices, ChevronLeft, ChevronRight, } from "@mui/icons-material"; import { DataGrid, GridColDef } from "@mui/x-data-grid"; import { zhCN } from "@mui/x-data-grid/locales"; import ReactECharts from "echarts-for-react"; import * as echarts from "echarts"; import "dayjs/locale/zh-cn"; // 引入中文包 import dayjs, { Dayjs } from "dayjs"; import utc from "dayjs/plugin/utc"; import timezone from "dayjs/plugin/timezone"; import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; import { DateTimePicker, LocalizationProvider } from "@mui/x-date-pickers"; import { zhCN as pickerZhCN } from "@mui/x-date-pickers/locales"; import config from "@/config/config"; import { useGetIdentity } from "@refinedev/core"; import { useNotification } from "@refinedev/core"; import { api } from "@/lib/api"; import { apiFetch } from "@/lib/apiFetch"; dayjs.extend(utc); dayjs.extend(timezone); type IUser = { id: number; name: string; }; export interface TimeSeriesPoint { /** ISO8601 时间戳 */ timestamp: string; /** 每个设备对应的值 */ values: Record; } export interface SCADADataPanelProps { /** 选中的设备 ID 列表 */ deviceIds: string[]; /** 可选:控制浮窗显示 */ visible?: boolean; /** 默认展示的选项卡 */ defaultTab?: "chart" | "table"; /** Y 轴数值的小数位数 */ fractionDigits?: number; /** 是否显示清洗功能 */ showCleaning?: boolean; /** 清洗数据的回调 */ onCleanData?: () => void; } type PanelTab = "chart" | "table"; type LoadingState = "idle" | "loading" | "success" | "error"; /** * 从后端 API 获取 SCADA 数据 */ const fetchFromBackend = async ( deviceIds: string[], range: { from: Date; to: Date }, ): Promise => { if (deviceIds.length === 0) { return []; } const device_ids = deviceIds.join(","); const start_time = dayjs(range.from).toISOString(); const end_time = dayjs(range.to).toISOString(); // 清洗数据接口 const cleaningDataUrl = `${config.BACKEND_URL}/api/v1/scada/by-ids-field-time-range?device_ids=${device_ids}&field=cleaned_value&start_time=${start_time}&end_time=${end_time}`; // 原始数据 const rawDataUrl = `${config.BACKEND_URL}/api/v1/scada/by-ids-field-time-range?device_ids=${device_ids}&field=monitored_value&start_time=${start_time}&end_time=${end_time}`; // 模拟数据接口 const simulationDataUrl = `${config.BACKEND_URL}/api/v1/composite/scada-simulation?device_ids=${device_ids}&start_time=${start_time}&end_time=${end_time}`; try { // 优先查询清洗数据和模拟数据 const [cleaningRes, simulationRes] = await Promise.all([ apiFetch(cleaningDataUrl) .then((r) => (r.ok ? r.json() : null)) .catch(() => null), apiFetch(simulationDataUrl) .then((r) => (r.ok ? r.json() : null)) .catch(() => null), ]); const cleaningData = transformBackendData(cleaningRes, deviceIds); const simulationData = transformBackendData(simulationRes, deviceIds); // 如果清洗数据有数据,返回清洗和模拟数据 if (cleaningData.length > 0) { return mergeTimeSeriesData( cleaningData, simulationData, deviceIds, "clean", "sim", ); } else { // 如果清洗数据没有数据,查询原始数据,返回模拟和原始数据 const rawRes = await apiFetch(rawDataUrl) .then((r) => (r.ok ? r.json() : null)) .catch(() => null); const rawData = transformBackendData(rawRes, deviceIds); return mergeTimeSeriesData( simulationData, rawData, deviceIds, "sim", "raw", ); } } catch (error) { console.error("[SCADADataPanel] 从后端获取数据失败:", error); throw error; } }; /** * 转换后端数据格式 * 根据实际后端返回的数据结构进行调整 */ const transformBackendData = ( backendData: any, deviceIds: string[], ): TimeSeriesPoint[] => { // 处理后端返回的对象格式: { deviceId: [{time: "...", value: ...}] } if (backendData && !Array.isArray(backendData)) { // 检查是否是设备ID为键的对象格式 const hasDeviceKeys = deviceIds.some((id) => id in backendData); if (hasDeviceKeys) { // 获取所有时间点的集合 const timeMap = new Map>(); deviceIds.forEach((deviceId) => { const deviceData = backendData[deviceId]; if (Array.isArray(deviceData)) { deviceData.forEach((item: any) => { const timestamp = item.time || item.timestamp || item._time; if (timestamp) { if (!timeMap.has(timestamp)) { timeMap.set(timestamp, {}); } const values = timeMap.get(timestamp)!; values[deviceId] = typeof item.value === "number" ? item.value : null; } }); } }); // 转换为 TimeSeriesPoint 数组并按时间排序 const result = Array.from(timeMap.entries()).map( ([timestamp, values]) => ({ timestamp, values, }), ); result.sort( (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(), ); return result; } } // 默认返回空数组 console.warn("[SCADADataPanel] 未知的后端数据格式:", backendData); return []; }; /** * 合并两个时间序列数据,为每个设备添加后缀 */ const mergeTimeSeriesData = ( data1: TimeSeriesPoint[], data2: TimeSeriesPoint[], deviceIds: string[], suffix1: string, suffix2: string, ): TimeSeriesPoint[] => { const timeMap = new Map>(); const processData = (data: TimeSeriesPoint[], suffix: string) => { data.forEach((point) => { if (!timeMap.has(point.timestamp)) { timeMap.set(point.timestamp, {}); } const values = timeMap.get(point.timestamp)!; deviceIds.forEach((deviceId) => { const value = point.values[deviceId]; if (value !== undefined) { values[`${deviceId}_${suffix}`] = value; } }); }); }; processData(data1, suffix1); processData(data2, suffix2); const result = Array.from(timeMap.entries()).map(([timestamp, values]) => ({ timestamp, values, })); result.sort( (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(), ); return result; }; const formatTimestamp = (timestamp: string) => dayjs(timestamp).tz("Asia/Shanghai").format("YYYY-MM-DD HH:mm"); const ensureValidRange = ( from: Dayjs, to: Dayjs, ): { from: Dayjs; to: Dayjs } => { if (from.isAfter(to)) { return { from: to, to: from }; } return { from, to }; }; const buildDataset = ( points: TimeSeriesPoint[], deviceIds: string[], fractionDigits: number, showCleaning: boolean, ) => { return points.map((point) => { const entry: Record = { time: dayjs(point.timestamp).toDate(), label: formatTimestamp(point.timestamp), }; if (showCleaning) { deviceIds.forEach((id) => { ["raw", "clean", "sim"].forEach((suffix) => { const key = `${id}_${suffix}`; const value = point.values[key]; entry[key] = typeof value === "number" ? Number.isFinite(value) ? parseFloat(value.toFixed(fractionDigits)) : null : value ?? null; }); }); } else { deviceIds.forEach((id) => { ["raw", "clean", "sim"].forEach((suffix) => { const key = `${id}_${suffix}`; const value = point.values[key]; if (value !== undefined && value !== null) { entry[key] = typeof value === "number" ? Number.isFinite(value) ? parseFloat(value.toFixed(fractionDigits)) : null : value ?? null; } }); }); } return entry; }); }; const emptyStateMessages: Record< PanelTab, { title: string; subtitle: string } > = { chart: { title: "暂无时序数据", subtitle: "请切换时间段来获取曲线", }, table: { title: "暂无表格数据", subtitle: "请切换时间段来获取记录", }, }; const SCADADataPanel: React.FC = ({ deviceIds, visible = true, defaultTab = "chart", fractionDigits = 2, showCleaning = false, onCleanData, }) => { const { open } = useNotification(); const { data: user } = useGetIdentity(); const customFetcher = useMemo(() => { if (!showCleaning) { return fetchFromBackend; } return async ( deviceIds: string[], range: { from: Date; to: Date }, ): Promise => { const device_ids = deviceIds.join(","); const start_time = dayjs(range.from).toISOString(); const end_time = dayjs(range.to).toISOString(); // 清洗数据接口 const cleaningDataUrl = `${config.BACKEND_URL}/api/v1/scada/by-ids-field-time-range?device_ids=${device_ids}&field=cleaned_value&start_time=${start_time}&end_time=${end_time}`; // 原始数据 const rawDataUrl = `${config.BACKEND_URL}/api/v1/scada/by-ids-field-time-range?device_ids=${device_ids}&field=monitored_value&start_time=${start_time}&end_time=${end_time}`; // 模拟数据接口 const simulationDataUrl = `${config.BACKEND_URL}/api/v1/composite/scada-simulation?device_ids=${device_ids}&start_time=${start_time}&end_time=${end_time}`; try { const [cleanRes, rawRes, simRes] = await Promise.all([ apiFetch(cleaningDataUrl) .then((r) => (r.ok ? r.json() : null)) .catch(() => null), apiFetch(rawDataUrl) .then((r) => (r.ok ? r.json() : null)) .catch(() => null), apiFetch(simulationDataUrl) .then((r) => (r.ok ? r.json() : null)) .catch(() => null), ]); const timeMap = new Map>(); const processData = (data: any, suffix: string) => { if (!data) return; deviceIds.forEach((deviceId) => { const deviceData = data[deviceId]; if (Array.isArray(deviceData)) { deviceData.forEach((item: any) => { const timestamp = item.time || item.timestamp || item._time; if (timestamp) { if (!timeMap.has(timestamp)) { timeMap.set(timestamp, {}); } const values = timeMap.get(timestamp)!; values[`${deviceId}_${suffix}`] = typeof item.value === "number" ? item.value : null; } }); } }); }; processData(cleanRes, "clean"); processData(rawRes, "raw"); processData(simRes, "sim"); const result = Array.from(timeMap.entries()).map( ([timestamp, values]) => ({ timestamp, values, }), ); result.sort( (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(), ); return result; } catch (error) { console.error("[SCADADataPanel] 获取三种数据失败:", error); throw error; } }; }, [showCleaning]); const [from, setFrom] = useState(() => dayjs().subtract(1, "day")); const [to, setTo] = useState(() => dayjs()); const [activeTab, setActiveTab] = useState(defaultTab); const [timeSeries, setTimeSeries] = useState([]); const [loadingState, setLoadingState] = useState("idle"); const [error, setError] = useState(null); const [isExpanded, setIsExpanded] = useState(true); const [isCleaning, setIsCleaning] = useState(false); const [selectedSource, setSelectedSource] = useState< "raw" | "clean" | "sim" | "all" >(() => (deviceIds.length === 1 ? "all" : "clean")); useEffect(() => { setActiveTab(defaultTab); }, [defaultTab]); const normalizedRange = useMemo(() => ensureValidRange(from, to), [from, to]); const hasDevices = deviceIds.length > 0; const hasData = timeSeries.length > 0; const dataset = useMemo( () => buildDataset(timeSeries, deviceIds, fractionDigits, showCleaning), [timeSeries, deviceIds, fractionDigits, showCleaning], ); const handleFetch = useCallback( async (reason: string) => { if (!hasDevices) { setTimeSeries([]); setLoadingState("idle"); setError(null); return; } setLoadingState("loading"); setError(null); try { const { from: rangeFrom, to: rangeTo } = normalizedRange; const result = await customFetcher(deviceIds, { from: rangeFrom.toDate(), to: rangeTo.toDate(), }); setTimeSeries(result); setLoadingState("success"); } catch (err) { setError(err instanceof Error ? err.message : "未知错误"); setLoadingState("error"); } }, [deviceIds, customFetcher, hasDevices, normalizedRange], ); // 处理数据清洗 const handleCleanData = useCallback(async () => { if (!hasDevices) { open?.({ type: "error", message: "请先选择设备", }); return; } if (!user || !user.name) { open?.({ type: "error", message: "用户信息无效,请重新登录", }); return; } setIsCleaning(true); try { const { from: rangeFrom, to: rangeTo } = normalizedRange; const startTime = dayjs(rangeFrom).toISOString(); const endTime = dayjs(rangeTo).toISOString(); // 调用后端清洗接口 const response = await api.post( `${ config.BACKEND_URL }/api/v1/composite/clean-scada?device_ids=${deviceIds.join( ",", )}&start_time=${startTime}&end_time=${endTime}`, ); console.log("[SCADADataPanel] 清洗响应:", response.data); // 处理成功响应 if (response.data === "success" || response.data?.success === true) { open?.({ type: "success", message: "数据清洗成功", description: `已完成 ${deviceIds.length} 个设备的数据清洗`, }); // 清洗完成后自动刷新数据 await handleFetch("after-cleaning"); // 如果父组件提供了回调,也调用它 onCleanData?.(); } else { throw new Error(response.data?.message || "清洗失败"); } } catch (err: any) { console.error("[SCADADataPanel] 数据清洗失败:", err); open?.({ type: "error", message: "数据清洗失败", description: err.response?.data?.message || err.message || "未知错误", }); } finally { setIsCleaning(false); } }, [ deviceIds, hasDevices, normalizedRange, user, open, onCleanData, handleFetch, ]); // 设备变化时自动查询 useEffect(() => { if (hasDevices) { handleFetch("device-change"); } else { setTimeSeries([]); } }, [deviceIds.join(",")]); // 当设备数量变化时,调整数据源选择 useEffect(() => { if (deviceIds.length > 1 && selectedSource === "all") { setSelectedSource("clean"); } }, [deviceIds.length, selectedSource]); const columns: GridColDef[] = useMemo(() => { const base: GridColDef[] = [ { field: "label", headerName: "时间", minWidth: 180, flex: 1, }, ]; const dynamic = (() => { if (showCleaning) { if (selectedSource === "all") { // 全部模式:显示所有设备的三种数据 return deviceIds.flatMap((id) => [ { field: `${id}_raw`, headerName: `${id} (原始)`, minWidth: 140, flex: 1, valueFormatter: (value: any) => { if (value === null || value === undefined) return "--"; if (Number.isFinite(Number(value))) { return Number(value).toFixed(fractionDigits); } return String(value); }, }, { field: `${id}_clean`, headerName: `${id} (清洗)`, minWidth: 140, flex: 1, valueFormatter: (value: any) => { if (value === null || value === undefined) return "--"; if (Number.isFinite(Number(value))) { return Number(value).toFixed(fractionDigits); } return String(value); }, }, { field: `${id}_sim`, headerName: `${id} (模拟)`, minWidth: 140, flex: 1, valueFormatter: (value: any) => { if (value === null || value === undefined) return "--"; if (Number.isFinite(Number(value))) { return Number(value).toFixed(fractionDigits); } return String(value); }, }, ]); } else { // 单一数据源模式:只显示选中的数据源 return deviceIds.map((id) => ({ field: `${id}_${selectedSource}`, headerName: id, minWidth: 140, flex: 1, valueFormatter: (value: any) => { if (value === null || value === undefined) return "--"; if (Number.isFinite(Number(value))) { return Number(value).toFixed(fractionDigits); } return String(value); }, })); } } else { // 非清洗模式:显示所有设备的所有有数据的列 const cols: GridColDef[] = []; deviceIds.forEach((id) => { const deviceName = id; // 为每个设备的每种数据类型创建列 const suffixes = [ { key: "raw", name: "原始值" }, { key: "clean", name: "清洗值" }, { key: "sim", name: "模拟值" }, ]; suffixes.forEach(({ key, name }) => { const fieldKey = `${id}_${key}`; // 检查是否有该字段的数据 const hasData = dataset.some( (item) => item[fieldKey] !== null && item[fieldKey] !== undefined, ); if (hasData) { cols.push({ field: fieldKey, headerName: `${deviceName} (${name})`, minWidth: 140, flex: 1, valueFormatter: (value: any) => { if (value === null || value === undefined) return "--"; if (Number.isFinite(Number(value))) { return Number(value).toFixed(fractionDigits); } return String(value); }, }); } }); }); return cols; } })(); return [...base, ...dynamic]; }, [deviceIds, fractionDigits, showCleaning, selectedSource, dataset]); const rows = useMemo( () => dataset.map((item, index) => ({ id: `${ item.time instanceof Date ? item.time.getTime() : index }-${index}`, ...item, })), [dataset], ); const renderEmpty = () => { const message = emptyStateMessages[activeTab]; return ( {message.title} {message.subtitle} ); }; const renderChart = () => { if (!hasData) return renderEmpty(); // 为每个设备生成独特的颜色和样式 const colors = [ "#1976d2", // 蓝色 "#dc004e", // 粉红色 "#ff9800", // 橙色 "#4caf50", // 绿色 "#9c27b0", // 紫色 "#00bcd4", // 青色 "#f44336", // 红色 "#8bc34a", // 浅绿色 "#ff5722", // 深橙色 "#3f51b5", // 靛蓝色 ]; const xData = dataset.map((item) => item.label); const getSeries = () => { if (showCleaning) { if (selectedSource === "all") { return deviceIds.flatMap((id, index) => [ { name: `${id} (原始)`, type: "line", symbol: "none", connectNulls: true, sampling: "lttb", itemStyle: { color: colors[index % colors.length] }, data: dataset.map((item) => item[`${id}_raw`]), }, { name: `${id} (清洗)`, type: "line", symbol: "none", connectNulls: true, sampling: "lttb", itemStyle: { color: colors[(index + 3) % colors.length] }, data: dataset.map((item) => item[`${id}_clean`]), }, { name: `${id} (模拟)`, type: "line", symbol: "none", connectNulls: true, sampling: "lttb", itemStyle: { color: colors[(index + 6) % colors.length] }, data: dataset.map((item) => item[`${id}_sim`]), }, ]); } else { return deviceIds.map((id, index) => ({ name: id, type: "line", symbol: "none", connectNulls: true, sampling: "lttb", itemStyle: { color: colors[index % colors.length] }, data: dataset.map((item) => item[`${id}_${selectedSource}`]), })); } } else { return deviceIds.flatMap((id, index) => { const series = []; ["raw", "clean", "sim"].forEach((suffix, sIndex) => { const key = `${id}_${suffix}`; const hasData = dataset.some( (item) => item[key] !== null && item[key] !== undefined, ); if (hasData) { series.push({ name: `${id} (${ suffix === "raw" ? "原始" : suffix === "clean" ? "清洗" : "模拟" })`, type: "line", symbol: "none", connectNulls: true, sampling: "lttb", itemStyle: { color: colors[(index * 3 + sIndex) % colors.length], }, data: dataset.map((item) => item[key]), areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: colors[(index * 3 + sIndex) % colors.length], }, { offset: 1, color: "rgba(255, 255, 255, 0)", }, ]), opacity: 0.3, }, }); } }); // 如果没有clean/raw/sim数据,则使用fallback if (series.length === 0) { series.push({ name: id, type: "line", symbol: "none", connectNulls: true, sampling: "lttb", itemStyle: { color: colors[index % colors.length] }, data: dataset.map((item) => item[id]), areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: colors[index % colors.length], }, { offset: 1, color: "rgba(255, 255, 255, 0)", }, ]), opacity: 0.3, }, }); } return series; }); } }; const option = { // animation: false, animationDuration: 500, tooltip: { trigger: "axis", confine: true, position: function (pt: any[]) { return [pt[0], "10%"]; }, }, legend: { top: "top", }, grid: { left: "5%", right: "5%", bottom: "11%", containLabel: true, }, toolbox: { feature: { dataZoom: { yAxisIndex: "none", }, restore: {}, saveAsImage: {}, }, }, xAxis: { type: "category", boundaryGap: false, data: xData, }, yAxis: { type: "value", scale: true, }, dataZoom: [ { type: "inside", start: 0, end: 100, }, { start: 0, end: 100, }, ], series: getSeries(), }; return ( ); }; const renderTable = () => { if (!hasData) return renderEmpty(); console.debug("[SCADADataPanel] 表格数据:", { rowsCount: rows.length, columnsCount: columns.length, sampleRow: rows[0], columns: columns.map((c) => c.field), }); return ( ); }; return ( <> {/* 收起时的触发按钮 */} {!isExpanded && hasDevices && ( setIsExpanded(true)} sx={{ zIndex: 1300 }} > 历史数据 )} {/* 主面板 */} {/* Header */} SCADA 历史数据 setIsExpanded(false)} sx={{ color: "primary.contrastText" }} > {/* Controls */} { if (value && dayjs.isDayjs(value) && value.isValid()) { setFrom(value); } }} onAccept={(value) => { if ( value && dayjs.isDayjs(value) && value.isValid() && hasDevices ) { handleFetch("date-change"); } }} maxDateTime={to} slotProps={{ textField: { fullWidth: true, size: "small" }, }} /> { if (value && dayjs.isDayjs(value) && value.isValid()) { setTo(value); } }} onAccept={(value) => { if ( value && dayjs.isDayjs(value) && value.isValid() && hasDevices ) { handleFetch("date-change"); } }} minDateTime={from} slotProps={{ textField: { fullWidth: true, size: "small" }, }} /> setActiveTab(value)} variant="fullWidth" > } iconPosition="start" label="曲线" /> } iconPosition="start" label="表格" /> {showCleaning && ( )} {showCleaning && hasDevices && ( 数据源: setSelectedSource(value) } > {deviceIds.length === 1 && ( )} )} {!hasDevices && ( 未选择任何设备,无法获取数据。 )} {error && ( 获取数据失败:{error} )} {/* Content */} {loadingState === "loading" && ( )} {activeTab === "chart" ? renderChart() : renderTable()} ); }; export default SCADADataPanel;