2019-05-26 23:04:29 +00:00
|
|
|
import React, { Component, Fragment } from 'react';
|
2020-02-09 10:21:13 +00:00
|
|
|
|
2020-05-19 23:32:49 +00:00
|
|
|
import { Avatar, Button, Divider, Dialog, DialogTitle, DialogContent, DialogActions, Box } from '@material-ui/core';
|
2020-02-09 10:21:13 +00:00
|
|
|
import { List, ListItem, ListItemAvatar, ListItemText } from '@material-ui/core';
|
2019-11-30 12:34:52 +00:00
|
|
|
|
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';
|
2020-05-25 10:04:13 +00:00
|
|
|
import FolderIcon from '@material-ui/icons/Folder';
|
2019-05-26 23:04:29 +00:00
|
|
|
import DataUsageIcon from '@material-ui/icons/DataUsage';
|
2020-05-19 23:32:49 +00:00
|
|
|
import PowerSettingsNewIcon from '@material-ui/icons/PowerSettingsNew';
|
2019-11-30 12:34:52 +00:00
|
|
|
import RefreshIcon from '@material-ui/icons/Refresh';
|
2020-05-19 23:32:49 +00:00
|
|
|
import SettingsBackupRestoreIcon from '@material-ui/icons/SettingsBackupRestore';
|
2019-05-26 23:04:29 +00:00
|
|
|
|
2020-05-19 23:32:49 +00:00
|
|
|
import { redirectingAuthorizedFetch, AuthenticatedContextProps, withAuthenticatedContext } from '../authentication';
|
|
|
|
import { RestFormProps, FormButton, ErrorButton } from '../components';
|
|
|
|
import { FACTORY_RESET_ENDPOINT, RESTART_ENDPOINT } from '../api';
|
2019-05-26 23:04:29 +00:00
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
import { SystemStatus } from './types';
|
2019-05-26 23:04:29 +00:00
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
interface SystemStatusFormState {
|
|
|
|
confirmRestart: boolean;
|
2020-05-19 23:32:49 +00:00
|
|
|
confirmFactoryReset: boolean;
|
2020-02-09 10:21:13 +00:00
|
|
|
processing: boolean;
|
|
|
|
}
|
2019-05-26 23:04:29 +00:00
|
|
|
|
2020-05-19 23:32:49 +00:00
|
|
|
type SystemStatusFormProps = AuthenticatedContextProps & RestFormProps<SystemStatus>;
|
2019-11-30 12:34:52 +00:00
|
|
|
|
2020-05-25 10:00:42 +00:00
|
|
|
function formatNumber(num: number) {
|
2020-05-24 23:41:45 +00:00
|
|
|
return new Intl.NumberFormat().format(num);
|
2020-05-24 21:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
class SystemStatusForm extends Component<SystemStatusFormProps, SystemStatusFormState> {
|
2019-11-30 12:34:52 +00:00
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
state: SystemStatusFormState = {
|
|
|
|
confirmRestart: false,
|
2020-05-19 23:32:49 +00:00
|
|
|
confirmFactoryReset: false,
|
2020-02-09 10:21:13 +00:00
|
|
|
processing: false
|
2019-11-30 12:34:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 21:52:07 +00:00
|
|
|
approxHeapFragmentation = (): number => {
|
|
|
|
const { data: { max_alloc_heap, free_heap } } = this.props;
|
|
|
|
return 100 - Math.round((max_alloc_heap / free_heap) * 100);
|
|
|
|
}
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
createListItems() {
|
|
|
|
const { data } = this.props
|
2019-05-26 23:04:29 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<ListItem >
|
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar>
|
|
|
|
<DevicesIcon />
|
|
|
|
</Avatar>
|
|
|
|
</ListItemAvatar>
|
2020-05-21 21:52:07 +00:00
|
|
|
<ListItemText primary="Device (Platform / SDK)" secondary={data.esp_platform + ' / ' + data.sdk_version} />
|
2019-05-26 23:04:29 +00:00
|
|
|
</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>
|
2020-05-24 23:41:45 +00:00
|
|
|
<ListItemText primary="Heap (Free / Max Alloc)" secondary={formatNumber(data.free_heap) + ' / ' + formatNumber(data.max_alloc_heap) + ' bytes (~' + this.approxHeapFragmentation() + '% fragmentation)'} />
|
2019-05-26 23:04:29 +00:00
|
|
|
</ListItem>
|
|
|
|
<Divider variant="inset" component="li" />
|
|
|
|
<ListItem >
|
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar>
|
|
|
|
<DataUsageIcon />
|
|
|
|
</Avatar>
|
|
|
|
</ListItemAvatar>
|
2020-05-24 23:41:45 +00:00
|
|
|
<ListItemText primary="Sketch (Size / Free)" secondary={formatNumber(data.sketch_size) + ' / ' + formatNumber(data.free_sketch_space) + ' bytes'} />
|
2019-05-26 23:04:29 +00:00
|
|
|
</ListItem>
|
|
|
|
<Divider variant="inset" component="li" />
|
|
|
|
<ListItem >
|
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar>
|
|
|
|
<SdStorageIcon />
|
|
|
|
</Avatar>
|
|
|
|
</ListItemAvatar>
|
2020-05-24 23:41:45 +00:00
|
|
|
<ListItemText primary="Flash Chip (Size / Speed)" secondary={formatNumber(data.flash_chip_size) + ' bytes / ' + (data.flash_chip_speed / 1000000).toFixed(0) + ' MHz'} />
|
2019-05-26 23:04:29 +00:00
|
|
|
</ListItem>
|
2020-05-25 10:00:42 +00:00
|
|
|
<Divider variant="inset" component="li" />
|
2020-05-24 21:35:32 +00:00
|
|
|
<ListItem >
|
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar>
|
2020-05-25 10:04:13 +00:00
|
|
|
<FolderIcon />
|
2020-05-24 21:35:32 +00:00
|
|
|
</Avatar>
|
|
|
|
</ListItemAvatar>
|
2020-05-25 10:04:13 +00:00
|
|
|
<ListItemText primary="File System (Used / Total)" secondary={formatNumber(data.fs_used) + ' / ' + formatNumber(data.fs_total) + ' bytes (' + formatNumber(data.fs_total - data.fs_used) + ' bytes free)'} />
|
2020-05-25 10:00:42 +00:00
|
|
|
</ListItem>
|
2019-08-09 17:21:28 +00:00
|
|
|
<Divider variant="inset" component="li" />
|
2019-05-26 23:04:29 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
2020-05-25 10:00:42 +00:00
|
|
|
}
|
2019-05-26 23:04:29 +00:00
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
renderRestartDialog() {
|
2019-05-26 23:04:29 +00:00
|
|
|
return (
|
2020-02-09 10:21:13 +00:00
|
|
|
<Dialog
|
|
|
|
open={this.state.confirmRestart}
|
|
|
|
onClose={this.onRestartRejected}
|
|
|
|
>
|
|
|
|
<DialogTitle>Confirm Restart</DialogTitle>
|
2020-02-19 00:04:57 +00:00
|
|
|
<DialogContent dividers>
|
2020-02-09 10:21:13 +00:00
|
|
|
Are you sure you want to restart the device?
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
2020-05-19 23:32:49 +00:00
|
|
|
<Button startIcon={<PowerSettingsNewIcon />} variant="contained" onClick={this.onRestartConfirmed} disabled={this.state.processing} color="primary" autoFocus>
|
2020-02-09 10:21:13 +00:00
|
|
|
Restart
|
|
|
|
</Button>
|
|
|
|
<Button variant="contained" onClick={this.onRestartRejected} color="secondary">
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</DialogActions>
|
|
|
|
</Dialog>
|
|
|
|
)
|
2019-05-26 23:04:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 08:28:40 +00:00
|
|
|
onRestart = () => {
|
|
|
|
this.setState({ confirmRestart: true });
|
2019-11-30 12:34:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 08:28:40 +00:00
|
|
|
onRestartRejected = () => {
|
|
|
|
this.setState({ confirmRestart: false });
|
2019-11-30 12:34:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 08:28:40 +00:00
|
|
|
onRestartConfirmed = () => {
|
2019-11-30 12:34:52 +00:00
|
|
|
this.setState({ processing: true });
|
2019-12-01 08:28:40 +00:00
|
|
|
redirectingAuthorizedFetch(RESTART_ENDPOINT, { method: 'POST' })
|
2019-11-30 12:34:52 +00:00
|
|
|
.then(response => {
|
|
|
|
if (response.status === 200) {
|
2019-12-01 08:28:40 +00:00
|
|
|
this.props.enqueueSnackbar("Device is restarting", { variant: 'info' });
|
|
|
|
this.setState({ processing: false, confirmRestart: false });
|
2019-11-30 12:34:52 +00:00
|
|
|
} else {
|
|
|
|
throw Error("Invalid status code: " + response.status);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2019-12-01 08:28:40 +00:00
|
|
|
this.props.enqueueSnackbar(error.message || "Problem restarting device", { variant: 'error' });
|
|
|
|
this.setState({ processing: false, confirmRestart: false });
|
2019-11-30 12:34:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 23:32:49 +00:00
|
|
|
renderFactoryResetDialog() {
|
|
|
|
return (
|
|
|
|
<Dialog
|
|
|
|
open={this.state.confirmFactoryReset}
|
|
|
|
onClose={this.onFactoryResetRejected}
|
|
|
|
>
|
|
|
|
<DialogTitle>Confirm Factory Reset</DialogTitle>
|
|
|
|
<DialogContent dividers>
|
|
|
|
Are you sure you want to reset the device to its factory defaults?
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<ErrorButton startIcon={<SettingsBackupRestoreIcon />} variant="contained" onClick={this.onFactoryResetConfirmed} disabled={this.state.processing} autoFocus>
|
|
|
|
Factory Reset
|
|
|
|
</ErrorButton>
|
|
|
|
<Button variant="contained" onClick={this.onFactoryResetRejected} color="secondary">
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</DialogActions>
|
|
|
|
</Dialog>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
onFactoryReset = () => {
|
|
|
|
this.setState({ confirmFactoryReset: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
onFactoryResetRejected = () => {
|
|
|
|
this.setState({ confirmFactoryReset: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
onFactoryResetConfirmed = () => {
|
|
|
|
this.setState({ processing: true });
|
|
|
|
redirectingAuthorizedFetch(FACTORY_RESET_ENDPOINT, { method: 'POST' })
|
|
|
|
.then(response => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
this.props.enqueueSnackbar("Factory reset in progress.", { variant: 'error' });
|
|
|
|
this.setState({ processing: false, confirmFactoryReset: false });
|
|
|
|
} else {
|
|
|
|
throw Error("Invalid status code: " + response.status);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
this.props.enqueueSnackbar(error.message || "Problem factory resetting device", { variant: 'error' });
|
|
|
|
this.setState({ processing: false, confirmRestart: false });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-26 23:04:29 +00:00
|
|
|
render() {
|
2020-05-19 23:32:49 +00:00
|
|
|
const me = this.props.authenticatedContext.me;
|
2019-05-26 23:04:29 +00:00
|
|
|
return (
|
2020-02-09 10:21:13 +00:00
|
|
|
<Fragment>
|
|
|
|
<List>
|
|
|
|
{this.createListItems()}
|
|
|
|
</List>
|
2020-05-19 23:32:49 +00:00
|
|
|
<Box display="flex" flexWrap="wrap">
|
|
|
|
<Box flexGrow={1} padding={1}>
|
|
|
|
<FormButton startIcon={<RefreshIcon />} variant="contained" color="secondary" onClick={this.props.loadData}>
|
|
|
|
Refresh
|
|
|
|
</FormButton>
|
|
|
|
</Box>
|
|
|
|
{me.admin &&
|
|
|
|
<Box flexWrap="none" padding={1} whiteSpace="nowrap">
|
|
|
|
<FormButton startIcon={<PowerSettingsNewIcon />} variant="contained" color="primary" onClick={this.onRestart}>
|
|
|
|
Restart
|
|
|
|
</FormButton>
|
|
|
|
<ErrorButton startIcon={<SettingsBackupRestoreIcon />} variant="contained" onClick={this.onFactoryReset}>
|
|
|
|
Factory reset
|
|
|
|
</ErrorButton>
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
</Box>
|
2019-12-01 08:28:40 +00:00
|
|
|
{this.renderRestartDialog()}
|
2020-05-19 23:32:49 +00:00
|
|
|
{this.renderFactoryResetDialog()}
|
2020-02-09 10:21:13 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
2019-05-26 23:04:29 +00:00
|
|
|
}
|
2019-11-30 12:34:52 +00:00
|
|
|
|
2019-05-26 23:04:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 23:32:49 +00:00
|
|
|
export default withAuthenticatedContext(SystemStatusForm);
|