63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
/**
|
|
* Users can add on-off switches. A on-off switch can turn on (or off) lights.
|
|
* If a light has an intensity level, when it gets switched back on, it gets the last available
|
|
* intensity level that was set by the user (or 100% if no such level exists).
|
|
* The user can change the state of a switch through the SmartHut interface.
|
|
*/
|
|
|
|
import React, { Component } from "react";
|
|
import { BottomPanel, StyledDiv } from "./styleComponents";
|
|
import { Image } from "semantic-ui-react";
|
|
import { imageStyle, nameStyle, turnedOnStyle } from "./SwitchStyle";
|
|
import { RemoteService } from "../../../remote";
|
|
import { connect } from "react-redux";
|
|
|
|
class Switch extends Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this.iconOn = "/img/switchOn.svg";
|
|
this.iconOff = "/img/switchOff.svg";
|
|
}
|
|
|
|
get turnedOn() {
|
|
return this.props.device.on;
|
|
}
|
|
|
|
getIcon = () => {
|
|
return this.turnedOn ? this.iconOn : this.iconOff;
|
|
};
|
|
|
|
onClickDevice = () => {
|
|
const newOn = !this.turnedOn;
|
|
const type = newOn ? "ON" : "OFF";
|
|
this.props
|
|
.switchOperate(this.props.id, type)
|
|
.catch((err) => console.error("switch operate failed", err));
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<StyledDiv onClick={this.onClickDevice}>
|
|
<Image src={this.getIcon()} style={imageStyle} />
|
|
<span style={nameStyle}>Switch</span>
|
|
|
|
<BottomPanel
|
|
style={
|
|
this.turnedOn
|
|
? { backgroundColor: "#505bda" }
|
|
: { backgroundColor: "#1a2849" }
|
|
}
|
|
>
|
|
<span style={turnedOnStyle}>{this.turnedOn ? "ON" : "OFF"}</span>
|
|
</BottomPanel>
|
|
</StyledDiv>
|
|
);
|
|
}
|
|
}
|
|
|
|
const mapStateToProps = (state, ownProps) => ({
|
|
device: state.devices[ownProps.id],
|
|
});
|
|
const SwitchContainer = connect(mapStateToProps, RemoteService)(Switch);
|
|
export default SwitchContainer;
|