Done real time output device updates

This commit is contained in:
Claudio Maggioni 2020-03-24 17:08:19 +01:00
parent 105bb4e088
commit 7928407259
3 changed files with 42 additions and 8 deletions

View File

@ -63,9 +63,7 @@ class ServiceSocket {
console.error("socket authentication failed"); console.error("socket authentication failed");
} }
} else { } else {
if (data.id && this.callbacks[data.id]) { this.invokeCallbacks(data);
this.callbacks[data.id].forEach((f) => f(data));
}
} }
}; };
@ -79,6 +77,12 @@ class ServiceSocket {
}; };
} }
invokeCallbacks(data) {
if (data.id && this.callbacks[data.id]) {
this.callbacks[data.id].forEach((f) => f(data));
}
}
/** /**
* Registers a new callback function to be called when updates on the device * Registers a new callback function to be called when updates on the device
* with the id given are recieved * with the id given are recieved
@ -226,9 +230,20 @@ export var call = {
if (typeDevice) { if (typeDevice) {
url = typeDevice; url = typeDevice;
} }
return axios.put(config + url, data, { let promiseRes = axios.put(config + url, data, {
headers: { Authorization: "Bearer " + tkn }, headers: { Authorization: "Bearer " + tkn },
}); });
if (typeDevice === "switch/operate") {
promiseRes = promiseRes.then((e) => {
if (e.status === 200) {
e.data.forEach((device) => socket.invokeCallbacks(device));
}
return e;
});
}
return promiseRes;
}, },
deviceDelete: function (data, headers) { deviceDelete: function (data, headers) {
return axios.delete(config + data.device + "/" + data.id, { return axios.delete(config + data.device + "/" + data.id, {

View File

@ -1,3 +1,5 @@
// vim: set ts=2 sw=2 et tw=80:
/** /**
* Users can add lights in their rooms. * Users can add lights in their rooms.
* Lights are devices like bulbs, LED strip lights, lamps. * Lights are devices like bulbs, LED strip lights, lamps.
@ -29,9 +31,27 @@ export default class Light extends Component {
super(props); super(props);
this.state = { this.state = {
turnedOn: false, turnedOn: false,
intensity: 0,
}; };
this.iconOn = "/img/lightOn.svg"; this.iconOn = "/img/lightOn.svg";
this.iconOff = "/img/lightOff.svg"; this.iconOff = "/img/lightOff.svg";
this.stateCallback = (e) => {
const update = {};
if (e.kind === "dimmableLight") {
update.intensity = e.intensity;
} else {
update.turnedOn = e.on;
}
this.setState(Object.assign(this.state, update));
};
call.socketSubscribe(this.props.device.id, this.stateCallback);
}
componentWillUnmount() {
call.socketUnsubscribe(this.props.device.id, this.stateCallback);
} }
onClickDevice = () => { onClickDevice = () => {
@ -75,7 +95,7 @@ export default class Light extends Component {
render() { render() {
const intensityLightView = ( const intensityLightView = (
<CircularInput <CircularInput
value={+(Math.round(this.props.device.intensity / 100 + "e+2") + "e-2")} value={+(Math.round(this.state.intensity / 100 + "e+2") + "e-2")}
onChange={this.setIntensity} onChange={this.setIntensity}
style={style} style={style}
> >
@ -91,7 +111,7 @@ export default class Light extends Component {
dy="0.3em" dy="0.3em"
fontWeight="bold" fontWeight="bold"
> >
{Math.round(this.props.device.intensity)}% {Math.round(this.state.intensity)}%
</text> </text>
<text <text
style={intensityLightStyle} style={intensityLightStyle}

View File

@ -84,8 +84,7 @@ export default class ModalWindow extends Component {
let reader = new FileReader(); let reader = new FileReader();
reader.readAsDataURL(file.target.files[0]); reader.readAsDataURL(file.target.files[0]);
reader.onload = () => { reader.onload = () => {
this.state.img = reader.result; this.setState(Object.assign(this.state, { img: reader.result }));
this.setState(this.state);
}; };
reader.onerror = console.error; reader.onerror = console.error;
} }