import { Box, FormControlLabel, Grid, IconButton, Typography } from '@material-ui/core';
import Accordion from '@material-ui/core/Accordion';
import AccordionDetails from '@material-ui/core/AccordionDetails';
import AccordionSummary from '@material-ui/core/AccordionSummary';
import { blueGrey, green, orange, red } from '@material-ui/core/colors';
import { Power, PowerOff, Refresh } from '@material-ui/icons/';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import { withStyles } from '@material-ui/styles';
import React, { Component } from 'react';
import { withRouter } from "react-router";
import DeviceService from '../../common/DeviceService';
import DevicesContext from '../../contexts/DevicesContext';
const styles = theme => ({
acc_summary: {
backgroundColor: blueGrey[50],
},
acc_details: {
backgroundColor: blueGrey[100],
},
grid_typo: {
fontSize: theme.typography.pxToRem(20),
fontWeight: theme.typography.fontWeightRegular,
},
grid_typo_2: {
marginLeft: '5px',
fontSize: theme.typography.pxToRem(15),
fontWeight: theme.typography.fontWeightRegular,
color: theme.palette.text.secondary,
},
grid_item: {
width: '100%',
marginLeft: '5px',
marginRight: '30px',
},
grid_item_typo: {
fontSize: theme.typography.pxToRem(15),
fontWeight: theme.typography.fontWeightRegular,
},
grid_item_typo_2: {
marginLeft: '5px',
fontSize: theme.typography.pxToRem(15),
fontWeight: theme.typography.fontWeightRegular,
color: theme.palette.text.secondary,
},
icon_box: {
marginRight: '15px',
}
});
class DeviceComponent extends Component {
constructor(props) {
super(props);
this.state = {
expanded: "",
}
}
static contextType = DevicesContext;
getColor(status) {
if (status == "Online") {
return { color: green[600] };
} else if (status == "Offline") {
return { color: orange[900] };
} else /* if (device.status == "unknown") */ {
return { color: red[800] };
}
}
componentDidMount() {
const id = this.props.match.params.id;
this.setState({ expanded: id });
}
renderSensorButtons(device, sensor) {
var service = new DeviceService();
return this.renderButtons(
() => service.onlinesensor(device.id, sensor.id),
() => service.offlinesensor(device.id, sensor.id),
() => this.context.updateDevice(device.id)
);
}
renderDeviceButtons(device) {
var service = new DeviceService();
return this.renderButtons(
() => service.onlinedevice(device.id),
() => service.offlinedevice(device.id),
() => this.context.updateDevice(device.id)
);
}
renderButtons(onPower, onPowerOff, onRefresh) {
const renderOnOff = () => {
return (
);
}
const { classes } = this.props;
return (
{this.props.isAdmin ? renderOnOff() : null}
);
}
render() {
const { classes } = this.props;
const Sensors = this.props.device.sensors.map((sensor, index) => (
Sensor {index}
{sensor.id}
Status: {sensor.status}
{this.renderSensorButtons(this.props.device, sensor)}
));
const handleChange = (panel) => (event, isExpanded) => {
this.setState({ expanded: isExpanded ? panel : "" });
};
return (
}
aria-controls={"device-panel-/" + this.props.device.id}
id={"device-panel-/" + this.props.device.id}>
Device {this.props.index}
{this.props.device.id}
Status: {this.props.device.status}
event.stopPropagation()}
onFocus={(event) => event.stopPropagation()}
control={this.renderDeviceButtons(this.props.device)}/>
{Sensors}
);
}
}
export default withRouter(withStyles(styles)(DeviceComponent));