2013-08-27 15:34:28 +02:00
//these settings are defined in the firmware (conf_defaults.lua) and will be initialized in loadSettings()
2013-12-05 17:32:29 +01:00
var settings = { }
2014-01-16 21:30:57 +01:00
var settingsPopup ;
2013-10-23 18:42:47 +02:00
//wrapper to prevent scoping issues in showSettings()
function openSettingsWindow ( ) {
2014-01-16 21:30:57 +01:00
settingsWindow . loadSettings ( function ( ) { // reload settings
settingsPopup . open ( ) ;
} ) ;
2013-10-23 18:42:47 +02:00
}
2013-08-27 15:34:28 +02:00
function SettingsWindow ( ) {
this . wifiboxURL ;
2013-10-16 12:31:22 +02:00
this . wifiboxCGIBinURL
2013-08-27 15:34:28 +02:00
this . window ;
2013-10-26 01:46:24 +02:00
this . btnOK ;
2013-08-27 15:34:28 +02:00
this . form ;
this . timeoutTime = 3000 ;
this . retryDelay = 2000 ; // retry setTimout delay
2013-10-10 12:12:33 +02:00
this . retryRetrieveNetworkStatusDelayTime = 1000 ; // retry setTimout delay
2013-10-23 18:42:47 +02:00
2013-08-27 15:34:28 +02:00
this . retryLoadSettingsDelay ; // retry setTimout instance
this . retrySaveSettingsDelay ; // retry setTimout instance
2013-11-22 17:45:16 +01:00
this . retryResetSettingsDelay // retry setTimout instance
2013-09-02 18:07:22 +02:00
this . retryRetrieveNetworkStatusDelay ; // retry setTimout instance
2013-12-05 17:32:29 +01:00
2013-09-02 18:07:22 +02:00
this . apFieldSet ;
this . clientFieldSet ;
this . networks ;
this . currentNetwork ; // the ssid of the network the box is on
2013-12-05 17:32:29 +01:00
this . selectedNetwork ; // the ssid of the selected network in the client mode settings
this . currentLocalIP = "" ;
this . clientModeState = SettingsWindow . NOT _CONNECTED ;
this . currentAP ;
this . apModeState = SettingsWindow . NO _AP ;
2013-10-23 18:42:47 +02:00
2013-12-05 17:32:29 +01:00
// after switching wifi network or creating a access point we delay the status retrieval
// because the webserver needs time to switch
this . retrieveNetworkStatusDelay ; // setTimout delay
this . retrieveNetworkStatusDelayTime = 1000 ;
2013-09-02 18:07:22 +02:00
2013-08-27 15:34:28 +02:00
// Events
2013-10-10 12:12:33 +02:00
SettingsWindow . SETTINGS _LOADED = "settingsLoaded" ;
2013-09-02 18:07:22 +02:00
2013-12-05 17:32:29 +01:00
// network client mode states
SettingsWindow . NOT _CONNECTED = "not connected" ; // also used as first item in networks list
SettingsWindow . CONNECTED = "connected" ;
SettingsWindow . CONNECTING = "connecting" ;
SettingsWindow . CONNECTING _FAILED = "connecting failed"
2013-10-23 18:42:47 +02:00
2013-12-05 17:32:29 +01:00
// network access point mode states
SettingsWindow . NO _AP = "no ap" ;
SettingsWindow . AP = "ap" ;
SettingsWindow . CREATING _AP = "creating ap" ;
2013-10-23 18:42:47 +02:00
2013-12-05 17:32:29 +01:00
SettingsWindow . API _CONNECTING _FAILED = - 1
SettingsWindow . API _NOT _CONNECTED = 0
SettingsWindow . API _CONNECTING = 1
SettingsWindow . API _CONNECTED = 2
SettingsWindow . API _CREATING = 3
SettingsWindow . API _CREATED = 4
2013-10-23 18:42:47 +02:00
2013-12-05 17:32:29 +01:00
// network mode
SettingsWindow . NETWORK _MODE _NEITHER = "neither" ;
SettingsWindow . NETWORK _MODE _CLIENT = "clientMode" ;
SettingsWindow . NETWORK _MODE _ACCESS _POINT = "accessPointMode" ;
2013-10-23 18:42:47 +02:00
2013-12-05 17:32:29 +01:00
this . networkMode = SettingsWindow . NETWORK _MODE _NEITHER ;
2013-10-23 18:42:47 +02:00
2013-12-05 17:32:29 +01:00
this . updatePanel = new UpdatePanel ( ) ;
this . printerPanel = new PrinterPanel ( ) ;
2013-10-23 18:42:47 +02:00
2013-08-27 15:34:28 +02:00
var self = this ;
2013-09-02 18:07:22 +02:00
2013-10-16 12:31:22 +02:00
this . init = function ( wifiboxURL , wifiboxCGIBinURL ) {
2013-08-27 15:34:28 +02:00
this . wifiboxURL = wifiboxURL ;
2013-10-16 12:31:22 +02:00
this . wifiboxCGIBinURL = wifiboxCGIBinURL ;
2013-09-02 18:07:22 +02:00
2014-01-16 21:30:57 +01:00
this . window = $ ( "#popupSettings" ) ;
2013-10-26 01:46:24 +02:00
this . btnOK = this . window . find ( ".btnOK" ) ;
enableButton ( this . btnOK , this . submitwindow ) ;
2014-01-16 21:30:57 +01:00
settingsPopup = new Popup ( $ ( "#popupSettings" ) , $ ( "#popupMask" ) ) ;
2013-12-05 17:32:29 +01:00
2014-01-10 17:21:43 +01:00
this . window . find ( "#settingsContainer" ) . load ( "settings.html" , function ( ) {
2013-12-05 17:32:29 +01:00
console . log ( "Settings:finished loading settings.html, now loading settings..." ) ;
2013-09-02 18:07:22 +02:00
2013-12-05 17:32:29 +01:00
self . form = self . window . find ( "form" ) ;
2013-08-27 15:34:28 +02:00
self . form . submit ( function ( e ) { self . submitwindow ( e ) } ) ;
2013-09-02 18:07:22 +02:00
2013-12-05 17:32:29 +01:00
$ . ajax ( {
url : self . wifiboxURL + "/printer/listall" ,
dataType : 'json' ,
timeout : self . timeoutTime ,
success : function ( response ) {
console . log ( "Settings:printer/listall response: " , response . data . printers ) ;
$ . each ( response . data . printers , function ( key , value ) {
// console.log(key,value);
$ ( '#printerType' ) . append ( $ ( '<option>' ) . text ( value ) . attr ( 'value' , key ) ) ;
} ) ;
self . loadSettings ( ) ;
var btnAP = self . form . find ( "label[for='ap']" ) ;
var btnClient = self . form . find ( "label[for='client']" ) ;
var btnRefresh = self . form . find ( "#refreshNetworks" ) ;
var btnConnect = self . form . find ( "#connectToNetwork" ) ;
var btnCreate = self . form . find ( "#createAP" ) ;
var networkSelector = self . form . find ( "#network" ) ;
self . apFieldSet = self . form . find ( "#apSettings" ) ;
self . clientFieldSet = self . form . find ( "#clientSettings" ) ;
self . btnRestoreSettings = self . form . find ( "#restoreSettings" ) ;
btnAP . on ( 'touchstart mousedown' , self . showAPSettings ) ;
btnClient . on ( 'touchstart mousedown' , self . showClientSettings ) ;
btnRefresh . on ( 'touchstart mousedown' , self . refreshNetworks ) ;
btnConnect . on ( 'touchstart mousedown' , self . connectToNetwork ) ;
btnCreate . on ( 'touchstart mousedown' , self . createAP ) ;
networkSelector . change ( self . networkSelectorChanged ) ;
self . btnRestoreSettings . on ( 'touchstart mousedown' , self . resetSettings ) ;
// update panel
var $updatePanelElement = self . form . find ( "#updatePanel" ) ;
self . updatePanel . init ( wifiboxURL , $updatePanelElement ) ;
// printer panel
var $printerPanelElement = self . form . find ( "#printerPanel" ) ;
self . printerPanel . init ( wifiboxURL , $printerPanelElement ) ;
self . printerPanel . fillForm = self . fillForm ;
}
} ) . fail ( function ( ) {
console . log ( "FATAL ERROR: Settings:printer/listall failed" ) ;
} ) ;
} ) ; //this.window.find
} //this.init
2014-01-16 21:30:57 +01:00
this . openSettings = function ( ) {
self . loadSettings ( function ( ) { // reload settings
settingsPopup . open ( ) ;
} ) ;
}
this . closeSettings = function ( complete ) {
settingsPopup . close ( complete ) ;
}
2013-12-05 17:32:29 +01:00
2013-08-27 15:34:28 +02:00
this . submitwindow = function ( e ) {
2013-10-26 01:46:24 +02:00
disableButton ( self . btnOK , self . submitwindow ) ;
2013-08-27 15:34:28 +02:00
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
2013-10-26 02:40:55 +02:00
self . saveSettings ( self . readForm ( ) , function ( success ) {
if ( success ) {
2014-01-16 21:30:57 +01:00
self . closeSettings ( function ( ) {
2013-10-26 02:40:55 +02:00
enableButton ( self . btnOK , self . submitwindow ) ;
} ) ;
2013-10-28 13:50:08 +01:00
self . signin ( ) ;
2013-10-26 02:40:55 +02:00
} else {
2013-10-26 01:46:24 +02:00
enableButton ( self . btnOK , self . submitwindow ) ;
2013-10-26 02:40:55 +02:00
}
2013-10-17 14:09:09 +02:00
} ) ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
clearTimeout ( self . retryRetrieveNetworkStatusDelay ) ;
2013-08-27 15:34:28 +02:00
}
2013-09-02 18:07:22 +02:00
2013-10-28 11:16:00 +01:00
this . loadSettings = function ( complete ) {
2013-08-27 15:34:28 +02:00
if ( ! communicateWithWifibox ) {
console . log ( " communicateWithWifibox is false: settings aren't being loaded from wifibox..." )
return ;
}
console . log ( "Settings:loadSettings() >> getting new data..." ) ;
2013-09-02 18:07:22 +02:00
2013-08-27 15:34:28 +02:00
$ . ajax ( {
url : this . wifiboxURL + "/config/all" ,
dataType : 'json' ,
timeout : this . timeoutTime ,
2013-10-18 13:23:50 +02:00
success : function ( response ) {
console . log ( "Settings:loadSettings response: " , response ) ;
settings = response . data ;
2013-08-27 15:34:28 +02:00
console . log ( " settings: " , settings ) ;
2013-12-01 18:38:02 +01:00
self . fillForm ( settings ) ;
2013-08-27 15:34:28 +02:00
$ ( document ) . trigger ( SettingsWindow . SETTINGS _LOADED ) ;
2013-10-28 11:16:00 +01:00
if ( complete ) complete ( ) ;
2013-08-27 15:34:28 +02:00
}
2013-09-02 18:07:22 +02:00
} ) . fail ( function ( ) {
2013-08-27 15:34:28 +02:00
console . log ( "Settings:loadSettings: failed" ) ;
clearTimeout ( self . retryLoadSettingsDelay ) ;
self . retryLoadSettingsDelay = setTimeout ( function ( ) { self . loadSettings ( ) } , self . retryDelay ) ; // retry after delay
} ) ;
2013-09-02 18:07:22 +02:00
this . refreshNetworks ( ) ;
2013-10-10 12:12:33 +02:00
this . retrieveNetworkStatus ( false ) ;
2013-08-27 15:34:28 +02:00
}
2013-12-01 18:38:02 +01:00
this . fillForm = function ( settings , form ) {
if ( ! form ) form = this . form ; // if no form specified, fill whole form
2013-08-28 18:33:25 +02:00
//fill form with loaded settings
2013-12-01 18:38:02 +01:00
var selects = form . find ( "select" ) ;
2013-08-28 18:33:25 +02:00
selects . each ( function ( index , element ) {
var element = $ ( element ) ;
element . val ( settings [ element . attr ( 'name' ) ] ) ;
} ) ;
2013-12-01 18:38:02 +01:00
var inputs = form . find ( "input" ) ;
2013-08-28 18:33:25 +02:00
inputs . each ( function ( index , element ) {
2013-08-27 15:34:28 +02:00
var element = $ ( element ) ;
//console.log("printer setting input: ",index,element.attr("type"),element.attr('name')); //,element);
2013-08-28 18:33:25 +02:00
switch ( element . attr ( "type" ) ) {
2013-09-02 18:07:22 +02:00
case "text" :
case "number" :
2013-08-28 18:33:25 +02:00
element . val ( settings [ element . attr ( 'name' ) ] ) ;
break ;
case "checkbox" :
element . prop ( 'checked' , settings [ element . attr ( 'name' ) ] ) ;
break ;
2013-08-27 15:34:28 +02:00
}
} ) ;
2013-12-01 18:38:02 +01:00
var textareas = form . find ( "textarea" ) ;
2013-08-28 18:33:25 +02:00
textareas . each ( function ( index , element ) {
var element = $ ( element ) ;
var value = settings [ element . attr ( 'name' ) ] ;
element . val ( value ) ;
} ) ;
}
2013-09-02 18:07:22 +02:00
2013-10-17 14:09:09 +02:00
this . saveSettings = function ( newSettings , complete ) {
2013-10-21 15:14:00 +02:00
settings = newSettings ; // store new settings in global settings
if ( communicateWithWifibox ) {
2013-10-17 14:09:09 +02:00
$ . ajax ( {
url : this . wifiboxURL + "/config" ,
type : "POST" ,
data : newSettings ,
dataType : 'json' ,
timeout : this . timeoutTime ,
2013-10-18 13:23:50 +02:00
success : function ( response ) {
console . log ( "Settings:saveSettings response: " , response ) ;
if ( response . status == "error" ) {
2013-10-17 14:09:09 +02:00
clearTimeout ( self . retrySaveSettingsDelay ) ;
2013-10-26 02:40:55 +02:00
self . retrySaveSettingsDelay = setTimeout ( function ( ) { self . saveSettings ( settings , complete ) } , self . retryDelay ) ; // retry after delay
2013-10-17 14:09:09 +02:00
} else {
2013-10-18 13:23:50 +02:00
var data = response . data ;
var validation = data . validation ;
2013-10-17 14:09:09 +02:00
self . clearValidationErrors ( ) ;
var validated = true ;
2013-10-18 13:23:50 +02:00
$ . each ( validation , function ( key , val ) {
2013-10-17 14:09:09 +02:00
if ( val != "ok" ) {
console . log ( "ERROR: setting '" + key + "' not successfully set. Message: " + val ) ;
self . displayValidationError ( key , val ) ;
validated = false ;
}
} ) ;
2013-10-18 13:23:50 +02:00
settings . substituted _ssid = data . substituted _ssid ;
2013-10-26 02:40:55 +02:00
if ( complete ) complete ( validated ) ;
2013-10-17 14:09:09 +02:00
}
}
} ) . fail ( function ( ) {
console . log ( "Settings:saveSettings: failed" ) ;
clearTimeout ( self . retrySaveSettingsDelay ) ;
2013-10-26 02:40:55 +02:00
self . retrySaveSettingsDelay = setTimeout ( function ( ) { self . saveSettings ( settings , complete ) } , self . retryDelay ) ; // retry after delay
2013-10-17 14:09:09 +02:00
} ) ;
}
}
2013-11-22 17:45:16 +01:00
this . resetSettings = function ( ) {
console . log ( "resetSettings" ) ;
//$("#restoreSettings").addClass("disabled");
self . btnRestoreSettings . attr ( "disabled" , true ) ;
//console.log(" self.wifiboxURL: ",self.wifiboxURL);
if ( communicateWithWifibox ) {
$ . ajax ( {
url : self . wifiboxURL + "/config/resetall" ,
type : "POST" ,
dataType : 'json' ,
timeout : this . timeoutTime ,
success : function ( response ) {
console . log ( "Settings:resetSettings response: " , response ) ;
if ( response . status == "error" ) {
clearTimeout ( self . retryResetSettingsDelay ) ;
self . retryResetSettingsDelay = setTimeout ( function ( ) { self . resetSettings ( ) } , self . retryDelay ) ; // retry after delay
} else {
settings = response . data ;
console . log ( " settings: " , settings ) ;
2013-12-01 18:38:02 +01:00
self . fillForm ( settings ) ;
2013-11-22 17:45:16 +01:00
$ ( document ) . trigger ( SettingsWindow . SETTINGS _LOADED ) ;
self . btnRestoreSettings . removeAttr ( "disabled" ) ;
}
}
} ) . fail ( function ( ) {
console . log ( "Settings:resetSettings: failed" ) ;
clearTimeout ( self . retryResetSettingsDelay ) ;
self . retryResetSettingsDelay = setTimeout ( function ( ) { self . resetSettings ( ) } , self . retryDelay ) ; // retry after delay
} ) ;
}
}
2013-10-17 14:09:09 +02:00
this . displayValidationError = function ( key , msg ) {
var formElement = self . form . find ( "[name|='" + key + "']" ) ;
formElement . addClass ( "error" ) ;
var errorMsg = "<p class='errorMsg'>" + msg + "</p>"
formElement . after ( errorMsg ) ;
}
this . clearValidationErrors = function ( ) {
2013-10-23 15:32:28 +02:00
self . form . find ( ".errorMsg" ) . remove ( ) ;
self . form . find ( ".error" ) . removeClass ( "error" ) ;
2013-10-17 14:09:09 +02:00
}
2013-10-23 18:42:47 +02:00
2013-09-02 18:07:22 +02:00
this . readForm = function ( ) {
2013-10-17 14:09:09 +02:00
//console.log("SettingsWindow:readForm");
var settings = { } ;
var selects = self . form . find ( "select" ) ;
2013-08-29 01:41:18 +02:00
selects . each ( function ( index , element ) {
var element = $ ( element ) ;
2013-10-17 14:09:09 +02:00
if ( element . attr ( 'name' ) != "network.client.network" ) {
settings [ element . attr ( 'name' ) ] = element . val ( ) ;
}
2013-08-29 01:41:18 +02:00
} ) ;
2013-09-02 18:07:22 +02:00
2013-10-17 14:09:09 +02:00
var inputs = self . form . find ( "input" ) ;
2013-08-29 01:41:18 +02:00
inputs . each ( function ( index , element ) {
var element = $ ( element ) ;
switch ( element . attr ( "type" ) ) {
2013-09-02 18:07:22 +02:00
case "text" :
case "number" :
2013-08-29 01:41:18 +02:00
settings [ element . attr ( 'name' ) ] = element . val ( ) ;
break ;
case "checkbox" :
settings [ element . attr ( 'name' ) ] = element . prop ( 'checked' )
break ;
}
} ) ;
2013-09-02 18:07:22 +02:00
2013-10-17 14:09:09 +02:00
var textareas = self . form . find ( "textarea" ) ;
2013-08-28 18:33:25 +02:00
textareas . each ( function ( index , element ) {
var element = $ ( element ) ;
settings [ element . attr ( 'name' ) ] = element . val ( ) ;
} ) ;
2013-10-17 14:09:09 +02:00
//console.log(settings);
return settings ;
2013-08-27 15:34:28 +02:00
}
2013-09-02 18:07:22 +02:00
2013-10-28 13:50:08 +01:00
this . signin = function ( ) {
$ . ajax ( {
url : self . wifiboxCGIBinURL + "/network/signin" ,
type : "GET" ,
dataType : 'json' ,
timeout : self . timeoutTime ,
success : function ( response ) {
console . log ( "Settings:signin response: " , response ) ;
}
} ) . fail ( function ( ) {
console . log ( "Settings:signin: failed" ) ;
} ) ;
}
2013-10-29 14:10:56 +01:00
this . downloadlogs = function ( ) {
window . location . href = self . wifiboxURL + "/info/logfiles"
}
2013-12-05 11:14:12 +01:00
this . downloadGcode = function ( ) {
var gcode = generate _gcode ( ) ;
if ( gcode != undefined ) {
var blob = new Blob ( [ gcode . join ( "\n" ) ] , { type : "text/plain;charset=utf-8" } ) ;
saveAs ( blob , "doodle3d.gcode" ) ;
}
}
2013-12-05 17:32:29 +01:00
this . downloadSvg = function ( ) {
var svg = saveToSvg ( ) ;
if ( svg != undefined ) {
var blob = new Blob ( [ svg ] , { type : "text/plain;charset=utf-8" } ) ;
saveAs ( blob , "doodle3d.svg" ) ;
}
}
2013-10-29 14:10:56 +01:00
2013-09-02 18:07:22 +02:00
/ *
* Networks ui
* /
this . showAPSettings = function ( ) {
self . apFieldSet . show ( ) ;
self . clientFieldSet . hide ( ) ;
}
this . showClientSettings = function ( ) {
self . clientFieldSet . show ( ) ;
self . apFieldSet . hide ( ) ;
}
this . refreshNetworks = function ( ) {
2013-10-10 12:12:33 +02:00
console . log ( "Settings:refreshNetworks" ) ;
2013-09-02 18:07:22 +02:00
if ( communicateWithWifibox ) {
$ . ajax ( {
url : self . wifiboxURL + "/network/scan" ,
type : "GET" ,
dataType : 'json' ,
timeout : self . timeoutTime ,
success : function ( response ) {
console . log ( "Settings:refreshNetworks response: " , response ) ;
if ( response . status == "error" ) {
//clearTimeout(self.retrySaveSettingsDelay);
//self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings() },self.retryDelay); // retry after delay
} else {
var networks = response . data . networks
self . networks = { } ;
var foundCurrentNetwork = false ;
var networkSelector = self . form . find ( "#network" ) ;
networkSelector . empty ( ) ;
networkSelector . append (
$ ( "<option></option>" ) . val ( SettingsWindow . NOT _CONNECTED ) . html ( "not connected" )
) ;
$ . each ( networks , function ( index , element ) {
if ( element . ssid == self . currentNetwork ) {
foundCurrentNetwork = true ;
}
networkSelector . append (
$ ( "<option></option>" ) . val ( element . ssid ) . html ( element . ssid )
) ;
self . networks [ element . ssid ] = element ;
} ) ;
if ( foundCurrentNetwork ) {
networkSelector . val ( self . currentNetwork ) ;
self . selectNetwork ( self . currentNetwork ) ;
}
}
}
} ) . fail ( function ( ) {
2013-10-23 18:42:47 +02:00
2013-09-02 18:07:22 +02:00
} ) ;
}
}
2013-10-10 12:12:33 +02:00
this . retrieveNetworkStatus = function ( connecting ) {
//console.log("Settings:retrieveNetworkStatus");
2013-09-02 18:07:22 +02:00
if ( communicateWithWifibox ) {
$ . ajax ( {
url : self . wifiboxURL + "/network/status" ,
type : "GET" ,
dataType : 'json' ,
timeout : self . timeoutTime ,
success : function ( response ) {
2013-09-04 19:24:52 +02:00
console . log ( "Settings:retrieveNetworkStatus response: " , response ) ;
2013-10-10 12:12:33 +02:00
if ( response . status == "error" ) {
2013-10-23 18:42:47 +02:00
2013-09-02 18:07:22 +02:00
} else {
var data = response . data ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
if ( typeof data . status === 'string' ) {
data . status = parseInt ( data . status ) ;
}
//console.log(" data.status: ",data.status,data.statusMessage);
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
// Determine which network settings to show
switch ( data . status ) {
case SettingsWindow . API _NOT _CONNECTED :
//console.log(" not connected & not a access point");
self . apFieldSet . show ( ) ;
self . clientFieldSet . show ( ) ;
2013-10-23 18:42:47 +02:00
2013-10-22 03:31:12 +02:00
self . networkMode = SettingsWindow . NETWORK _MODE _NEITHER ;
2013-10-10 12:12:33 +02:00
break ;
case SettingsWindow . API _CONNECTING _FAILED :
case SettingsWindow . API _CONNECTING :
case SettingsWindow . API _CONNECTED :
//console.log(" client mode");
self . form . find ( "#client" ) . prop ( 'checked' , true ) ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
self . apFieldSet . hide ( ) ;
self . clientFieldSet . show ( ) ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
if ( data . status == SettingsWindow . API _CONNECTED ) {
var networkSelector = self . form . find ( "#network" ) ;
networkSelector . val ( data . ssid ) ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
self . currentNetwork = data . ssid ;
self . currentLocalIP = data . localip ;
self . selectNetwork ( data . ssid ) ;
} else {
self . currentLocalIP = ""
}
2013-10-22 03:31:12 +02:00
self . networkMode = SettingsWindow . NETWORK _MODE _CLIENT ;
2013-10-10 12:12:33 +02:00
break ;
case SettingsWindow . API _CREATING :
case SettingsWindow . API _CREATED :
//console.log(" access point mode");
self . form . find ( "#ap" ) . prop ( 'checked' , true ) ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
self . apFieldSet . show ( ) ;
self . clientFieldSet . hide ( ) ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
self . currentNetwork = undefined ;
self . selectNetwork ( SettingsWindow . NOT _CONNECTED ) ;
var networkSelector = self . form . find ( "#network" ) ;
networkSelector . val ( SettingsWindow . NOT _CONNECTED ) ;
2013-10-23 18:42:47 +02:00
if ( data . ssid && data . status == SettingsWindow . API _CREATED ) {
2013-10-10 12:12:33 +02:00
self . currentAP = data . ssid ;
}
2013-10-22 03:31:12 +02:00
self . networkMode = SettingsWindow . NETWORK _MODE _ACCESS _POINT ;
2013-10-10 12:12:33 +02:00
break ;
}
2013-10-22 03:31:12 +02:00
self . updatePanel . setNetworkMode ( self . networkMode ) ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
// update status message
switch ( data . status ) {
case SettingsWindow . API _CONNECTING _FAILED :
2013-10-23 18:42:47 +02:00
self . setClientModeState ( SettingsWindow . CONNECTING _FAILED , data . statusMessage ) ;
2013-10-10 12:12:33 +02:00
self . setAPModeState ( SettingsWindow . NO _AP , "" ) ;
break ;
2013-10-23 18:42:47 +02:00
case SettingsWindow . API _NOT _CONNECTED :
self . setClientModeState ( SettingsWindow . NOT _CONNECTED , "" ) ;
2013-10-10 12:12:33 +02:00
self . setAPModeState ( SettingsWindow . NO _AP , "" ) ;
break ;
case SettingsWindow . API _CONNECTING :
self . setClientModeState ( S ettingsWindow . CONNECTING , "" ) ;
self . setAPModeState ( SettingsWindow . NO _AP , "" ) ;
break ;
case SettingsWindow . API _CONNECTED :
self . setClientModeState ( SettingsWindow . CONNECTED , "" ) ;
self . setAPModeState ( SettingsWindow . NO _AP , "" ) ;
break ;
case SettingsWindow . API _CREATING :
2013-10-23 18:42:47 +02:00
self . setClientModeState ( SettingsWindow . NOT _CONNECTED , "" ) ;
2013-10-10 12:12:33 +02:00
self . setAPModeState ( SettingsWindow . CREATING _AP , "" ) ;
break ;
case SettingsWindow . API _CREATED :
2013-10-23 18:42:47 +02:00
self . setClientModeState ( SettingsWindow . NOT _CONNECTED , "" ) ;
2013-10-10 12:12:33 +02:00
self . setAPModeState ( SettingsWindow . AP , "" ) ;
break ;
}
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
// Keep checking for updates?
if ( connecting ) {
switch ( data . status ) {
case SettingsWindow . API _CONNECTING :
case SettingsWindow . API _CREATING :
clearTimeout ( self . retryRetrieveNetworkStatusDelay ) ;
self . retryRetrieveNetworkStatusDelay = setTimeout ( function ( ) { self . retrieveNetworkStatus ( connecting ) } , self . retryRetrieveNetworkStatusDelayTime ) ; // retry after delay
break ;
}
2013-09-02 18:07:22 +02:00
}
}
}
} ) . fail ( function ( ) {
2013-09-04 19:24:52 +02:00
console . log ( "Settings:retrieveNetworkStatus: failed" ) ;
2013-09-02 18:07:22 +02:00
clearTimeout ( self . retryRetrieveNetworkStatusDelay ) ;
2013-10-10 12:12:33 +02:00
self . retryRetrieveNetworkStatusDelay = setTimeout ( function ( ) { self . retrieveNetworkStatus ( connecting ) } , self . retryDelay ) ; // retry after delay
2013-09-02 18:07:22 +02:00
} ) ;
}
}
this . networkSelectorChanged = function ( e ) {
var selectedOption = $ ( this ) . find ( "option:selected" ) ;
self . selectNetwork ( selectedOption . val ( ) ) ;
}
this . selectNetwork = function ( ssid ) {
console . log ( "select network: " , ssid ) ;
2013-10-22 03:31:12 +02:00
if ( ssid == "" ) return ;
console . log ( " checked" ) ;
2013-09-02 18:07:22 +02:00
this . selectedNetwork = ssid ;
if ( this . networks == undefined || ssid == SettingsWindow . NOT _CONNECTED ) {
2013-10-10 12:12:33 +02:00
this . hideWiFiPassword ( ) ;
2013-09-02 18:07:22 +02:00
} else {
var network = this . networks [ ssid ] ;
if ( network . encryption == "none" ) {
2013-10-10 12:12:33 +02:00
this . hideWiFiPassword ( ) ;
2013-09-02 18:07:22 +02:00
} else {
2013-10-10 12:12:33 +02:00
this . showWiFiPassword ( ) ;
2013-09-02 18:07:22 +02:00
}
this . form . find ( "#password" ) . val ( "" ) ;
}
}
2013-10-10 12:12:33 +02:00
this . showWiFiPassword = function ( ) {
this . form . find ( "#passwordLabel" ) . show ( ) ;
this . form . find ( "#password" ) . show ( ) ;
}
this . hideWiFiPassword = function ( ) {
this . form . find ( "#passwordLabel" ) . hide ( ) ;
this . form . find ( "#password" ) . hide ( ) ;
}
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
this . setClientModeState = function ( state , msg ) {
2013-09-02 18:07:22 +02:00
var field = this . form . find ( "#clientModeState" ) ;
var btnConnect = self . form . find ( "#connectToNetwork" ) ;
switch ( state ) {
case SettingsWindow . NOT _CONNECTED :
btnConnect . removeAttr ( "disabled" ) ;
field . html ( "Not connected" ) ;
break ;
case SettingsWindow . CONNECTED :
btnConnect . removeAttr ( "disabled" ) ;
2013-10-23 18:42:47 +02:00
2013-10-18 13:23:50 +02:00
var fieldText = "Connected to: <b>" + this . currentNetwork + "</b>." ;
2013-10-10 12:12:33 +02:00
if ( this . currentLocalIP != undefined && this . currentLocalIP != "" ) {
var a = "<a href='http://" + this . currentLocalIP + "' target='_black'>" + this . currentLocalIP + "</a>" ;
2013-10-18 13:23:50 +02:00
fieldText += " (IP: " + a + ")" ;
2013-10-10 12:12:33 +02:00
}
field . html ( fieldText ) ;
2013-09-02 18:07:22 +02:00
break ;
case SettingsWindow . CONNECTING :
btnConnect . attr ( "disabled" , true ) ;
2013-10-18 13:23:50 +02:00
field . html ( "Connecting... Reconnect by connecting your device to <b>" + this . selectedNetwork + "</b> and going to <a href='http://connect.doodle3d.com'>connect.doodle3d.com</a>" ) ;
2013-09-02 18:07:22 +02:00
break ;
2013-10-10 12:12:33 +02:00
case SettingsWindow . CONNECTING _FAILED :
btnConnect . removeAttr ( "disabled" ) ;
2013-10-23 18:42:47 +02:00
field . html ( msg ) ;
2013-10-10 12:12:33 +02:00
break ;
2013-09-02 18:07:22 +02:00
}
this . clientModeState = state ;
}
2013-10-10 12:12:33 +02:00
this . setAPModeState = function ( state , msg ) {
var field = this . form . find ( "#apModeState" ) ;
var btnCreate = this . form . find ( "#createAP" ) ;
switch ( state ) {
case SettingsWindow . NO _AP :
btnCreate . removeAttr ( "disabled" ) ;
field . html ( "Not currently a access point" ) ;
break ;
case SettingsWindow . AP :
btnCreate . removeAttr ( "disabled" ) ;
2013-10-18 13:23:50 +02:00
field . html ( "Is access point: <b>" + this . currentAP + "</b>" ) ;
2013-10-10 12:12:33 +02:00
break ;
case SettingsWindow . CREATING _AP :
btnCreate . attr ( "disabled" , true ) ;
2013-10-18 13:23:50 +02:00
field . html ( "Creating access point... Reconnect by connecting your device to <b>" + settings . substituted _ssid + "</b> and going to <a href='http://draw.doodle3d.com'>draw.doodle3d.com</a>" ) ;
2013-10-10 12:12:33 +02:00
break ;
}
this . apModeState = state ;
}
2013-09-02 18:07:22 +02:00
this . connectToNetwork = function ( ) {
console . log ( "connectToNetwork" ) ;
if ( self . selectedNetwork == undefined ) return ;
2013-10-17 14:09:09 +02:00
var postData = {
2013-09-02 18:07:22 +02:00
ssid : self . selectedNetwork ,
2013-09-04 19:24:52 +02:00
phrase : self . form . find ( "#password" ) . val ( ) ,
recreate : true
2013-09-02 18:07:22 +02:00
}
console . log ( " postData: " , postData ) ;
if ( communicateWithWifibox ) {
2013-10-23 18:42:47 +02:00
2013-10-17 14:09:09 +02:00
// save network related settings and on complete, connect to network
2013-10-26 02:40:55 +02:00
self . saveSettings ( self . readForm ( ) , function ( success ) {
if ( ! success ) return ;
2013-10-17 14:09:09 +02:00
$ . ajax ( {
url : self . wifiboxCGIBinURL + "/network/associate" ,
type : "POST" ,
data : postData ,
dataType : 'json' ,
timeout : self . timeoutTime ,
success : function ( response ) {
console . log ( "Settings:connectToNetwork response: " , response ) ;
}
} ) . fail ( function ( ) {
console . log ( "Settings:connectToNetwork: timeout (normal behavior)" ) ;
//clearTimeout(self.retrySaveSettingsDelay);
//self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings() },self.retryDelay); // retry after delay
} ) ;
2013-09-02 18:07:22 +02:00
} ) ;
}
2013-10-10 12:12:33 +02:00
self . setClientModeState ( SettingsWindow . CONNECTING , "" ) ;
2013-10-23 18:42:47 +02:00
2013-10-10 12:12:33 +02:00
// after switching wifi network or creating a access point we delay the status retrieval
// because the webserver needs time to switch
clearTimeout ( self . retrieveNetworkStatusDelay ) ;
self . retrieveNetworkStatusDelay = setTimeout ( function ( ) { self . retrieveNetworkStatus ( true ) } , self . retrieveNetworkStatusDelayTime ) ;
2013-09-02 18:07:22 +02:00
}
this . createAP = function ( ) {
2013-10-17 14:09:09 +02:00
console . log ( "createAP" ) ;
2013-09-02 18:07:22 +02:00
if ( communicateWithWifibox ) {
2013-10-23 18:42:47 +02:00
2013-10-17 14:09:09 +02:00
// save network related settings and on complete, create access point
2013-10-26 02:40:55 +02:00
self . saveSettings ( self . readForm ( ) , function ( success ) {
if ( ! success ) return ;
2013-10-18 13:23:50 +02:00
self . setAPModeState ( SettingsWindow . CREATING _AP ) ; // get latest substituted ssid
2013-10-17 14:09:09 +02:00
$ . ajax ( {
url : self . wifiboxCGIBinURL + "/network/openap" ,
type : "POST" ,
dataType : 'json' ,
timeout : self . timeoutTime ,
success : function ( response ) {
console . log ( "Settings:createAP response: " , response ) ;
}
} ) . fail ( function ( ) {
console . log ( "Settings:createAP: timeout (normal behavior)" ) ;
//clearTimeout(self.retrySaveSettingsDelay);
//self.retrySaveSettingsDelay = setTimeout(function() { self.saveSettings() },self.retryDelay); // retry after delay
} ) ;
2013-10-23 18:42:47 +02:00
2013-10-17 14:09:09 +02:00
self . setAPModeState ( SettingsWindow . CREATING _AP , "" ) ;
// after switching wifi network or creating a access point we delay the status retrieval
// because the webserver needs time to switch
clearTimeout ( self . retrieveNetworkStatusDelay ) ;
self . retrieveNetworkStatusDelay = setTimeout ( function ( ) { self . retrieveNetworkStatus ( true ) } , self . retrieveNetworkStatusDelayTime ) ;
2013-09-02 18:07:22 +02:00
} ) ;
}
}
2013-08-27 15:34:28 +02:00
}
/ * * * * * * * * * * * * * * * * * * * * * * * * *
*
*
* FROM DOODLE3D . INI
*
* /
//TODO: find all references to these variables, replace them and finally remove these.
var objectHeight = 20 ;
var layerHeight = . 2 ;
//var wallThickness = .5;
2013-09-07 16:06:59 +02:00
//var hop = 0;
2013-08-27 15:34:28 +02:00
//var speed = 70;
//var travelSpeed = 200;
var enableTraveling = true ;
//var filamentThickness = 2.89;
var minScale = . 3 ;
var maxScale = 1 ;
var shape = "%" ;
var twists = 0 ;
2013-09-07 16:06:59 +02:00
//var useSubLayers = true;
2013-08-27 15:34:28 +02:00
//var debug = false; // debug moved to main.js
var loglevel = 2 ;
2013-09-07 16:06:59 +02:00
//var zOffset = 0;
2013-08-27 15:34:28 +02:00
var serverport = 8888 ;
var autoLoadImage = "hand.txt" ;
var loadOffset = [ 0 , 0 ] ; // x en y ?
var showWarmUp = true ;
var loopAlways = false ;
var firstLayerSlow = true ;
var useSubpathColors = false ;
var autoWarmUp = true ;
2013-09-07 16:06:59 +02:00
//var maxObjectHeight = 150;
2013-08-27 15:34:28 +02:00
var maxScaleDifference = . 1 ;
var frameRate = 60 ;
var quitOnEscape = true ;
var screenToMillimeterScale = . 3 ; // 0.3
2013-09-07 16:06:59 +02:00
//var targetTemperature = 220;
//var simplifyiterations = 10;
//var simplifyminNumPoints = 15;
//var simplifyminDistance = 3;
//var retractionspeed = 50;
//var retractionminDistance = 5;
//var retractionamount = 3;
2013-08-27 15:34:28 +02:00
var sideis3D = true ;
var sidevisible = true ;
var sidebounds = [ 900 , 210 , 131 , 390 ] ;
var sideborder = [ 880 , 169 , 2 , 471 ] ;
var windowbounds = [ 0 , 0 , 800 , 500 ] ;
var windowcenter = true ;
var windowfullscreen = false ;
var autoWarmUpCommand = "M104 S230" ;
//var checkTemperatureInterval = 3;
var autoWarmUpDelay = 3 ;