WordClockESP/interface/src/containers/SystemStatus.js

189 lines
5.9 KiB
JavaScript
Raw Normal View History

2019-05-26 23:04:29 +00:00
import React, { Component, Fragment } from 'react';
import { withSnackbar } from 'notistack';
2019-05-26 23:04:29 +00:00
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemAvatar from '@material-ui/core/ListItemAvatar';
import ListItemText from '@material-ui/core/ListItemText';
import Avatar from '@material-ui/core/Avatar';
import Divider from '@material-ui/core/Divider';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogTitle from '@material-ui/core/DialogTitle';
import DialogContent from '@material-ui/core/DialogContent';
2019-05-26 23:04:29 +00:00
import DevicesIcon from '@material-ui/icons/Devices';
import MemoryIcon from '@material-ui/icons/Memory';
import ShowChartIcon from '@material-ui/icons/ShowChart';
import SdStorageIcon from '@material-ui/icons/SdStorage';
import DataUsageIcon from '@material-ui/icons/DataUsage';
import AutorenewIcon from '@material-ui/icons/Autorenew';
import RefreshIcon from '@material-ui/icons/Refresh';
2019-05-26 23:04:29 +00:00
import { SYSTEM_STATUS_ENDPOINT, RESET_ENDPOINT } from '../constants/Endpoints';
2019-05-26 23:04:29 +00:00
import { restComponent } from '../components/RestComponent';
2019-08-09 17:21:28 +00:00
import LoadingNotification from '../components/LoadingNotification';
2019-05-26 23:04:29 +00:00
import SectionContent from '../components/SectionContent';
import { redirectingAuthorizedFetch } from '../authentication/Authentication';
2019-05-26 23:04:29 +00:00
const styles = theme => ({
button: {
2019-06-02 18:01:06 +00:00
marginRight: theme.spacing(2),
marginTop: theme.spacing(2),
2019-05-26 23:04:29 +00:00
}
});
class SystemStatus extends Component {
constructor(props) {
super(props);
this.state = {
confirmReset: false,
processing: false
}
}
2019-05-26 23:04:29 +00:00
componentDidMount() {
this.props.loadData();
}
2019-05-26 23:04:56 +00:00
2019-05-26 23:04:29 +00:00
createListItems(data, classes) {
return (
<Fragment>
<ListItem >
<ListItemAvatar>
<Avatar>
<DevicesIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary="Platform" secondary={data.esp_platform} />
</ListItem>
<Divider variant="inset" component="li" />
<ListItem >
<ListItemAvatar>
<Avatar>
<ShowChartIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary="CPU Frequency" secondary={data.cpu_freq_mhz + ' MHz'} />
</ListItem>
<Divider variant="inset" component="li" />
<ListItem >
<ListItemAvatar>
<Avatar>
<MemoryIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary="Free Heap" secondary={data.free_heap + ' bytes'} />
</ListItem>
<Divider variant="inset" component="li" />
<ListItem >
<ListItemAvatar>
<Avatar>
<DataUsageIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary="Sketch Size (used/max)" secondary={data.sketch_size + ' / ' + data.free_sketch_space + ' bytes'} />
</ListItem>
<Divider variant="inset" component="li" />
<ListItem >
<ListItemAvatar>
<Avatar>
<SdStorageIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary="Flash Chip Size" secondary={data.flash_chip_size + ' bytes'} />
</ListItem>
2019-08-09 17:21:28 +00:00
<Divider variant="inset" component="li" />
2019-05-26 23:04:29 +00:00
</Fragment>
);
}
renderSystemStatus(data, classes) {
2019-05-26 23:04:29 +00:00
return (
<div>
<List>
{this.createListItems(data, classes)}
</List>
<Button startIcon={<RefreshIcon />} variant="contained" color="secondary" className={classes.button} onClick={this.props.loadData}>
2019-05-26 23:04:29 +00:00
Refresh
</Button>
<Button startIcon={<AutorenewIcon />} variant="contained" color="secondary" className={classes.button} onClick={this.onReset}>
Reset
</Button>
2019-05-26 23:04:29 +00:00
</div>
);
}
onReset = () => {
this.setState({ confirmReset: true });
}
onResetRejected = () => {
this.setState({ confirmReset: false });
}
onResetConfirmed = () => {
this.setState({ processing: true });
redirectingAuthorizedFetch(RESET_ENDPOINT, { method: 'POST' })
.then(response => {
if (response.status === 200) {
2019-11-30 16:51:31 +00:00
this.props.enqueueSnackbar("Device is resetting", { variant: 'info' });
this.setState({ processing: false, confirmReset: false });
} else {
throw Error("Invalid status code: " + response.status);
}
})
.catch(error => {
this.props.enqueueSnackbar(error.message || "Problem resetting device", { variant: 'error' });
this.setState({ processing: false, confirmReset: false });
});
}
renderResetDialog() {
return (
<Dialog
open={this.state.confirmReset}
onClose={this.onResetRejected}
>
<DialogTitle>Confirm Reset</DialogTitle>
<DialogContent dividers={true}>
Are you sure you want to reset the device?
</DialogContent>
<DialogActions>
<Button startIcon={<AutorenewIcon />} variant="contained" onClick={this.onResetConfirmed} disabled={this.state.processing} color="primary" autoFocus>
Reset
</Button>
<Button variant="contained" onClick={this.onResetRejected} color="secondary">
Cancel
</Button>
</DialogActions>
</Dialog>
)
}
2019-05-26 23:04:29 +00:00
render() {
2019-08-09 17:21:28 +00:00
const { data, fetched, errorMessage, loadData, classes } = this.props;
2019-05-26 23:04:29 +00:00
return (
<SectionContent title="System Status">
2019-08-09 17:21:28 +00:00
<LoadingNotification
onReset={loadData}
fetched={fetched}
errorMessage={errorMessage}
render={
() => this.renderSystemStatus(data, classes)
}
/>
{this.renderResetDialog()}
2019-05-26 23:04:29 +00:00
</SectionContent>
)
}
2019-05-26 23:04:29 +00:00
}
export default withSnackbar(restComponent(SYSTEM_STATUS_ENDPOINT, withStyles(styles)(SystemStatus)));