diff --git a/components/Settings.tsx b/components/Settings.tsx index 7902630..a9b2053 100644 --- a/components/Settings.tsx +++ b/components/Settings.tsx @@ -20,8 +20,20 @@ const Settings: React.FC = () => { try { await serialService.enterFactoryModeRead(); - const p = serialService.readRegister.bind(serialService); - const pBytes = serialService.readRegisterBytes.bind(serialService); + // Helper with delay + const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + + const p = async (reg: number) => { + const val = await serialService.readRegister(reg); + await delay(250); + return val; + }; + + const pBytes = async (reg: number) => { + const val = await serialService.readRegisterBytes(reg); + await delay(250); + return val; + }; // 병렬 처리는 시리얼 통신에서 꼬일 수 있으므로 순차 처리 권장 (await 사용) // 주요 레지스터 읽기 @@ -69,13 +81,13 @@ const Settings: React.FC = () => { newConfig.cellCnt = await p(JBD.REG_CELL_CNT); newConfig.cycleCnt = await p(JBD.REG_CYCLE_CNT); newConfig.serialNum = await p(JBD.REG_SERIAL_NUM); - + newConfig.mfgDate = await p(JBD.REG_MFG_DATE); - + const mfgNameRaw = await pBytes(JBD.REG_MFG_NAME); const deviceNameRaw = await pBytes(JBD.REG_DEVICE_NAME); const barcodeRaw = await pBytes(JBD.REG_BARCODE); - + newConfig.mfgName = JBD.JBDProtocol.parseString(mfgNameRaw); newConfig.deviceName = JBD.JBDProtocol.parseString(deviceNameRaw); newConfig.barcode = JBD.JBDProtocol.parseString(barcodeRaw); @@ -98,16 +110,16 @@ const Settings: React.FC = () => { setSuccessMsg(null); try { await serialService.enterFactoryModeRead(); - + let val: any; if (type === 'int' || type === 'date') { // date reads as int raw val = await serialService.readRegister(reg); } else if (type === 'string' || type === 'bytes') { const bytes = await serialService.readRegisterBytes(reg); if (type === 'string') { - val = JBD.JBDProtocol.parseString(bytes); + val = JBD.JBDProtocol.parseString(bytes); } else { - val = bytes; // bytes not fully supported in single row yet unless custom + val = bytes; // bytes not fully supported in single row yet unless custom } } @@ -150,11 +162,11 @@ const Settings: React.FC = () => { }; // 그룹 컴포넌트 - const Group: React.FC<{title: string, children: React.ReactNode}> = ({title, children}) => ( + const Group: React.FC<{ title: string, children: React.ReactNode }> = ({ title, children }) => (

- {title} + {title}

{children}
@@ -162,11 +174,11 @@ const Settings: React.FC = () => { ); // 개별 설정 행 컴포넌트 - const SettingRow: React.FC<{ - label: string; - val: number | string | undefined; - unit?: string; - reg: number; + const SettingRow: React.FC<{ + label: string; + val: number | string | undefined; + unit?: string; + reg: number; field: keyof BMSConfig; scale?: number; type?: 'int' | 'string' | 'bytes' | 'date'; @@ -177,40 +189,40 @@ const Settings: React.FC = () => { // config 값이 업데이트되면 로컬 상태 동기화 useEffect(() => { - if (val === undefined || val === null) { - setLocalVal(''); - setIsDirty(false); + if (val === undefined || val === null) { + setLocalVal(''); + setIsDirty(false); + } else { + if (type === 'date') { + setLocalVal(JBD.JBDProtocol.parseDate(val as number)); + } else if (type === 'int') { + setLocalVal(Number(val) * scale); } else { - if (type === 'date') { - setLocalVal(JBD.JBDProtocol.parseDate(val as number)); - } else if (type === 'int') { - setLocalVal(Number(val) * scale); - } else { - setLocalVal(val as string); - } - setIsDirty(false); + setLocalVal(val as string); } + setIsDirty(false); + } }, [val, scale, type]); const handleChange = (e: React.ChangeEvent) => { - setLocalVal(e.target.value); - setIsDirty(true); + setLocalVal(e.target.value); + setIsDirty(true); }; const onRead = () => { - handleReadSingle(reg, field, type as 'int' | 'string' | 'bytes' | 'date'); + handleReadSingle(reg, field, type as 'int' | 'string' | 'bytes' | 'date'); }; const onSave = () => { - let payload: any = localVal; - if (type === 'int') { - payload = Number(localVal) / scale; - } else if (type === 'date') { - payload = JBD.JBDProtocol.encodeDate(localVal as string); - } - - handleSaveSingle(reg, payload, type as 'int' | 'string' | 'bytes' | 'date'); - setIsDirty(false); + let payload: any = localVal; + if (type === 'int') { + payload = Number(localVal) / scale; + } else if (type === 'date') { + payload = JBD.JBDProtocol.encodeDate(localVal as string); + } + + handleSaveSingle(reg, payload, type as 'int' | 'string' | 'bytes' | 'date'); + setIsDirty(false); }; return ( @@ -219,10 +231,10 @@ const Settings: React.FC = () => {
{label}
ADDR: 0x{reg.toString(16).toUpperCase().padStart(2, '0')}
- +
- { {unit && {type !== 'date' ? unit : ''}}
- -
-
);