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

148 lines
3.7 KiB
JavaScript
Raw Normal View History

/**
Users can add dimmers, a particular kind of switch that can also modify the intensity level of a given light.
There are two types of dimmers:
A dimmer with state stores a given intensity level and sets the light to that level. <-- StatefulDimmer
A dimmer without state can just increase or decrease the intensity of a light. <-- DefualtDimmer
The user can change the state of a dimmer through an intuitive UI in SmartHut .
**/
2020-03-25 16:20:53 +00:00
import React, { Component } from "react";
import {
2020-03-25 16:20:53 +00:00
CircularInput,
CircularProgress,
CircularThumb,
} from "react-circular-input";
2020-03-25 16:20:53 +00:00
import {
ButtonDimmerContainer,
MinusPanel,
PlusPanel,
ThumbText,
} from "./styleComponents";
import {
2020-03-25 16:20:53 +00:00
CircularThumbStyle,
KnobDimmerStyle,
KnobProgress,
textStyle,
knobIcon,
knobContainer,
} from "./DimmerStyle";
2020-04-10 15:25:52 +00:00
import { RemoteService } from "../../../remote";
import { connect } from "react-redux";
2020-03-25 18:58:19 +00:00
import { call } from "../../../client_server";
2020-04-10 15:25:52 +00:00
export class ButtonDimmerComponent extends Component {
2020-03-25 16:20:53 +00:00
constructor(props) {
super(props);
this.state = {};
}
2020-03-25 16:20:53 +00:00
increaseIntensity = () => {
2020-03-25 18:58:19 +00:00
let data = {
dimType: "UP",
id: this.props.device.id,
};
call.deviceUpdate(data, "buttonDimmer/dim").then((res) => {
if (res.status === 200) {
}
});
2020-03-25 16:20:53 +00:00
};
decreaseIntensity = () => {
2020-03-25 18:58:19 +00:00
let data = {
dimType: "DOWN",
id: this.props.device.id,
};
call.deviceUpdate(data, "buttonDimmer/dim").then((res) => {
if (res.status === 200) {
}
});
2020-03-25 16:20:53 +00:00
};
2020-03-23 20:24:17 +00:00
2020-03-25 16:20:53 +00:00
componentDidMount() {}
2020-03-23 20:24:17 +00:00
2020-03-25 16:20:53 +00:00
render() {
return (
<ButtonDimmerContainer>
<img alt="icon" src="/img/buttonDimmer.svg" />
2020-03-25 23:15:02 +00:00
<span className="knob">
{this.props.device.name} ({this.props.device.id})
</span>
2020-03-25 18:58:19 +00:00
<PlusPanel name="UP" onClick={this.increaseIntensity}>
2020-03-25 16:20:53 +00:00
<span>&#43;</span>
</PlusPanel>
2020-03-25 18:58:19 +00:00
<MinusPanel name="DOWN" onClick={this.decreaseIntensity}>
2020-03-25 16:20:53 +00:00
<span>&minus;</span>
</MinusPanel>
</ButtonDimmerContainer>
);
}
}
2020-04-10 15:25:52 +00:00
export class KnobDimmerComponent extends Component {
2020-03-25 16:20:53 +00:00
constructor(props) {
super(props);
this.state = {
pointingDevices: [],
value: 1,
};
}
2020-03-23 20:24:17 +00:00
2020-03-25 18:58:19 +00:00
setIntensity = (newValue) => {
let val = Math.round(newValue * 100) <= 1 ? 1 : Math.round(newValue * 100);
let data = {
id: this.props.device.id,
intensity: val,
};
call.deviceUpdate(data, "knobDimmer/dimTo").then((res) => {
if (res.status === 200) {
this.setState({
value: val,
});
}
});
};
componentDidMount() {
this.setState({
value: 1,
});
}
2020-03-23 20:24:17 +00:00
2020-03-25 16:20:53 +00:00
render() {
return (
<div style={knobContainer}>
<CircularInput
style={KnobDimmerStyle}
2020-03-25 18:58:19 +00:00
value={+(Math.round(this.state.value / 100 + "e+2") + "e-2")}
onChange={this.setIntensity}
2020-03-25 16:20:53 +00:00
>
<text
style={textStyle}
x={100}
y={120}
textAnchor="middle"
dy="0.3em"
fontWeight="bold"
>
2020-03-25 23:15:02 +00:00
{this.props.device.name} ({this.props.device.id})
2020-03-25 16:20:53 +00:00
</text>
<CircularProgress
style={{ ...KnobProgress, opacity: this.state.value + 0.1 }}
/>
<CircularThumb style={CircularThumbStyle} />
<ThumbText color={"#1a2849"} />
</CircularInput>
<img alt="Knob Icon" style={knobIcon} src="/img/knobDimmer.svg" />
</div>
);
}
}
2020-04-10 15:25:52 +00:00
const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id],
});
const conn = connect(mapStateToProps, RemoteService);
export const KnobDimmer = conn(KnobDimmerComponent);
export const ButtonDimmer = conn(ButtonDimmerComponent);