import React, { useState, useEffect } from 'react'; import { Modal, Form, Row, Col, InputNumber, Input, Checkbox, Slider, Spin, } from 'antd'; import { useSelector } from 'react-redux'; import { RootState } from '@/store'; import { deviceRunParamSelectByDeviceId, deviceRunParamSave } from '@/api/stationManage/index'; import utils from '@/lib/utils'; import { useTranslation } from 'react-i18next'; const GainSliderInput = ({ value, onChange, disabled, min = 0.1, max = 1, step = 0.01 }) => { const { t } = useTranslation(); const handleSliderChange = (val) => { onChange?.(val); }; const handleInputChange = (val) => { let num = Number(val); if (isNaN(num)) num = min; num = Math.max(min, Math.min(max, num)); onChange?.(Number(num.toFixed(2))); }; return (
`${v?.toFixed(2)}` }} marks={{ [min]: { style: { color: '#8c8c8c', fontSize: 10 }, label: min }, [(min + max) / 2]: { style: { color: '#8c8c8c', fontSize: 10 }, label: ((min + max) / 2).toFixed(2) }, [max]: { style: { color: '#8c8c8c', fontSize: 10 }, label: max }, }} />
); }; const editableKeys = ['leftForwardGain', 'leftBackwardGain', 'rightForwardGain', 'rightBackwardGain']; interface DeviceParamModalProps { open: boolean; device?: { serialNumber?: string; device_sn?: string; deviceAlias?: string; deviceName?: string; callsign?: string; drone_callsign?: string; [key: string]: any; } | null; onCancel: () => void; onSuccess?: () => void; } const DeviceParamModal: React.FC = ({ open, device, onCancel, onSuccess }) => { const { stationId, currentStation } = useSelector((state: RootState) => state.station); const [paramForm] = Form.useForm(); const [paramData, setParamData] = useState(null); const [loading, setLoading] = useState(false); const { t } = useTranslation(); const deviceId = device?.serialNumber || device?.device_sn || ''; const deviceName = device?.deviceAlias || device?.deviceName || device?.drone_callsign || device?.callsign || deviceId; useEffect(() => { if (open && deviceId) { fetchParamData(deviceId); } else { setParamData(null); paramForm.resetFields(); } }, [open, deviceId]); const fetchParamData = async (id: string) => { setLoading(true); try { const res = await deviceRunParamSelectByDeviceId({ deviceId: id }); if (res.code == 200) { if (res.data) { setParamData(res.data); paramForm.setFieldsValue({ ...res.data }); } else { utils.message.error(t('devices.fetchParamFail')); setParamData(null); paramForm.resetFields(); } } else { utils.message.error(t('devices.fetchParamFail')); } } catch (err) { utils.message.error(t('devices.fetchParamFail')); } finally { setLoading(false); } }; const saveParam = async (values) => { setLoading(true); try { const payload = { leftForwardGain: Math.max(0.1, Math.min(1, values.leftForwardGain >= 10 ? values.leftForwardGain / 10 : values.leftForwardGain)), leftBackwardGain: Math.max(0.1, Math.min(1, values.leftBackwardGain >= 10 ? values.leftBackwardGain / 10 : values.leftBackwardGain)), rightForwardGain: Math.max(0.1, Math.min(1, values.rightForwardGain >= 10 ? values.rightForwardGain / 10 : values.rightForwardGain)), rightBackwardGain: Math.max(0.1, Math.min(1, values.rightBackwardGain >= 10 ? values.rightBackwardGain / 10 : values.rightBackwardGain)), deviceId, siteId: stationId, orgId: currentStation?.orgId, id: paramData?.id, }; const res = await deviceRunParamSave(payload); if (res.code === 200) { utils.message.success(t('devices.paramSaveSuccess')); onSuccess?.(); onCancel?.(); } else { utils.message.error(res.msg || t('devices.paramSaveFail')); } } catch (err) { utils.message.error(t('devices.paramSaveException')); } finally { setLoading(false); } }; const handleCancel = () => { setParamData(null); paramForm.resetFields(); onCancel?.(); }; const paramFieldConfigs = [ { key: 'leftForwardGain', label: t('devices.paramLeftForwardGain'), type: 'InputNumber', editable: true, min: 0, max: 1, step: 0.01 }, { key: 'leftBackwardGain', label: t('devices.paramLeftBackwardGain'), type: 'InputNumber', editable: true, min: 0, max: 1, step: 0.01 }, { key: 'rightForwardGain', label: t('devices.paramRightForwardGain'), type: 'InputNumber', editable: true, min: 0, max: 1, step: 0.01 }, { key: 'rightBackwardGain', label: t('devices.paramRightBackwardGain'), type: 'InputNumber', editable: true, min: 0, max: 1, step: 0.01 }, { key: 'runSpeed', label: t('devices.paramRunSpeed'), type: 'InputNumber', editable: true }, //hasHost: "hasHost", //rtkMode: "rtkMode", cameraNum: "cameraNum", wheelType: "wheelType", engineType: "engineType", capabilityList: "capabilityList", powerType: "powerType", { key: 'hasHost', label: t('devices.hasHost'), type: 'Input', editable: false }, { key: 'rtkMode', label: t('devices.rtkMode'), type: 'Input', editable: false }, { key: 'cameraNum', label: t('devices.cameraNum'), type: 'Input', editable: false }, { key: 'wheelType', label: t('devices.wheelType'), type: 'Input', editable: false }, { key: 'engineType', label: t('devices.engineType'), type: 'Input', editable: false }, { key: 'capabilityList', label: t('devices.capabilityList'), type: 'Input', editable: false }, { key: 'chipUidSign', label: t('devices.paramChipUidSign'), type: 'InputNumber', editable: false }, { key: 'chipUid', label: t('devices.paramChipUid'), type: 'Input', editable: false }, { key: 'remoteConfig', label: t('devices.paramRemoteConfig'), type: 'InputNumber', editable: false }, { key: 'knifeMotorMode', label: t('devices.paramKnifeMotorMode'), type: 'InputNumber', editable: false }, { key: 'walkMotorMode', label: t('devices.paramWalkMotorMode'), type: 'InputNumber', editable: false }, { key: 'leftMotorReverse', label: t('devices.paramLeftMotorReverse'), type: 'InputNumber', editable: false }, { key: 'rightMotorReverse', label: t('devices.paramRightMotorReverse'), type: 'InputNumber', editable: false }, { key: 'swapChannel', label: t('devices.paramSwapChannel'), type: 'InputNumber', editable: false }, { key: 'use4G', label: t('devices.paramUse4G'), type: 'InputNumber', editable: false }, { key: 'forwardSpeedLimit', label: t('devices.paramForwardSpeedLimit'), type: 'InputNumber', editable: false }, { key: 'turnSpeedLimit', label: t('devices.paramTurnSpeedLimit'), type: 'InputNumber', editable: false }, { key: 'knifePolarity', label: t('devices.paramKnifePolarity'), type: 'InputNumber', editable: false }, { key: 'fanPolarity', label: t('devices.paramFanPolarity'), type: 'InputNumber', editable: false }, { key: 'throttlePolarity', label: t('devices.paramThrottlePolarity'), type: 'InputNumber', editable: false }, { key: 'liftProtectTime', label: t('devices.paramLiftProtectTime'), type: 'InputNumber', editable: false }, { key: 'dualRtk', label: t('devices.paramDualRtk'), type: 'InputNumber', editable: false }, { key: 'knifeChannel', label: t('devices.paramKnifeChannel'), type: 'InputNumber', editable: false }, { key: 'fanChannel', label: t('devices.paramFanChannel'), type: 'InputNumber', editable: false }, { key: 'throttleChannel', label: t('devices.paramThrottleChannel'), type: 'InputNumber', editable: false }, { key: 'remoteType', label: t('devices.paramRemoteType'), type: 'InputNumber', editable: false }, { key: 'relayBoard', label: t('devices.paramRelayBoard'), type: 'Checkbox', editable: false }, { key: 'liftChannel', label: t('devices.paramLiftChannel'), type: 'InputNumber', editable: false }, { key: 'chassisChannel', label: t('devices.paramChassisChannel'), type: 'InputNumber', editable: false }, { key: 'armChannel', label: t('devices.paramArmChannel'), type: 'InputNumber', editable: false }, { key: 'fuelPumpChannel', label: t('devices.paramFuelPumpChannel'), type: 'InputNumber', editable: false }, { key: 'wifiName', label: t('devices.paramWifiName'), type: 'Input', editable: false }, { key: 'wifiPassword', label: t('devices.paramWifiPassword'), type: 'Input', editable: false }, { key: 'batteryType', label: t('devices.paramBatteryType'), type: 'InputNumber', editable: false }, { key: 'driveType', label: t('devices.paramDriveType'), type: 'InputNumber', editable: false }, { key: 'gearRatio', label: t('devices.paramGearRatio'), type: 'InputNumber', editable: false, step: 0.01 }, { key: 'robotLength', label: t('devices.paramRobotLength'), type: 'InputNumber', editable: false, step: 0.01 }, { key: 'robotWidth', label: t('devices.paramRobotWidth'), type: 'InputNumber', editable: false, step: 0.01 }, { key: 'robotHeight', label: t('devices.paramRobotHeight'), type: 'InputNumber', editable: false, step: 0.01 }, { key: 'knifeWidth', label: t('devices.paramKnifeWidth'), type: 'InputNumber', editable: false, step: 0.01 }, { key: 'tyreSize', label: t('devices.paramTyreSize'), type: 'InputNumber', editable: false, step: 0.01 }, { key: 'firmwareVersion', label: t('devices.paramFirmwareVersion'), type: 'Input', editable: false }, { key: 'crc16', label: 'CRC16', type: 'InputNumber', editable: false }, { key: 'tail1', label: 'tail1', type: 'InputNumber', editable: false }, { key: 'tail2', label: 'tail2', type: 'InputNumber', editable: false }, { key: 'remark', label: t('roles.remark'), type: 'Input', editable: false }, { key: 'delFlag', label: t('devices.paramDelFlag'), type: 'Checkbox', editable: false }, ]; return ( {t('devices.paramViewEditTitle')} - {deviceName} } open={open} onCancel={handleCancel} onOk={() => paramForm.submit()} confirmLoading={loading} width={780} destroyOnClose > {/* 加载时表单区域简单遮罩提示(打开弹窗拉取参数时的loading反馈) */}
{editableKeys.map((key) => { const field = paramFieldConfigs.find(f => f.key === key); if (!field) return null; const isGainSliderField = ['leftForwardGain', 'leftBackwardGain', 'rightForwardGain', 'rightBackwardGain'].includes(key); const disabled = !field.editable; return ( {field.label}} name={field.key} > {isGainSliderField ? ( ) : ( )} ); })} {paramFieldConfigs.filter(f => !editableKeys.includes(f.key)).map((field) => { const disabled = !field.editable; return ( {field.type === 'InputNumber' ? ( ) : field.type === 'Checkbox' ? ( ) : ( )} ); })}
); }; export default DeviceParamModal;