2017-07-18 22:32:02 +00:00
angular . module ( 'appControllers' ) . controller ( 'SettingsCtrl' , SettingsCtrl ) ; // get the main module controllers set
2017-01-08 03:45:19 +00:00
SettingsCtrl . $inject = [ '$rootScope' , '$scope' , '$state' , '$location' , '$window' , '$http' ] ; // Inject my dependencies
2017-07-22 18:48:11 +00:00
2017-01-08 03:45:19 +00:00
// create our controller function with all necessary logic
function SettingsCtrl ( $rootScope , $scope , $state , $location , $window , $http ) {
$scope . $parent . helppage = 'plates/settings-help.html' ;
2017-03-05 22:47:38 +00:00
var toggles = [ 'UAT_Enabled' , 'ES_Enabled' , 'Ping_Enabled' , 'GPS_Enabled' , 'IMU_Sensor_Enabled' ,
2017-03-05 23:34:25 +00:00
'BMP_Sensor_Enabled' , 'DisplayTrafficSource' , 'DEBUG' , 'ReplayLog' , 'AHRSLog' ] ;
2017-01-08 03:45:19 +00:00
var settings = { } ;
2017-07-18 22:32:02 +00:00
for ( var i = 0 ; i < toggles . length ; i ++ ) {
2017-01-08 03:45:19 +00:00
settings [ toggles [ i ] ] = undefined ;
}
$scope . update _files = '' ;
2017-01-09 04:04:16 +00:00
2017-01-08 03:45:19 +00:00
function loadSettings ( data ) {
settings = angular . fromJson ( data ) ;
// consider using angular.extend()
$scope . rawSettings = angular . toJson ( data , true ) ;
$scope . visible _serialout = false ;
if ( ( settings . SerialOutputs !== undefined ) && ( settings . SerialOutputs !== null ) && ( settings . SerialOutputs [ '/dev/serialout0' ] !== undefined ) ) {
$scope . Baud = settings . SerialOutputs [ '/dev/serialout0' ] . Baud ;
$scope . visible _serialout = true ;
}
$scope . UAT _Enabled = settings . UAT _Enabled ;
$scope . ES _Enabled = settings . ES _Enabled ;
$scope . Ping _Enabled = settings . Ping _Enabled ;
$scope . GPS _Enabled = settings . GPS _Enabled ;
2017-04-04 17:16:32 +00:00
2017-03-05 22:47:38 +00:00
$scope . IMU _Sensor _Enabled = settings . IMU _Sensor _Enabled ;
$scope . BMP _Sensor _Enabled = settings . BMP _Sensor _Enabled ;
2017-01-08 03:45:19 +00:00
$scope . DisplayTrafficSource = settings . DisplayTrafficSource ;
$scope . DEBUG = settings . DEBUG ;
$scope . ReplayLog = settings . ReplayLog ;
2017-03-05 23:34:25 +00:00
$scope . AHRSLog = settings . AHRSLog ;
2017-04-04 17:16:32 +00:00
2017-01-08 03:45:19 +00:00
$scope . PPM = settings . PPM ;
$scope . WatchList = settings . WatchList ;
$scope . OwnshipModeS = settings . OwnshipModeS ;
$scope . DeveloperMode = settings . DeveloperMode ;
2017-07-22 18:48:11 +00:00
$scope . GLimits = settings . GLimits ;
2016-12-25 07:03:56 +00:00
$scope . StaticIps = settings . StaticIps ;
2017-07-18 22:32:02 +00:00
2017-07-22 18:48:11 +00:00
$scope . WiFiSSID = settings . WiFiSSID ;
$scope . WiFiPassphrase = settings . WiFiPassphrase ;
$scope . WiFiSecurityEnabled = settings . WiFiSecurityEnabled ;
$scope . WiFiChannel = settings . WiFiChannel . toString ( ) ;
2017-01-08 03:45:19 +00:00
}
function getSettings ( ) {
2017-01-09 04:04:16 +00:00
// Simple GET request example (note: response is asynchronous)
2017-01-08 03:45:19 +00:00
$http . get ( URL _SETTINGS _GET ) .
then ( function ( response ) {
loadSettings ( response . data ) ;
// $scope.$apply();
} , function ( response ) {
$scope . rawSettings = "error getting settings" ;
for ( i = 0 ; i < toggles . length ; i ++ ) {
settings [ toggles [ i ] ] = false ;
}
} ) ;
2017-01-09 04:04:16 +00:00
}
2017-01-08 03:45:19 +00:00
function setSettings ( msg ) {
2017-01-09 04:04:16 +00:00
// Simple POST request example (note: response is asynchronous)
2017-01-08 03:45:19 +00:00
$http . post ( URL _SETTINGS _SET , msg ) .
then ( function ( response ) {
loadSettings ( response . data ) ;
// $scope.$apply();
} , function ( response ) {
$scope . rawSettings = "error setting settings" ;
for ( i = 0 ; i < toggles . length ; i ++ ) {
settings [ toggles [ i ] ] = false ;
}
} ) ;
}
2017-07-22 18:48:11 +00:00
function isValidSSID ( str ) { return /^[!#;].|[+\[\]/"\t\s].*$/ . test ( str ) ; }
function isValidWPA ( str ) { return /^[\u0020-\u007e\u00a0-\u00ff]*$/ . test ( str ) ; }
2017-01-08 03:45:19 +00:00
getSettings ( ) ;
2017-07-18 22:32:02 +00:00
// Reset all settings from a button on the page
$scope . resetSettings = function ( ) {
getSettings ( ) ;
} ;
2017-01-08 03:45:19 +00:00
$scope . $watchGroup ( toggles , function ( newValues , oldValues , scope ) {
2017-01-09 04:04:16 +00:00
var newsettings = { } ;
2017-01-08 03:45:19 +00:00
var dirty = false ;
for ( i = 0 ; i < newValues . length ; i ++ ) {
if ( ( newValues [ i ] !== undefined ) && ( settings [ toggles [ i ] ] !== undefined ) ) {
if ( newValues [ i ] !== settings [ toggles [ i ] ] ) {
settings [ toggles [ i ] ] = newValues [ i ] ;
newsettings [ toggles [ i ] ] = newValues [ i ] ;
dirty = true ;
2017-01-09 04:04:16 +00:00
}
2017-01-08 03:45:19 +00:00
}
}
if ( dirty ) {
// console.log(angular.toJson(newsettings));
setSettings ( angular . toJson ( newsettings ) ) ;
}
} ) ;
$scope . updateppm = function ( ) {
2017-01-09 04:04:16 +00:00
settings [ "PPM" ] = 0 ;
2017-01-08 03:45:19 +00:00
if ( ( $scope . PPM !== undefined ) && ( $scope . PPM !== null ) && ( $scope . PPM !== settings [ "PPM" ] ) ) {
settings [ "PPM" ] = parseInt ( $scope . PPM ) ;
2017-07-18 22:32:02 +00:00
var newsettings = {
2017-01-08 03:45:19 +00:00
"PPM" : settings [ "PPM" ]
} ;
// console.log(angular.toJson(newsettings));
setSettings ( angular . toJson ( newsettings ) ) ;
}
} ;
$scope . updateBaud = function ( ) {
2017-01-09 04:04:16 +00:00
settings [ "Baud" ] = 0 ;
2017-01-08 03:45:19 +00:00
if ( ( $scope . Baud !== undefined ) && ( $scope . Baud !== null ) && ( $scope . Baud !== settings [ "Baud" ] ) ) {
settings [ "Baud" ] = parseInt ( $scope . Baud ) ;
2017-07-18 22:32:02 +00:00
var newsettings = {
2017-01-08 03:45:19 +00:00
"Baud" : settings [ "Baud" ]
} ;
// console.log(angular.toJson(newsettings));
setSettings ( angular . toJson ( newsettings ) ) ;
}
} ;
2016-12-25 07:03:56 +00:00
$scope . updatewatchlist = function ( ) {
if ( $scope . WatchList !== settings [ "WatchList" ] ) {
settings [ "WatchList" ] = "" ;
if ( $scope . WatchList !== undefined ) {
settings [ "WatchList" ] = $scope . WatchList . toUpperCase ( ) ;
}
2017-07-18 22:32:02 +00:00
var newsettings = {
2016-12-25 07:03:56 +00:00
"WatchList" : settings [ "WatchList" ]
} ;
// console.log(angular.toJson(newsettings));
setSettings ( angular . toJson ( newsettings ) ) ;
}
} ;
2017-05-13 00:40:53 +00:00
2017-01-08 03:45:19 +00:00
$scope . updatemodes = function ( ) {
if ( $scope . OwnshipModeS !== settings [ "OwnshipModeS" ] ) {
settings [ "OwnshipModeS" ] = $scope . OwnshipModeS . toUpperCase ( ) ;
2017-07-18 22:32:02 +00:00
var newsettings = {
2017-01-08 03:45:19 +00:00
"OwnshipModeS" : $scope . OwnshipModeS . toUpperCase ( )
} ;
// console.log(angular.toJson(newsettings));
setSettings ( angular . toJson ( newsettings ) ) ;
}
} ;
2016-12-25 07:03:56 +00:00
$scope . updatestaticips = function ( ) {
if ( $scope . StaticIps !== settings . StaticIps ) {
2017-07-18 22:32:02 +00:00
var newsettings = {
2016-12-25 07:03:56 +00:00
"StaticIps" : $scope . StaticIps === undefined ? "" : $scope . StaticIps . join ( ' ' )
} ;
// console.log(angular.toJson(newsettings));
setSettings ( angular . toJson ( newsettings ) ) ;
}
} ;
2017-07-18 22:32:02 +00:00
$scope . updateGLimits = function ( ) {
2017-05-20 11:35:40 +00:00
if ( $scope . GLimits !== settings [ "GLimits" ] ) {
settings [ "GLimits" ] = $scope . GLimits ;
2017-07-18 22:32:02 +00:00
var newsettings = {
2017-05-20 11:35:40 +00:00
"GLimits" : settings [ "GLimits" ]
} ;
2017-06-28 23:09:45 +00:00
// console.log(angular.toJson(newsettings));
2017-05-20 11:35:40 +00:00
setSettings ( angular . toJson ( newsettings ) ) ;
}
} ;
2017-04-04 17:16:32 +00:00
2017-07-18 22:32:02 +00:00
$scope . postShutdown = function ( ) {
$window . location . href = "/" ;
$location . path ( '/home' ) ;
$http . post ( URL _SHUTDOWN ) .
then ( function ( response ) {
// do nothing
// $scope.$apply();
} , function ( response ) {
// do nothing
} ) ;
2017-01-08 03:45:19 +00:00
} ;
$scope . postReboot = function ( ) {
$window . location . href = "/" ;
$location . path ( '/home' ) ;
$http . post ( URL _REBOOT ) .
then ( function ( response ) {
// do nothing
// $scope.$apply();
} , function ( response ) {
// do nothing
} ) ;
} ;
$scope . setUploadFile = function ( files ) {
$scope . update _files = files ;
$scope . $apply ( ) ;
2017-01-09 04:04:16 +00:00
} ;
2017-07-18 22:32:02 +00:00
2017-01-08 03:45:19 +00:00
$scope . resetUploadFile = function ( ) {
$scope . update _files = '' ;
$scope . $apply ( ) ;
2017-01-09 04:04:16 +00:00
} ;
2017-07-18 22:32:02 +00:00
2017-01-08 03:45:19 +00:00
$scope . uploadFile = function ( ) {
var fd = new FormData ( ) ;
//Take the first selected file
var file = $scope . update _files [ 0 ] ;
// check for empty string
if ( file === undefined || file === null ) {
2017-01-09 04:04:16 +00:00
alert ( "update file not selected" ) ;
2017-01-08 03:45:19 +00:00
return ;
}
var filename = file . name ;
// check for expected file naming convention
var re = /^update.*\.sh$/ ;
if ( ! re . exec ( filename ) ) {
2017-01-09 04:04:16 +00:00
alert ( "file does not appear to be an update" ) ;
2017-01-08 03:45:19 +00:00
return ;
}
2017-07-18 22:32:02 +00:00
2017-01-08 03:45:19 +00:00
fd . append ( "update_file" , file ) ;
$http . post ( URL _UPDATE _UPLOAD , fd , {
withCredentials : true ,
headers : {
'Content-Type' : undefined
} ,
transformRequest : angular . identity
} ) . success ( function ( data ) {
alert ( "success. wait 60 seconds and refresh home page to verify new version." ) ;
window . location . replace ( "/" ) ;
} ) . error ( function ( data ) {
alert ( "error" ) ;
} ) ;
} ;
2017-01-09 04:04:16 +00:00
$scope . setOrientation = function ( action ) {
2017-06-28 23:09:45 +00:00
// console.log("sending " + action + " message.");
2017-01-09 04:04:16 +00:00
$http . post ( URL _AHRS _ORIENT , action ) .
then ( function ( response ) {
2017-06-28 23:09:45 +00:00
// console.log("sent " + action + " message.");
2017-01-09 04:04:16 +00:00
} , function ( response ) {
// failure: cancel the calibration
2017-06-28 23:09:45 +00:00
// console.log(response.data);
2017-04-04 17:16:32 +00:00
$scope . Orientation _Failure _Message = response . data ;
2017-07-06 23:45:01 +00:00
$scope . Ui . turnOff ( 'modalCalibrateDone' ) ;
2017-04-04 17:16:32 +00:00
$scope . Ui . turnOn ( "modalCalibrateFailed" ) ;
2017-01-09 04:04:16 +00:00
} ) ;
2017-07-18 22:32:02 +00:00
} ;
2017-07-12 17:27:26 +00:00
2017-07-18 22:32:02 +00:00
$scope . calibrateGyros = function ( ) {
console . log ( "sending calibrate message." ) ;
$http . post ( URL _AHRS _CAL ) . then ( function ( response ) {
console . log ( "Sent calibrate message." ) ;
} , function ( response ) {
console . log ( response . data ) ;
$scope . Calibration _Failure _Message = response . data ;
$scope . Ui . turnOff ( "modalCalibrateGyros" ) ;
$scope . Ui . turnOn ( "modalCalibrateGyrosFailed" ) ;
2017-07-12 17:27:26 +00:00
} ) ;
} ;
2017-07-18 22:32:02 +00:00
$scope . updateWiFi = function ( action ) {
$scope . WiFiErrors = {
2017-07-22 16:43:32 +00:00
'WiFiSSID' : '' ,
'WiFiPassphrase' : '' ,
'Errors' : false
2017-07-18 22:32:02 +00:00
} ;
2017-07-22 18:48:11 +00:00
if ( ( $scope . WiFiSSID == undefined ) || ( $scope . WiFiSSID == null ) || ( $scope . WiFiSSID . length == 0 ) || ( $scope . WiFiSSID . length > 32 ) || ( isValidSSID ( $scope . WiFiSSID ) ) ) {
$scope . WiFiErrors . WiFiSSID = "Your Network Name(\"SSID\") must be at least 1 charecter but not more then 32 charecters. It cannot start with: ! , ; , # or contain: + , [ , ] , \" , or a tab" ;
$scope . WiFiErrors . Errors = true ;
}
2017-07-22 16:43:32 +00:00
if ( $scope . WiFiSecurityEnabled ) {
2017-07-22 18:48:11 +00:00
if ( ! isValidWPA ( $scope . WiFiPassphrase ) ) {
$scope . WiFiErrors . WiFiPassphrase = "Your WiFi Password, " + $scope . WiFiPassphrase + ", contains invalid charecters." ;
2017-07-22 16:43:32 +00:00
$scope . WiFiErrors . Errors = true ;
}
2017-07-22 18:48:11 +00:00
if ( $scope . WiFiPassphrase . length < 8 || $scope . WiFiPassphrase . length > 63 ) {
$scope . WiFiErrors . WiFiPassphrase = "Your WiFi Password must be at between 8 and 63 characters long." ;
2017-07-22 16:43:32 +00:00
$scope . WiFiErrors . Errors = true ;
2017-07-11 00:38:22 +00:00
}
2017-07-18 22:32:02 +00:00
}
if ( ! $scope . WiFiErrors . Errors ) {
2017-07-22 18:48:11 +00:00
newsettings = {
2017-07-18 22:32:02 +00:00
"WiFiSSID" : $scope . WiFiSSID ,
2017-07-22 18:48:11 +00:00
"WiFiSecurityEnabled" : $scope . WiFiSecurityEnabled ,
2017-07-22 16:43:32 +00:00
"WiFiPassphrase" : $scope . WiFiPassphrase ,
"WiFiChannel" : parseInt ( $scope . WiFiChannel )
2017-07-18 22:32:02 +00:00
} ;
2017-07-22 18:48:11 +00:00
console . log ( angular . toJson ( newsettings ) ) ;
2017-07-18 22:32:02 +00:00
$scope . Ui . turnOn ( "modalSuccessWiFi" ) ;
2017-07-22 18:48:11 +00:00
setSettings ( angular . toJson ( newsettings ) ) ;
2017-07-18 22:32:02 +00:00
} else {
2017-07-22 18:48:11 +00:00
$scope . Ui . turnOn ( "modalErrorWiFi" ) ;
2017-07-18 22:32:02 +00:00
}
} ;
2017-06-28 23:09:45 +00:00
}