frontend/smart-hut/src/components/SimulationPanelSlider.js

132 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-05-26 11:45:06 +00:00
import React, { Component } from 'react';
import {
2020-05-27 10:04:16 +00:00
Form, Grid, Checkbox, Message, Label,
2020-05-26 11:45:06 +00:00
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import { RemoteService } from '../remote';
class SimulationPanelSlider extends Component {
constructor(props) {
super(props);
console.log(this.props.device);
2020-05-26 15:20:56 +00:00
this.state = {
value: this.internalValue,
2020-05-27 10:04:16 +00:00
error: this.internalValue,
2020-05-26 15:20:56 +00:00
};
2020-05-27 16:46:40 +00:00
this.props.update(this.props.error, this.internalValue);
2020-05-26 15:20:56 +00:00
this.updateSliderValues = this.updateSliderValues.bind(this);
}
updateSliderValues(data) {
this.setState({
[this.props.error ? 'error' : 'value']: data,
});
2020-05-27 10:04:16 +00:00
console.log(this.state);
this.props.update(this.props.error, data, this.props.device.kind === 'motionSensor');
2020-05-26 15:20:56 +00:00
}
settings() {
let max;
let step;
switch (this.props.device.kind) {
case 'sensor':
if (this.props.device.sensorType === 'LIGHT') {
max = this.props.error ? 1000 : 15000;
step = 10;
} else {
max = 100;
step = 1;
}
break;
case 'thermostat':
max = 100;
step = 1;
break;
default:
break;
}
return { max, step };
2020-05-26 11:45:06 +00:00
}
get internalValue() {
2020-05-27 10:04:16 +00:00
if (this.props.device.kind === 'motionSensor') {
return this.props.device.detected;
}
return this.props.error ? this.props.device.err : this.props.device.typical.toFixed(2);
}
get getValue() {
2020-05-26 11:45:06 +00:00
switch (this.props.device.kind) {
case 'motionSensor':
2020-05-27 10:04:16 +00:00
return this.props.device.detected;
case 'thermostat':
return this.props.device.internalSensorTemperature;
case 'sensor':
return this.props.device.value.toFixed(2);
2020-05-26 15:20:56 +00:00
default:
return '';
2020-05-26 11:45:06 +00:00
}
}
render() {
return (
<Grid columns={2}>
<Grid.Column as={Form} textAlign="center">
2020-05-27 10:04:16 +00:00
<Message>
<Message.Header>{this.props.error ? 'Edit error' : 'Edit value'}</Message.Header>
<p>
Actual value of
{' '}
{this.props.device.name}
:
{this.props.device.kind === 'motionSensor'
? this.getValue ? 'on' : 'off' : this.getValue}
</p>
<p>
{this.props.error ? 'error' : 'typical value'}
{' '}
:
{this.props.device.kind === 'motionSensor'
? this.internalValue ? 'on' : 'off' : this.internalValue}
</p>
</Message>
2020-05-26 15:20:56 +00:00
{
this.props.device.kind === 'motionSensor'
? (
<Checkbox
toggle
name="value"
checked={this.state.value}
2020-05-27 10:04:16 +00:00
onChange={(e, { checked }) => this.updateSliderValues(checked)}
2020-05-26 15:20:56 +00:00
/>
2020-05-27 10:04:16 +00:00
)
2020-05-26 15:20:56 +00:00
: (
2020-05-27 10:04:16 +00:00
<>
<Label>
Value :
<Label.Detail>{this.props.error ? this.state.error : this.state.value}</Label.Detail>
</Label>
<Form.Input
min={0}
max={this.settings().max}
name="value"
onChange={(e, { value }) => this.updateSliderValues(value)}
step={this.settings().step}
type="range"
value={this.props.error ? this.state.error : this.state.value}
/>
</>
)
2020-05-26 15:20:56 +00:00
}
2020-05-26 11:45:06 +00:00
</Grid.Column>
</Grid>
);
}
}
const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id],
});
const SimulationPanelSliderContainer = connect(mapStateToProps, RemoteService)(SimulationPanelSlider);
export default SimulationPanelSliderContainer;