import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Grid, Typography, } from "@mui/material"; import { TimePicker } from "@mui/x-date-pickers"; import React from "react"; import { Device, DeviceRelay } from "../api/DeviceApi"; import { ServerApi } from "../api/ServerApi"; import { useAlert } from "../hooks/context_providers/AlertDialogProvider"; import { useLoadingMessage } from "../hooks/context_providers/LoadingMessageProvider"; import { useSnackbar } from "../hooks/context_providers/SnackbarProvider"; import { dayjsToTimeOfDay, timeOfDay } from "../utils/DateUtils"; import { lenValid } from "../utils/StringsUtils"; import { CheckboxInput } from "../widgets/forms/CheckboxInput"; import { TextInput } from "../widgets/forms/TextInput"; import { MultipleSelectInput } from "../widgets/forms/MultipleSelectInput"; export function EditDeviceRelaysDialog(p: { onClose: () => void; relay?: DeviceRelay; device: Device; onUpdated: () => void; }): React.ReactElement { const loadingMessage = useLoadingMessage(); const alert = useAlert(); const snackbar = useSnackbar(); const [relay, setRelay] = React.useState( p.relay ?? { id: "", name: "relay", enabled: false, priority: 1, consumption: 500, minimal_downtime: 60 * 5, minimal_uptime: 60 * 5, depends_on: [], conflicts_with: [], } ); const creating = !p.relay; const onSubmit = async () => { try { loadingMessage.show( `${creating ? "Creating" : "Updating"} relay information` ); // TODO snackbar( `The relay have been successfully ${creating ? "created" : "updated"}!` ); p.onUpdated(); } catch (e) { console.error("Failed to update device relay information!" + e); alert(`Failed to ${creating ? "create" : "update"} relay! ${e}`); } finally { loadingMessage.hide(); } }; const canSubmit = lenValid(relay.name, ServerApi.Config.constraints.relay_name_len) && relay.priority >= 0; return ( Edit relay information General info setRelay((r) => { return { ...r, name: v ?? "", }; }) } size={ServerApi.Config.constraints.dev_name_len} /> setRelay((r) => { return { ...r, enabled: v, }; }) } /> setRelay((r) => { return { ...r, priority: Number(v) ?? 0, }; }) } size={ServerApi.Config.constraints.relay_priority} helperText="Relay priority when selecting relays to turn on. 0 = lowest priority" /> setRelay((r) => { return { ...r, consumption: Number(v) ?? 0, }; }) } size={ServerApi.Config.constraints.relay_consumption} helperText="Estimated consumption of device powered by relay" /> setRelay((r) => { return { ...r, minimal_uptime: Number(v) ?? 0, }; }) } size={ServerApi.Config.constraints.relay_minimal_uptime} helperText="Minimal time this relay shall be left on before it can be turned off (in seconds)" /> setRelay((r) => { return { ...r, minimal_downtime: Number(v) ?? 0, }; }) } size={ServerApi.Config.constraints.relay_minimal_downtime} helperText="Minimal time this relay shall be left on before it can be turned off (in seconds)" /> Daily runtime setRelay((r) => { return { ...r, daily_runtime: v ? { reset_time: 0, min_runtime: 3600, catch_up_hours: [] } : undefined, }; }) } /> {!!relay.daily_runtime && ( <> setRelay((r) => { return { ...r, daily_runtime: { ...r.daily_runtime!, min_runtime: Number(v), }, }; }) } size={ ServerApi.Config.constraints.relay_daily_minimal_runtime } helperText="Minimum time, in seconds, that this relay should run each day" /> setRelay((r) => { return { ...r, daily_runtime: { ...r.daily_runtime!, reset_time: d ? dayjsToTimeOfDay(d) : 0, }, }; }) } /> { return { label: `${i.toString().padStart(2, "0")}:00`, value: i, }; })} selected={relay.daily_runtime!.catch_up_hours} onChange={(d) => setRelay((r) => { return { ...r, daily_runtime: { ...r.daily_runtime!, catch_up_hours: d, }, }; }) } /> )} ); } function DialogFormTitle(p: React.PropsWithChildren): React.ReactElement { return ( <> {p.children} ); }