2014-02-04 11:00:32 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Doodle3D project (http://doodle3d.com).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, Doodle3D
|
|
|
|
* This software is licensed under the terms of the GNU GPL v2 or later.
|
|
|
|
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
|
|
|
|
*/
|
|
|
|
function NetworkAPI() {
|
|
|
|
|
|
|
|
NetworkAPI.STATUS = {
|
|
|
|
CONNECTING_FAILED: -1,
|
|
|
|
NOT_CONNECTED: 0,
|
|
|
|
CONNECTING: 1,
|
|
|
|
CONNECTED: 2,
|
|
|
|
CREATING: 3,
|
|
|
|
CREATED: 4
|
|
|
|
};
|
|
|
|
|
|
|
|
var _wifiboxURL;
|
|
|
|
var _wifiboxCGIBinURL;
|
|
|
|
var _timeoutTime = 3000;
|
|
|
|
var _retryDelay = 2000; // retry setTimout delay
|
|
|
|
|
|
|
|
var _self = this;
|
|
|
|
|
|
|
|
this.init = function(wifiboxURL,wifiboxCGIBinURL) {
|
|
|
|
console.log("NetworkAPI:init");
|
|
|
|
console.log(" wifiboxURL: ",wifiboxURL);
|
|
|
|
console.log(" wifiboxCGIBinURL: ",wifiboxCGIBinURL);
|
|
|
|
_wifiboxURL = wifiboxURL;
|
|
|
|
_wifiboxCGIBinURL = wifiboxCGIBinURL;
|
|
|
|
}
|
|
|
|
this.scan = function(completeHandler) {
|
2014-02-04 22:56:58 +01:00
|
|
|
//console.log("NetworkAPI:scan");
|
2014-02-04 11:00:32 +01:00
|
|
|
//console.log(" _wifiboxURL: ",_wifiboxURL);
|
|
|
|
$.ajax({
|
|
|
|
url: _wifiboxURL + "/network/scan",
|
|
|
|
type: "GET",
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: _timeoutTime,
|
|
|
|
success: function(response){
|
|
|
|
//console.log("NetworkAPI:scan response: ",response);
|
|
|
|
if(response.status == "error" || response.status == "fail") {
|
|
|
|
console.log("NetworkAPI:scan failed: ",response);
|
|
|
|
} else {
|
|
|
|
completeHandler(response.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
console.log("NetworkAPI:scan failed");
|
|
|
|
});
|
|
|
|
};
|
|
|
|
this.status = function(completeHandler,failedHandler) {
|
|
|
|
//console.log("NetworkAPI:status");
|
|
|
|
$.ajax({
|
|
|
|
url: _wifiboxURL + "/network/status",
|
|
|
|
type: "GET",
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: _timeoutTime,
|
|
|
|
success: function(response){
|
|
|
|
//console.log("NetworkAPI:status response: ",response);
|
|
|
|
if(response.status == "error" || response.status == "fail") {
|
|
|
|
failedHandler();
|
|
|
|
} else {
|
|
|
|
completeHandler(response.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
failedHandler();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.associate = function(ssid,phrase,recreate) {
|
|
|
|
console.log("NetworkAPI:associate");
|
|
|
|
var postData = {
|
|
|
|
ssid:ssid,
|
|
|
|
phrase:phrase,
|
|
|
|
recreate:recreate
|
|
|
|
};
|
|
|
|
$.ajax({
|
2014-02-04 14:33:08 +01:00
|
|
|
url: _wifiboxCGIBinURL + "/network/associate",
|
2014-02-04 11:00:32 +01:00
|
|
|
type: "POST",
|
|
|
|
data: postData,
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: _timeoutTime,
|
|
|
|
success: function(response){
|
2014-02-04 22:56:58 +01:00
|
|
|
//console.log("NetworkAPI:associate response: ",response);
|
2014-02-04 11:00:32 +01:00
|
|
|
}
|
|
|
|
}).fail(function() {
|
2014-02-04 22:56:58 +01:00
|
|
|
//console.log("NetworkAPI:associate: timeout (normal behavior)");
|
2014-02-04 11:00:32 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-04 14:34:03 +01:00
|
|
|
this.openAP = function() {
|
|
|
|
console.log("NetworkAPI:openAP");
|
|
|
|
$.ajax({
|
|
|
|
url: _wifiboxCGIBinURL + "/network/openap",
|
|
|
|
type: "POST",
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: _timeoutTime,
|
|
|
|
success: function(response){
|
2014-02-04 22:56:58 +01:00
|
|
|
//console.log("NetworkAPI:openAP response: ",response);
|
2014-02-04 14:34:03 +01:00
|
|
|
}
|
|
|
|
}).fail(function() {
|
2014-02-04 22:56:58 +01:00
|
|
|
//console.log("NetworkAPI:openAP: timeout (normal behavior)");
|
2014-02-04 14:34:03 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-04 11:00:32 +01:00
|
|
|
this.signin = function() {
|
|
|
|
$.ajax({
|
2014-02-07 12:32:35 +01:00
|
|
|
url: _wifiboxCGIBinURL + "/network/signin",
|
2014-02-04 11:00:32 +01:00
|
|
|
type: "GET",
|
|
|
|
dataType: 'json',
|
2014-02-07 12:32:35 +01:00
|
|
|
timeout: _timeoutTime,
|
2014-02-04 11:00:32 +01:00
|
|
|
success: function(response){
|
2014-02-07 12:32:35 +01:00
|
|
|
console.log("NetworkAPI:signin response: ",response);
|
2014-02-04 11:00:32 +01:00
|
|
|
}
|
|
|
|
}).fail(function() {
|
2014-02-07 12:32:35 +01:00
|
|
|
console.log("NetworkAPI:signin: failed");
|
2014-02-04 11:00:32 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|