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

132 lines
3.3 KiB
JavaScript
Raw Normal View History

/**
* 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";
import { iconStyle, StyledDiv } from "./styleComponents";
import Settings from "./DeviceSettings";
2020-03-23 20:24:17 +00:00
import { Image } from "semantic-ui-react";
import {
CircularInput,
CircularProgress,
CircularThumb,
CircularTrack,
} from "react-circular-input";
import {
valueStyle,
intensityLightStyle,
style,
nameStyle,
} from "./LightStyle";
import { call } from "../../../client_server";
export default class Light extends Component {
constructor(props) {
super(props);
this.state = {
turnedOn: false,
};
this.iconOn = "/img/lightOn.svg";
2020-03-23 20:24:17 +00:00
this.iconOff = "/img/lightOff.svg";
}
onClickDevice = () => {
2020-03-23 01:17:13 +00:00
this.props.device.on = !this.state.turnedOn;
2020-03-23 20:24:17 +00:00
call.deviceUpdate(this.props.device, "regularLight").then((res) => {
if (res.status === 200) {
this.setState((prevState) => ({ turnedOn: !prevState.turnedOn }));
}
});
};
setIntensity = (newValue) => {
2020-03-23 20:24:17 +00:00
this.props.device.intensity =
Math.round(newValue * 100) <= 1 ? 1 : Math.round(newValue * 100);
call.deviceUpdate(this.props.device, "dimmableLight").then((res) => {
if (res.status === 200) {
this.setState({ intensity: newValue });
}
});
};
getIcon = () => {
if (this.state.turnedOn) {
return this.iconOn;
}
return this.iconOff;
};
componentDidMount() {
2020-03-20 17:42:38 +00:00
if (this.props.device.hasOwnProperty("intensity")) {
this.setState({
2020-03-23 20:24:17 +00:00
intensity: this.props.device.intensity,
});
}
2020-03-23 01:17:13 +00:00
this.setState({
2020-03-23 20:24:17 +00:00
turnedOn: this.props.device.on,
2020-03-23 01:17:13 +00:00
});
// Get the state and update it
}
render() {
const intensityLightView = (
<CircularInput
2020-03-23 20:24:17 +00:00
value={+(Math.round(this.props.device.intensity / 100 + "e+2") + "e-2")}
onChange={this.setIntensity}
style={style}
>
2020-03-23 20:24:17 +00:00
<CircularTrack />
<CircularProgress />
<CircularThumb />
2020-03-23 20:24:17 +00:00
<text
style={valueStyle}
x={100}
y={100}
textAnchor="middle"
dy="0.3em"
fontWeight="bold"
>
2020-03-22 16:58:27 +00:00
{Math.round(this.props.device.intensity)}%
</text>
2020-03-23 20:24:17 +00:00
<text
style={intensityLightStyle}
x={100}
y={150}
textAnchor="middle"
dy="0.3em"
fontWeight="bold"
>
{this.props.device.name}
</text>
</CircularInput>
);
const normalLightView = (
2020-03-23 20:24:17 +00:00
<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-23 20:24:17 +00:00
return (
<StyledDiv>
<Settings
deviceId={this.props.device.id}
edit={this.props.edit}
2020-03-23 20:24:17 +00:00
onChangeData={(id, newSettings) =>
this.props.onChangeData(id, newSettings)
}
/>
{this.props.device.intensity >= 0
? intensityLightView
: normalLightView}
</StyledDiv>
2020-03-23 20:24:17 +00:00
);
}
}