frontend/smart-hut/src/components/dashboard/devices/Device.js

159 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React from 'react';
import {
Header, Button, Icon, Card,
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import Light from './Light';
import SmartPlug from './SmartPlug';
import Sensor from './Sensor';
import { ButtonDimmer, KnobDimmer } from './Dimmer';
import Switcher from './Switch';
import Videocam from './Videocam';
import Curtains from './Curtain';
import Thermostat from './Thermostats';
import { RemoteService } from '../../../remote';
import DeviceSettingsModal from './DeviceSettingsModal';
import mapStateToProps from '../../../deviceProps';
2020-04-10 15:25:52 +00:00
2020-05-01 09:18:25 +00:00
const centerComponent = {
2020-05-12 13:18:33 +00:00
marginLeft: '50%',
transform: 'translateX(-50%)',
marginTop: '10%',
marginBottom: '10%',
2020-05-01 09:18:25 +00:00
};
2020-04-10 15:25:52 +00:00
class Device extends React.Component {
2020-04-25 15:46:04 +00:00
constructor(props) {
super(props);
2020-04-10 15:25:52 +00:00
2020-04-25 15:46:04 +00:00
this.modalRef = React.createRef();
this.edit = this.edit.bind(this);
this.resetSmartPlug = this.resetSmartPlug.bind(this);
2020-04-27 13:20:48 +00:00
this.deleteState = this.deleteState.bind(this);
2020-04-25 15:46:04 +00:00
}
2020-04-10 15:25:52 +00:00
2020-04-25 15:46:04 +00:00
edit() {
2020-05-12 13:18:33 +00:00
console.log(`editing device with id=${this.props.id}`);
2020-04-25 15:46:04 +00:00
this.modalRef.current.openModal();
}
2020-04-10 15:25:52 +00:00
2020-04-25 15:46:04 +00:00
resetSmartPlug() {
this.props
.smartPlugReset(this.props.id)
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('Smart plug reset error', err));
2020-04-25 15:46:04 +00:00
}
2020-04-10 15:25:52 +00:00
2020-04-25 16:27:09 +00:00
deleteState() {
2020-05-02 11:58:02 +00:00
this.props.deleteState(this.props.id, this.props.stateOrDevice.kind);
2020-04-25 16:27:09 +00:00
}
2020-04-25 15:46:04 +00:00
renderDeviceComponent() {
2020-05-03 16:09:53 +00:00
const mapKindToComponent = {
curtains: Curtains,
thermostat: Thermostat,
regularLight: Light,
sensor: Sensor,
motionSensor: Sensor,
buttonDimmer: ButtonDimmer,
knobDimmer: KnobDimmer,
smartPlug: SmartPlug,
switch: Switcher,
dimmableLight: Light,
securityCamera: Videocam,
};
if (!(this.props.type in mapKindToComponent)) {
throw new Error(`device kind ${this.props.type} not known`);
}
2020-05-03 16:09:53 +00:00
return React.createElement(
mapKindToComponent[this.props.type],
{
tab: this.props.tab,
id: this.props.id,
hostId: this.props.hostId,
},
2020-05-12 13:18:33 +00:00
'',
2020-05-03 16:09:53 +00:00
);
2020-04-25 15:46:04 +00:00
}
2020-05-01 09:18:25 +00:00
deviceDescription() {
return (
<div className="ui two buttons">
<Button color="blue" icon onClick={this.edit} labelPosition="left">
<Icon name="pencil" />
Edit
</Button>
2020-05-12 13:18:33 +00:00
{this.props.stateOrDevice.kind === 'smartPlug' ? (
2020-05-01 09:18:25 +00:00
<Button
color="orange"
icon
onClick={this.resetSmartPlug}
labelPosition="left"
>
<Icon name="undo" />
Reset
</Button>
) : null}
</div>
);
}
stateDescription() {
return (
2020-05-02 11:58:02 +00:00
<div className="ui two buttons">
2020-05-01 09:18:25 +00:00
<Button
color="red"
icon
onClick={this.deleteState}
labelPosition="left"
>
<Icon name="undo" />
Delete
</Button>
</div>
);
}
get deviceName() {
return this.props.device.name;
2020-05-01 09:18:25 +00:00
}
2020-04-25 15:46:04 +00:00
render() {
2020-05-03 14:37:04 +00:00
return (
2020-05-12 13:18:33 +00:00
<Card style={{ height: '27em' }}>
2020-05-03 14:37:04 +00:00
<Card.Content>
<Card.Header textAlign="center">
<Header as="h3">{this.deviceName}</Header>
2020-05-12 13:18:33 +00:00
<Header as="h4" style={{ marginTop: '.5rem' }}>
{this.props.roomName}
</Header>
2020-05-03 14:37:04 +00:00
</Card.Header>
2020-05-01 09:18:25 +00:00
<Card.Description
style={
2020-05-12 13:18:33 +00:00
this.props.device.kind !== 'curtains' ? centerComponent : null
}
>
2020-05-03 14:37:04 +00:00
{this.renderDeviceComponent()}
</Card.Description>
</Card.Content>
<Card.Content extra>
2020-05-12 13:18:33 +00:00
{this.props.tab === 'Devices'
2020-05-03 14:37:04 +00:00
? this.deviceDescription()
2020-05-12 13:18:33 +00:00
: this.props.tab === 'Scenes' && this.stateDescription()}
2020-05-03 14:37:04 +00:00
</Card.Content>
2020-05-12 13:18:33 +00:00
{this.props.tab === 'Devices' ? (
2020-05-03 14:37:04 +00:00
<DeviceSettingsModal
ref={this.modalRef}
id={this.props.stateOrDevice.id}
/>
) : null}
</Card>
);
2020-04-25 15:46:04 +00:00
}
2020-04-10 15:25:52 +00:00
}
const DeviceContainer = connect(mapStateToProps, RemoteService)(Device);
export default DeviceContainer;