21 lines
2.2 KiB
TypeScript
21 lines
2.2 KiB
TypeScript
'use client';
|
|
import {useRef,useState} from 'react';
|
|
import {Button} from '@/components/ui/button';
|
|
import {api_motorCommand} from '@/actions/sensoren/api-motor-opc';
|
|
import type {MotorLiveRow} from '@/types/motor-opc';
|
|
const commands=[['Taster_1','Start rechts'],['Taster_2','Stopp'],['Taster_3','Start links'],['UmschaltungAutomatik','Automatik'],['UmschaltungHand','Handbetrieb'],['UmschaltungRevision','Revision']] as const;
|
|
export default function MotorControl({motor,anlageId,online}:{motor:MotorLiveRow;anlageId:string;online:boolean}){
|
|
const pending=useRef(false),[busy,setBusy]=useState(false),[message,setMessage]=useState(''),[error,setError]=useState('');
|
|
async function send(command:string){
|
|
if(pending.current||!online||!motor.control)return;
|
|
pending.current=true;setBusy(true);setMessage('');setError('');
|
|
try{const result=await api_motorCommand(motor.componentId,{anlageId,revision:motor.revision??0,controlId:motor.control.id,command});
|
|
const answer=result.results.find(r=>r.field===command);if(!answer?.good)throw new Error('Befehl nicht bestätigt: '+(answer?.status??'Ergebnis unbekannt'));
|
|
setMessage('Schreibzugriff bestätigt. Die CPU setzt das Befehlsbit zurück; die Ausführung zeigt die Statusrückmeldung.');
|
|
}catch(cause){setError(cause instanceof Error?cause.message:'Schreibergebnis unbekannt. CPU-Rückmeldung prüfen.');}
|
|
finally{pending.current=false;setBusy(false);}
|
|
}
|
|
if(!motor.control)return <p className="text-xs">Keine Steuerstruktur zugeordnet.</p>;
|
|
return <div className="space-y-2 border-t pt-2"><p className="text-sm">Motorbedienung · {motor.control.name}</p><p className="text-xs">Steuert beide Motoren gemeinsam, falls vorhanden. Jeder Klick schreibt einmal true; die CPU setzt das Bit zurück.</p><fieldset disabled={busy||!online} className="flex flex-wrap gap-2">{commands.map(([field,label])=><Button key={field} size="sm" variant={field==='Taster_2'?'danger':'secondary'} onClick={()=>void send(field)}>{label}</Button>)}</fieldset>{!online&&<p className="text-xs">Bedienung bei fehlender Live-Verbindung gesperrt.</p>}{message&&<p role="status" className="text-xs">{message}</p>}{error&&<p role="alert" className="text-sm text-red-700">{error}</p>}</div>;
|
|
}
|