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