/** * 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"; import { iconStyle, StyledDiv } from "./styleComponents"; import Settings from "./DeviceSettings"; 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"; this.iconOff = "/img/lightOff.svg"; } onClickDevice = () => { this.props.device.on = !this.state.turnedOn; call.deviceUpdate(this.props.device, "regularLight").then((res) => { if (res.status === 200) { this.setState((prevState) => ({ turnedOn: !prevState.turnedOn })); } }); }; setIntensity = (newValue) => { 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() { if (this.props.device.hasOwnProperty("intensity")) { this.setState({ intensity: this.props.device.intensity, }); } this.setState({ turnedOn: this.props.device.on, }); // Get the state and update it } render() { const intensityLightView = ( {Math.round(this.props.device.intensity)}% {this.props.device.name} ); const normalLightView = (
{} : this.onClickDevice}>
{this.props.device.name}
); return ( this.props.onChangeData(id, newSettings) } /> {this.props.device.intensity >= 0 ? intensityLightView : normalLightView} ); } }