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.

155 lines
4.9 KiB

  1. import React, { Component, Fragment } from 'react';
  2. import { Avatar, Button, Divider, Dialog, DialogTitle, DialogContent, DialogActions } from '@material-ui/core';
  3. import { List, ListItem, ListItemAvatar, ListItemText } from '@material-ui/core';
  4. import DevicesIcon from '@material-ui/icons/Devices';
  5. import MemoryIcon from '@material-ui/icons/Memory';
  6. import ShowChartIcon from '@material-ui/icons/ShowChart';
  7. import SdStorageIcon from '@material-ui/icons/SdStorage';
  8. import DataUsageIcon from '@material-ui/icons/DataUsage';
  9. import AutorenewIcon from '@material-ui/icons/Autorenew';
  10. import RefreshIcon from '@material-ui/icons/Refresh';
  11. import { redirectingAuthorizedFetch } from '../authentication';
  12. import { RestFormProps, FormButton, FormActions } from '../components';
  13. import { RESTART_ENDPOINT } from '../api';
  14. import { SystemStatus } from './types';
  15. interface SystemStatusFormState {
  16. confirmRestart: boolean;
  17. processing: boolean;
  18. }
  19. type SystemStatusFormProps = RestFormProps<SystemStatus>;
  20. class SystemStatusForm extends Component<SystemStatusFormProps, SystemStatusFormState> {
  21. state: SystemStatusFormState = {
  22. confirmRestart: false,
  23. processing: false
  24. }
  25. createListItems() {
  26. const { data } = this.props
  27. return (
  28. <Fragment>
  29. <ListItem >
  30. <ListItemAvatar>
  31. <Avatar>
  32. <DevicesIcon />
  33. </Avatar>
  34. </ListItemAvatar>
  35. <ListItemText primary="Platform" secondary={data.esp_platform} />
  36. </ListItem>
  37. <Divider variant="inset" component="li" />
  38. <ListItem >
  39. <ListItemAvatar>
  40. <Avatar>
  41. <ShowChartIcon />
  42. </Avatar>
  43. </ListItemAvatar>
  44. <ListItemText primary="CPU Frequency" secondary={data.cpu_freq_mhz + ' MHz'} />
  45. </ListItem>
  46. <Divider variant="inset" component="li" />
  47. <ListItem >
  48. <ListItemAvatar>
  49. <Avatar>
  50. <MemoryIcon />
  51. </Avatar>
  52. </ListItemAvatar>
  53. <ListItemText primary="Free Heap" secondary={data.free_heap + ' bytes'} />
  54. </ListItem>
  55. <Divider variant="inset" component="li" />
  56. <ListItem >
  57. <ListItemAvatar>
  58. <Avatar>
  59. <DataUsageIcon />
  60. </Avatar>
  61. </ListItemAvatar>
  62. <ListItemText primary="Sketch Size (used/max)" secondary={data.sketch_size + ' / ' + data.free_sketch_space + ' bytes'} />
  63. </ListItem>
  64. <Divider variant="inset" component="li" />
  65. <ListItem >
  66. <ListItemAvatar>
  67. <Avatar>
  68. <SdStorageIcon />
  69. </Avatar>
  70. </ListItemAvatar>
  71. <ListItemText primary="Flash Chip Size" secondary={data.flash_chip_size + ' bytes'} />
  72. </ListItem>
  73. <Divider variant="inset" component="li" />
  74. </Fragment>
  75. );
  76. }
  77. renderRestartDialog() {
  78. return (
  79. <Dialog
  80. open={this.state.confirmRestart}
  81. onClose={this.onRestartRejected}
  82. >
  83. <DialogTitle>Confirm Restart</DialogTitle>
  84. <DialogContent dividers>
  85. Are you sure you want to restart the device?
  86. </DialogContent>
  87. <DialogActions>
  88. <Button startIcon={<AutorenewIcon />} variant="contained" onClick={this.onRestartConfirmed} disabled={this.state.processing} color="primary" autoFocus>
  89. Restart
  90. </Button>
  91. <Button variant="contained" onClick={this.onRestartRejected} color="secondary">
  92. Cancel
  93. </Button>
  94. </DialogActions>
  95. </Dialog>
  96. )
  97. }
  98. onRestart = () => {
  99. this.setState({ confirmRestart: true });
  100. }
  101. onRestartRejected = () => {
  102. this.setState({ confirmRestart: false });
  103. }
  104. onRestartConfirmed = () => {
  105. this.setState({ processing: true });
  106. redirectingAuthorizedFetch(RESTART_ENDPOINT, { method: 'POST' })
  107. .then(response => {
  108. if (response.status === 200) {
  109. this.props.enqueueSnackbar("Device is restarting", { variant: 'info' });
  110. this.setState({ processing: false, confirmRestart: false });
  111. } else {
  112. throw Error("Invalid status code: " + response.status);
  113. }
  114. })
  115. .catch(error => {
  116. this.props.enqueueSnackbar(error.message || "Problem restarting device", { variant: 'error' });
  117. this.setState({ processing: false, confirmRestart: false });
  118. });
  119. }
  120. render() {
  121. return (
  122. <Fragment>
  123. <List>
  124. {this.createListItems()}
  125. </List>
  126. <FormActions>
  127. <FormButton startIcon={<RefreshIcon />} variant="contained" color="secondary" onClick={this.props.loadData}>
  128. Refresh
  129. </FormButton>
  130. <FormButton startIcon={<AutorenewIcon />} variant="contained" color="primary" onClick={this.onRestart}>
  131. Restart
  132. </FormButton>
  133. </FormActions>
  134. {this.renderRestartDialog()}
  135. </Fragment>
  136. );
  137. }
  138. }
  139. export default SystemStatusForm;