2026-11-17 08:51:48 +01:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
|
import { ActionIcon, Button, Card, Checkbox, Code, Group, Modal, ScrollArea, Select, Stack, Table, Text, TextInput } from "@mantine/core";
|
2026-11-16 12:40:03 +01:00
|
|
|
import { notifications } from "@mantine/notifications";
|
2026-11-17 08:51:48 +01:00
|
|
|
import { IconPlus, IconX } from "@tabler/icons-react";
|
2026-11-16 12:40:03 +01:00
|
|
|
|
|
|
|
|
// A request to the API; a refusal of the server shows as a notification -> the JSON returned, or null
|
|
|
|
|
export async function call(path, method = "GET", body) {
|
|
|
|
|
try {
|
|
|
|
|
const r = await fetch(path, body === undefined ? { method }
|
|
|
|
|
: { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
|
|
|
|
const json = await r.json().catch(() => ({ error: r.statusText }));
|
|
|
|
|
if (r.ok) return json;
|
|
|
|
|
notifications.show({ color: "red", title: "Refused", message: json.error || r.statusText });
|
|
|
|
|
} catch (e) {
|
|
|
|
|
notifications.show({ color: "red", title: "Server unreachable", message: e.message });
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The station and the range (UTC, To excluded; empty: the whole period), shared by the pages through the address
|
|
|
|
|
export function Selector({ selection, choose }) {
|
2026-11-17 08:51:48 +01:00
|
|
|
const [stations, setStations] = useState([]), [s, setS] = useState(selection), [editing, setEditing] = useState(false);
|
|
|
|
|
useEffect(() => { call("/api/stations").then(l => l && setStations(l)); }, [selection]); // a pumping may add one
|
|
|
|
|
useEffect(() => setS(selection), [selection]); // a new object at each change of the address, or at each Load
|
2026-11-16 12:40:03 +01:00
|
|
|
return (
|
|
|
|
|
<Card shadow="sm" withBorder>
|
|
|
|
|
<Group align="flex-end" gap="md">
|
|
|
|
|
<Select label="Station" placeholder="Choose" data={stations} value={s.station || null} searchable w={200}
|
|
|
|
|
onChange={v => setS({ ...s, station: v || "" })} />
|
|
|
|
|
<TextInput label="From (UTC)" type="date" value={s.start} onChange={e => setS({ ...s, start: e.currentTarget.value })} />
|
|
|
|
|
<TextInput label="To (excluded)" type="date" value={s.end} onChange={e => setS({ ...s, end: e.currentTarget.value })} />
|
|
|
|
|
<Button onClick={() => choose(s)} disabled={!s.station}>Load</Button>
|
2026-11-17 08:51:48 +01:00
|
|
|
<Button variant="default" onClick={() => setEditing(true)}>Stations and pumping</Button>
|
2026-11-16 12:40:03 +01:00
|
|
|
</Group>
|
2026-11-17 08:51:48 +01:00
|
|
|
<Stations opened={editing} close={() => setEditing(false)} selection={s} reload={() => choose(selection)} />
|
2026-11-16 12:40:03 +01:00
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-17 08:51:48 +01:00
|
|
|
|
|
|
|
|
const shift = (d, n) => (d ? new Date(Date.parse(d) + n * 864e5).toISOString().slice(0, 10) : "");
|
|
|
|
|
|
|
|
|
|
// The tracked stations (extraction/stations.txt of ~/aimsight-data): serial and position (for the weather of the Data page);
|
|
|
|
|
// the checked ones pumped (extraction/pump.sh) over a range, or without range their missing days up to now. The state of
|
|
|
|
|
// the pumping read every 2 s while the window is open or a pumping runs; at its end, the page loads again
|
|
|
|
|
function Stations({ opened, close, selection, reload }) {
|
|
|
|
|
const [rows, setRows] = useState([]), [checked, setChecked] = useState([]), [range, setRange] = useState({});
|
|
|
|
|
const [images, setImages] = useState(true), [state, setState] = useState(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!opened) return;
|
|
|
|
|
call("/api/tracked").then(l => l && setRows(l));
|
|
|
|
|
setChecked(selection.station ? [selection.station] : []);
|
|
|
|
|
setRange({ start: shift(selection.start, -3), end: selection.end }); // 3 days before: the history of the detectors
|
|
|
|
|
}, [opened]);
|
|
|
|
|
const watch = opened || !!state?.running;
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!watch) return;
|
|
|
|
|
const read = () => fetch("/api/pump").then(r => r.ok && r.json()).then(p => p && setState(p)).catch(() => {}); // silently
|
|
|
|
|
read();
|
|
|
|
|
const timer = setInterval(read, 2000);
|
|
|
|
|
return () => clearInterval(timer);
|
|
|
|
|
}, [watch]);
|
|
|
|
|
const running = !!state?.running, was = useRef(false);
|
|
|
|
|
useEffect(() => { if (was.current && !running) reload(); was.current = running; }, [running]);
|
|
|
|
|
|
|
|
|
|
const set = (i, k, v) => setRows(rows.map((r, n) => (n === i ? { ...r, [k]: v } : r)));
|
|
|
|
|
const save = () => call("/api/tracked", "PUT", { stations: rows.filter(r => r.serial) }).then(l => l && setRows(l));
|
|
|
|
|
const pump = () => call("/api/pump", "POST", { stations: checked, ...range, images }).then(p => p && setState(p));
|
|
|
|
|
return (
|
|
|
|
|
<Modal opened={opened} onClose={close} title="Stations and pumping" size="xl">
|
|
|
|
|
<Stack gap="md">
|
|
|
|
|
<Table withRowBorders={false} verticalSpacing={4}>
|
|
|
|
|
<Table.Thead><Table.Tr><Table.Th w={40}>Pump</Table.Th><Table.Th>Serial</Table.Th>
|
|
|
|
|
<Table.Th>Position (lat,lon or a Swiss place, for the weather)</Table.Th><Table.Th w={40} /></Table.Tr></Table.Thead>
|
|
|
|
|
<Table.Tbody>
|
|
|
|
|
{rows.map((r, i) => (
|
|
|
|
|
<Table.Tr key={i}>
|
|
|
|
|
<Table.Td><Checkbox checked={checked.includes(r.serial)} disabled={!r.serial} aria-label={`Pump ${r.serial}`}
|
|
|
|
|
onChange={e => setChecked(e.currentTarget.checked ? [...checked, r.serial] : checked.filter(c => c !== r.serial))} /></Table.Td>
|
|
|
|
|
<Table.Td><TextInput size="xs" value={r.serial} onChange={e => set(i, "serial", e.currentTarget.value.trim())} /></Table.Td>
|
|
|
|
|
<Table.Td><TextInput size="xs" value={r.position} placeholder="46.6895,7.7790"
|
|
|
|
|
onChange={e => set(i, "position", e.currentTarget.value)} /></Table.Td>
|
|
|
|
|
<Table.Td><ActionIcon variant="subtle" color="gray" aria-label="Remove" onClick={() => setRows(rows.filter((_, n) => n !== i))}>
|
|
|
|
|
<IconX size={16} /></ActionIcon></Table.Td>
|
|
|
|
|
</Table.Tr>
|
|
|
|
|
))}
|
|
|
|
|
</Table.Tbody>
|
|
|
|
|
</Table>
|
|
|
|
|
<Group gap="sm">
|
|
|
|
|
<Button size="xs" variant="default" leftSection={<IconPlus size={14} />}
|
|
|
|
|
onClick={() => setRows([...rows, { serial: "", position: "" }])}>Add a station</Button>
|
|
|
|
|
<Button size="xs" onClick={save}>Save the list</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
<Group align="flex-end" gap="md">
|
|
|
|
|
<TextInput label="From (UTC)" type="date" value={range.start || ""} onChange={e => setRange({ ...range, start: e.currentTarget.value })} />
|
|
|
|
|
<TextInput label="To (excluded)" type="date" value={range.end || ""} onChange={e => setRange({ ...range, end: e.currentTarget.value })} />
|
|
|
|
|
<Checkbox label="Images" checked={images} onChange={e => setImages(e.currentTarget.checked)} mb={8} />
|
|
|
|
|
<Button onClick={pump} loading={running} disabled={!checked.length}>Pump {checked.length || ""}</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
<Text size="xs" c="dimmed">From is 3 days before the range shown (the history of the detectors); without dates, the missing days of
|
|
|
|
|
each station up to now. Read-only on the platform; a long range, outside busy hours.</Text>
|
|
|
|
|
{state?.log?.length > 0 && (
|
|
|
|
|
<ScrollArea h={220} viewportRef={v => v && (v.scrollTop = v.scrollHeight)}>
|
|
|
|
|
<Code block>{state.log.join("\n")}</Code>
|
|
|
|
|
</ScrollArea>
|
|
|
|
|
)}
|
|
|
|
|
</Stack>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|