2020-03-24 16:08:19 +00:00
|
|
|
// vim: set ts=2 sw=2 et tw=80:
|
|
|
|
|
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).
|
|
|
|
*/
|
2020-03-23 20:24:17 +00:00
|
|
|
import React, { Component } from "react";
|
2020-03-25 16:20:53 +00:00
|
|
|
import {
|
|
|
|
iconStyle,
|
|
|
|
StyledDiv,
|
|
|
|
BottomPanel,
|
|
|
|
ThumbText,
|
|
|
|
} from "./styleComponents";
|
2020-03-23 20:24:17 +00:00
|
|
|
import { Image } from "semantic-ui-react";
|
|
|
|
import {
|
|
|
|
CircularInput,
|
|
|
|
CircularProgress,
|
|
|
|
CircularThumb,
|
|
|
|
} from "react-circular-input";
|
|
|
|
import {
|
2020-03-25 11:13:15 +00:00
|
|
|
LightDimmerContainer,
|
|
|
|
LightDimmerStyle,
|
|
|
|
textStyle,
|
2020-03-23 20:24:17 +00:00
|
|
|
nameStyle,
|
2020-03-25 11:13:15 +00:00
|
|
|
KnobProgress,
|
|
|
|
CircularThumbStyle,
|
2020-03-25 16:20:53 +00:00
|
|
|
knobIcon,
|
2020-03-23 20:24:17 +00:00
|
|
|
} from "./LightStyle";
|
2020-04-10 15:25:52 +00:00
|
|
|
import { RemoteService } from "../../../remote";
|
|
|
|
import { connect } from "react-redux";
|
2020-05-03 16:09:53 +00:00
|
|
|
import mapStateToProps from "../../../deviceProps";
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-04-10 15:25:52 +00:00
|
|
|
class Light extends Component {
|
2020-03-17 22:24:40 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-04-25 15:46:04 +00:00
|
|
|
this.state = {
|
|
|
|
intensity: this.props.stateOrDevice.intensity,
|
|
|
|
timeout: null,
|
|
|
|
};
|
2020-04-11 20:47:47 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
this.iconOn = "/img/lightOn.svg";
|
2020-03-25 16:20:53 +00:00
|
|
|
this.iconOff = "/img/lightOff.svg";
|
2020-04-11 20:47:47 +00:00
|
|
|
|
|
|
|
this.setIntensity = this.setIntensity.bind(this);
|
2020-04-10 22:42:23 +00:00
|
|
|
}
|
2020-03-25 16:20:53 +00:00
|
|
|
|
2020-05-02 11:58:02 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
2020-04-25 15:46:04 +00:00
|
|
|
if (
|
|
|
|
this.props.stateOrDevice.intensity !== prevProps.stateOrDevice.intensity
|
|
|
|
) {
|
|
|
|
this.setState({
|
|
|
|
intensity: this.props.stateOrDevice.intensity,
|
|
|
|
timeout: null,
|
|
|
|
});
|
2020-04-16 13:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:42:23 +00:00
|
|
|
get turnedOn() {
|
2020-04-25 15:27:56 +00:00
|
|
|
return this.props.stateOrDevice.on;
|
2020-04-10 22:42:23 +00:00
|
|
|
}
|
2020-03-25 16:20:53 +00:00
|
|
|
|
2020-04-10 22:42:23 +00:00
|
|
|
get intensity() {
|
2020-05-03 16:09:53 +00:00
|
|
|
return this.state.intensity || 0;
|
2020-03-17 22:24:40 +00:00
|
|
|
}
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
onClickDevice = () => {
|
2020-04-11 16:29:32 +00:00
|
|
|
const on = !this.turnedOn;
|
2020-04-25 15:46:04 +00:00
|
|
|
if (this.props.tab === "Devices") {
|
2020-04-25 13:37:40 +00:00
|
|
|
this.props
|
2020-04-25 15:46:04 +00:00
|
|
|
.saveDevice({ ...this.props.stateOrDevice, on })
|
|
|
|
.catch((err) => console.error("regular light update error", err));
|
|
|
|
} else {
|
2020-04-28 08:48:07 +00:00
|
|
|
if (this.props.device.kind === "regularLight") {
|
|
|
|
this.props
|
|
|
|
.updateState(
|
|
|
|
{
|
|
|
|
id: this.props.stateOrDevice.id,
|
|
|
|
on: on,
|
|
|
|
sceneId: this.props.stateOrDevice.sceneId,
|
|
|
|
},
|
|
|
|
this.props.stateOrDevice.kind
|
|
|
|
)
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
});
|
|
|
|
}
|
2020-04-25 13:37:40 +00:00
|
|
|
}
|
2020-03-17 22:24:40 +00:00
|
|
|
};
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-25 11:13:15 +00:00
|
|
|
getIcon = () => {
|
2020-04-10 22:42:23 +00:00
|
|
|
return this.turnedOn ? this.iconOn : this.iconOff;
|
2020-03-25 11:13:15 +00:00
|
|
|
};
|
|
|
|
|
2020-04-11 20:47:47 +00:00
|
|
|
setIntensity(intensity) {
|
|
|
|
intensity *= 100;
|
2020-04-12 15:49:29 +00:00
|
|
|
|
2020-04-11 20:47:47 +00:00
|
|
|
if (this.state.timeout) {
|
|
|
|
clearTimeout(this.state.timeout);
|
|
|
|
}
|
|
|
|
|
2020-04-12 15:49:29 +00:00
|
|
|
this.setState({
|
2020-04-11 20:47:47 +00:00
|
|
|
intensity,
|
|
|
|
timeout: setTimeout(() => {
|
|
|
|
this.saveIntensity();
|
|
|
|
this.setState({
|
|
|
|
intensity: this.state.intensity,
|
2020-04-12 15:49:29 +00:00
|
|
|
timeout: null,
|
2020-04-11 20:47:47 +00:00
|
|
|
});
|
2020-04-12 15:49:29 +00:00
|
|
|
}, 100),
|
2020-04-11 20:47:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
saveIntensity = () => {
|
|
|
|
const intensity = Math.round(this.state.intensity);
|
2020-04-25 15:46:04 +00:00
|
|
|
if (this.props.tab === "Devices") {
|
2020-04-25 13:37:40 +00:00
|
|
|
this.props
|
2020-04-25 15:46:04 +00:00
|
|
|
.saveDevice({ ...this.props.stateOrDevice, intensity })
|
|
|
|
.catch((err) => console.error("regular light update error", err));
|
|
|
|
} else {
|
2020-04-28 08:48:07 +00:00
|
|
|
this.props
|
|
|
|
.updateState(
|
|
|
|
{ id: this.props.stateOrDevice.id, intensity: intensity },
|
|
|
|
this.props.stateOrDevice.kind
|
|
|
|
)
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res, this.props.stateOrDevice.kind);
|
|
|
|
});
|
2020-04-25 13:37:40 +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 = (
|
2020-03-25 11:13:15 +00:00
|
|
|
<div style={LightDimmerContainer}>
|
2020-03-25 16:20:53 +00:00
|
|
|
<CircularInput
|
|
|
|
style={LightDimmerStyle}
|
2020-05-03 16:13:50 +00:00
|
|
|
value={+(Math.round(this.intensity / 100 + "e+2") + "e-2")}
|
2020-03-25 16:20:53 +00:00
|
|
|
onChange={this.setIntensity}
|
2020-04-11 20:47:47 +00:00
|
|
|
onMouseUp={this.saveIntensity}
|
2020-03-25 16:20:53 +00:00
|
|
|
>
|
|
|
|
<text
|
|
|
|
style={textStyle}
|
|
|
|
x={100}
|
|
|
|
y={120}
|
|
|
|
textAnchor="middle"
|
|
|
|
dy="0.3em"
|
|
|
|
fontWeight="bold"
|
|
|
|
>
|
2020-04-10 22:42:23 +00:00
|
|
|
Intensity light
|
2020-03-25 11:13:15 +00:00
|
|
|
</text>
|
2020-03-25 16:20:53 +00:00
|
|
|
<CircularProgress
|
|
|
|
style={{
|
|
|
|
...KnobProgress,
|
2020-05-03 16:09:53 +00:00
|
|
|
opacity: this.intensity / 100 + 0.3,
|
2020-03-25 16:20:53 +00:00
|
|
|
}}
|
|
|
|
/>
|
2020-03-25 11:13:15 +00:00
|
|
|
<CircularThumb style={CircularThumbStyle} />
|
|
|
|
<ThumbText color={"#ffd31d"} />
|
|
|
|
</CircularInput>
|
|
|
|
<Image style={knobIcon} src="/img/intensityLightIcon.svg" />
|
|
|
|
</div>
|
2020-03-17 22:24:40 +00:00
|
|
|
);
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
const normalLightView = (
|
2020-04-21 10:13:01 +00:00
|
|
|
<StyledDiv onClick={this.onClickDevice}>
|
|
|
|
<div>
|
2020-03-25 11:13:15 +00:00
|
|
|
<Image src={this.getIcon()} style={iconStyle} />
|
2020-03-25 15:04:02 +00:00
|
|
|
<BottomPanel style={{ backgroundColor: "#ffa41b" }}>
|
2020-04-11 16:29:32 +00:00
|
|
|
<h5 style={nameStyle}>Light</h5>
|
2020-03-25 15:04:02 +00:00
|
|
|
</BottomPanel>
|
2020-03-25 11:13:15 +00:00
|
|
|
</div>
|
|
|
|
</StyledDiv>
|
2020-03-17 22:24:40 +00:00
|
|
|
);
|
2020-03-23 20:24:17 +00:00
|
|
|
|
2020-03-17 22:24:40 +00:00
|
|
|
return (
|
2020-03-25 11:13:15 +00:00
|
|
|
<div>
|
2020-04-28 08:24:58 +00:00
|
|
|
{this.props.device.kind === "dimmableLight"
|
2020-03-25 16:20:53 +00:00
|
|
|
? intensityLightView
|
|
|
|
: normalLightView}
|
2020-03-25 11:13:15 +00:00
|
|
|
</div>
|
2020-03-25 16:20:53 +00:00
|
|
|
);
|
2020-03-17 22:24:40 +00:00
|
|
|
}
|
2020-03-15 14:50:25 +00:00
|
|
|
}
|
2020-04-10 15:25:52 +00:00
|
|
|
|
|
|
|
const LightContainer = connect(mapStateToProps, RemoteService)(Light);
|
|
|
|
export default LightContainer;
|