import { FC, useState } from 'react';
import { ValidateFieldsError } from 'async-validator';
import { useSnackbar } from 'notistack';
import { Box, Button, Checkbox, MenuItem, Grid, Typography, Divider, InputAdornment } from '@mui/material';
import WarningIcon from '@mui/icons-material/Warning';
import CancelIcon from '@mui/icons-material/Cancel';
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew';
import { validate } from 'validators';
import { createSettingsValidator } from './validators';
import {
SectionContent,
FormLoader,
BlockFormControlLabel,
ValidatedTextField,
ButtonRow,
MessageBox,
BlockNavigation
} from 'components';
import { numberValue, extractErrorMessage, updateValueDirty, useRest } from 'utils';
import * as EMSESP from './api';
import { Settings, BOARD_PROFILES } from './types';
import { useI18nContext } from 'i18n/i18n-react';
import RestartMonitor from 'framework/system/RestartMonitor';
export function boardProfileSelectItems() {
return Object.keys(BOARD_PROFILES).map((code) => (
));
}
const SettingsApplication: FC = () => {
const {
loadData,
saveData,
saving,
setData,
data,
origData,
dirtyFlags,
setDirtyFlags,
blocker,
errorMessage,
restartNeeded
} = useRest({
read: EMSESP.readSettings,
update: EMSESP.writeSettings
});
const [restarting, setRestarting] = useState();
const { LL } = useI18nContext();
const { enqueueSnackbar } = useSnackbar();
const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, setData);
const [fieldErrors, setFieldErrors] = useState();
const [processingBoard, setProcessingBoard] = useState(false);
const updateBoardProfile = async (board_profile: string) => {
setProcessingBoard(true);
try {
const response = await EMSESP.getBoardProfile({ board_profile: board_profile });
if (data) {
setData({
...data,
board_profile: board_profile,
led_gpio: response.data.led_gpio,
dallas_gpio: response.data.dallas_gpio,
rx_gpio: response.data.rx_gpio,
tx_gpio: response.data.tx_gpio,
pbutton_gpio: response.data.pbutton_gpio,
phy_type: response.data.phy_type,
eth_power: response.data.eth_power,
eth_phy_addr: response.data.eth_phy_addr,
eth_clock_mode: response.data.eth_clock_mode
});
}
} catch (error) {
enqueueSnackbar(extractErrorMessage(error, LL.PROBLEM_UPDATING()), { variant: 'error' });
} finally {
setProcessingBoard(false);
}
};
const content = () => {
if (!data) {
return ;
}
const validateAndSubmit = async () => {
try {
setFieldErrors(undefined);
await validate(createSettingsValidator(data), data);
saveData();
} catch (errors: any) {
setFieldErrors(errors);
}
};
const changeBoardProfile = (event: React.ChangeEvent) => {
const board_profile = event.target.value;
if (board_profile === 'CUSTOM') {
setData({
...data,
board_profile: board_profile
});
} else {
updateBoardProfile(board_profile);
}
};
const restart = async () => {
validateAndSubmit();
try {
await EMSESP.restart();
setRestarting(true);
} catch (error) {
enqueueSnackbar(extractErrorMessage(error, LL.PROBLEM_UPDATING()), { variant: 'error' });
}
};
return (
<>
{LL.INTERFACE_BOARD_PROFILE()}
{LL.BOARD_PROFILE_TEXT()}
{boardProfileSelectItems()}
{data.board_profile === 'CUSTOM' && (
<>
{data.phy_type !== 0 && (
)}
>
)}
{LL.SETTINGS_OF(LL.EMS_BUS(0))}
{LL.GENERAL_OPTIONS()}
{data.led_gpio !== 0 && (
}
label={LL.HIDE_LED()}
disabled={saving}
/>
)}
}
label={LL.ENABLE_TELNET()}
disabled={saving}
/>
}
label={LL.ENABLE_ANALOG()}
disabled={saving}
/>
}
label={LL.CONVERT_FAHRENHEIT()}
disabled={saving}
/>
}
label={LL.BYPASS_TOKEN()}
disabled={saving}
/>
}
label={LL.READONLY()}
disabled={saving}
/>
}
label={LL.UNDERCLOCK_CPU()}
disabled={saving}
/>
}
label={LL.ENABLE_SHOWER_TIMER()}
disabled={saving}
/>
}
label={LL.ENABLE_SHOWER_ALERT()}
disabled={!data.shower_timer}
/>
{data.shower_alert && (
<>
{LL.MINUTES()}
}}
variant="outlined"
value={numberValue(data.shower_alert_trigger)}
type="number"
onChange={updateFormValue}
size="small"
disabled={!data.shower_timer}
/>
{LL.SECONDS()}
}}
variant="outlined"
value={numberValue(data.shower_alert_coldshot)}
type="number"
onChange={updateFormValue}
size="small"
disabled={!data.shower_timer}
/>
>
)}
{LL.FORMATTING_OPTIONS()}
{data.dallas_gpio !== 0 && (
<>
{LL.TEMP_SENSORS()}
}
label={LL.ENABLE_PARASITE()}
disabled={saving}
/>
>
)}
{LL.LOGGING()}
}
label={LL.LOG_HEX()}
disabled={saving}
/>
}
label={LL.ENABLE_SYSLOG()}
/>
{data.syslog_enabled && (
{LL.SECONDS()}
}}
fullWidth
variant="outlined"
value={numberValue(data.syslog_mark_interval)}
type="number"
onChange={updateFormValue}
margin="normal"
disabled={saving}
/>
)}
{restartNeeded && (
} variant="contained" color="error" onClick={restart}>
{LL.RESTART()}
)}
{!restartNeeded && dirtyFlags && dirtyFlags.length !== 0 && (
}
disabled={saving}
variant="outlined"
color="primary"
type="submit"
onClick={() => loadData()}
>
{LL.CANCEL()}
}
disabled={saving}
variant="contained"
color="info"
type="submit"
onClick={validateAndSubmit}
>
{LL.APPLY_CHANGES(dirtyFlags.length)}
)}
>
);
};
return (
{blocker ? : null}
{restarting ? : content()}
);
};
export default SettingsApplication;