import { useState, useEffect } from 'react'; import { Card } from '../ui/card'; import { Button } from '../ui/button'; import { Badge } from '../ui/badge'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '../ui/tabs'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select'; import { Input } from '../ui/input'; import { Textarea } from '../ui/textarea'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from '../ui/dialog'; import { Label } from '../ui/label'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '../ui/table'; import { Switch } from '../ui/switch'; import { WaterFertilizerDevice } from './WaterFertilizerDevice'; import { WaterFertilizerComponent } from './WaterFertilizerComponent'; import { WaterFertilizerParameter } from './WaterFertilizerParameter'; import { WaterFertilizerMapping } from './WaterFertilizerMapping'; import { Search, Plus, Edit, Eye, Trash2, Download, Upload, Filter, RefreshCw, Settings, CheckCircle, XCircle, AlertCircle, Activity, Cpu, Zap, Gauge, Droplets, Wifi, MapPin, Link, GitBranch, Layers, Server, HardDrive, Signal, Power, Info, Copy, } from 'lucide-react'; import { toast } from 'sonner@2.0.3'; interface WaterFertilizerManagementProps { activePath?: string; } type DeviceStatus = '在线' | '离线' | '故障' | '维护'; type ComponentType = '泵体' | '传感器' | '控制器' | '阀门' | '流量计'; type ParameterType = '压力' | '流量' | 'EC值' | 'PH值' | '温度' | '湿度'; type ParameterUnit = 'bar' | 'L/min' | 'mS/cm' | 'pH' | '℃' | '%'; type ProtocolType = 'Modbus-RTU' | 'Modbus-TCP' | 'HTTP' | 'MQTT' | 'OPC-UA'; interface WFDevice { id: string; deviceNo: string; deviceName: string; model: string; manufacturer: string; status: DeviceStatus; fieldName: string; location: string; installDate: string; ipAddress?: string; port?: number; protocol: ProtocolType; lastOnlineTime: string; workingHours: number; notes?: string; } interface WFComponent { id: string; componentNo: string; componentName: string; type: ComponentType; deviceName: string; specification: string; range: string; accuracy: string; unit: string; manufacturer: string; installDate: string; status: '正常' | '异常' | '维护'; notes?: string; } interface WFParameter { id: string; parameterNo: string; parameterName: string; type: ParameterType; unit: ParameterUnit; minValue: number; maxValue: number; defaultValue: number; warningThreshold?: number; alarmThreshold?: number; deviceName: string; description: string; isEnabled: boolean; } interface DeviceMapping { id: string; mappingNo: string; logicName: string; logicType: '阀门' | '泵' | '施肥机' | '传感器'; physicalDevice: string; deviceAddress: string; protocol: ProtocolType; controlPoint: string; readAddress?: string; writeAddress?: string; dataType: 'Boolean' | 'Int16' | 'Int32' | 'Float' | 'String'; status: '已映射' | '未映射' | '异常'; testResult?: '成功' | '失败'; lastTestTime?: string; notes?: string; } export function WaterFertilizerManagement({ activePath }: WaterFertilizerManagementProps) { // 如果是水肥机设备页面,直接渲染专门的设备管理组件 if (activePath?.includes('/device')) { return ; } // 如果是水肥机部件配置页面,直接渲染专门的部件配置组件 if (activePath?.includes('/component')) { return ; } // 如果是水肥机参数配置页面,直接渲染专门的参数配置组件 if (activePath?.includes('/parameter')) { return ; } // 如果是水肥设备映射页面,直接渲染专门的映射管理组件 if (activePath?.includes('/mapping')) { return ; } const [activeTab, setActiveTab] = useState('device'); const [showDeviceDialog, setShowDeviceDialog] = useState(false); const [showComponentDialog, setShowComponentDialog] = useState(false); const [showParameterDialog, setShowParameterDialog] = useState(false); const [showMappingDialog, setShowMappingDialog] = useState(false); const [showDetailDialog, setShowDetailDialog] = useState(false); const [editingItem, setEditingItem] = useState(null); const [viewingItem, setViewingItem] = useState(null); // 根据路径自动切换Tab useEffect(() => { if (activePath) { if (activePath.includes('/component')) { setActiveTab('component'); } else if (activePath.includes('/parameter')) { setActiveTab('parameter'); } else if (activePath.includes('/mapping')) { setActiveTab('mapping'); } } }, [activePath]); // 水肥机设备数据 const [devices, setDevices] = useState([ { id: 'dev-1', deviceNo: 'WF-2024-001', deviceName: '1号大棚水肥机', model: 'SF-3000Pro', manufacturer: '智农科技', status: '在线', fieldName: '1号大棚', location: '大棚东南角', installDate: '2024-03-15', ipAddress: '192.168.1.101', port: 502, protocol: 'Modbus-TCP', lastOnlineTime: '2024-10-15 18:30:25', workingHours: 2580, notes: '主力设备,性能稳定', }, { id: 'dev-2', deviceNo: 'WF-2024-002', deviceName: '2号田块水肥机', model: 'SF-2000', manufacturer: '农智通', status: '在线', fieldName: '2号田块', location: '田块中央', installDate: '2024-05-20', ipAddress: '192.168.1.102', port: 502, protocol: 'Modbus-TCP', lastOnlineTime: '2024-10-15 18:28:10', workingHours: 1850, }, { id: 'dev-3', deviceNo: 'WF-2024-003', deviceName: '3号田块水肥机', model: 'SF-3000Pro', manufacturer: '智农科技', status: '离线', fieldName: '3号田块', location: '田块北侧', installDate: '2024-06-10', ipAddress: '192.168.1.103', port: 502, protocol: 'Modbus-TCP', lastOnlineTime: '2024-10-15 16:45:00', workingHours: 1420, notes: '通信异常,需检查', }, ]); // 水肥机部件数据 const [components, setComponents] = useState([ { id: 'comp-1', componentNo: 'COMP-001', componentName: '主水泵', type: '泵体', deviceName: '1号大棚水肥机', specification: '1.5kW/220V', range: '0-50L/min', accuracy: '±2%', unit: 'L/min', manufacturer: '格兰富', installDate: '2024-03-15', status: '正常', }, { id: 'comp-2', componentNo: 'COMP-002', componentName: 'EC传感器', type: '传感器', deviceName: '1号大棚水肥机', specification: 'EC-200', range: '0-5 mS/cm', accuracy: '±0.01 mS/cm', unit: 'mS/cm', manufacturer: '梅特勒-托利多', installDate: '2024-03-15', status: '正常', }, { id: 'comp-3', componentNo: 'COMP-003', componentName: 'PH传感器', type: '传感器', deviceName: '1号大棚水肥机', specification: 'PH-300', range: '0-14 pH', accuracy: '±0.02 pH', unit: 'pH', manufacturer: '梅特勒-托利多', installDate: '2024-03-15', status: '正常', }, { id: 'comp-4', componentNo: 'COMP-004', componentName: 'PLC控制器', type: '控制器', deviceName: '1号大棚水肥机', specification: 'S7-1200', range: '-', accuracy: '-', unit: '-', manufacturer: '西门子', installDate: '2024-03-15', status: '正常', }, { id: 'comp-5', componentNo: 'COMP-005', componentName: '流量计', type: '流量计', deviceName: '2号田块水肥机', specification: 'FM-100', range: '0-100L/min', accuracy: '±1%', unit: 'L/min', manufacturer: '艾默生', installDate: '2024-05-20', status: '正常', }, ]); // 水肥机参数数据 const [parameters, setParameters] = useState([ { id: 'param-1', parameterNo: 'PARAM-001', parameterName: '系统压力', type: '压力', unit: 'bar', minValue: 0, maxValue: 10, defaultValue: 2.5, warningThreshold: 8, alarmThreshold: 9.5, deviceName: '1号大棚水肥机', description: '系统主管道压力,正常工作范围2-6bar', isEnabled: true, }, { id: 'param-2', parameterNo: 'PARAM-002', parameterName: '灌溉流量', type: '流量', unit: 'L/min', minValue: 0, maxValue: 50, defaultValue: 20, warningThreshold: 45, alarmThreshold: 48, deviceName: '1号大棚水肥机', description: '主管道流量,建议设置20-30L/min', isEnabled: true, }, { id: 'param-3', parameterNo: 'PARAM-003', parameterName: '溶液EC值', type: 'EC值', unit: 'mS/cm', minValue: 0, maxValue: 5, defaultValue: 2.0, warningThreshold: 4.5, alarmThreshold: 4.8, deviceName: '1号大棚水肥机', description: '营养液电导率,番茄适宜范围1.8-2.5 mS/cm', isEnabled: true, }, { id: 'param-4', parameterNo: 'PARAM-004', parameterName: '溶液PH值', type: 'PH值', unit: 'pH', minValue: 0, maxValue: 14, defaultValue: 6.5, warningThreshold: 8, alarmThreshold: 9, deviceName: '1号大棚水肥机', description: '营养液酸碱度,大多数作物适宜范围5.5-7.0', isEnabled: true, }, { id: 'param-5', parameterNo: 'PARAM-005', parameterName: '水温', type: '温度', unit: '℃', minValue: 0, maxValue: 50, defaultValue: 20, warningThreshold: 35, alarmThreshold: 40, deviceName: '2号田块水肥机', description: '灌溉水温度,建议15-25℃', isEnabled: true, }, ]); // 设备映射数据 const [mappings, setMappings] = useState([ { id: 'map-1', mappingNo: 'MAP-001', logicName: '1号主阀门', logicType: '阀门', physicalDevice: '1号大棚水肥机-电磁阀1', deviceAddress: '192.168.1.101:502', protocol: 'Modbus-TCP', controlPoint: 'DO0', readAddress: '40001', writeAddress: '40001', dataType: 'Boolean', status: '已映射', testResult: '成功', lastTestTime: '2024-10-15 10:30:00', }, { id: 'map-2', mappingNo: 'MAP-002', logicName: '1号主水泵', logicType: '泵', physicalDevice: '1号大棚水肥机-水泵1', deviceAddress: '192.168.1.101:502', protocol: 'Modbus-TCP', controlPoint: 'DO1', readAddress: '40002', writeAddress: '40002', dataType: 'Boolean', status: '已映射', testResult: '成功', lastTestTime: '2024-10-15 10:30:00', }, { id: 'map-3', mappingNo: 'MAP-003', logicName: '1号施肥机-A通道', logicType: '施肥机', physicalDevice: '1号大棚水肥机-施肥泵A', deviceAddress: '192.168.1.101:502', protocol: 'Modbus-TCP', controlPoint: 'AO0', readAddress: '40010', writeAddress: '40010', dataType: 'Float', status: '已映射', testResult: '成功', lastTestTime: '2024-10-15 10:30:00', notes: '控制施肥量,单位mL/min', }, { id: 'map-4', mappingNo: 'MAP-004', logicName: 'EC传感器', logicType: '传感器', physicalDevice: '1号大棚水肥机-EC传感器', deviceAddress: '192.168.1.101:502', protocol: 'Modbus-TCP', controlPoint: 'AI0', readAddress: '30001', dataType: 'Float', status: '已映射', testResult: '成功', lastTestTime: '2024-10-15 10:30:00', }, { id: 'map-5', mappingNo: 'MAP-005', logicName: '3号主阀门', logicType: '阀门', physicalDevice: '3号田块水肥机-电磁阀1', deviceAddress: '192.168.1.103:502', protocol: 'Modbus-TCP', controlPoint: 'DO0', readAddress: '40001', writeAddress: '40001', dataType: 'Boolean', status: '异常', testResult: '失败', lastTestTime: '2024-10-15 16:45:00', notes: '通信超时', }, ]); const getStatusColor = (status: DeviceStatus) => { switch (status) { case '在线': return 'bg-green-100 text-green-700'; case '离线': return 'bg-gray-100 text-gray-700'; case '故障': return 'bg-red-100 text-red-700'; case '维护': return 'bg-yellow-100 text-yellow-700'; default: return 'bg-gray-100 text-gray-700'; } }; const getComponentStatusColor = (status: string) => { switch (status) { case '正常': return 'bg-green-100 text-green-700'; case '异常': return 'bg-red-100 text-red-700'; case '维护': return 'bg-yellow-100 text-yellow-700'; default: return 'bg-gray-100 text-gray-700'; } }; const getMappingStatusColor = (status: string) => { switch (status) { case '已映射': return 'bg-green-100 text-green-700'; case '未映射': return 'bg-gray-100 text-gray-700'; case '异常': return 'bg-red-100 text-red-700'; default: return 'bg-gray-100 text-gray-700'; } }; const handleAddDevice = () => { setEditingItem(null); setShowDeviceDialog(true); }; const handleEditDevice = (device: WFDevice) => { setEditingItem(device); setShowDeviceDialog(true); }; const handleDeleteDevice = (deviceNo: string) => { setDevices(devices.filter(d => d.deviceNo !== deviceNo)); toast.success(`设备 ${deviceNo} 已删除`); }; const handleAddComponent = () => { setEditingItem(null); setShowComponentDialog(true); }; const handleEditComponent = (component: WFComponent) => { setEditingItem(component); setShowComponentDialog(true); }; const handleDeleteComponent = (componentNo: string) => { setComponents(components.filter(c => c.componentNo !== componentNo)); toast.success(`部件 ${componentNo} 已删除`); }; const handleAddParameter = () => { setEditingItem(null); setShowParameterDialog(true); }; const handleEditParameter = (parameter: WFParameter) => { setEditingItem(parameter); setShowParameterDialog(true); }; const handleDeleteParameter = (parameterNo: string) => { setParameters(parameters.filter(p => p.parameterNo !== parameterNo)); toast.success(`参数 ${parameterNo} 已删除`); }; const handleAddMapping = () => { setEditingItem(null); setShowMappingDialog(true); }; const handleEditMapping = (mapping: DeviceMapping) => { setEditingItem(mapping); setShowMappingDialog(true); }; const handleDeleteMapping = (mappingNo: string) => { setMappings(mappings.filter(m => m.mappingNo !== mappingNo)); toast.success(`映射 ${mappingNo} 已删除`); }; const handleTestMapping = (mappingNo: string) => { toast.success(`映射 ${mappingNo} 连接测试成功`); }; const handleViewDetail = (item: any, type: string) => { setViewingItem({ ...item, _type: type }); setShowDetailDialog(true); }; return (

水肥机管理

设备信息、部件配置、参数管理、设备映射

{/* 统计卡片 */}

水肥机设备

{devices.length}

在线: {devices.filter(d => d.status === '在线').length}台

部件数量

{components.length}

正常: {components.filter(c => c.status === '正常').length}个

参数配置

{parameters.length}

启用: {parameters.filter(p => p.isEnabled).length}个

设备映射

{mappings.length}

正常: {mappings.filter(m => m.status === '已映射').length}个

水肥机设备 部件配置 参数配置 设备映射 {/* 水肥机设备 */}

水肥机设备管理功能:

  • 数字化映射: 实现农场所有水肥机设备的数字化管理
  • 设备信息: 型号、状态、所属地块、安装位置等完整信息
  • 状态监控: 在线/离线状态、工作时长、最后在线时间
  • 网络配置: IP地址、端口、通信协议配置
  • 完整功能: 新增、编辑、查看、搜索、删除等全流程管理
  • 数据一致性: 确保系统信息与实际部署一致
} />
设备信息 型号/厂商 状态 所属地块 网络配置 工作状态 操作 {devices.map((device) => (
{device.deviceName}
#{device.deviceNo}
{device.model}
{device.manufacturer}
{device.status}
{device.fieldName}
{device.location}
{device.ipAddress}:{device.port}
{device.protocol}
运行: {device.workingHours}小时
{device.lastOnlineTime.substring(5, 16)}
))}
{/* 部件配置 */}

水肥机部件配置功能:

  • 核心部件: 泵体、传感器、控制器等核心部件管理
  • 运行参数: 量程、精度、单位等关键参数配置
  • 部件关联: 与物理设备一一对应
  • 规格管理: 详细的规格参数与技术指标
  • 状态跟踪: 部件工作状态实时监控
  • 故障诊断: 为参数映射与故障诊断提供依据
} />
部件信息 类型 所属设备 技术参数 厂商 状态 操作 {components.map((component) => (
{component.componentName}
#{component.componentNo}
{component.type} {component.deviceName}
规格: {component.specification}
量程: {component.range}
精度: {component.accuracy}
{component.manufacturer} {component.status}
))}
{/* 参数配置 */}

水肥机参数配置功能:

  • 运行参数: 压力范围、流量阈值、EC/PH标定值等
  • 阈值管理: 最小值、最大值、默认值、预警值、报警值
  • 参数映射: 系统内配置与物理设备参数映射
  • 标准化管理: 统一参数标准,保障控制准确性
  • 设备协同: 确保多设备控制指令的一致性
  • 精准控制: 为精准灌溉施肥提供参数基础
} />
参数信息 类型/单位 取值范围 阈值配置 所属设备 状态 操作 {parameters.map((parameter) => (
{parameter.parameterName}
#{parameter.parameterNo}
{parameter.type}
{parameter.unit}
最小: {parameter.minValue} {parameter.unit}
最大: {parameter.maxValue} {parameter.unit}
默认: {parameter.defaultValue} {parameter.unit}
{parameter.warningThreshold && (
预警: {parameter.warningThreshold} {parameter.unit}
)} {parameter.alarmThreshold && (
报警: {parameter.alarmThreshold} {parameter.unit}
)}
{parameter.deviceName}
))}
{/* 设备映射 */}

水肥设备映射功能:

  • 逻辑映射: 系统逻辑控制对象与物理设备一一对应
  • 设备配置: 设备地址、通信协议、控制点位配置
  • 地址管理: 读地址、写地址、数据类型配置
  • 指令准确: 确保控制指令准确作用于目标设备
  • 连接测试: 支持映射关系连接测试验证
  • 可靠联动: 实现虚拟控制与实物动作的可靠联动
} />
逻辑对象 物理设备 网络配置 地址配置 测试结果 状态 操作 {mappings.map((mapping) => (
{mapping.logicName}
{mapping.logicType}
{mapping.physicalDevice}
点位: {mapping.controlPoint}
{mapping.deviceAddress}
{mapping.protocol}
{mapping.readAddress &&
读: {mapping.readAddress}
} {mapping.writeAddress &&
写: {mapping.writeAddress}
} {mapping.dataType}
{mapping.testResult && (
{mapping.testResult} {mapping.lastTestTime && (
{mapping.lastTestTime.substring(5, 16)}
)}
)}
{mapping.status}
))}
{/* 设备对话框 */} {editingItem ? '编辑设备' : '新增设备'} 填写水肥机设备的基本信息和网络配置