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

191 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import { Checkbox, Icon } from 'semantic-ui-react';
import { RemoteService } from '../../../remote';
import { connect } from 'react-redux';
import './Thermostat.css';
import Slider from 'react-rangeslider';
import 'react-rangeslider/lib/index.css';
import mapStateToProps from '../../../deviceProps';
2020-05-02 09:11:21 +00:00
import {
2020-04-24 21:34:01 +00:00
stateTag,
container,
deviceName,
targetTemperature,
toggle,
stateTagContainer,
2020-05-12 13:18:33 +00:00
} from './ThermostatStyle';
2020-04-23 14:02:02 +00:00
class Thermostats extends Component {
2020-04-24 21:34:01 +00:00
constructor(props) {
super(props);
this.state = {
2020-05-02 11:58:02 +00:00
targetTemperature: this.props.device.targetTemperature,
mode: this.props.device.mode,
measuredTemperature: this.props.device.measuredTemperature,
2020-04-23 14:02:02 +00:00
};
2020-04-24 21:34:01 +00:00
this.setMode = this.setMode.bind(this);
this.setTargetTemperature = this.setTargetTemperature.bind(this);
}
setMode(mode) {
2020-05-12 13:18:33 +00:00
// i came to the conclusion that is not possible to set mode.
2020-05-02 09:11:21 +00:00
// Good job Jacob (Claudio)
2020-05-12 13:18:33 +00:00
// this.mode = "HEATING";
2020-04-24 21:34:01 +00:00
const turnOn = mode;
2020-05-12 13:18:33 +00:00
if (this.props.tab === 'Devices') {
2020-04-25 13:37:40 +00:00
this.props
2020-04-26 11:38:54 +00:00
.saveDevice({ ...this.props.stateOrDevice, turnOn })
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('thermostat update error', err));
2020-04-26 11:38:54 +00:00
} else {
this.props.updateState(
2020-05-01 17:55:08 +00:00
{ id: this.props.stateOrDevice.id, on: turnOn },
2020-05-12 13:18:33 +00:00
this.props.stateOrDevice.kind,
2020-04-26 11:38:54 +00:00
);
2020-04-25 13:37:40 +00:00
}
2020-04-24 21:34:01 +00:00
}
onClickDevice = () => {
const on = !this.turnedOn;
2020-05-12 13:18:33 +00:00
if (this.props.tab === 'Devices') {
2020-04-25 13:37:40 +00:00
this.props
2020-04-26 11:38:54 +00:00
.saveDevice({ ...this.props.stateOrDevice, on })
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('thermostat update error', err));
2020-04-26 11:38:54 +00:00
} else {
this.props.updateState(
2020-05-02 09:11:21 +00:00
{ id: this.props.stateOrDevice.id, on },
2020-05-12 13:18:33 +00:00
this.props.stateOrDevice.kind,
2020-04-26 11:38:54 +00:00
);
2020-04-25 13:37:40 +00:00
}
2020-04-24 21:34:01 +00:00
};
saveTargetTemperature(targetTemperature) {
2020-05-12 13:18:33 +00:00
const turn = this.props.stateOrDevice.mode !== 'OFF';
if (this.props.tab === 'Devices') {
2020-04-25 13:37:40 +00:00
this.props
2020-04-27 17:24:21 +00:00
.saveDevice({
...this.props.stateOrDevice,
targetTemperature,
turnOn: turn,
})
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('thermostat update error', err));
2020-04-26 11:38:54 +00:00
} else {
this.props.updateState(
2020-04-27 13:47:59 +00:00
{
id: this.props.stateOrDevice.id,
2020-05-12 13:18:33 +00:00
targetTemperature,
2020-04-27 13:47:59 +00:00
},
2020-05-12 13:18:33 +00:00
this.props.stateOrDevice.kind,
2020-04-26 11:38:54 +00:00
);
2020-04-25 13:37:40 +00:00
}
2020-04-24 21:34:01 +00:00
}
2020-05-02 09:11:21 +00:00
setTargetTemperature() {
this.saveTargetTemperature(this.state.targetTemperature);
2020-04-24 21:34:01 +00:00
}
handleChange = (value) => {
2020-05-02 09:11:21 +00:00
this.setState({ ...this.state, targetTemperature: value });
2020-04-24 21:34:01 +00:00
};
handleCheckbox = (val) => {
const useExternalSensors = val;
2020-05-12 13:18:33 +00:00
const turnOn = this.props.stateOrDevice.mode !== 'OFF';
if (this.props.tab === 'Devices') {
this.props
.saveDevice({ ...this.props.stateOrDevice, useExternalSensors, turnOn })
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('thermostat update error', err));
}
};
2020-04-24 21:34:01 +00:00
render() {
return (
<div style={container}>
2020-05-02 09:11:21 +00:00
<h3 style={deviceName}>
Thermostat
<Checkbox
2020-05-04 09:13:19 +00:00
disabled={this.props.disabled}
2020-05-02 11:58:02 +00:00
checked={
2020-05-12 13:18:33 +00:00
this.props.tab === 'Devices'
? this.props.device.mode !== 'OFF'
2020-05-02 11:58:02 +00:00
: this.props.stateOrDevice.on
}
2020-05-03 12:39:44 +00:00
toggle
2020-05-02 09:11:21 +00:00
style={toggle}
onChange={(e, val) => this.setMode(val.checked)}
/>
</h3>
<hr />
<div style={targetTemperature}>
2020-05-12 13:18:33 +00:00
<Icon name="thermometer half" />
{' '}
{this.props.device.measuredTemperature}
{' '}
ºC
<br />
<Icon name="target" />
{' '}
{this.props.device.targetTemperature.toFixed(1)}
{' '}
ºC
2020-05-02 09:11:21 +00:00
</div>
2020-05-12 13:18:33 +00:00
{this.props.tab === 'Devices' ? (
<>
<Slider
disabled={this.props.disabled}
min={10}
max={30}
step={0.1}
tooltip={false}
className="slider-css"
value={this.state.targetTemperature}
onChange={(event) => this.handleChange(event)}
onChangeComplete={() => this.setTargetTemperature()}
/>
<Checkbox
2020-05-12 13:18:33 +00:00
style={{ padding: '0 .7rem' }}
label="Use external sensors"
name="external"
toggle
checked={this.props.stateOrDevice.useExternalSensors}
disabled={!this.props.tempSensorsInRoom}
onChange={(e, val) => this.handleCheckbox(val.checked)}
/>
2020-05-12 13:18:33 +00:00
</>
2020-05-01 17:55:08 +00:00
) : null}
2020-04-24 21:34:01 +00:00
<div style={stateTagContainer}>
2020-05-02 11:58:02 +00:00
<span style={stateTag}>
2020-05-12 13:18:33 +00:00
{this.props.tab !== 'Scenes'
2020-05-02 11:58:02 +00:00
? this.props.device.mode
: this.props.stateOrDevice.on
2020-05-12 13:18:33 +00:00
? 'WILL TURN ON'
: 'WILL TURN OFF'}
2020-05-02 11:58:02 +00:00
</span>
2020-04-24 21:34:01 +00:00
</div>
</div>
);
}
2020-04-23 14:02:02 +00:00
}
const mapStateToProps2 = (state, ownProps) => ({
...mapStateToProps(state, ownProps),
get tempSensorsInRoom() {
2020-05-12 13:18:33 +00:00
if (state.active.activeTab !== 'Devices') return false;
const room = state.rooms[state.devices[ownProps.id].roomId];
if (!room) return false;
const deviceIds = room.devices;
const devices = [...deviceIds].map((id) => state.devices[id]);
const sensors = devices.filter(
2020-05-12 13:18:33 +00:00
(d) => d.kind === 'sensor' && d.sensor === 'TEMPERATURE',
);
return sensors.length > 0;
},
});
const ThermostatContainer = connect(
mapStateToProps2,
2020-05-12 13:18:33 +00:00
RemoteService,
)(Thermostats);
export default ThermostatContainer;