Skip to content

Instantly share code, notes, and snippets.

@mikekavouras
Last active August 29, 2015 14:10
Show Gist options
  • Save mikekavouras/4ed898520613e14a7cbb to your computer and use it in GitHub Desktop.
Save mikekavouras/4ed898520613e14a7cbb to your computer and use it in GitHub Desktop.
// API
function Wemo(devices) {
this.devices = devices;
this.baseURL = "http://localhost:5000/api/device/";
this.state = {}
this.checked = 0;
this.resetState();
}
Wemo.prototype = {
checkState: function(callback) {
for (var i = 0; i < this.devices.length; i++) {
this.checkStateForDevice(this.devices[i], callback)
}
},
checkStateForDevice: function(device, callback) {
var self = this;
$.ajax({
url: self.baseURL + device,
type: "GET",
dataType: "json",
success: function(data) {
self.updateState();
if (checked == devices.length) {
callback(state);
self.resetState();
}
},
error: function(data) {
errorAlert("CheckWemo error",90000);
}
});
},
updateState: function(data) {
if (this.checked == 0) {
this.on = data.state;
} else {
this.on = (this.on == false) ? false : data.state;
}
self.checked++;
},
resetState: function() {
this.state.on = false;
this.checked = 0;
}
}
// Useage
var devices = [device1, device2];
var wemo = new Wemo(devices);
wemo.checkState(function(state) {
if (state.on) {
alert("on");
} else {
alert("off");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment