feat: add ioBroker REST API integration
- New iobroker_sources + iobroker_states tables - Source manager with discover, test, CRUD - State poller (10s interval, getBulk endpoint) - REST API routes under /api/iobroker - Frontend page with search + pagination - Sidebar link, app route Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import Dashboard from './pages/Dashboard';
|
||||
import MqttBrokers from './pages/MqttBrokers';
|
||||
import MqttTopics from './pages/MqttTopics';
|
||||
import PgSources from './pages/PgSources';
|
||||
import IoBroker from './pages/IoBroker';
|
||||
import Datapoints from './pages/Datapoints';
|
||||
import Displays from './pages/Displays';
|
||||
import Layouts from './pages/Layouts';
|
||||
@@ -31,6 +32,7 @@ export default function App() {
|
||||
<Route path="/mqtt/brokers" element={<ProtectedRoute><MqttBrokers /></ProtectedRoute>} />
|
||||
<Route path="/mqtt/topics/:brokerId" element={<ProtectedRoute><MqttTopics /></ProtectedRoute>} />
|
||||
<Route path="/pg/sources" element={<ProtectedRoute><PgSources /></ProtectedRoute>} />
|
||||
<Route path="/iobroker" element={<ProtectedRoute><IoBroker /></ProtectedRoute>} />
|
||||
<Route path="/datapoints" element={<ProtectedRoute><Datapoints /></ProtectedRoute>} />
|
||||
<Route path="/displays" element={<ProtectedRoute><Displays /></ProtectedRoute>} />
|
||||
<Route path="/layouts" element={<ProtectedRoute><Layouts /></ProtectedRoute>} />
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 44 KiB |
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 4.0 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 8.5 KiB |
@@ -5,6 +5,7 @@ const links = [
|
||||
{ to: '/', label: 'Dashboard', exact: true },
|
||||
{ to: '/mqtt/brokers', label: 'MQTT Broker' },
|
||||
{ to: '/pg/sources', label: 'PostgreSQL Quellen' },
|
||||
{ to: '/iobroker', label: 'ioBroker' },
|
||||
{ to: '/datapoints', label: 'Datenpunkte' },
|
||||
{ to: '/displays', label: 'Displays' },
|
||||
{ to: '/layouts', label: 'Layouts' },
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
import { useEffect, useState, useCallback } from 'react';
|
||||
import type { FormEvent } from 'react';
|
||||
import { api } from '../api/client';
|
||||
import StatusBadge from '../components/StatusBadge';
|
||||
|
||||
interface IoBrokerSource {
|
||||
id: number;
|
||||
name: string;
|
||||
host: string;
|
||||
port: number;
|
||||
enabled: number;
|
||||
status: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
interface IoBrokerState {
|
||||
id: number;
|
||||
source_id: number;
|
||||
state_id: string;
|
||||
selected: number;
|
||||
last_value: string | null;
|
||||
last_ts: number | null;
|
||||
discovered_at: string;
|
||||
}
|
||||
|
||||
interface StatesResponse {
|
||||
states: IoBrokerState[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
const emptyForm = { name: '', host: '', port: '8087' };
|
||||
const PAGE_SIZE = 50;
|
||||
|
||||
export default function IoBroker() {
|
||||
const [sources, setSources] = useState<IoBrokerSource[]>([]);
|
||||
const [selectedSource, setSelectedSource] = useState<IoBrokerSource | null>(null);
|
||||
const [form, setForm] = useState(emptyForm);
|
||||
const [error, setError] = useState('');
|
||||
const [testResult, setTestResult] = useState<string | null>(null);
|
||||
const [discoveryMsg, setDiscoveryMsg] = useState('');
|
||||
|
||||
// States panel state
|
||||
const [statesData, setStatesData] = useState<StatesResponse>({ states: [], total: 0 });
|
||||
const [search, setSearch] = useState('');
|
||||
const [searchInput, setSearchInput] = useState('');
|
||||
const [showSelected, setShowSelected] = useState(false);
|
||||
const [page, setPage] = useState(0);
|
||||
|
||||
async function loadSources() {
|
||||
try {
|
||||
setSources(await api.get<IoBrokerSource[]>('/api/iobroker/sources'));
|
||||
} catch (e) {
|
||||
setError((e as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
const loadStates = useCallback(async () => {
|
||||
if (!selectedSource) return;
|
||||
try {
|
||||
const params = new URLSearchParams();
|
||||
if (search) params.set('search', search);
|
||||
if (showSelected) params.set('selected', '1');
|
||||
params.set('limit', String(PAGE_SIZE));
|
||||
params.set('offset', String(page * PAGE_SIZE));
|
||||
const result = await api.get<StatesResponse>(
|
||||
`/api/iobroker/sources/${selectedSource.id}/states?${params}`
|
||||
);
|
||||
setStatesData(result);
|
||||
} catch (e) {
|
||||
setError((e as Error).message);
|
||||
}
|
||||
}, [selectedSource, search, showSelected, page]);
|
||||
|
||||
useEffect(() => { loadSources(); }, []);
|
||||
useEffect(() => { loadStates(); }, [loadStates]);
|
||||
|
||||
// Debounced search
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => { setSearch(searchInput); setPage(0); }, 400);
|
||||
return () => clearTimeout(timer);
|
||||
}, [searchInput]);
|
||||
|
||||
async function handleAdd(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
try {
|
||||
await api.post('/api/iobroker/sources', { ...form, port: parseInt(form.port) });
|
||||
setForm(emptyForm);
|
||||
loadSources();
|
||||
} catch (e) {
|
||||
setError((e as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDelete(id: number) {
|
||||
if (!confirm('Quelle wirklich loeschen?')) return;
|
||||
try {
|
||||
await api.del(`/api/iobroker/sources/${id}`);
|
||||
if (selectedSource?.id === id) {
|
||||
setSelectedSource(null);
|
||||
setStatesData({ states: [], total: 0 });
|
||||
}
|
||||
loadSources();
|
||||
} catch (e) {
|
||||
setError((e as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleTest(source: IoBrokerSource) {
|
||||
setTestResult(null);
|
||||
try {
|
||||
const result = await api.post<{ success: boolean }>(
|
||||
`/api/iobroker/sources/${source.id}/test`
|
||||
);
|
||||
setTestResult(result.success ? 'Verbindung erfolgreich' : 'Nicht erreichbar');
|
||||
} catch (e) {
|
||||
setTestResult('Fehler: ' + (e as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDiscover(source: IoBrokerSource) {
|
||||
setDiscoveryMsg('Discovery laeuft...');
|
||||
try {
|
||||
await api.post(`/api/iobroker/sources/${source.id}/discover`);
|
||||
setDiscoveryMsg('Discovery gestartet. States werden geladen...');
|
||||
setTimeout(() => {
|
||||
loadStates();
|
||||
setDiscoveryMsg('');
|
||||
}, 3000);
|
||||
} catch (e) {
|
||||
setDiscoveryMsg('Fehler: ' + (e as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSelectState(state: IoBrokerState, selected: boolean) {
|
||||
try {
|
||||
await api.put(`/api/iobroker/states/${state.id}`, { selected: selected ? 1 : 0 });
|
||||
loadStates();
|
||||
} catch (e) {
|
||||
setError((e as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
function handleSelectSource(source: IoBrokerSource) {
|
||||
setSelectedSource(source);
|
||||
setPage(0);
|
||||
setSearch('');
|
||||
setSearchInput('');
|
||||
setShowSelected(false);
|
||||
setDiscoveryMsg('');
|
||||
setTestResult(null);
|
||||
}
|
||||
|
||||
const totalPages = Math.ceil(statesData.total / PAGE_SIZE);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold text-gray-800 mb-6">ioBroker Quellen</h2>
|
||||
{error && (
|
||||
<div className="mb-4 p-3 bg-red-50 border border-red-200 rounded text-red-700 text-sm">
|
||||
{error}
|
||||
<button className="ml-2 font-bold" onClick={() => setError('')}>x</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Sources table */}
|
||||
<div className="bg-white rounded-lg border border-gray-200 overflow-hidden mb-6">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50">
|
||||
<tr>
|
||||
<th className="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Name</th>
|
||||
<th className="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Host:Port</th>
|
||||
<th className="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
|
||||
<th className="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Aktionen</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{sources.length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={4} className="px-4 py-6 text-center text-gray-400">
|
||||
Keine ioBroker Quellen konfiguriert
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
{sources.map((s) => (
|
||||
<tr
|
||||
key={s.id}
|
||||
className={`hover:bg-gray-50 cursor-pointer ${selectedSource?.id === s.id ? 'bg-sky-50 ring-1 ring-inset ring-sky-200' : ''}`}
|
||||
onClick={() => handleSelectSource(s)}
|
||||
>
|
||||
<td className="px-4 py-2 font-medium text-gray-800">{s.name}</td>
|
||||
<td className="px-4 py-2 text-gray-600">{s.host}:{s.port}</td>
|
||||
<td className="px-4 py-2"><StatusBadge status={s.status} /></td>
|
||||
<td className="px-4 py-2 flex gap-2">
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); handleTest(s); }}
|
||||
className="text-sky-500 hover:text-sky-700 text-xs"
|
||||
>
|
||||
Testen
|
||||
</button>
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); handleSelectSource(s); handleDiscover(s); }}
|
||||
className="text-indigo-500 hover:text-indigo-700 text-xs"
|
||||
>
|
||||
Discovery
|
||||
</button>
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); handleDelete(s.id); }}
|
||||
className="text-red-500 hover:text-red-700 text-xs"
|
||||
>
|
||||
Loeschen
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{testResult && (
|
||||
<div className="mb-4 p-3 bg-blue-50 border border-blue-200 rounded text-blue-700 text-sm">
|
||||
{testResult}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Add form */}
|
||||
<div className="bg-white rounded-lg border border-gray-200 p-6 mb-8">
|
||||
<h3 className="font-medium text-gray-700 mb-4">Quelle hinzufuegen</h3>
|
||||
<form onSubmit={handleAdd} className="grid grid-cols-3 gap-4">
|
||||
<div>
|
||||
<label className="block text-xs text-gray-600 mb-1">Name *</label>
|
||||
<input
|
||||
type="text"
|
||||
required
|
||||
className="w-full border border-gray-300 rounded px-3 py-1.5 text-sm"
|
||||
value={form.name}
|
||||
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs text-gray-600 mb-1">Host *</label>
|
||||
<input
|
||||
type="text"
|
||||
required
|
||||
className="w-full border border-gray-300 rounded px-3 py-1.5 text-sm"
|
||||
value={form.host}
|
||||
onChange={(e) => setForm({ ...form, host: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs text-gray-600 mb-1">Port</label>
|
||||
<input
|
||||
type="number"
|
||||
className="w-full border border-gray-300 rounded px-3 py-1.5 text-sm"
|
||||
value={form.port}
|
||||
onChange={(e) => setForm({ ...form, port: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="col-span-3">
|
||||
<button
|
||||
type="submit"
|
||||
className="bg-sky-500 hover:bg-sky-600 text-white text-sm font-medium px-4 py-2 rounded transition-colors"
|
||||
>
|
||||
Hinzufuegen
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{/* States panel */}
|
||||
{selectedSource && (
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold text-gray-700 mb-2">
|
||||
States — {selectedSource.name}
|
||||
</h3>
|
||||
<p className="text-sm text-gray-500 mb-4">
|
||||
{statesData.total} States gefunden
|
||||
</p>
|
||||
|
||||
{discoveryMsg && (
|
||||
<p className="text-sm text-indigo-600 mb-3">{discoveryMsg}</p>
|
||||
)}
|
||||
|
||||
{/* Controls */}
|
||||
<div className="flex gap-3 mb-4 flex-wrap items-center">
|
||||
<input
|
||||
placeholder="States suchen..."
|
||||
className="border border-gray-300 rounded px-3 py-1.5 text-sm w-72"
|
||||
value={searchInput}
|
||||
onChange={(e) => setSearchInput(e.target.value)}
|
||||
/>
|
||||
<label className="flex items-center gap-1.5 text-sm text-gray-600 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={showSelected}
|
||||
onChange={(e) => { setShowSelected(e.target.checked); setPage(0); }}
|
||||
className="accent-green-500"
|
||||
/>
|
||||
Nur ausgewaehlte
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="bg-white rounded-lg border border-gray-200 overflow-hidden">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50">
|
||||
<tr>
|
||||
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 w-10">Aktiv</th>
|
||||
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500">State ID</th>
|
||||
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 w-48">Letzter Wert</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{statesData.states.length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={3} className="px-3 py-6 text-center text-gray-400">
|
||||
{statesData.total === 0
|
||||
? 'Keine States gefunden. Starte Discovery.'
|
||||
: 'Keine States fuer diese Suche.'}
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
{statesData.states.map((s) => (
|
||||
<tr key={s.id} className={`hover:bg-gray-50 ${s.selected ? 'bg-green-50' : ''}`}>
|
||||
<td className="px-3 py-1.5 text-center">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={!!s.selected}
|
||||
onChange={(e) => handleSelectState(s, e.target.checked)}
|
||||
className="w-4 h-4 accent-green-500 cursor-pointer"
|
||||
/>
|
||||
</td>
|
||||
<td className="px-3 py-1.5 font-mono text-xs text-gray-700 truncate max-w-md" title={s.state_id}>
|
||||
{s.state_id}
|
||||
</td>
|
||||
<td className="px-3 py-1.5 text-xs text-gray-500 truncate max-w-48" title={s.last_value ?? ''}>
|
||||
{s.last_value ?? '—'}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{totalPages > 1 && (
|
||||
<div className="flex items-center gap-2 mt-3 text-sm text-gray-600">
|
||||
<button
|
||||
onClick={() => setPage(Math.max(0, page - 1))}
|
||||
disabled={page === 0}
|
||||
className="px-3 py-1 rounded border border-gray-300 disabled:opacity-30 hover:bg-gray-50"
|
||||
>
|
||||
Zurueck
|
||||
</button>
|
||||
<span>Seite {page + 1} von {totalPages}</span>
|
||||
<button
|
||||
onClick={() => setPage(Math.min(totalPages - 1, page + 1))}
|
||||
disabled={page >= totalPages - 1}
|
||||
className="px-3 py-1 rounded border border-gray-300 disabled:opacity-30 hover:bg-gray-50"
|
||||
>
|
||||
Weiter
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user