import { FC, useState } from 'react';
import { ValidateFieldsError } from 'async-validator';
import { useSnackbar } from 'notistack';
import { Box, Button, Checkbox, MenuItem, Grid, Typography, Divider } from '@mui/material';
import SaveIcon from '@mui/icons-material/Save';
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew';
import { validate } from '../validators';
import { createSettingsValidator } from './validators';
import {
SectionContent,
FormLoader,
BlockFormControlLabel,
ValidatedTextField,
ButtonRow,
MessageBox
} from '../components';
import { numberValue, extractErrorMessage, updateValue, useRest } from '../utils';
import * as EMSESP from './api';
import { Settings, BOARD_PROFILES } from './types';
export function boardProfileSelectItems() {
return Object.keys(BOARD_PROFILES).map((code) => (
));
}
const SettingsApplication: FC = () => {
const { loadData, saveData, saving, setData, data, errorMessage, restartNeeded } = useRest({
read: EMSESP.readSettings,
update: EMSESP.writeSettings
});
const { enqueueSnackbar } = useSnackbar();
const updateFormValue = updateValue(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: unknown) {
enqueueSnackbar(extractErrorMessage(error, 'Problem fetching board profile'), { 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();
enqueueSnackbar('EMS-ESP is restarting...', { variant: 'info' });
} catch (error: unknown) {
enqueueSnackbar(extractErrorMessage(error, 'Problem restarting device'), { variant: 'error' });
}
};
return (
<>
Interface Board Profile
Select a pre-configured interface board profile from the list below or choose "Custom" to configure your own
hardware settings.
{boardProfileSelectItems()}
{data.board_profile === 'CUSTOM' && (
<>
{data.phy_type !== 0 && (
)}
>
)}
EMS Bus Settings
General Options
{data.led_gpio !== 0 && (
}
label="Hide LED"
disabled={saving}
/>
)}
}
label="Enable Telnet Console"
disabled={saving}
/>
}
label="Enable Analog Sensors"
disabled={saving}
/>
}
label="Convert temperature values to Fahrenheit"
disabled={saving}
/>
}
label="Underclock CPU speed"
disabled={saving}
/>
}
label="Bypass Access Token authorization on API calls"
disabled={saving}
/>
}
label="Enable Read only mode (blocks all outgoing EMS Tx write commands)"
disabled={saving}
/>
}
label="Enable Shower Timer"
disabled={saving}
/>
}
label="Enable Shower Alert"
disabled={saving}
/>
Formatting Options
{data.dallas_gpio !== 0 && (
<>
Temperature Sensors
}
label="Enable parasite power"
disabled={saving}
/>
>
)}
Logging
}
label="Log EMS telegrams in hexadecimal"
disabled={saving}
/>
}
label="Enable Syslog"
/>
{data.syslog_enabled && (
)}
{restartNeeded && (
} variant="contained" color="error" onClick={restart}>
Restart
)}
{!restartNeeded && (
}
disabled={saving}
variant="outlined"
color="primary"
type="submit"
onClick={validateAndSubmit}
>
Save
)}
>
);
};
return (
{content()}
);
};
export default SettingsApplication;