Liesten ProCon

This commit is contained in:
marco committed 2026-10-27 10:23:17 +01:00
1 parent d9962e22b8
commit 0de185c014
54 files changed
+1105 -392

No files matched your search

@@ -0,0 +1,63 @@
'use client';
import { useEffect, useState } from 'react';
import Modal from '@/components/ui/Modal';
import { Button } from '@/components/ui/button';
import { Select } from '@/components/ui/select';
import { api_compareAnalog, api_syncAnalog } from '@/actions/sensoren/api-analog';
import { analogLabels, knownAnalogStates, type AnalogComparison, type AnalogSlot, type AnalogTable } from '@/types/sensoren/analog';
export default function AnalogSyncDialog({ id, onDone, onClose }: { id: string; onDone: (data: AnalogTable) => void; onClose: () => void }) {
const [data, setData] = useState<AnalogComparison | null>(null), [busy, setBusy] = useState(true), [error, setError] = useState('');
const [parts, setParts] = useState({ fields: false, layout: true, graphics: false });
const [targets, setTargets] = useState<Partial<Record<AnalogSlot, number>>>({}), [valueTarget, setValueTarget] = useState('');
async function load() {
setBusy(true); setError('');
try {
const next = await api_compareAnalog(id); setData(next);
const chosen: Partial<Record<AnalogSlot, number>> = {};
for (const graphic of next.local.graphics) {
const state = knownAnalogStates[graphic.slot];
const matches = state === undefined ? [] : next.visuals.filter(v => v.SensorVisualisierung_State === state);
if (matches.length === 1) chosen[graphic.slot] = matches[0].Id;
}
setTargets(chosen);
const values = next.visuals.filter(v => v.SensorVisualisierung_State === 2 && v.SensorVisualisierung_ViewId == null);
setValueTarget(values.length === 1 ? String(values[0].Id) : '');
} catch (cause) { setError(cause instanceof Error ? cause.message : 'Vergleich fehlgeschlagen'); }
finally { setBusy(false); }
}
useEffect(() => { void load(); }, [id]); // eslint-disable-line react-hooks/exhaustive-deps
async function apply(direction: 'toLocal' | 'toProcon') {
if (!data || busy) return; setBusy(true); setError('');
try { onDone(await api_syncAnalog(id, { anlageId: data.anlageId, verbindungId: data.verbindungId, version: data.version,
direction, ...parts, targets, ...(valueTarget ? { valueTargetId: Number(valueTarget) } : {}) })); onClose(); }
catch (cause) { setError(cause instanceof Error ? cause.message : 'Synchronisierung fehlgeschlagen'); }
finally { setBusy(false); }
}
return <Modal onClose={() => { if (!busy) onClose(); }}><div className="max-h-[85vh] w-[min(55rem,85vw)] space-y-3 overflow-auto p-2">
<h2 className="font-semibold">Analogsensor mit ProCon vergleichen</h2>
<Button size="sm" variant="secondary" disabled={busy} onClick={() => void load()}>Vergleich neu laden</Button>
{data && <><p className="text-sm">Daten-Nr. {data.local.datenNr} · ProCon-ID {data.remote.Id}</p>
<table className="w-full text-left text-xs"><thead><tr><th>Feld</th><th>Lokal</th><th>ProCon</th></tr></thead><tbody>
{([['Beschreibung', data.local.beschreibung, data.remote.Text], ['Einheit', data.local.einheit, data.remote.Einheit], ['Statistik-ID', data.local.statistikProconId, data.remote.StatistikTyp]]).map(([label, local, remote]) =>
<tr key={String(label)}><th>{label}</th><td>{local ?? 'leer'}</td><td>{remote ?? 'leer'}</td></tr>)}
</tbody></table>
<p className="rounded border p-2 text-xs text-amber-800">{data.notice}</p>
<label className="block text-xs">Wertanzeige / Wertkasten<Select disabled={busy} value={valueTarget} onChange={e => setValueTarget(e.target.value)}><option value="">Hauptview auswählen</option>
{data.visuals.filter(v => v.SensorVisualisierung_State === 2).map(v => <option key={v.Id} value={v.Id}>{v.Id} · X {v.PositionX}, Y {v.PositionY}, {v.Width} × {v.Height} · XAML {v.SensorVisualisierung_ViewId ?? 'keines'}</option>)}
</Select></label>
{data.local.graphics.map(graphic => <div key={graphic.slot} className="rounded border p-2 text-xs"><label>{analogLabels[graphic.slot]}<Select disabled={busy} value={targets[graphic.slot] ?? ''} onChange={e => {
const next = { ...targets }; if (e.target.value) next[graphic.slot] = Number(e.target.value); else delete next[graphic.slot]; setTargets(next);
}}><option value="">ProCon-Visualisierung auswählen</option>
{data.visuals.filter(v => knownAnalogStates[graphic.slot] === undefined || v.SensorVisualisierung_State === knownAnalogStates[graphic.slot]).map(v =>
<option key={v.Id} value={v.Id}>{v.Id} · Zustand {v.SensorVisualisierung_State} · XAML {v.SensorVisualisierung_ViewId ?? 'keines'} · X {v.PositionX}, Y {v.PositionY}, {v.Width} × {v.Height}</option>)}
</Select></label><p>Lokal: {(() => { const p = data.local.placements.find(p => p.id === graphic.placementId)!; return `X ${p.x}, Y ${p.y}, ${p.width} × ${p.height}, Z ${p.z}`; })()}</p></div>)}
<fieldset disabled={busy} className="flex flex-wrap gap-3 text-sm">{(['fields', 'layout', 'graphics'] as const).map(key => <label key={key}>
<input type="checkbox" checked={parts[key]} onChange={e => setParts({ ...parts, [key]: e.target.checked })} /> {({ fields: 'Fachdaten', layout: 'Position und Größe', graphics: 'XAML-Grafiken' })[key]}</label>)}</fieldset>
<div className="flex gap-2"><Button size="sm" disabled={busy || !Object.values(parts).some(Boolean)} onClick={() => void apply('toProcon')}>Nach ProCon übernehmen</Button>
<Button size="sm" variant="secondary" disabled={busy || !Object.values(parts).some(Boolean)} onClick={() => void apply('toLocal')}>Aus ProCon übernehmen</Button></div>
</>}
{busy && <p role="status">Wird verarbeitet …</p>}{error && <p role="alert" className="text-red-600">{error}</p>}
<Button size="sm" variant="ghost" disabled={busy} onClick={onClose}>Schließen</Button>
</div></Modal>;
}