stateOrDevice() added in various places

This commit is contained in:
Jacob Salvi 2020-04-25 17:27:56 +02:00
parent 7f9a42fb84
commit fd5c28b1c9
6 changed files with 98 additions and 55 deletions

View File

@ -2,30 +2,29 @@ import React, { Component } from "react";
import "./Curtains.css"; import "./Curtains.css";
import { RemoteService } from "../../../remote"; import { RemoteService } from "../../../remote";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { Slider } from "@material-ui/core";
class Curtain extends Component { class Curtain extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { intensity: this.props.device.intensity, timeout: null }; this.state = { intensity: this.props.stateOrDevice.intensity, timeout: null };
this.setIntensity = this.setIntensity.bind(this); this.setIntensity = this.setIntensity.bind(this);
} }
//getters //getters
get turnedOn() { get turnedOn() {
return this.props.device.on; return this.props.stateOrDevice.on;
} }
get intensity() { get intensity() {
return this.props.device.intensity || 0; return this.props.stateOrDevice.intensity || 0;
} }
onClickDevice = () => { onClickDevice = () => {
const on = !this.turnedOn; const on = !this.turnedOn;
if(this.props.tab==="Devices"){ if(this.props.tab==="Devices"){
this.props this.props
.saveDevice({ ...this.props.device, on }) .saveDevice({ ...this.props.stateOrDevice, on })
.catch((err) => console.error("curtains update error", err)); .catch((err) => console.error("curtains update error", err));
}else{ }else{
this.props.updateState({ id: this.props.sceneState.id, on: on },this.props.sceneState.kind); this.props.updateState({ id: this.props.sceneState.id, on: on },this.props.sceneState.kind);
@ -55,7 +54,7 @@ class Curtain extends Component {
const intensity = Math.round(this.state.intensity); const intensity = Math.round(this.state.intensity);
if(this.props.tab==="Devices"){ if(this.props.tab==="Devices"){
this.props this.props
.saveDevice({ ...this.props.device, intensity }) .saveDevice({ ...this.props.stateOrDevice, intensity })
.catch((err) => console.error("curtain update error", err)); .catch((err) => console.error("curtain update error", err));
}else{ }else{
this.props.updateState({ id: this.props.sceneState.id, intensity: intensity },this.props.sceneState.kind); this.props.updateState({ id: this.props.sceneState.id, intensity: intensity },this.props.sceneState.kind);
@ -67,7 +66,7 @@ class Curtain extends Component {
this.setIntensity(1); this.setIntensity(1);
this.saveIntensity(); this.saveIntensity();
} else { } else {
this.setIntensity(this.props.device.intensity / 100 + 0.1); this.setIntensity(this.props.stateOrDevice.intensity / 100 + 0.1);
this.saveIntensity(); this.saveIntensity();
} }
}; };
@ -86,15 +85,15 @@ class Curtain extends Component {
<div <div
className="open-container" className="open-container"
style={{ style={{
height: (9 * this.props.device.intensity) / 100 + "rem", height: (9 * this.props.stateOrDevice.intensity) / 100 + "rem",
}} }}
></div>{" "} ></div>{" "}
<span className="span-open"> <span className="span-open">
{Math.round(this.props.device.intensity)}% {Math.round(this.props.stateOrDevice.intensity)}%
</span> </span>
<input <input
onChange={this.handleChange} onChange={this.handleChange}
value={this.props.device.intensity} value={this.props.stateOrDevice.intensity}
className="slider" className="slider"
type="range" type="range"
min="0" min="0"
@ -107,7 +106,15 @@ class Curtain extends Component {
} }
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id], get stateOrDevice(){
if(state.active.activeTab==="Devices"){
return state.devices[ownProps.id];
}else{
const sceneState = state.sceneStates[ownProps.id];
return state.devices[sceneState];
}
},
//device: state.devices[ownProps.id],
}); });
const CurtainContainer = connect(mapStateToProps, RemoteService)(Curtain); const CurtainContainer = connect(mapStateToProps, RemoteService)(Curtain);
export default CurtainContainer; export default CurtainContainer;

View File

@ -64,7 +64,7 @@ export class KnobDimmerComponent extends Component {
super(props); super(props);
this.state = { this.state = {
intensity: this.props.device.intensity || 0, intensity: this.props.stateOrDevice.intensity || 0,
timeout: null, timeout: null,
}; };
@ -129,7 +129,14 @@ export class KnobDimmerComponent extends Component {
} }
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id], get stateOrDevice(){
if(state.active.activeTab==="Devices"){
return state.devices[ownProps.id];
}else{
const sceneState = state.sceneStates[ownProps.id];
return state.devices[sceneState];
}
},
}); });
const conn = connect(mapStateToProps, RemoteService); const conn = connect(mapStateToProps, RemoteService);

View File

@ -35,7 +35,7 @@ import { connect } from "react-redux";
class Light extends Component { class Light extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { intensity: this.props.device.intensity, timeout: null }; this.state = { intensity: this.props.stateOrDevice.intensity, timeout: null };
this.iconOn = "/img/lightOn.svg"; this.iconOn = "/img/lightOn.svg";
this.iconOff = "/img/lightOff.svg"; this.iconOff = "/img/lightOff.svg";
@ -44,24 +44,24 @@ class Light extends Component {
} }
componentDidUpdate(prevProps, prevState) { componentDidUpdate(prevProps, prevState) {
if (this.props.device.intensity !== prevProps.device.intensity) { if (this.props.stateOrDevice.intensity !== prevProps.stateOrDevice.intensity) {
this.setState({ intensity: this.props.device.intensity, timeout: null }); this.setState({ intensity: this.props.stateOrDevice.intensity, timeout: null });
} }
} }
get turnedOn() { get turnedOn() {
return this.props.device.on; return this.props.stateOrDevice.on;
} }
get intensity() { get intensity() {
return this.props.device.intensity || 0; return this.props.stateOrDevice.intensity || 0;
} }
onClickDevice = () => { onClickDevice = () => {
const on = !this.turnedOn; const on = !this.turnedOn;
if(this.props.tab==="Devices"){ if(this.props.tab==="Devices"){
this.props this.props
.saveDevice({ ...this.props.device, on }) .saveDevice({ ...this.props.stateOrDevice, on })
.catch((err) => console.error("regular light update error", err)); .catch((err) => console.error("regular light update error", err));
}else{ }else{
this.props.updateState({ id: this.props.sceneState.id, on: on }, this.props.sceneState.kind); this.props.updateState({ id: this.props.sceneState.id, on: on }, this.props.sceneState.kind);
@ -95,7 +95,7 @@ class Light extends Component {
const intensity = Math.round(this.state.intensity); const intensity = Math.round(this.state.intensity);
if(this.props.tab==="Devices"){ if(this.props.tab==="Devices"){
this.props this.props
.saveDevice({ ...this.props.device, intensity }) .saveDevice({ ...this.props.stateOrDevice, intensity })
.catch((err) => console.error("regular light update error", err)); .catch((err) => console.error("regular light update error", err));
}else{ }else{
this.props.updateState({ id: this.props.sceneState.id, intensity: intensity }, this.props.sceneState.kind); this.props.updateState({ id: this.props.sceneState.id, intensity: intensity }, this.props.sceneState.kind);
@ -147,7 +147,7 @@ class Light extends Component {
return ( return (
<div> <div>
{this.props.device.kind === "dimmableLight" {this.props.stateOrDevice.kind === "dimmableLight"
? intensityLightView ? intensityLightView
: normalLightView} : normalLightView}
</div> </div>
@ -156,7 +156,15 @@ class Light extends Component {
} }
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id], get stateOrDevice(){
if(state.active.activeTab==="Devices"){
return state.devices[ownProps.id];
}else{
const sceneState = state.sceneStates[ownProps.id];
return state.devices[sceneState];
}
},
//device: state.devices[ownProps.id],
}); });
const LightContainer = connect(mapStateToProps, RemoteService)(Light); const LightContainer = connect(mapStateToProps, RemoteService)(Light);

View File

@ -65,20 +65,20 @@ class Sensor extends Component {
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
if ( if (
this.props.device.kind === "sensor" && this.props.stateOrDevice.kind === "sensor" &&
this.props.device.value !== prevProps.device.value this.props.stateOrDevice.value !== prevProps.stateOrDevice.value
) { ) {
this.setState({ value: this.props.device.value }); this.setState({ value: this.props.stateOrDevice.value });
} else if ( } else if (
this.props.device.kind === "motionSensor" && this.props.stateOrDevice.kind === "motionSensor" &&
this.props.device.detected !== prevProps.device.detected this.props.stateOrDevice.detected !== prevProps.stateOrDevice.detected
) { ) {
this.setState({ motion: true, detected: this.props.device.detected }); this.setState({ motion: true, detected: this.props.stateOrDevice.detected });
} }
} }
componentDidMount() { componentDidMount() {
if (this.props.device.kind === "sensor") { if (this.props.stateOrDevice.kind === "sensor") {
switch (this.props.device.sensor) { switch (this.props.device.sensor) {
case "TEMPERATURE": case "TEMPERATURE":
this.units = "ºC"; this.units = "ºC";
@ -102,11 +102,11 @@ class Sensor extends Component {
this.units = ""; this.units = "";
} }
this.setState({ this.setState({
value: this.props.device.value, value: this.props.stateOrDevice.value,
}); });
} else { } else {
this.setState({ this.setState({
detected: this.props.device.detected, detected: this.props.stateOrDevice.detected,
motion: true, motion: true,
}); });
} }
@ -149,7 +149,7 @@ class Sensor extends Component {
<React.Fragment> <React.Fragment>
<CircularInput <CircularInput
value={ value={
this.props.device.sensor === "LIGHT" this.props.stateOrDevice.sensor === "LIGHT"
? this.state.value / 2000 ? this.state.value / 2000
: this.state.value / 100 : this.state.value / 100
} }
@ -192,7 +192,14 @@ class Sensor extends Component {
} }
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id], get stateOrDevice(){
if(state.active.activeTab==="Devices"){
return state.devices[ownProps.id];
}else{
const sceneState = state.sceneStates[ownProps.id];
return state.devices[sceneState];
}
},
}); });
const SensorContainer = connect(mapStateToProps, RemoteService)(Sensor); const SensorContainer = connect(mapStateToProps, RemoteService)(Sensor);
export default SensorContainer; export default SensorContainer;

View File

@ -24,18 +24,18 @@ class SmartPlug extends Component {
} }
get turnedOn() { get turnedOn() {
return this.props.device.on; return this.props.stateOrDevice.on;
} }
get energyConsumed() { get energyConsumed() {
return (this.props.device.totalConsumption / 1000).toFixed(3); return (this.props.stateOrDevice.totalConsumption / 1000).toFixed(3);
} }
onClickDevice = () => { onClickDevice = () => {
const on = !this.turnedOn; const on = !this.turnedOn;
if(this.props.tab==="Devices"){ if(this.props.tab==="Devices"){
this.props this.props
.saveDevice({ ...this.props.device, on }) .saveDevice({ ...this.props.stateOrDevice, on })
.catch((err) => console.error("smart plug update error", err)); .catch((err) => console.error("smart plug update error", err));
}else{ }else{
this.props.updateState({ id: this.props.sceneState.id, on: on},this.props.sceneState.kind); this.props.updateState({ id: this.props.sceneState.id, on: on},this.props.sceneState.kind);
@ -68,7 +68,14 @@ class SmartPlug extends Component {
} }
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id], get stateOrDevice(){
if(state.active.activeTab==="Devices"){
return state.devices[ownProps.id];
}else{
const sceneState = state.sceneStates[ownProps.id];
return state.devices[sceneState];
}
},
}); });
const SmartPlugContainer = connect(mapStateToProps, RemoteService)(SmartPlug); const SmartPlugContainer = connect(mapStateToProps, RemoteService)(SmartPlug);
export default SmartPlugContainer; export default SmartPlugContainer;

View File

@ -19,11 +19,11 @@ class Thermostats extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
targetTemperature: this.props.device.targetTemperature, targetTemperature: this.props.stateOrDevice.targetTemperature,
internalSensorTemperature: this.props.device.internalSensorTemperature, internalSensorTemperature: this.props.stateOrDevice.internalSensorTemperature,
mode: this.props.device.mode, mode: this.props.stateOrDevice.mode,
measuredTemperature: this.props.device.measuredTemperature, measuredTemperature: this.props.stateOrDevice.measuredTemperature,
useExternalSensors: this.props.device.useExternalSensors, useExternalSensors: this.props.stateOrDevice.useExternalSensors,
timeout: null, timeout: null,
}; };
this.setMode = this.setMode.bind(this); this.setMode = this.setMode.bind(this);
@ -35,23 +35,23 @@ class Thermostats extends Component {
//getters //getters
get getMode() { get getMode() {
return this.props.device.mode; return this.props.stateOrDevice.mode;
} }
get getTargetTemperature() { get getTargetTemperature() {
return this.props.device.targetTemperature; return this.props.stateOrDevice.targetTemperature;
} }
get getInternalSensorTemperature() { get getInternalSensorTemperature() {
return this.props.device.internalSensorTemperature; return this.props.stateOrDevice.internalSensorTemperature;
} }
get getMeasuredTemperature() { get getMeasuredTemperature() {
return this.props.device.measuredTemperature; return this.props.stateOrDevice.measuredTemperature;
} }
get getUseExternalSensors() { get getUseExternalSensors() {
return this.props.device.useExternalSensors; return this.props.stateOrDevice.useExternalSensors;
} }
setMode(mode) { setMode(mode) {
@ -65,7 +65,7 @@ class Thermostats extends Component {
const turnOn = mode; const turnOn = mode;
if(this.props.tab==="Devices"){ if(this.props.tab==="Devices"){
this.props this.props
.saveDevice({ ...this.props.device, turnOn }) .saveDevice({ ...this.props.stateOrDevice, turnOn })
.catch((err) => console.error("thermostat update error", err)); .catch((err) => console.error("thermostat update error", err));
}else{ }else{
this.props.updateState({ id: this.props.sceneState.id, turnOn: turnOn },this.props.sceneState.kind); this.props.updateState({ id: this.props.sceneState.id, turnOn: turnOn },this.props.sceneState.kind);
@ -76,7 +76,7 @@ class Thermostats extends Component {
const on = !this.turnedOn; const on = !this.turnedOn;
if(this.props.tab==="Devices"){ if(this.props.tab==="Devices"){
this.props this.props
.saveDevice({ ...this.props.device, on }) .saveDevice({ ...this.props.stateOrDevice, on })
.catch((err) => console.error("thermostat update error", err)); .catch((err) => console.error("thermostat update error", err));
}else{ }else{
this.props.updateState({ id: this.props.sceneState.id, on: on },this.props.sceneState.kind); this.props.updateState({ id: this.props.sceneState.id, on: on },this.props.sceneState.kind);
@ -87,7 +87,7 @@ class Thermostats extends Component {
saveTargetTemperature(targetTemperature) { saveTargetTemperature(targetTemperature) {
if(this.props.tab==="Devices"){ if(this.props.tab==="Devices"){
this.props this.props
.saveDevice({ ...this.props.device, targetTemperature }) .saveDevice({ ...this.props.stateOrDevice, targetTemperature })
.catch((err) => console.error("thermostat update error", err)); .catch((err) => console.error("thermostat update error", err));
}else{ }else{
this.props.updateState({id: this.props.sceneState.id, targetTemperature: targetTemperature},this.props.sceneState.kind); this.props.updateState({id: this.props.sceneState.id, targetTemperature: targetTemperature},this.props.sceneState.kind);
@ -153,7 +153,7 @@ class Thermostats extends Component {
return ( return (
<div style={container}> <div style={container}>
<h3 style={deviceName}>{this.props.device.name}</h3> <h3 style={deviceName}>{this.props.stateOrDevice.name}</h3>
<div style={line}></div> <div style={line}></div>
<Checkbox <Checkbox
slider slider
@ -163,19 +163,19 @@ class Thermostats extends Component {
/> />
<span style={targetTemperature}> <span style={targetTemperature}>
{this.props.device.targetTemperature}ºC {this.props.stateOrDevice.targetTemperature}ºC
</span> </span>
<input <input
type="range" type="range"
min="0" min="0"
max="40" max="40"
className="slider-css" className="slider-css"
value={this.props.device.targetTemperature} value={this.props.stateOrDevice.targetTemperature}
onChange={(event) => this.handleChange(event.target.value)} onChange={(event) => this.handleChange(event.target.value)}
id="targetTemperature" id="targetTemperature"
/> />
<div style={stateTagContainer}> <div style={stateTagContainer}>
<span style={stateTag}>{this.props.device.mode}</span> <span style={stateTag}>{this.props.stateOrDevice.mode}</span>
</div> </div>
</div> </div>
); );
@ -183,7 +183,14 @@ class Thermostats extends Component {
} }
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
device: state.devices[ownProps.id], get stateOrDevice(){
if(state.active.activeTab==="Devices"){
return state.devices[ownProps.id];
}else{
const sceneState = state.sceneStates[ownProps.id];
return state.devices[sceneState];
}
},
}); });
const ThermostatContainer = connect( const ThermostatContainer = connect(