import { useEffect, useState } from 'react' import { Power, Minus, Plus, RefreshCw, Loader2 } from 'lucide-react' import { MHI_MODES, MHI_FAN_SPEEDS } from '../types' import { fetchMhiStatus, sendMhiControl } from '../api' // Manual on/off + mode + setpoint + fan speed controls for one assigned MHI // aircon unit (mhi_modbus). No NewBook/scheduler integration — every change // here is an explicit staff action via POST /devices/:id/mhi-control. export default function MhiControlPanel({ deviceId, canControl }: { deviceId: number; canControl: boolean }) { const [loading, setLoading] = useState(true) const [error, setError] = useState('') const [applying, setApplying] = useState(false) const [onOff, setOnOff] = useState(false) const [mode, setMode] = useState('auto') const [setpoint, setSetpoint] = useState(21) const [fanSpeed, setFanSpeed] = useState('medium') const [roomTemp, setRoomTemp] = useState(null) function load() { setLoading(true); setError('') fetchMhiStatus(deviceId) .then(s => { setOnOff(s.onOff === 'on') if (s.mode) setMode(s.mode) if (s.setpoint != null) setSetpoint(s.setpoint) if (s.fanSpeed) setFanSpeed(s.fanSpeed) setRoomTemp(s.roomTemp ?? null) }) .catch(e => setError(e instanceof Error ? e.message : 'Failed to read device status')) .finally(() => setLoading(false)) } useEffect(load, [deviceId]) async function apply(patch: Partial<{ onOff: boolean; mode: string; tempC: number; fanSpeed: string }>) { setApplying(true); setError('') try { const res = await sendMhiControl(deviceId, patch) if (!res.ok) setError('Device did not accept the command') } catch (e) { setError(e instanceof Error ? e.message : 'Command failed') } finally { setApplying(false) } } if (loading) return

Reading aircon status…

return (
{error &&
{error}
}
{setpoint.toFixed(1)}°C
{roomTemp != null && Room: {roomTemp.toFixed(1)}°C}
) }