Liesten ProCon
This commit is contained in:
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>;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
'use client';
|
||||
import { useEffect, useState } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Select } from '@/components/ui/select';
|
||||
import PlacementFields from '@/components/visualisierung/placementFields';
|
||||
import GraphicBindingsEditor from '@/components/visualisierung/graphicBindingsEditor';
|
||||
import AnalogSyncDialog from './analogSyncDialog';
|
||||
import { api_analog, api_saveAnalog } from '@/actions/sensoren/api-analog';
|
||||
import { analogSlots, analogLabels, type AnalogTable as TableData, type AnalogRow, type AnalogSource } from '@/types/sensoren/analog';
|
||||
|
||||
export default function AnalogTable({ initial, sources }: { initial: TableData; sources: AnalogSource[] }) {
|
||||
const [data, setData] = useState(initial), [selectedId, setSelectedId] = useState(initial.rows[0]?.id ?? '');
|
||||
const [dirty, setDirty] = useState(false), [busy, setBusy] = useState(false), [error, setError] = useState(''), [message, setMessage] = useState('');
|
||||
const [filter, setFilter] = useState(''), [graphicFilter, setGraphicFilter] = useState('all'), [syncId, setSyncId] = useState<string | null>(null);
|
||||
const selected = data.rows.find(row => row.id === selectedId);
|
||||
const terms = filter.toLocaleLowerCase('de').split(/\s+/).filter(Boolean);
|
||||
const visible = data.rows.filter(row => terms.every(t => `${row.datenNr} ${row.beschreibung} ${row.einheit} ${row.statistikTyp}`.toLocaleLowerCase('de').includes(t)) &&
|
||||
(graphicFilter === 'all' || (graphicFilter === 'with' ? row.graphics.length > 0 : !row.graphics.length)));
|
||||
useEffect(() => {
|
||||
if (!dirty) return; const warn = (e: BeforeUnloadEvent) => { e.preventDefault(); e.returnValue = ''; };
|
||||
window.addEventListener('beforeunload', warn); return () => window.removeEventListener('beforeunload', warn);
|
||||
}, [dirty]);
|
||||
function update(next: TableData) { setData(next); setDirty(true); setMessage(''); }
|
||||
function edit(patch: Partial<AnalogRow>) { if (selected) update({ ...data, rows: data.rows.map(row => row.id === selected.id ? { ...row, ...patch } : row) }); }
|
||||
function add() {
|
||||
let datenNr = 0; while (data.rows.some(row => row.datenNr === datenNr)) datenNr++;
|
||||
if (datenNr >= data.reservedCount) { setError('Alle Plätze belegt. Anzahl erhöhen.'); return; }
|
||||
const row: AnalogRow = { id: crypto.randomUUID(), datenNr, beschreibung: '', statistikTyp: '', statistikProconId: null, einheit: '', hinzugefuegtAm: '',
|
||||
componentId: null, prozentkasten: false, valuePlacementId: 'wert', graphics: [], placements: [
|
||||
{ id: 'wert', x: 0, y: 0, width: 100, height: 60, z: 0, show: true },
|
||||
{ id: 'grafik', x: 0, y: 80, width: 60, height: 60, z: 0, show: true },
|
||||
] };
|
||||
update({ ...data, rows: [...data.rows, row] }); setSelectedId(row.id);
|
||||
}
|
||||
async function run(save: boolean) {
|
||||
if (busy || !save && dirty) return; setBusy(true); setError('');
|
||||
try { setData(save ? await api_saveAnalog(data) : await api_analog()); setDirty(false); setMessage(save ? 'Sensoren gespeichert.' : 'Neu geladen.'); }
|
||||
catch (cause) { setError(cause instanceof Error ? cause.message : 'Aktion fehlgeschlagen'); }
|
||||
finally { setBusy(false); }
|
||||
}
|
||||
function navigate(e: React.MouseEvent) { if (dirty || busy) { e.preventDefault(); setError('Bitte zuerst Änderungen speichern.'); } }
|
||||
return <div className="space-y-3">
|
||||
<div className="flex gap-3 text-sm"><Link href="/rk2/anzeigewerte" className="underline" onClick={navigate}>Allgemeine Anzeigewerte</Link><Link href="/visualisierung" className="underline" onClick={navigate}>Web-Visualisierung</Link></div>
|
||||
<h1 className="text-xl font-semibold">Sensoren_Analog</h1>
|
||||
<fieldset disabled={busy} className="flex flex-wrap items-end gap-2">
|
||||
<label className="text-xs">Anzahl Sensoren Analog<Input className="w-24" type="number" min={1} max={10000} value={Number.isFinite(data.reservedCount) ? data.reservedCount : ''} onChange={e => update({ ...data, reservedCount: e.target.valueAsNumber })} /></label>
|
||||
<Button size="sm" onClick={add}>Sensor hinzufügen</Button><Button size="sm" disabled={!dirty} onClick={() => void run(true)}>Speichern</Button>
|
||||
<Button size="sm" variant="secondary" disabled={dirty} onClick={() => void run(false)}>Neu laden</Button>
|
||||
<Button asChild size="sm" variant="secondary"><a href="/rk2/sensoren-analog/export" download onClick={navigate}>Excel herunterladen</a></Button>
|
||||
<Button asChild size="sm" variant="secondary"><a href="/rk2/sensoren-analog/xaml" download onClick={navigate}>XAML herunterladen</a></Button>
|
||||
</fieldset>
|
||||
<p className="text-xs text-gray-600">Der Export enthält alle reservierten Daten-Nummern ab 0, auch leere Plätze. Wertkasten und Grafikbelegungen werden unabhängig voneinander platziert.</p>
|
||||
<div className="flex gap-2"><Input className="max-w-sm" type="search" aria-label="Sensoren suchen" placeholder="Nummer, Beschreibung, Statistik oder Einheit" value={filter} onChange={e => setFilter(e.target.value)} />
|
||||
<Select className="w-40" aria-label="Grafikfilter" value={graphicFilter} onChange={e => setGraphicFilter(e.target.value)}><option value="all">Alle</option><option value="with">Mit Grafik</option><option value="without">Ohne Grafik</option></Select>
|
||||
<span className="text-xs">{visible.length} von {data.rows.length}</span></div>
|
||||
<div className="grid gap-3 xl:grid-cols-[minmax(18rem,1fr)_2fr]">
|
||||
<div className="max-h-[70vh] overflow-auto rounded border"><table className="w-full text-left text-sm"><thead><tr><th>Nr.</th><th>Beschreibung</th><th>Einheit</th><th>Kasten</th></tr></thead><tbody>
|
||||
{visible.sort((a, b) => a.datenNr - b.datenNr).map(row => <tr key={row.id} className={row.id === selectedId ? 'bg-blue-50' : ''}><td className="p-2">{row.datenNr}</td>
|
||||
<td><button type="button" className="underline" disabled={busy} onClick={() => setSelectedId(row.id)}>{row.beschreibung || '(leer)'}</button></td><td>{row.einheit}</td><td>{row.prozentkasten ? 'Ja' : 'Nein'}</td></tr>)}
|
||||
</tbody></table>{!visible.length && <p className="p-3 text-sm">Keine Sensoren gefunden.</p>}</div>
|
||||
{selected && <fieldset disabled={busy} className="space-y-3 rounded border p-3">
|
||||
<div className="flex gap-2"><Button size="sm" variant="secondary" disabled={dirty} onClick={() => setSyncId(selected.id)}>Mit ProCon vergleichen</Button>
|
||||
<Button size="sm" variant="ghost" onClick={() => { update({ ...data, rows: data.rows.filter(row => row.id !== selected.id) }); setSelectedId(''); }}>Zeile leeren</Button></div>
|
||||
<div className="flex flex-wrap gap-2"><label className="text-xs">Daten-Nr.<Input className="w-20" type="number" value={Number.isFinite(selected.datenNr) ? selected.datenNr : ''} onChange={e => edit({ datenNr: e.target.valueAsNumber })} /></label>
|
||||
<label className="text-xs">Beschreibung<Input className="w-64" value={selected.beschreibung} onChange={e => edit({ beschreibung: e.target.value })} /></label>
|
||||
<label className="text-xs">Einheit<Input className="w-16" value={selected.einheit} onChange={e => edit({ einheit: e.target.value })} /></label>
|
||||
<label className="text-xs">StatistikTyp (Excel)<Input className="w-32" value={selected.statistikTyp} onChange={e => edit({ statistikTyp: e.target.value })} /></label>
|
||||
<label className="text-xs">StatistikTyp-ID (ProCon)<Input className="w-24" type="number" value={selected.statistikProconId ?? ''} onChange={e => edit({ statistikProconId: e.target.value ? e.target.valueAsNumber : null })} /></label>
|
||||
<label className="text-xs">Hinzugefügt Am<Input type="date" value={selected.hinzugefuegtAm} onChange={e => edit({ hinzugefuegtAm: e.target.value })} /></label>
|
||||
</div>
|
||||
<label className="block text-xs">Unit-Komponente (optional)<Select value={selected.componentId ?? ''} onChange={e => edit({ componentId: e.target.value || null })}><option value="">Keine Zuordnung</option>
|
||||
{sources.map(source => <option key={source.componentId} value={source.componentId}>{source.label} · {source.type}</option>)}</Select></label>
|
||||
<Button size="sm" variant="secondary" disabled={!selected.componentId} onClick={() => { const source = sources.find(s => s.componentId === selected.componentId); if (source) edit({ beschreibung: source.label, ...(source.type === 'Fuellstand' ? { statistikTyp: 'Fuellstand' } : {}) }); }}>Beschreibung aus Unit übernehmen</Button>
|
||||
<h2 className="text-sm font-semibold">Wertanzeige</h2>
|
||||
<label className="flex items-center gap-2 text-sm"><input type="checkbox" checked={selected.prozentkasten} onChange={e => edit({ prozentkasten: e.target.checked })} />Prozentkasten anzeigen (Skala 0–100)</label>
|
||||
<p className="text-xs text-gray-600">Unabhängig von der Einheit. Ohne Haken erscheint eine Text-/Wertanzeige. Der Schalter bleibt beim ProCon-Import erhalten; ProCon verwendet laut bestehender Dokumentation die Einheit % zur Kastensteuerung.</p>
|
||||
<label className="text-xs">Platzierung der Wertanzeige<Select value={selected.valuePlacementId} onChange={e => edit({ valuePlacementId: e.target.value })}>{selected.placements.map(p => <option key={p.id}>{p.id}</option>)}</Select></label>
|
||||
{selected.placements.map(p => <div key={p.id} className="space-y-1 rounded border p-2"><p className="text-xs">{p.id}{p.id === selected.valuePlacementId ? ' · Wertanzeige' : ''}</p>
|
||||
<PlacementFields value={p} onChange={next => edit({ placements: selected.placements.map(old => old.id === p.id ? next : old) })} /></div>)}
|
||||
<Button size="sm" variant="secondary" onClick={() => edit({ placements: [...selected.placements, { id: crypto.randomUUID(), x: 0, y: 0, width: 60, height: 60, z: 0, show: true }] })}>Separate Platzierung hinzufügen</Button>
|
||||
<h2 className="text-sm font-semibold">XAML-Grafikbelegungen</h2>
|
||||
<GraphicBindingsEditor key={selected.id} slots={analogSlots} labels={analogLabels} graphics={selected.graphics} placements={selected.placements}
|
||||
defaultPlacementId={selected.placements.find(p => p.id !== selected.valuePlacementId)?.id ?? selected.valuePlacementId} onChange={graphics => edit({ graphics })} />
|
||||
</fieldset>}
|
||||
</div>
|
||||
{syncId && <AnalogSyncDialog id={syncId} onClose={() => setSyncId(null)} onDone={next => { setData(next); setDirty(false); setMessage('Synchronisierung abgeschlossen.'); }} />}
|
||||
{dirty && <p className="text-sm text-amber-700">Ungespeicherte Änderungen</p>}{message && <p role="status">{message}</p>}{error && <p role="alert" className="text-red-600">{error}</p>}
|
||||
</div>;
|
||||
}
|
||||
Reference in new issue
Block a user