Last active
August 29, 2015 14:10
-
-
Save mikekavouras/4ed898520613e14a7cbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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