diff --git a/www/ajax/editip.php b/www/ajax/editip.php index fdb641d8..7b4a6bae 100644 --- a/www/ajax/editip.php +++ b/www/ajax/editip.php @@ -20,9 +20,26 @@ public function postData() $id = intval($_POST['id']); $camera = new ipCamera($id); $result = $camera->edit($_POST); - data::responseJSON($result[0], $result[1]); + $camera = new ipCamera($id); // Reinstance the object to update information + $camera->checkConnection(); // Test the connection + data::responseJSON($result[0], $result[1], array( + json_encode($camera->info['connection_status']), + str_replace("%TYPE%", str_replace("IP-", "", $camera->info['protocol']), + $camera->info['connection_status']['success'] ? AIP_CONNECTION_SUCCESS : AIP_CONNECTION_FAIL) + )); exit(); } -} - + public function postTest() + { + $id = intval($_POST['id']); + $camera = new ipCamera($id); + $camera->checkConnection(); + data::responseJSON(10, "", array( + json_encode($camera->info['connection_status']), + str_replace("%TYPE%", str_replace("IP-", "", $camera->info['protocol']), + $camera->info['connection_status']['success'] ? AIP_CONNECTION_SUCCESS : AIP_CONNECTION_FAIL) + )); + exit(); + } +} \ No newline at end of file diff --git a/www/ajax/ipcameracheck.php b/www/ajax/ipcameracheck.php index 2de8c7dd..dfb04d47 100644 --- a/www/ajax/ipcameracheck.php +++ b/www/ajax/ipcameracheck.php @@ -44,13 +44,9 @@ private function initProc() $camera->checkConnection(); - $status_message = ''; - foreach($camera->info['connection_status'] as $type => $status){ - if ($status!='OK'){ - $status_message .= str_replace('%TYPE%', $type, constant('IP_ACCESS_STATUS_'.$status)).'

'; - } - }; - + $status_message = str_replace("%TYPE%", str_replace("IP-", "", $camera->info['protocol']), + $camera->info['connection_status']['success'] ? AIP_CONNECTION_SUCCESS : AIP_CONNECTION_FAIL); + echo $status_message; } } diff --git a/www/lib/lang.php b/www/lib/lang.php index 544da49a..bcd029ad 100644 --- a/www/lib/lang.php +++ b/www/lib/lang.php @@ -384,6 +384,10 @@ define('AIP_CHECK_ONVIF_SUCCESS', 'Successfull'); define('AIP_CHECK_ONVIF_ERROR', 'Unsuccessful'); define('AIP_LIMIT_ALLOWED_DEVICES', 'Could not add a camera, because exceeds the limit of the allowed devices.'); +define('AIP_TEST_CONNECTION', 'Test Connection'); +define('AIP_TEST_CONNECTION_MESSAGE', 'Test connection to IP Camera'); +define('AIP_CONNECTION_SUCCESS', 'Connection Successful using %TYPE%'); +define('AIP_CONNECTION_FAIL', 'Connection Unsuccessful using %TYPE%'); # HLS configuration define('AIP_HLS_WINDOW_SIZE', 'HLS window size'); diff --git a/www/lib/lib.php b/www/lib/lib.php index 18b3094f..ec398bcd 100644 --- a/www/lib/lib.php +++ b/www/lib/lib.php @@ -767,20 +767,37 @@ public function getInfo($id){ $this->ptzControl = new cameraPtz($this); } } - public function checkConnection(){ + public function checkConnection() { + //FIXME: Currently only supports RTSP. MJPEG is currently untested ini_set('default_socket_timeout', 1); - #needs server to check for RTSP //// $paths['rtsp'] = 'http://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').$this->info['ipAddr'].':'.$this->info['port'].$this->info['rtsp']; - $paths['mjpeg'] = 'http://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').((empty($this->info['ipAddrMjpeg'])) ? $this->info['ipAddr'] : $this->info['ipAddrMjpeg']).':'.$this->info['portMjpeg'].$this->info['mjpeg_path']; - $paths['http'] = 'http://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').((empty($this->info['ipAddrMjpeg'])) ? $this->info['ipAddr'] : $this->info['ipAddrMjpeg']); - foreach($paths as $type => $path){ - $headers = @get_headers($path); - $contents = @file_get_contents($path); - if (!$headers) { $this->info['connection_status'][$type] = 'F'; continue; } - preg_match("/([0-9]{3})/", $headers[0], $response_code); - $this->info['connection_status'][$type] = ($response_code[0]=='200') ? 'OK' : $response_code[0]; + + $path = ""; + $args = ""; + + switch($this->info['protocol']) { + case 'IP-RTSP': + $path = 'rtsp://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').$this->info['ipAddr'].':'.$this->info['port'].$this->info['rtsp']; + $args = array("-rtsp_flags +prefer_tcp", "-rtsp_transport tcp", "-rtsp_transport tcp")[$this->info['rtsp_rtp_prefer_tcp']]; + break; + case 'IP-MJPEG': + //FIXME: This is the old logic for testing MJPEG. Testing for MJPEG is currently not supported by the bundled ffprobe method used for RTSP + $path = 'http://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').((empty($this->info['ipAddrMjpeg'])) ? $this->info['ipAddr'] : $this->info['ipAddrMjpeg']).':'.$this->info['portMjpeg'].$this->info['mjpeg_path']; + $headers = @get_headers($path); + if (!$headers) { $this->info['connection_status']['success'] = false; return; } + preg_match("/([0-9]{3})/", $headers[0], $response_code); + $this->info['connection_status']['success'] = ($response_code[0]=='200') ? true : false; + return; } - return $this->info['connection_status']; + + //-> '-stimeout' is measured in microseconds + $ffprobe_output = shell_exec( + "/usr/lib/bluecherry/ffprobe -stimeout 5000000 -hide_banner -show_format -show_streams -print_format json ".$args. " " . escapeshellarg($path)); + + $rtsp_data = json_decode($ffprobe_output, true); + + $this->info['connection_status']['success'] = array_key_exists('streams', $rtsp_data) ? count($rtsp_data['streams']) > 0 : false; } + protected static function autoConfigure($driver, $info){ #auto configure known cameras include_once("ipcamlib.php"); $result = false; @@ -857,6 +874,7 @@ public function edit($data){ #if there were no errors, edit the camera $query = data::formQueryFromArray('update', 'Devices', $data[1], 'id', $this->info['id']); $result = data::query($query, true); + return array($result, false); } public static function create($rawData){ diff --git a/www/template/ajax/editip.php b/www/template/ajax/editip.php index ad9fab61..25044f0d 100644 --- a/www/template/ajax/editip.php +++ b/www/template/ajax/editip.php @@ -14,13 +14,27 @@
-
- - - -
-
- +
+
+ +
+
+
+ + + + + + + + +
+
+
+ +
+ +
@@ -196,18 +210,16 @@ info['debug_level']==1) ? 'checked' : ''; ?> />
-
- +
- -
+
- - - +
diff --git a/www/template/dist/js/editip.js b/www/template/dist/js/editip.js new file mode 100644 index 00000000..ae6f09d5 --- /dev/null +++ b/www/template/dist/js/editip.js @@ -0,0 +1,16 @@ +$(function() { +}); +function testCameraConnection(form, msg) { + let data = JSON.parse(msg.data[0]); + + $('#connStat').find('form').show(); + if(data.success) + $('#connStat').removeClass().addClass('alert alert-success').find('span').html(msg.data[1]); + else + $('#connStat').removeClass().addClass('alert alert-danger').find('span').html(msg.data[1]); +} + +function testCameraLoadingHeader(form, msg) { + $('#connStat').removeClass().addClass('alert alert-warning').find('span').html('Testing...'); + $('#connStat').find('form').hide(); +} \ No newline at end of file diff --git a/www/template/main_admin.php b/www/template/main_admin.php index a627dce7..93fa26e1 100644 --- a/www/template/main_admin.php +++ b/www/template/main_admin.php @@ -322,6 +322,7 @@ +