relax the colours

This commit is contained in:
proddy
2020-10-09 23:04:38 +02:00
parent 5f4a24d689
commit e54499dc1b

View File

@@ -1,26 +1,40 @@
import React, { Component, Fragment } from "react"; import React, { Component, Fragment } from "react";
import { withStyles, Theme, createStyles } from '@material-ui/core/styles'; import { withStyles, Theme, createStyles } from "@material-ui/core/styles";
import { import {
Table, TableBody, TableCell, TableHead, TableRow, TableContainer, Table,
withWidth, WithWidthProps, isWidthDown, TableBody,
Button, Tooltip, TableCell,
DialogTitle, DialogContent, DialogActions, Box, Dialog, Typography TableHead,
TableRow,
TableContainer,
withWidth,
WithWidthProps,
isWidthDown,
Button,
Tooltip,
DialogTitle,
DialogContent,
DialogActions,
Box,
Dialog,
Typography,
} from "@material-ui/core"; } from "@material-ui/core";
import RefreshIcon from "@material-ui/icons/Refresh"; import RefreshIcon from "@material-ui/icons/Refresh";
import ListIcon from '@material-ui/icons/List'; import ListIcon from "@material-ui/icons/List";
import { redirectingAuthorizedFetch, withAuthenticatedContext, AuthenticatedContextProps } from '../authentication';
import { import {
RestFormProps, redirectingAuthorizedFetch,
FormButton, withAuthenticatedContext,
} from "../components"; AuthenticatedContextProps,
} from "../authentication";
import { RestFormProps, FormButton } from "../components";
import { EMSESPDevices, EMSESPDeviceData, Device } from "./EMSESPtypes"; import { EMSESPDevices, EMSESPDeviceData, Device } from "./EMSESPtypes";
import { ENDPOINT_ROOT } from '../api'; import { ENDPOINT_ROOT } from "../api";
export const SCANDEVICES_ENDPOINT = ENDPOINT_ROOT + "scanDevices"; export const SCANDEVICES_ENDPOINT = ENDPOINT_ROOT + "scanDevices";
export const DEVICE_DATA_ENDPOINT = ENDPOINT_ROOT + "deviceData"; export const DEVICE_DATA_ENDPOINT = ENDPOINT_ROOT + "deviceData";
@@ -33,7 +47,7 @@ const StyledTableCell = withStyles((theme: Theme) =>
body: { body: {
fontSize: 14, fontSize: 14,
}, },
}), })
)(TableCell); )(TableCell);
function compareDevices(a: Device, b: Device) { function compareDevices(a: Device, b: Device) {
@@ -52,39 +66,31 @@ interface EMSESPDevicesFormState {
deviceData?: EMSESPDeviceData; deviceData?: EMSESPDeviceData;
} }
const LightTooltip = withStyles((theme: Theme) => ({ type EMSESPDevicesFormProps = RestFormProps<EMSESPDevices> &
tooltip: { AuthenticatedContextProps &
// backgroundColor: theme.palette.common.black, WithWidthProps;
backgroundColor: theme.palette.primary.dark,
color: theme.palette.common.white,
boxShadow: theme.shadows[1],
borderRadius: "18px",
fontSize: 11
},
}))(Tooltip);
type EMSESPDevicesFormProps = RestFormProps<EMSESPDevices> & AuthenticatedContextProps & WithWidthProps;
class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesFormState> {
class EMSESPDevicesForm extends Component<
EMSESPDevicesFormProps,
EMSESPDevicesFormState
> {
state: EMSESPDevicesFormState = { state: EMSESPDevicesFormState = {
confirmScanDevices: false, confirmScanDevices: false,
processing: false processing: false,
} };
noDevices = () => { noDevices = () => {
return (this.props.data.devices.length === 0); return this.props.data.devices.length === 0;
}; };
noSensors = () => { noSensors = () => {
return (this.props.data.sensors.length === 0); return this.props.data.sensors.length === 0;
}; };
noDeviceData = () => { noDeviceData = () => {
return ((this.state.deviceData?.deviceData || []).length === 0); return (this.state.deviceData?.deviceData || []).length === 0;
}; };
createDeviceItems() { createDeviceItems() {
const { width, data } = this.props; const { width, data } = this.props;
return ( return (
@@ -94,7 +100,10 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
</Typography> </Typography>
<p></p> <p></p>
{!this.noDevices() && ( {!this.noDevices() && (
<Table size="small" padding={isWidthDown('xs', width!) ? "none" : "default"}> <Table
size="small"
padding={isWidthDown("xs", width!) ? "none" : "default"}
>
<TableHead> <TableHead>
<TableRow> <TableRow>
<StyledTableCell>Type</StyledTableCell> <StyledTableCell>Type</StyledTableCell>
@@ -107,47 +116,53 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
</TableRow> </TableRow>
</TableHead> </TableHead>
<TableBody> <TableBody>
{data.devices.sort(compareDevices).map(device => ( {data.devices.sort(compareDevices).map((device) => (
<TableRow hover key={device.id} onClick={() => this.handleRowClick(device.id)}> <TableRow
hover
key={device.id}
onClick={() => this.handleRowClick(device.id)}
>
<TableCell component="th" scope="row"> <TableCell component="th" scope="row">
<LightTooltip title="Show values..." placement="right"> <Tooltip title="show values..." arrow placement="right-end">
<Button startIcon={<ListIcon />} size="small" variant="outlined" color="primary"> <Button
startIcon={<ListIcon />}
size="small"
variant="outlined"
>
{device.type} {device.type}
</Button> </Button>
</LightTooltip > </Tooltip>
</TableCell> </TableCell>
<TableCell align="center">{device.brand}</TableCell>
<TableCell align="center">{device.name}</TableCell>
<TableCell align="center"> <TableCell align="center">
{device.brand} 0x
</TableCell> {("00" + device.deviceid.toString(16).toUpperCase()).slice(
<TableCell align="center"> -2
{device.name} )}
</TableCell>
<TableCell align="center">
0x{('00' + device.deviceid.toString(16).toUpperCase()).slice(-2)}
</TableCell>
<TableCell align="center">
{device.productid}
</TableCell>
<TableCell align="center">
{device.version}
</TableCell>
<TableCell>
</TableCell> </TableCell>
<TableCell align="center">{device.productid}</TableCell>
<TableCell align="center">{device.version}</TableCell>
<TableCell></TableCell>
</TableRow> </TableRow>
))} ))}
</TableBody> </TableBody>
</Table> </Table>
)} )}
{this.noDevices() && {this.noDevices() && (
( <Box
<Box bgcolor="error.main" color="error.contrastText" p={2} mt={2} mb={2}> bgcolor="error.main"
color="error.contrastText"
p={2}
mt={2}
mb={2}
>
<Typography variant="body1"> <Typography variant="body1">
No EMS devices found. Check the connections and for possible Tx errors. No EMS devices found. Check the connections and for possible Tx
errors.
</Typography> </Typography>
</Box> </Box>
) )}
}
</TableContainer> </TableContainer>
); );
} }
@@ -169,7 +184,7 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
</TableRow> </TableRow>
</TableHead> </TableHead>
<TableBody> <TableBody>
{data.sensors.map(sensorData => ( {data.sensors.map((sensorData) => (
<TableRow key={sensorData.id}> <TableRow key={sensorData.id}>
<TableCell component="th" scope="row"> <TableCell component="th" scope="row">
{sensorData.id} {sensorData.id}
@@ -182,15 +197,13 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
</TableBody> </TableBody>
</Table> </Table>
)} )}
{this.noSensors() && {this.noSensors() && (
(
<Box color="warning.main" p={0} mt={0} mb={0}> <Box color="warning.main" p={0} mt={0} mb={0}>
<Typography variant="body1"> <Typography variant="body1">
<i>There are no external temperature sensors connected</i> <i>There are no external temperature sensors connected</i>
</Typography> </Typography>
</Box> </Box>
) )}
}
</TableContainer> </TableContainer>
); );
} }
@@ -203,65 +216,88 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
> >
<DialogTitle>Confirm Scan Devices</DialogTitle> <DialogTitle>Confirm Scan Devices</DialogTitle>
<DialogContent dividers> <DialogContent dividers>
Are you sure you want to initiate a scan on the EMS bus for all new devices? Are you sure you want to initiate a scan on the EMS bus for all new
devices?
</DialogContent> </DialogContent>
<DialogActions> <DialogActions>
<Button variant="contained" onClick={this.onScanDevicesRejected} color="secondary"> <Button
variant="contained"
onClick={this.onScanDevicesRejected}
color="secondary"
>
Cancel Cancel
</Button> </Button>
<Button startIcon={<RefreshIcon />} variant="contained" onClick={this.onScanDevicesConfirmed} disabled={this.state.processing} color="primary" autoFocus> <Button
startIcon={<RefreshIcon />}
variant="contained"
onClick={this.onScanDevicesConfirmed}
disabled={this.state.processing}
color="primary"
autoFocus
>
Start Scan Start Scan
</Button> </Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
) );
} }
onScanDevices = () => { onScanDevices = () => {
this.setState({ confirmScanDevices: true }); this.setState({ confirmScanDevices: true });
} };
onScanDevicesRejected = () => { onScanDevicesRejected = () => {
this.setState({ confirmScanDevices: false }); this.setState({ confirmScanDevices: false });
} };
onScanDevicesConfirmed = () => { onScanDevicesConfirmed = () => {
this.setState({ processing: true }); this.setState({ processing: true });
redirectingAuthorizedFetch(SCANDEVICES_ENDPOINT).then(response => { redirectingAuthorizedFetch(SCANDEVICES_ENDPOINT)
.then((response) => {
if (response.status === 200) { if (response.status === 200) {
this.props.enqueueSnackbar("Device scan is starting...", { variant: 'info' }); this.props.enqueueSnackbar("Device scan is starting...", {
variant: "info",
});
this.setState({ processing: false, confirmScanDevices: false }); this.setState({ processing: false, confirmScanDevices: false });
} else { } else {
throw Error("Invalid status code: " + response.status); throw Error("Invalid status code: " + response.status);
} }
}) })
.catch(error => { .catch((error) => {
this.props.enqueueSnackbar(error.message || "Problem with scan", { variant: 'error' }); this.props.enqueueSnackbar(error.message || "Problem with scan", {
variant: "error",
});
this.setState({ processing: false, confirmScanDevices: false }); this.setState({ processing: false, confirmScanDevices: false });
}); });
} };
handleRowClick = (id: any) => { handleRowClick = (id: any) => {
this.setState({ deviceData: undefined }); this.setState({ deviceData: undefined });
redirectingAuthorizedFetch(DEVICE_DATA_ENDPOINT, { redirectingAuthorizedFetch(DEVICE_DATA_ENDPOINT, {
method: 'POST', method: "POST",
body: JSON.stringify({ id: id }), body: JSON.stringify({ id: id }),
headers: { headers: {
'Content-Type': 'application/json' "Content-Type": "application/json",
} },
}).then(response => { })
.then((response) => {
if (response.status === 200) { if (response.status === 200) {
return response.json(); return response.json();
// this.setState({ errorMessage: undefined }, this.props.loadData); // this.setState({ errorMessage: undefined }, this.props.loadData);
} }
throw Error("Unexpected response code: " + response.status); throw Error("Unexpected response code: " + response.status);
}).then(json => { })
.then((json) => {
this.setState({ deviceData: json }); this.setState({ deviceData: json });
}).catch(error => { })
this.props.enqueueSnackbar(error.message || "Problem getting device data", { variant: 'error' }); .catch((error) => {
this.props.enqueueSnackbar(
error.message || "Problem getting device data",
{ variant: "error" }
);
this.setState({ deviceData: undefined }); this.setState({ deviceData: undefined });
}); });
} };
renderDeviceData() { renderDeviceData() {
const { deviceData } = this.state; const { deviceData } = this.state;
@@ -278,43 +314,40 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
return ( return (
<Fragment> <Fragment>
<p></p> <p></p>
<Box bgcolor="info.main" p={2} mt={1} mb={1}> <Box bgcolor="info.main" p={1} mt={1} mb={1}>
<Typography variant="body1"> <Typography variant="body1" color="initial">
{deviceData.deviceName} {deviceData.deviceName}
</Typography> </Typography>
</Box> </Box>
{!this.noDeviceData() && ( {!this.noDeviceData() && (
<TableContainer> <TableContainer>
<Table size="small" padding={isWidthDown('xs', width!) ? "none" : "default"}> <Table
<TableHead> size="small"
</TableHead> padding={isWidthDown("xs", width!) ? "none" : "default"}
>
<TableHead></TableHead>
<TableBody> <TableBody>
{deviceData.deviceData.map(deviceData => ( {deviceData.deviceData.map((deviceData) => (
<TableRow key={deviceData.n}> <TableRow key={deviceData.n}>
<TableCell component="th" scope="row"> <TableCell component="th" scope="row">
{deviceData.n} {deviceData.n}
</TableCell> </TableCell>
<TableCell align="right"> <TableCell align="right">{deviceData.v}</TableCell>
{deviceData.v}
</TableCell>
</TableRow> </TableRow>
))} ))}
</TableBody> </TableBody>
</Table> </Table>
</TableContainer> </TableContainer>
)} )}
{this.noDeviceData() && {this.noDeviceData() && (
(
<Box color="warning.main" p={0} mt={0} mb={0}> <Box color="warning.main" p={0} mt={0} mb={0}>
<Typography variant="body1"> <Typography variant="body1">
<i>No data available for this device</i> <i>No data available for this device</i>
</Typography> </Typography>
</Box> </Box>
) )}
}
</Fragment> </Fragment>
); );
} }
render() { render() {
@@ -327,12 +360,22 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
<br></br> <br></br>
<Box display="flex" flexWrap="wrap"> <Box display="flex" flexWrap="wrap">
<Box flexGrow={1} padding={1}> <Box flexGrow={1} padding={1}>
<FormButton startIcon={<RefreshIcon />} variant="contained" color="secondary" onClick={this.props.loadData}> <FormButton
startIcon={<RefreshIcon />}
variant="contained"
color="secondary"
onClick={this.props.loadData}
>
Refresh Refresh
</FormButton> </FormButton>
</Box> </Box>
<Box flexWrap="none" padding={1} whiteSpace="nowrap"> <Box flexWrap="none" padding={1} whiteSpace="nowrap">
<FormButton startIcon={<RefreshIcon />} variant="contained" color="primary" onClick={this.onScanDevices}> <FormButton
startIcon={<RefreshIcon />}
variant="contained"
color="primary"
onClick={this.onScanDevices}
>
Scan Devices Scan Devices
</FormButton> </FormButton>
</Box> </Box>
@@ -341,7 +384,6 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
</Fragment> </Fragment>
); );
} }
} }
export default withAuthenticatedContext(withWidth()(EMSESPDevicesForm)); export default withAuthenticatedContext(withWidth()(EMSESPDevicesForm));