2020-03-15 14:50:25 +00:00
|
|
|
/**
|
|
|
|
* Users can add lights in their rooms.
|
|
|
|
* Lights are devices like bulbs, LED strip lights, lamps.
|
|
|
|
* Lights may support an intensity level (from 0% to 100%).
|
|
|
|
* Lights have an internal state that can be changed and it must
|
|
|
|
* be shown accordingly in the SmartHut views (house view and room views).
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, {Component} from "react";
|
2020-03-17 16:38:03 +00:00
|
|
|
import {iconStyle, StyledDiv} from "./styleComponents";
|
2020-03-15 14:50:25 +00:00
|
|
|
import Settings from "./DeviceSettings";
|
|
|
|
import {Image} from "semantic-ui-react";
|
|
|
|
import {CircularInput, CircularProgress, CircularThumb, CircularTrack} from "react-circular-input";
|
2020-03-17 16:38:03 +00:00
|
|
|
import {valueStyle, intensityLightStyle, style, nameStyle} from "./LightStyle";
|
2020-03-15 14:50:25 +00:00
|
|
|
|
|
|
|
export default class Light extends Component {
|
2020-03-17 22:24:40 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
turnedOn: false,
|
2020-03-15 14:50:25 +00:00
|
|
|
};
|
2020-03-17 22:24:40 +00:00
|
|
|
this.iconOn = "/img/lightOn.svg";
|
|
|
|
this.iconOff = "/img/lightOff.svg"
|
|
|
|
}
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
onClickDevice = () => {
|
|
|
|
this.setState((prevState) => ({turnedOn: !prevState.turnedOn}));
|
|
|
|
};
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
setIntensity = (newValue) => {
|
2020-03-20 17:42:38 +00:00
|
|
|
this.setState({intensity: newValue});
|
2020-03-17 22:24:40 +00:00
|
|
|
};
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
getIcon = () => {
|
|
|
|
if (this.state.turnedOn) {
|
|
|
|
return this.iconOn;
|
|
|
|
}
|
|
|
|
return this.iconOff;
|
|
|
|
};
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
componentDidMount() {
|
2020-03-20 17:42:38 +00:00
|
|
|
if (this.props.device.hasOwnProperty("intensity")) {
|
2020-03-17 22:24:40 +00:00
|
|
|
this.setState({
|
2020-03-20 17:42:38 +00:00
|
|
|
intensity: this.props.device.intensity
|
2020-03-17 22:24:40 +00:00
|
|
|
});
|
2020-03-15 14:50:25 +00:00
|
|
|
}
|
2020-03-17 22:24:40 +00:00
|
|
|
// Get the state and update it
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
}
|
2020-03-15 14:50:25 +00:00
|
|
|
|
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
render() {
|
|
|
|
const intensityLightView = (
|
|
|
|
<CircularInput
|
2020-03-20 17:42:38 +00:00
|
|
|
value={this.state.intensity}
|
2020-03-17 22:24:40 +00:00
|
|
|
onChange={this.setIntensity}
|
|
|
|
style={style}
|
|
|
|
>
|
|
|
|
<CircularTrack/>
|
|
|
|
<CircularProgress/>
|
|
|
|
<CircularThumb/>
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
<text style={valueStyle} x={100} y={100} textAnchor="middle" dy="0.3em" fontWeight="bold">
|
2020-03-20 17:42:38 +00:00
|
|
|
{Math.round(this.state.intensity * 100)}%
|
2020-03-17 22:24:40 +00:00
|
|
|
</text>
|
|
|
|
<text style={intensityLightStyle} x={100} y={150} textAnchor="middle" dy="0.3em" fontWeight="bold">
|
|
|
|
{this.props.device.name}
|
|
|
|
</text>
|
|
|
|
</CircularInput>
|
|
|
|
);
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
const normalLightView = (
|
|
|
|
<div onClick={this.props.edit.mode ? () => {
|
|
|
|
} : this.onClickDevice}>
|
|
|
|
<Image src={this.getIcon()} style={iconStyle}/>
|
|
|
|
<h5 style={nameStyle}>{this.props.device.name}</h5>
|
|
|
|
</div>
|
|
|
|
);
|
2020-03-20 17:42:38 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
return (
|
|
|
|
<StyledDiv>
|
|
|
|
<Settings
|
|
|
|
deviceId={this.props.device.id}
|
|
|
|
edit={this.props.edit}
|
|
|
|
onChangeData={(id, newSettings) => this.props.onChangeData(id, newSettings)}/>
|
2020-03-20 17:42:38 +00:00
|
|
|
{this.state.intensity ? (intensityLightView) : (normalLightView)}
|
2020-03-17 22:24:40 +00:00
|
|
|
</StyledDiv>
|
|
|
|
)
|
|
|
|
}
|
2020-03-15 14:50:25 +00:00
|
|
|
}
|