2024-07-17 21:19:04 +00:00
|
|
|
import RefreshIcon from "@mui/icons-material/Refresh";
|
|
|
|
import VisibilityIcon from "@mui/icons-material/Visibility";
|
2024-07-04 17:52:09 +00:00
|
|
|
import {
|
|
|
|
IconButton,
|
|
|
|
Paper,
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableContainer,
|
|
|
|
TableHead,
|
|
|
|
TableRow,
|
2024-07-17 21:19:04 +00:00
|
|
|
Tooltip,
|
2024-07-04 17:52:09 +00:00
|
|
|
} from "@mui/material";
|
|
|
|
import React from "react";
|
2024-07-17 21:19:04 +00:00
|
|
|
import { Link, useNavigate } from "react-router-dom";
|
2024-09-09 19:27:15 +00:00
|
|
|
import { Device, DeviceApi, DevicesState, DeviceURL } from "../api/DeviceApi";
|
2024-07-17 21:19:04 +00:00
|
|
|
import { AsyncWidget } from "../widgets/AsyncWidget";
|
2024-09-25 20:00:36 +00:00
|
|
|
import { BoolText } from "../widgets/BoolText";
|
2024-07-17 21:19:04 +00:00
|
|
|
import { SolarEnergyRouteContainer } from "../widgets/SolarEnergyRouteContainer";
|
|
|
|
import { TimeWidget } from "../widgets/TimeWidget";
|
2024-07-04 17:52:09 +00:00
|
|
|
|
2024-09-26 21:14:18 +00:00
|
|
|
export function DevicesRoute(p: { homeWidget?: boolean }): React.ReactElement {
|
2024-07-04 17:52:09 +00:00
|
|
|
const loadKey = React.useRef(1);
|
|
|
|
|
|
|
|
const [list, setList] = React.useState<Device[] | undefined>();
|
2024-09-09 19:27:15 +00:00
|
|
|
const [states, setStates] = React.useState<DevicesState | undefined>();
|
2024-07-04 17:52:09 +00:00
|
|
|
|
|
|
|
const load = async () => {
|
|
|
|
setList(await DeviceApi.ValidatedList());
|
2024-09-09 19:27:15 +00:00
|
|
|
setStates(await DeviceApi.DevicesState());
|
2024-07-04 17:52:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const reload = () => {
|
|
|
|
loadKey.current += 1;
|
|
|
|
setList(undefined);
|
2024-09-09 19:27:15 +00:00
|
|
|
setStates(undefined);
|
2024-07-04 17:52:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SolarEnergyRouteContainer
|
2024-09-26 21:14:18 +00:00
|
|
|
homeWidget={p.homeWidget}
|
2024-07-04 17:52:09 +00:00
|
|
|
label="Devices"
|
|
|
|
actions={
|
|
|
|
<Tooltip title="Refresh table">
|
|
|
|
<IconButton onClick={reload}>
|
|
|
|
<RefreshIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<AsyncWidget
|
|
|
|
loadKey={loadKey.current}
|
2024-09-09 19:27:15 +00:00
|
|
|
ready={!!list && !!states}
|
2024-07-04 17:52:09 +00:00
|
|
|
errMsg="Failed to load the list of validated devices!"
|
|
|
|
load={load}
|
2024-09-09 19:27:15 +00:00
|
|
|
build={() => (
|
|
|
|
<ValidatedDevicesList
|
|
|
|
onReload={reload}
|
|
|
|
list={list!}
|
|
|
|
states={states!}
|
|
|
|
/>
|
|
|
|
)}
|
2024-07-04 17:52:09 +00:00
|
|
|
/>
|
|
|
|
</SolarEnergyRouteContainer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ValidatedDevicesList(p: {
|
|
|
|
list: Device[];
|
2024-09-09 19:27:15 +00:00
|
|
|
states: DevicesState;
|
2024-07-04 17:52:09 +00:00
|
|
|
onReload: () => void;
|
|
|
|
}): React.ReactElement {
|
2024-07-17 21:19:04 +00:00
|
|
|
const navigate = useNavigate();
|
2024-07-04 17:52:09 +00:00
|
|
|
|
|
|
|
if (p.list.length === 0) {
|
|
|
|
return <p>There is no device validated yet.</p>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TableContainer component={Paper}>
|
|
|
|
<Table sx={{ minWidth: 650 }} aria-label="simple table">
|
|
|
|
<TableHead>
|
|
|
|
<TableRow>
|
|
|
|
<TableCell>#</TableCell>
|
2024-09-26 21:14:18 +00:00
|
|
|
<TableCell align="center">Model</TableCell>
|
|
|
|
<TableCell align="center">Version</TableCell>
|
|
|
|
<TableCell align="center">Max relays</TableCell>
|
|
|
|
<TableCell align="center">Created</TableCell>
|
|
|
|
<TableCell align="center">Updated</TableCell>
|
|
|
|
<TableCell align="center">Status</TableCell>
|
2024-07-04 17:52:09 +00:00
|
|
|
<TableCell></TableCell>
|
|
|
|
</TableRow>
|
|
|
|
</TableHead>
|
|
|
|
<TableBody>
|
|
|
|
{p.list.map((dev) => (
|
2024-07-17 21:19:04 +00:00
|
|
|
<TableRow
|
|
|
|
hover
|
|
|
|
key={dev.id}
|
|
|
|
onDoubleClick={() => navigate(DeviceURL(dev))}
|
|
|
|
>
|
2024-07-04 17:52:09 +00:00
|
|
|
<TableCell component="th" scope="row">
|
|
|
|
{dev.id}
|
|
|
|
</TableCell>
|
2024-09-26 21:14:18 +00:00
|
|
|
<TableCell align="center">{dev.info.reference}</TableCell>
|
|
|
|
<TableCell align="center">{dev.info.version}</TableCell>
|
|
|
|
<TableCell align="center">{dev.info.max_relays}</TableCell>
|
|
|
|
<TableCell align="center">
|
2024-07-04 17:52:09 +00:00
|
|
|
<TimeWidget time={dev.time_create} />
|
|
|
|
</TableCell>
|
2024-09-26 21:14:18 +00:00
|
|
|
<TableCell align="center">
|
2024-07-04 17:52:09 +00:00
|
|
|
<TimeWidget time={dev.time_update} />
|
|
|
|
</TableCell>
|
2024-09-09 19:27:15 +00:00
|
|
|
<TableCell align="center">
|
2024-09-25 20:00:36 +00:00
|
|
|
<BoolText
|
|
|
|
val={p.states.get(dev.id)!.online}
|
|
|
|
positive="Online"
|
|
|
|
negative="Offline"
|
|
|
|
/>
|
2024-09-09 19:27:15 +00:00
|
|
|
<br />
|
|
|
|
<TimeWidget diff time={p.states.get(dev.id)!.last_ping} />
|
|
|
|
</TableCell>
|
2024-07-04 17:52:09 +00:00
|
|
|
<TableCell>
|
2024-07-17 21:19:04 +00:00
|
|
|
<Tooltip title="Open device page">
|
|
|
|
<Link to={DeviceURL(dev)}>
|
|
|
|
<IconButton>
|
|
|
|
<VisibilityIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Link>
|
|
|
|
</Tooltip>
|
2024-07-04 17:52:09 +00:00
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
</TableContainer>
|
|
|
|
);
|
|
|
|
}
|