I've cobbled together a javascript to control and report back status. The control part works, but I found that the feedback comes back before the command is completed and reports the previous state.
A quick google search found the 'setTimeout' function which works great, but errors if I don't hard code the variables into the function that the setTimeout function triggers. (Sorry for the crappy explanation)
The function I am using to control a device calls the feedback function after performing the control function.
setTimeout('getData(devRef)',1000) is called a second after the command is sent in order to allow HomeSeer to record the device status change. If I hardcode setTimeout('getData(558)',1000) everyting works, but trying to use a variable(devRef) causes an error. If it helps, this is the HTML for calling deviceCmd.
This is the feedback function.
If anyone has any suggestions, I'm all ears.
Thanks.
A quick google search found the 'setTimeout' function which works great, but errors if I don't hard code the variables into the function that the setTimeout function triggers. (Sorry for the crappy explanation)
The function I am using to control a device calls the feedback function after performing the control function.
PHP Code:
function deviceCmd(devRef,cmd) {
xhttp.open('GET','/JSON?request=controldevicebylabel&ref=' + devRef + '&label=' + cmd ,true);
xhttp.send(null);
var msg = document.getElementById('retrieved');
setTimeout('getData(devRef)',1000);
}
PHP Code:
<button value="lightOn" onClick="deviceCmd('558','on')">Light On</button>
This is the feedback function.
PHP Code:
function getData(devRef){
var xhr1 = new XMLHttpRequest();
xhr1.open('GET', '/JSON?request=getstatus&ref=' + devRef, true);
xhr1.send(null);
xhr1.onreadystatechange = function() {
if (xhr1.readyState == 4) {
var status=JSON.parse(xhr1.responseText).Devices[0].value;
var loc=JSON.parse(xhr1.responseText).Devices[0].location;
var name=JSON.parse(xhr1.responseText).Devices[0].name;
var onoff = document.getElementById('status');
onoff.innerHTML = 'the status of device : ' + loc + " " + name + ' is ' + status;
}
}
}
Thanks.
Comment