Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.9 KiB

5 years ago
  1. import React, { Component, Fragment } from 'react';
  2. import { withSnackbar } from 'notistack';
  3. import { withStyles } from '@material-ui/core/styles';
  4. import Button from '@material-ui/core/Button';
  5. import List from '@material-ui/core/List';
  6. import ListItem from '@material-ui/core/ListItem';
  7. import ListItemAvatar from '@material-ui/core/ListItemAvatar';
  8. import ListItemText from '@material-ui/core/ListItemText';
  9. import Avatar from '@material-ui/core/Avatar';
  10. import Divider from '@material-ui/core/Divider';
  11. import Dialog from '@material-ui/core/Dialog';
  12. import DialogActions from '@material-ui/core/DialogActions';
  13. import DialogTitle from '@material-ui/core/DialogTitle';
  14. import DialogContent from '@material-ui/core/DialogContent';
  15. import DevicesIcon from '@material-ui/icons/Devices';
  16. import MemoryIcon from '@material-ui/icons/Memory';
  17. import ShowChartIcon from '@material-ui/icons/ShowChart';
  18. import SdStorageIcon from '@material-ui/icons/SdStorage';
  19. import DataUsageIcon from '@material-ui/icons/DataUsage';
  20. import AutorenewIcon from '@material-ui/icons/Autorenew';
  21. import RefreshIcon from '@material-ui/icons/Refresh';
  22. import { SYSTEM_STATUS_ENDPOINT, RESET_ENDPOINT } from '../constants/Endpoints';
  23. import { restComponent } from '../components/RestComponent';
  24. import LoadingNotification from '../components/LoadingNotification';
  25. import SectionContent from '../components/SectionContent';
  26. import { redirectingAuthorizedFetch } from '../authentication/Authentication';
  27. const styles = theme => ({
  28. button: {
  29. marginRight: theme.spacing(2),
  30. marginTop: theme.spacing(2),
  31. }
  32. });
  33. class SystemStatus extends Component {
  34. constructor(props) {
  35. super(props);
  36. this.state = {
  37. confirmReset: false,
  38. processing: false
  39. }
  40. }
  41. componentDidMount() {
  42. this.props.loadData();
  43. }
  44. createListItems(data, classes) {
  45. return (
  46. <Fragment>
  47. <ListItem >
  48. <ListItemAvatar>
  49. <Avatar>
  50. <DevicesIcon />
  51. </Avatar>
  52. </ListItemAvatar>
  53. <ListItemText primary="Platform" secondary={data.esp_platform} />
  54. </ListItem>
  55. <Divider variant="inset" component="li" />
  56. <ListItem >
  57. <ListItemAvatar>
  58. <Avatar>
  59. <ShowChartIcon />
  60. </Avatar>
  61. </ListItemAvatar>
  62. <ListItemText primary="CPU Frequency" secondary={data.cpu_freq_mhz + ' MHz'} />
  63. </ListItem>
  64. <Divider variant="inset" component="li" />
  65. <ListItem >
  66. <ListItemAvatar>
  67. <Avatar>
  68. <MemoryIcon />
  69. </Avatar>
  70. </ListItemAvatar>
  71. <ListItemText primary="Free Heap" secondary={data.free_heap + ' bytes'} />
  72. </ListItem>
  73. <Divider variant="inset" component="li" />
  74. <ListItem >
  75. <ListItemAvatar>
  76. <Avatar>
  77. <DataUsageIcon />
  78. </Avatar>
  79. </ListItemAvatar>
  80. <ListItemText primary="Sketch Size (used/max)" secondary={data.sketch_size + ' / ' + data.free_sketch_space + ' bytes'} />
  81. </ListItem>
  82. <Divider variant="inset" component="li" />
  83. <ListItem >
  84. <ListItemAvatar>
  85. <Avatar>
  86. <SdStorageIcon />
  87. </Avatar>
  88. </ListItemAvatar>
  89. <ListItemText primary="Flash Chip Size" secondary={data.flash_chip_size + ' bytes'} />
  90. </ListItem>
  91. <Divider variant="inset" component="li" />
  92. </Fragment>
  93. );
  94. }
  95. renderSystemStatus(data, classes) {
  96. return (
  97. <div>
  98. <List>
  99. {this.createListItems(data, classes)}
  100. </List>
  101. <Button startIcon={<RefreshIcon />} variant="contained" color="secondary" className={classes.button} onClick={this.props.loadData}>
  102. Refresh
  103. </Button>
  104. <Button startIcon={<AutorenewIcon />} variant="contained" color="secondary" className={classes.button} onClick={this.onReset}>
  105. Reset
  106. </Button>
  107. </div>
  108. );
  109. }
  110. onReset = () => {
  111. this.setState({ confirmReset: true });
  112. }
  113. onResetRejected = () => {
  114. this.setState({ confirmReset: false });
  115. }
  116. onResetConfirmed = () => {
  117. this.setState({ processing: true });
  118. redirectingAuthorizedFetch(RESET_ENDPOINT, { method: 'POST' })
  119. .then(response => {
  120. if (response.status === 200) {
  121. this.props.enqueueSnackbar("Device is resetting", { variant: 'info' });
  122. this.setState({ processing: false, confirmReset: false });
  123. } else {
  124. throw Error("Invalid status code: " + response.status);
  125. }
  126. })
  127. .catch(error => {
  128. this.props.enqueueSnackbar(error.message || "Problem resetting device", { variant: 'error' });
  129. this.setState({ processing: false, confirmReset: false });
  130. });
  131. }
  132. renderResetDialog() {
  133. return (
  134. <Dialog
  135. open={this.state.confirmReset}
  136. onClose={this.onResetRejected}
  137. >
  138. <DialogTitle>Confirm Reset</DialogTitle>
  139. <DialogContent dividers={true}>
  140. Are you sure you want to reset the device?
  141. </DialogContent>
  142. <DialogActions>
  143. <Button startIcon={<AutorenewIcon />} variant="contained" onClick={this.onResetConfirmed} disabled={this.state.processing} color="primary" autoFocus>
  144. Reset
  145. </Button>
  146. <Button variant="contained" onClick={this.onResetRejected} color="secondary">
  147. Cancel
  148. </Button>
  149. </DialogActions>
  150. </Dialog>
  151. )
  152. }
  153. render() {
  154. const { data, fetched, errorMessage, loadData, classes } = this.props;
  155. return (
  156. <SectionContent title="System Status">
  157. <LoadingNotification
  158. onReset={loadData}
  159. fetched={fetched}
  160. errorMessage={errorMessage}
  161. render={
  162. () => this.renderSystemStatus(data, classes)
  163. }
  164. />
  165. {this.renderResetDialog()}
  166. </SectionContent>
  167. )
  168. }
  169. }
  170. export default withSnackbar(restComponent(SYSTEM_STATUS_ENDPOINT, withStyles(styles)(SystemStatus)));