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.

138 lines
4.4 KiB

  1. import React, { Component, Fragment } from 'react';
  2. import { withStyles } from '@material-ui/core/styles';
  3. import Button from '@material-ui/core/Button';
  4. import List from '@material-ui/core/List';
  5. import ListItem from '@material-ui/core/ListItem';
  6. import ListItemAvatar from '@material-ui/core/ListItemAvatar';
  7. import ListItemText from '@material-ui/core/ListItemText';
  8. import Avatar from '@material-ui/core/Avatar';
  9. import Divider from '@material-ui/core/Divider';
  10. import SwapVerticalCircleIcon from '@material-ui/icons/SwapVerticalCircle';
  11. import AccessTimeIcon from '@material-ui/icons/AccessTime';
  12. import DNSIcon from '@material-ui/icons/Dns';
  13. import UpdateIcon from '@material-ui/icons/Update';
  14. import AvTimerIcon from '@material-ui/icons/AvTimer';
  15. import RefreshIcon from '@material-ui/icons/Refresh';
  16. import { isNtpActive, ntpStatusHighlight, ntpStatus } from '../constants/NTPStatus';
  17. import * as Highlight from '../constants/Highlight';
  18. import { formatIsoDateTime } from '../constants/TimeFormat';
  19. import { NTP_STATUS_ENDPOINT } from '../constants/Endpoints';
  20. import { restComponent } from '../components/RestComponent';
  21. import LoadingNotification from '../components/LoadingNotification';
  22. import SectionContent from '../components/SectionContent';
  23. import moment from 'moment';
  24. const styles = theme => ({
  25. ["ntpStatus_" + Highlight.SUCCESS]: {
  26. backgroundColor: theme.palette.highlight_success
  27. },
  28. ["ntpStatus_" + Highlight.ERROR]: {
  29. backgroundColor: theme.palette.highlight_error
  30. },
  31. ["ntpStatus_" + Highlight.WARN]: {
  32. backgroundColor: theme.palette.highlight_warn
  33. },
  34. button: {
  35. marginRight: theme.spacing(2),
  36. marginTop: theme.spacing(2),
  37. }
  38. });
  39. class NTPStatus extends Component {
  40. componentDidMount() {
  41. this.props.loadData();
  42. }
  43. createListItems(data, classes) {
  44. return (
  45. <Fragment>
  46. <ListItem >
  47. <ListItemAvatar>
  48. <Avatar className={classes["ntpStatus_" + ntpStatusHighlight(data)]}>
  49. <UpdateIcon />
  50. </Avatar>
  51. </ListItemAvatar>
  52. <ListItemText primary="Status" secondary={ntpStatus(data)} />
  53. </ListItem>
  54. <Divider variant="inset" component="li" />
  55. {
  56. isNtpActive(data) && (
  57. <Fragment>
  58. <ListItem>
  59. <ListItemAvatar>
  60. <Avatar>
  61. <AccessTimeIcon />
  62. </Avatar>
  63. </ListItemAvatar>
  64. <ListItemText primary="Local Time" secondary={formatIsoDateTime(data.time_local)} />
  65. </ListItem>
  66. <Divider variant="inset" component="li" />
  67. <ListItem>
  68. <ListItemAvatar>
  69. <Avatar>
  70. <AccessTimeIcon />
  71. </Avatar>
  72. </ListItemAvatar>
  73. <ListItemText primary="UTC Time" secondary={formatIsoDateTime(data.time_utc)} />
  74. </ListItem>
  75. <Divider variant="inset" component="li" />
  76. </Fragment>
  77. )}
  78. <ListItem>
  79. <ListItemAvatar>
  80. <Avatar>
  81. <DNSIcon />
  82. </Avatar>
  83. </ListItemAvatar>
  84. <ListItemText primary="NTP Server" secondary={data.server} />
  85. </ListItem>
  86. <Divider variant="inset" component="li" />
  87. <ListItem>
  88. <ListItemAvatar>
  89. <Avatar>
  90. <AvTimerIcon />
  91. </Avatar>
  92. </ListItemAvatar>
  93. <ListItemText primary="Uptime" secondary={moment.duration(data.uptime, 'seconds').humanize()} />
  94. </ListItem>
  95. <Divider variant="inset" component="li" />
  96. </Fragment>
  97. );
  98. }
  99. renderNTPStatus(data, classes) {
  100. return (
  101. <div>
  102. <List>
  103. {this.createListItems(data, classes)}
  104. </List>
  105. <Button startIcon={<RefreshIcon />} variant="contained" color="secondary" className={classes.button} onClick={this.props.loadData}>
  106. Refresh
  107. </Button>
  108. </div>
  109. );
  110. }
  111. render() {
  112. const { data, fetched, errorMessage, loadData, classes } = this.props;
  113. return (
  114. <SectionContent title="NTP Status">
  115. <LoadingNotification
  116. onReset={loadData}
  117. fetched={fetched}
  118. errorMessage={errorMessage}
  119. render={
  120. () => this.renderNTPStatus(data, classes)
  121. }
  122. />
  123. </SectionContent>
  124. );
  125. }
  126. }
  127. export default restComponent(NTP_STATUS_ENDPOINT, withStyles(styles)(NTPStatus));