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

232 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-04-10 15:25:52 +00:00
import React from "react";
import Light from "./Light";
import SmartPlug from "./SmartPlug";
import Sensor from "./Sensor";
import { ButtonDimmer, KnobDimmer } from "./Dimmer";
import Switcher from "./Switch";
2020-04-22 12:25:24 +00:00
import Videocam from "./Videocam";
2020-04-23 14:02:02 +00:00
import Curtains from "./Curtain";
import Thermostat from "./Thermostats";
2020-05-01 09:18:25 +00:00
import { Segment, Grid, Header, Button, Icon, Card } from "semantic-ui-react";
2020-04-10 15:25:52 +00:00
import { RemoteService } from "../../../remote";
import { connect } from "react-redux";
import DeviceSettingsModal from "./DeviceSettingsModal";
2020-05-01 09:18:25 +00:00
const centerComponent = {
marginLeft: "50%",
transform: "translateX(-50%)",
marginTop: "10%",
marginBottom: "10%",
};
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() {
console.log("editing device with id=" + this.props.id);
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)
.catch((err) => console.error(`Smart plug reset error`, err));
}
2020-04-10 15:25:52 +00:00
2020-04-25 16:27:09 +00:00
deleteState() {
2020-04-25 16:44:54 +00:00
//console.log("alpaca "+this.props);
2020-04-25 16:27:09 +00:00
this.props.deleteState(this.props.id, this.props.type);
}
2020-04-25 15:46:04 +00:00
renderDeviceComponent() {
switch (
this.props.tab === "Devices"
? this.props.stateOrDevice.kind
: this.props.type
) {
case "curtains":
return (
2020-04-25 15:46:04 +00:00
<Curtains
tab={this.props.tab}
sceneState={this.props.sceneState}
id={this.props.id}
/>
);
case "thermostat":
return (
<Thermostat
tab={this.props.tab}
sceneState={this.props.sceneStat}
id={this.props.stateOrDevice.id}
/>
);
case "regularLight":
return (
<Light
tab={this.props.tab}
sceneState={this.props.sceneState}
id={this.props.stateOrDevice.id}
/>
);
case "sensor":
return (
<Sensor
tab={this.props.tab}
sceneState={this.props.sceneState}
id={this.props.stateOrDevice.id}
/>
);
case "motionSensor":
2020-04-27 13:47:59 +00:00
return <Sensor tab={this.props.tab} id={this.props.stateOrDevice.id} />;
2020-04-25 15:46:04 +00:00
case "buttonDimmer":
return (
2020-04-27 13:47:59 +00:00
<ButtonDimmer tab={this.props.tab} id={this.props.stateOrDevice.id} />
2020-04-25 15:46:04 +00:00
);
case "knobDimmer":
return (
2020-04-27 13:47:59 +00:00
<KnobDimmer tab={this.props.tab} id={this.props.stateOrDevice.id} />
2020-04-25 15:46:04 +00:00
);
case "smartPlug":
return (
2020-04-27 13:47:59 +00:00
<SmartPlug tab={this.props.tab} id={this.props.stateOrDevice.id} />
2020-04-25 15:46:04 +00:00
);
case "switch":
return (
2020-04-27 13:47:59 +00:00
<Switcher tab={this.props.tab} id={this.props.stateOrDevice.id} />
2020-04-25 15:46:04 +00:00
);
case "dimmableLight":
2020-04-27 13:47:59 +00:00
return <Light id={this.props.stateOrDevice.id} tab={this.props.tab} />;
2020-04-25 15:46:04 +00:00
case "securityCamera":
return (
2020-04-27 13:47:59 +00:00
<Videocam id={this.props.stateOrDevice.id} tab={this.props.tab} />
);
2020-04-25 15:46:04 +00:00
default:
2020-05-01 09:18:25 +00:00
throw new Error("Device type unknown");
}
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>
{this.props.stateOrDevice.kind === "smartPlug" ? (
<Button
color="orange"
icon
onClick={this.resetSmartPlug}
labelPosition="left"
>
<Icon name="undo" />
Reset
</Button>
) : null}
</div>
);
}
stateDescription() {
return (
<div class="ui one button">
<Button
color="red"
icon
onClick={this.deleteState}
labelPosition="left"
>
<Icon name="undo" />
Delete
</Button>
</div>
);
}
get deviceName() {
return this.props.tab === "Devices"
? this.props.stateOrDevice.name
: this.props.device.name;
}
2020-04-25 15:46:04 +00:00
render() {
2020-04-25 16:23:40 +00:00
{
2020-05-01 09:18:25 +00:00
return (
<Card>
<Card.Content>
<Card.Header textAlign="center">
<Header as="h3">{this.deviceName}</Header>
{this.props.tab === "Scenes" ? (
<Header as="h4">{this.props.roomName}</Header>
2020-04-25 16:23:40 +00:00
) : (
2020-05-01 09:18:25 +00:00
""
2020-04-25 16:23:40 +00:00
)}
2020-05-01 09:18:25 +00:00
</Card.Header>
<Card.Description style={centerComponent}>
{this.renderDeviceComponent()}
</Card.Description>
</Card.Content>
<Card.Content extra>
{this.props.tab === "Devices"
? this.deviceDescription()
: this.stateDescription()}
</Card.Content>
<DeviceSettingsModal
ref={this.modalRef}
id={this.props.stateOrDevice.id}
/>
</Card>
);
2020-04-25 16:23:40 +00:00
}
2020-04-25 15:46:04 +00:00
}
2020-04-10 15:25:52 +00:00
}
const mapStateToProps = (state, ownProps) => ({
2020-04-25 15:46:04 +00:00
get stateOrDevice() {
if (state.active.activeTab === "Devices") {
return state.devices[ownProps.id];
} else {
return state.sceneStates[ownProps.id];
}
},
2020-04-27 13:47:59 +00:00
get device() {
if (state.active.activeTab === "Devices") {
return state.devices[ownProps.id];
} else {
return state.devices[state.sceneStates[ownProps.id].deviceId];
}
},
get roomName() {
2020-04-25 15:46:04 +00:00
if (state.active.activeTab === "Scenes") {
2020-04-27 13:47:59 +00:00
const device = state.devices[state.sceneStates[ownProps.id].deviceId];
return state.rooms[device.roomId].name;
2020-04-25 15:46:04 +00:00
} else {
2020-04-27 13:47:59 +00:00
return "";
2020-04-25 15:46:04 +00:00
}
},
get type() {
if (state.active.activeTab === "Scenes") {
2020-04-25 16:23:40 +00:00
if (state.sceneStates[ownProps.id]) {
2020-04-27 13:20:48 +00:00
//console.log(state.sceneStates[ownProps.id], ownProps.id);
2020-04-25 16:23:40 +00:00
const id = state.sceneStates[ownProps.id].deviceId;
2020-04-27 13:20:48 +00:00
//console.log(id, state.devices[id].kind);
2020-04-25 16:23:40 +00:00
return state.devices[id].kind;
} else {
return "";
}
2020-04-25 15:46:04 +00:00
} else {
return null;
}
},
2020-04-10 15:25:52 +00:00
});
const DeviceContainer = connect(mapStateToProps, RemoteService)(Device);
export default DeviceContainer;