2024-08-31 18:46:02 +00:00
|
|
|
import RefreshIcon from "@mui/icons-material/Refresh";
|
|
|
|
import {
|
|
|
|
IconButton,
|
|
|
|
Paper,
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableContainer,
|
|
|
|
TableHead,
|
|
|
|
TableRow,
|
|
|
|
Tooltip,
|
|
|
|
} from "@mui/material";
|
|
|
|
import React from "react";
|
2024-09-26 21:21:56 +00:00
|
|
|
import { Device, DeviceApi, DeviceRelay, DeviceURL } from "../api/DeviceApi";
|
2024-09-25 17:35:39 +00:00
|
|
|
import { RelayApi, RelaysStatus } from "../api/RelayApi";
|
2024-08-31 18:46:02 +00:00
|
|
|
import { AsyncWidget } from "../widgets/AsyncWidget";
|
2024-09-25 19:56:54 +00:00
|
|
|
import { BoolText } from "../widgets/BoolText";
|
2024-08-31 18:46:02 +00:00
|
|
|
import { SolarEnergyRouteContainer } from "../widgets/SolarEnergyRouteContainer";
|
2024-09-25 17:35:39 +00:00
|
|
|
import { TimeWidget } from "../widgets/TimeWidget";
|
2024-09-26 21:21:56 +00:00
|
|
|
import { useNavigate } from "react-router-dom";
|
2024-08-31 18:46:02 +00:00
|
|
|
|
2024-09-26 21:14:18 +00:00
|
|
|
export function RelaysListRoute(p: {
|
|
|
|
homeWidget?: boolean;
|
|
|
|
}): React.ReactElement {
|
2024-08-31 18:46:02 +00:00
|
|
|
const loadKey = React.useRef(1);
|
|
|
|
|
|
|
|
const [list, setList] = React.useState<DeviceRelay[] | undefined>();
|
2024-09-26 21:21:56 +00:00
|
|
|
const [devices, setDevices] = React.useState<Device[] | undefined>();
|
2024-09-25 17:35:39 +00:00
|
|
|
const [status, setStatus] = React.useState<RelaysStatus | undefined>();
|
2024-08-31 18:46:02 +00:00
|
|
|
|
|
|
|
const load = async () => {
|
|
|
|
setList(await RelayApi.GetList());
|
2024-09-26 21:21:56 +00:00
|
|
|
setDevices(await DeviceApi.ValidatedList());
|
2024-09-25 17:35:39 +00:00
|
|
|
setStatus(await RelayApi.GetRelaysStatus());
|
2024-09-02 19:52:45 +00:00
|
|
|
|
|
|
|
list?.sort((a, b) => b.priority - a.priority);
|
2024-08-31 18:46:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const reload = () => {
|
|
|
|
loadKey.current += 1;
|
|
|
|
setList(undefined);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-09-26 21:21:56 +00:00
|
|
|
<>
|
|
|
|
<SolarEnergyRouteContainer
|
|
|
|
label="Relays list"
|
|
|
|
homeWidget={p.homeWidget}
|
|
|
|
actions={
|
|
|
|
<Tooltip title="Refresh list">
|
|
|
|
<IconButton onClick={reload}>
|
|
|
|
<RefreshIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<AsyncWidget
|
|
|
|
loadKey={loadKey.current}
|
|
|
|
ready={!!list}
|
|
|
|
errMsg="Failed to load the list of relays!"
|
|
|
|
load={load}
|
|
|
|
build={() => (
|
|
|
|
<RelaysList
|
|
|
|
onReload={reload}
|
|
|
|
list={list!}
|
|
|
|
devices={devices!}
|
|
|
|
status={status!}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</SolarEnergyRouteContainer>
|
|
|
|
</>
|
2024-08-31 18:46:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function RelaysList(p: {
|
|
|
|
list: DeviceRelay[];
|
2024-09-26 21:21:56 +00:00
|
|
|
devices: Device[];
|
2024-09-25 17:35:39 +00:00
|
|
|
status: RelaysStatus;
|
2024-08-31 18:46:02 +00:00
|
|
|
onReload: () => void;
|
|
|
|
}): React.ReactElement {
|
2024-09-26 21:21:56 +00:00
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
|
const openDevicePage = (relay: DeviceRelay) => {
|
|
|
|
const dev = p.devices.find((d) => d.relays.find((r) => r.id === relay.id));
|
|
|
|
navigate(DeviceURL(dev!));
|
|
|
|
};
|
|
|
|
|
2024-09-02 19:52:45 +00:00
|
|
|
return (
|
|
|
|
<TableContainer component={Paper}>
|
2024-09-26 21:21:56 +00:00
|
|
|
<Table sx={{ minWidth: 650 }}>
|
2024-09-02 19:52:45 +00:00
|
|
|
<TableHead>
|
|
|
|
<TableRow>
|
|
|
|
<TableCell>Name</TableCell>
|
|
|
|
<TableCell>Enabled</TableCell>
|
|
|
|
<TableCell>Priority</TableCell>
|
|
|
|
<TableCell>Consumption</TableCell>
|
|
|
|
<TableCell>Status</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
</TableHead>
|
|
|
|
<TableBody>
|
|
|
|
{p.list.map((row) => (
|
|
|
|
<TableRow
|
|
|
|
key={row.name}
|
|
|
|
sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
|
2024-09-26 21:21:56 +00:00
|
|
|
hover
|
|
|
|
onDoubleClick={() => openDevicePage(row)}
|
2024-09-02 19:52:45 +00:00
|
|
|
>
|
|
|
|
<TableCell>{row.name}</TableCell>
|
2024-09-15 20:01:06 +00:00
|
|
|
<TableCell>
|
2024-09-25 17:35:39 +00:00
|
|
|
<BoolText val={row.enabled} positive="YES" negative="NO" />
|
2024-09-15 20:01:06 +00:00
|
|
|
</TableCell>
|
2024-09-02 19:52:45 +00:00
|
|
|
<TableCell>{row.priority}</TableCell>
|
|
|
|
<TableCell>{row.consumption}</TableCell>
|
2024-09-25 17:35:39 +00:00
|
|
|
<TableCell>
|
|
|
|
<BoolText
|
|
|
|
val={p.status.get(row.id)!.on}
|
|
|
|
positive="ON"
|
|
|
|
negative="OFF"
|
|
|
|
/>{" "}
|
|
|
|
for <TimeWidget diff time={p.status.get(row.id)!.for} />
|
|
|
|
</TableCell>
|
2024-09-02 19:52:45 +00:00
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
</TableContainer>
|
|
|
|
);
|
2024-08-31 18:46:02 +00:00
|
|
|
}
|