2014-05-23 19:09:46 +02: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 ( w ) {
var _page ;
var _statusField ;
var _descriptionField ;
var _updateAPI = new UpdateAPI ( ) ;
var _configAPI = new ConfigAPI ( ) ;
var _pageData = { } ;
var _formData = { } ;
var _installing = false ;
var UPDATED _REDIRECT _DELAY = 5000 ;
var _updatedRedirectDelay ;
2014-05-26 17:36:21 +02:00
var _no _retain ;
2014-05-23 19:09:46 +02:00
var PAGE _ID = "#updating" ;
var _self = this ;
$ . mobile . document . on ( "pageinit" , PAGE _ID , function ( event , data ) {
//console.log(PAGE_ID+":pageinit");
_page = $ ( this ) ;
_statusField = _page . find ( "#status" ) ;
_descriptionField = _page . find ( "#description" ) ;
} ) ;
$ . mobile . document . on ( "pagebeforeshow" , PAGE _ID , function ( event , data ) {
2014-05-26 17:36:21 +02:00
//console.log(PAGE_ID+":pagebeforeshow");
2014-05-23 19:09:46 +02:00
_pageData = d3d . util . getPageParams ( PAGE _ID ) ;
var form = data . prevPage . find ( "form" ) ;
// check if there are url params and
// a form from a prev page
if ( _pageData === undefined ||
form . length === 0 ) {
$ . mobile . changePage ( "#boxes" ) ;
return ;
}
_formData = d3d . util . getFormData ( form ) ;
2014-05-26 17:36:21 +02:00
//console.log(" _formData: ",_formData);
2014-05-23 19:09:46 +02:00
var boxURL = "http://" + _pageData . localip ;
_updateAPI . init ( boxURL ) ;
_updateAPI . refreshing = onRefreshing ;
_updateAPI . updated = onStatusUpdated ;
downloadUpdate ( ) ;
_updateAPI . startAutoRefresh ( ) ;
} ) ;
$ . mobile . document . on ( "pagebeforehide" , PAGE _ID , function ( event , data ) {
//console.log(PAGE_ID+":pagebeforehide");
_updateAPI . stopAutoRefresh ( ) ;
clearTimeout ( _updatedRedirectDelay ) ;
} ) ;
function downloadUpdate ( ) {
2014-05-26 17:36:21 +02:00
//console.log(PAGE_ID+":downloadUpdate");
2014-05-23 19:09:46 +02:00
_updateAPI . download ( ) ;
}
function installUpdate ( ) {
2014-05-26 17:36:21 +02:00
//console.log(PAGE_ID+":installUpdate");
//console.log(" _formData: ",_formData);
_no _retain = ( _formData . no _retain ) ? true : false ;
//console.log(" no_retain: ",_no_retain);
_updateAPI . install ( _no _retain ) ;
2014-05-23 19:09:46 +02:00
_installing = true ;
}
function onRefreshing ( ) {
//console.log("ConnectingToNetworkPage:onRefreshing");
d3d . util . showLoader ( true ) ;
}
function onStatusUpdated ( data ) {
2014-05-26 17:36:21 +02:00
//console.log(PAGE_ID+": onStatusUpdated ");
//console.log(" state_code: ",data.state_code," text: ",data.state_text);
updatePage ( data ) ;
2014-05-23 19:09:46 +02:00
switch ( data . state _code ) {
case UpdateAPI . STATUS . IMAGE _READY :
if ( ! _installing ) {
installUpdate ( ) ;
}
break ;
2014-05-26 17:36:21 +02:00
case UpdateAPI . STATUS . INSTALLED :
2015-06-17 14:47:49 +02:00
_installing = false ;
2014-05-26 17:36:21 +02:00
_updateAPI . stopAutoRefresh ( ) ;
clearTimeout ( _updatedRedirectDelay ) ;
_updatedRedirectDelay = setTimeout ( function ( ) {
if ( _no _retain ) {
//console.log(" redirect to boxes");
$ . mobile . changePage ( "#boxes" ) ;
} else {
//console.log(" redirect to box");
2014-05-23 19:09:46 +02:00
// replace this page with boxes page in history
window . history . replaceState ( null , "" , "#boxes" ) ;
2014-05-26 17:36:21 +02:00
var link = d3d . util . replaceURLParameters ( "#box" , _pageData ) ;
2014-05-23 19:09:46 +02:00
$ . mobile . changePage ( link ) ;
2014-05-26 17:36:21 +02:00
}
} , UPDATED _REDIRECT _DELAY ) ;
2014-05-23 19:09:46 +02:00
break ;
2015-06-17 14:47:49 +02:00
case UpdateAPI . STATUS . INSTALL _FAILED :
_installing = false ;
break ;
2014-05-23 19:09:46 +02:00
}
}
function updatePage ( data ) {
console . log ( PAGE _ID + ": updatePage state: " , data . state _code ) ;
var status = "" ;
switch ( data . state _code ) {
case UpdateAPI . STATUS . DOWNLOADING :
status = "Downloading update..." ;
break ;
case UpdateAPI . STATUS . DOWNLOAD _FAILED :
status = "Downloading update failed" ;
break ;
case UpdateAPI . STATUS . IMAGE _READY :
status = "Update downloaded" ;
break ;
case UpdateAPI . STATUS . INSTALLING :
status = "Installing update... " ;
break ;
case UpdateAPI . STATUS . INSTALLED :
status = "Update complete!" ;
break ;
case UpdateAPI . STATUS . INSTALL _FAILED :
status = "Installing update failed" ;
break ;
}
console . log ( " status: " , status ) ;
_statusField . text ( status ) ;
// description
var description = "" ;
switch ( data . state _code ) {
case UpdateAPI . STATUS . INSTALLING :
description = "Do not remove power from the WiFi-Box." ;
2014-05-26 17:36:21 +02:00
if ( _no _retain ) {
description += "<br/><small>Because you didn’ t preserve your personal sketches and settings you will need to reconnect your WiFi-Box to your WiFi network. <br/>After an estimated update time you will be redirected to the boxes page. When it does, please connect your device to the WiFi network of the WiFi-Box and return to the boxes page. </small>" ;
}
2014-05-23 19:09:46 +02:00
break ;
case UpdateAPI . STATUS . DOWNLOAD _FAILED :
case UpdateAPI . STATUS . INSTALL _FAILED :
description = data . state _text ;
break ;
}
console . log ( " description: " , description ) ;
2014-05-26 17:36:21 +02:00
_descriptionField . html ( description ) ;
2014-05-23 19:09:46 +02:00
}
} ) ( window ) ;