fixed many things

This commit is contained in:
britea 2020-04-26 13:38:54 +02:00
parent cce1650bd9
commit 2a1cc0292c
6 changed files with 91 additions and 55 deletions

View file

@ -3,19 +3,32 @@ import { connect } from "react-redux";
import { RemoteService } from "../../remote";
import Device from "./devices/Device";
import NewSceneDevice from "./NewSceneDevice";
import { Grid } from "semantic-ui-react";
import { Grid, Button } from "semantic-ui-react";
class ScenesPanel extends Component {
constructor(props) {
super(props);
this.applyScene = this.applyScene.bind(this);
}
applyScene() {
console.log(this.props.activeScene);
this.props.sceneApply(this.props.activeScene);
}
render() {
return (
<Grid doubling columns={2} divided="vertically">
{!this.props.isActiveDefaultScene ? (
<Grid.Row centered>
<Button color="blue" onClick={this.applyScene}>
Apply Scene
</Button>
</Grid.Row>
) : null}
{!this.props.isActiveDefaultScene
? this.props.sceneStates.map((e, i) => {
console.log(this.props.sceneStates);
return (
<Grid.Column key={i}>
<Device tab={this.props.tab} id={e.id} />

View file

@ -20,7 +20,8 @@ class Thermostats extends Component {
super(props);
this.state = {
targetTemperature: this.props.stateOrDevice.targetTemperature,
internalSensorTemperature: this.props.stateOrDevice.internalSensorTemperature,
internalSensorTemperature: this.props.stateOrDevice
.internalSensorTemperature,
mode: this.props.stateOrDevice.mode,
measuredTemperature: this.props.stateOrDevice.measuredTemperature,
useExternalSensors: this.props.stateOrDevice.useExternalSensors,
@ -63,34 +64,43 @@ class Thermostats extends Component {
//i came to the conclusion that is not possible to set mode.
//this.mode = "HEATING";
const turnOn = mode;
if(this.props.tab==="Devices"){
if (this.props.tab === "Devices") {
this.props
.saveDevice({ ...this.props.stateOrDevice, turnOn })
.catch((err) => console.error("thermostat update error", err));
}else{
this.props.updateState({ id: this.props.sceneState.id, turnOn: turnOn },this.props.sceneState.kind);
} else {
this.props.updateState(
{ id: this.props.sceneState.id, turnOn: turnOn },
this.props.sceneState.kind
);
}
}
onClickDevice = () => {
const on = !this.turnedOn;
if(this.props.tab==="Devices"){
if (this.props.tab === "Devices") {
this.props
.saveDevice({ ...this.props.stateOrDevice, on })
.catch((err) => console.error("thermostat update error", err));
}else{
this.props.updateState({ id: this.props.sceneState.id, on: on },this.props.sceneState.kind);
} else {
this.props.updateState(
{ id: this.props.sceneState.id, on: on },
this.props.sceneState.kind
);
}
};
//It seems to work
saveTargetTemperature(targetTemperature) {
if(this.props.tab==="Devices"){
if (this.props.tab === "Devices") {
this.props
.saveDevice({ ...this.props.stateOrDevice, targetTemperature })
.catch((err) => console.error("thermostat update error", err));
}else{
this.props.updateState({id: this.props.sceneState.id, targetTemperature: targetTemperature},this.props.sceneState.kind);
} else {
this.props.updateState(
{ id: this.props.sceneState.id, targetTemperature: targetTemperature },
this.props.sceneState.kind
);
}
}
@ -113,12 +123,18 @@ class Thermostats extends Component {
//I have no idea why it doesn't want to update the temperature
saveInternalSensorTemperature(internalSensorTemperature) {
if(this.props.tab==="Devices"){
if (this.props.tab === "Devices") {
this.props
.saveDevice({ ...this.props.device, internalSensorTemperature })
.catch((err) => console.error("thermostat update error", err));
}else{
this.props.updateState({ id: this.props.sceneState.id, internalSensorTemperature:internalSensorTemperature },this.props.sceneState.kind);
} else {
this.props.updateState(
{
id: this.props.sceneState.id,
internalSensorTemperature: internalSensorTemperature,
},
this.props.sceneState.kind
);
}
}
@ -151,7 +167,6 @@ class Thermostats extends Component {
render() {
return (
<div style={container}>
<h3 style={deviceName}>{this.props.stateOrDevice.name}</h3>
<div style={line}></div>
@ -183,12 +198,11 @@ class Thermostats extends Component {
}
const mapStateToProps = (state, ownProps) => ({
get stateOrDevice(){
if(state.active.activeTab==="Devices"){
get stateOrDevice() {
if (state.active.activeTab === "Devices") {
return state.devices[ownProps.id];
}else{
const sceneState = state.sceneStates[ownProps.id];
return state.devices[sceneState];
} else {
return state.sceneStates[ownProps.id];
}
},
});

View file

@ -40,7 +40,7 @@ class Videocam extends Component {
</video>
</div>
</StyledDivCamera>
<Grid columns="equal" divide padded>
<Grid columns="equal" padded>
<Grid.Row textAlign="center">
<Grid.Column>
<VideocamModal

View file

@ -386,7 +386,7 @@ export const RemoteService = {
return Endpoint.put(url, {}, data)
.then((res) => {
dispatch(actions.stateSave(res.data));
dispatch(actions.stateUpdate(res.data));
return res.data;
})
.catch((err) => {
@ -396,15 +396,15 @@ export const RemoteService = {
};
},
deleteState: (id, type)=>{
return (dispatch)=>{
deleteState: (id, type) => {
return (dispatch) => {
let url;
if (type === "dimmableState") {
url = "/dimmableState";
} else {
url = "/switchableState";
}
return Endpoint.delete(url+`/${id}`)
return Endpoint.delete(url + `/${id}`)
.then((_) => dispatch(actions.stateDelete(id)))
.catch((err) => {
console.warn("state delete error", err);
@ -413,14 +413,14 @@ export const RemoteService = {
};
},
sceneApply: (id)=>{
return (dispatch)=>{
let url=`/scene/${id}/apply`;
sceneApply: (id) => {
return (dispatch) => {
let url = `/scene/${id}/apply`;
return Endpoint.post(url)
.then((res)=> dispatch(actions.sceneApply(id)))
.catch((err)=>{
console.warn('scene apply error',err);
.then((res) => dispatch(actions.sceneApply(res.data)))
.catch((err) => {
console.warn("scene apply error", err);
throw new RemoteError(["Network error"]);
});
};

View file

@ -105,7 +105,7 @@ function reducer(previousState, action) {
createOrUpdateScene(scene);
}
break;
case "STATE_UPDATE":
case "STATES_UPDATE":
//console.log(action.sceneStates);
newState = previousState;
change = null;
@ -164,6 +164,9 @@ function reducer(previousState, action) {
newState = update(newState, change);
break;
case "STATE_UPDATE":
//update the state
break;
case "DEVICES_UPDATE":
change = null;
@ -368,7 +371,9 @@ function reducer(previousState, action) {
sceneStates: { $unset: [action.stateId] },
};
if (previousState.scenes[previousState.sceneStates[action.stateId].sceneId]) {
if (
previousState.scenes[previousState.sceneStates[action.stateId].sceneId]
) {
change.scenes = {
[previousState.sceneStates[action.stateId].sceneId]: {
sceneStates: { $remove: [action.stateId] },
@ -380,12 +385,12 @@ function reducer(previousState, action) {
break;
case "SCENE_APPLY":
console.log(action);
//checking that the scene actually exists
if (!(action.sceneId in previousState.scenes)) {
/*if (!(action.sceneId in previousState.scenes)) {
console.warn(`Scene ${action.sceneId} does not exist`);
break;
}
}*/
break;
case "DEVICE_DELETE":

View file

@ -30,21 +30,25 @@ const actions = {
sceneState,
}),
statesUpdate: (sceneId, sceneStates) => ({
type: "STATE_UPDATE",
type: "STATES_UPDATE",
sceneId,
sceneStates,
}),
stateUpdate: (state) => ({
type: "STATE_UPDATE",
state,
}),
devicesUpdate: (roomId, devices, partial = false) => ({
type: "DEVICES_UPDATE",
roomId,
devices,
partial,
}),
stateDelete: (stateId)=>({
stateDelete: (stateId) => ({
type: "STATE_DELETE",
stateId,
}),
sceneApply: (sceneId)=>({
sceneApply: (sceneId) => ({
type: "SCENE_APPLY",
sceneId,
}),