Difference between revisions of "Foscam FI9803EP"
(→Auto reboot camera when it fails: unharmful wrong date (I instead of i)) |
(→Auto reboot camera when it fails: new script version) |
||
Line 53: | Line 53: | ||
<?php | <?php | ||
set_time_limit(0); | set_time_limit(0); | ||
/* MODIFY | /* MODIFY FOR YOUR NEEDS */ | ||
define('ZM_LOG_FILE', '/var/log/zm/zm.log'); | define('ZM_LOG_FILE', '/var/log/zm/zm.log'); | ||
define('FOSCAM_IP', ' | define('FOSCAM_IP', '10.0.0.11'); | ||
define('FOSCAM_PORT', ' | define('FOSCAM_PORT', '88'); | ||
define('FOSCAM_LOGIN', ' | define('FOSCAM_LOGIN', 'adm'); | ||
define('FOSCAM_PASS', ' | define('FOSCAM_PASS', 'admin123'); | ||
define('RESTARTER_DIR', '/var/foscam_restarter/'); | |||
define('R_TIMEZONE', 'Europe/Warsaw'); | |||
/* | /* DONT MODIFY BELOW THIS LINE UNLESS YOU KNOW WHAT ARE YOU ARE DOING */ | ||
define('LOCK_FILE', '/tmp/foscam_restarter/lock'); | define('LOCK_FILE', '/tmp/foscam_restarter/lock'); | ||
define('SLEEP_TIME', '120'); // sleep time in seconds | define('SLEEP_TIME', '120'); // sleep time in seconds | ||
/* | /* lock file, only one instance of this script can be run */ | ||
if ( is_file( LOCK_FILE ) ) { | |||
exit(); | exit(); | ||
} | } | ||
touch( LOCK_FILE ); | |||
class FoscamCamera { | |||
private $ip; | |||
private $port; | |||
private $login; | |||
private $password; | |||
public function __construct( $ip, $port, $login, $password ) { | |||
$this->ip = $ip; | |||
$this->port = $port; | |||
$this->login = $login; | |||
$this->password = $password; | |||
} | |||
public function restart() { | |||
$return = $this->doCommand('rebootSystem'); | |||
if ( str_replace('<result>0</result>', '', $return) != $return) { | |||
return true; | |||
} | |||
return false; | |||
} | |||
function | private function doCommand( $command, $params = array() ) { | ||
$url = $this->getCgiUrl(); | |||
$url.= '?cmd=' . $command; | |||
$url.= '&usr=' . $this->login . '&pwd=' . $this->password; | |||
foreach ($params as $param_key => $param_val) { | |||
$url.='&' . $param_key . '=' . $param_val; | |||
} | |||
return shell_exec('curl -s "' . $url . '"'); | |||
} | |||
private function getCgiUrl() { | |||
return 'http://' . $this->ip . ':' . $this->port . '/cgi-bin/CGIProxy.fcgi'; | |||
} | } | ||
} | } | ||
if ( | function endscript( $success = true ) { | ||
if ( $success ) { | |||
file_put_contents( RESTARTER_DIR . 'info_file', time() ); | |||
} | |||
unlink( LOCK_FILE ); | |||
exit(); | exit(); | ||
} | } | ||
if ( !is_file( ZM_LOG_FILE ) ) { | if ( !is_file( ZM_LOG_FILE ) ) { | ||
echo('Cannot find ZoneMinder Log File!!'); | echo('Cannot find ZoneMinder Log File!!'); | ||
endscript(); | endscript( false ); | ||
} | } | ||
system('mkdir -p ' . RESTARTER_DIR); | |||
if ( !is_file( | if ( !is_file( RESTARTER_DIR . 'info_file' ) ) { | ||
/* first run, save run time and wait for next check */ | |||
endscript(); | |||
} | |||
system('cat ' . ZM_LOG_FILE . ' | grep ' . FOSCAM_IP . ' | grep ERR | grep "Unable to open input" > ' . RESTARTER_DIR . 'cat001'); | |||
$error_lines = file( RESTARTER_DIR . 'cat001'); | |||
if ( count($error_lines) < 2 ) { | |||
// no action needed | |||
endscript(); | |||
} | } | ||
$error_lines = array_reverse( array_filter( $error_lines, 'trim' ) ); | |||
$last_fail = new DateTime( trim( substr($error_lines[0], 0, 15) ), new DateTimeZone(R_TIMEZONE) ); | |||
$last_fail2 = new DateTime( trim( substr($error_lines[1], 0, 15) ), new DateTimeZone(R_TIMEZONE) ); | |||
$last_check_pure = file_get_contents( RESTARTER_DIR . 'info_file' ); | |||
$last_check = new DateTime(); | |||
$last_check->setTimezone( new DateTimeZone(R_TIMEZONE) ); | |||
$last_check->setTimestamp( $last_check_pure ); | |||
if (in_array('force',$argv)) { | if (in_array('force',$argv)) { | ||
echo 'Forced | echo 'Forced restart!' . PHP_EOL; | ||
} | } | ||
if ( $ | if ( in_array('force',$argv) || ( $last_fail > $last_check && $last_fail2 > $last_check ) ) { | ||
echo date('d- | echo date('d-m-Y H:i.s') . ' - fails detected restarting!' . PHP_EOL; | ||
echo ' | $foscam = new FoscamCamera( FOSCAM_IP, FOSCAM_PORT, FOSCAM_LOGIN, FOSCAM_PASS ); | ||
if ( $foscam->restart() ) { | |||
echo 'Restarted. Sleeping for ' . SLEEP_TIME . ' seconds...'; | |||
sleep( SLEEP_TIME ); | |||
echo 'OK' . PHP_EOL; | |||
} else { | |||
echo 'Cannot restart camera, check camera IP, PORT, LOGIN and PASSWORD. Provided user must have rights to reboot camera.' . PHP_EOL; | |||
endscript(false); | |||
} | |||
} | } | ||
endscript(); | endscript(); | ||
</pre> | </pre> | ||
Revision as of 15:04, 23 August 2015
Check also informations made for Foscam_FI9805W
Support only Windows while configuring - camera software need plugin which is provided as exe installer.
- System Firmware Version 1.5.1.11
- Application Firmware Version 2.22.1.143
Currently i am using this camera settings:
Off Enhanced Vision (it always slow down when IR ON to 5fps) User-defined Resolution 720P Bit Rate 4M Frame rate 15 Key Frame Interval 10 Variable bitrate Yes
And Zoneminder settings:
Source type: FFmpeg Source: rtsp://[LOGIN]:[PASSWORD]@10.0.0.11:88/videoMain Remote method: RTP/RTSP Capture width: 800 Capture height: 450
I think 720P resolution for this camera is interpolated, so i decided to decrease capture width & height to 800x450.
If you dont care about image size (and quality...) use capture width/height 1280 / 720.
Differences between FI9805W
Issues with RTSP stream also is here. There is no option 920P for this camera, use 720P while configuring via camera menu.
Commands
- Rebooting VIA Command line
http://[CAMERA_IP]:[CAMERA_PORT]/cgi-bin/CGIProxy.fcgi?cmd=rebootSystem&usr=[USERNAME]&pwd=[PASSWORD]
RTSP stream can hang, but API will still work fine. You can reboot camera by using URL provided above.
Auto reboot camera when it fails
Script is written in PHP (i assume You have PHP installed on Your server) Tested on Ubuntu 14.04.3 LTS
1. Paste script in /bin/foscam_restarter.php
#!/usr/bin/php <?php set_time_limit(0); /* MODIFY FOR YOUR NEEDS */ define('ZM_LOG_FILE', '/var/log/zm/zm.log'); define('FOSCAM_IP', '10.0.0.11'); define('FOSCAM_PORT', '88'); define('FOSCAM_LOGIN', 'adm'); define('FOSCAM_PASS', 'admin123'); define('RESTARTER_DIR', '/var/foscam_restarter/'); define('R_TIMEZONE', 'Europe/Warsaw'); /* DONT MODIFY BELOW THIS LINE UNLESS YOU KNOW WHAT ARE YOU ARE DOING */ define('LOCK_FILE', '/tmp/foscam_restarter/lock'); define('SLEEP_TIME', '120'); // sleep time in seconds /* lock file, only one instance of this script can be run */ if ( is_file( LOCK_FILE ) ) { exit(); } touch( LOCK_FILE ); class FoscamCamera { private $ip; private $port; private $login; private $password; public function __construct( $ip, $port, $login, $password ) { $this->ip = $ip; $this->port = $port; $this->login = $login; $this->password = $password; } public function restart() { $return = $this->doCommand('rebootSystem'); if ( str_replace('<result>0</result>', '', $return) != $return) { return true; } return false; } private function doCommand( $command, $params = array() ) { $url = $this->getCgiUrl(); $url.= '?cmd=' . $command; $url.= '&usr=' . $this->login . '&pwd=' . $this->password; foreach ($params as $param_key => $param_val) { $url.='&' . $param_key . '=' . $param_val; } return shell_exec('curl -s "' . $url . '"'); } private function getCgiUrl() { return 'http://' . $this->ip . ':' . $this->port . '/cgi-bin/CGIProxy.fcgi'; } } function endscript( $success = true ) { if ( $success ) { file_put_contents( RESTARTER_DIR . 'info_file', time() ); } unlink( LOCK_FILE ); exit(); } if ( !is_file( ZM_LOG_FILE ) ) { echo('Cannot find ZoneMinder Log File!!'); endscript( false ); } system('mkdir -p ' . RESTARTER_DIR); if ( !is_file( RESTARTER_DIR . 'info_file' ) ) { /* first run, save run time and wait for next check */ endscript(); } system('cat ' . ZM_LOG_FILE . ' | grep ' . FOSCAM_IP . ' | grep ERR | grep "Unable to open input" > ' . RESTARTER_DIR . 'cat001'); $error_lines = file( RESTARTER_DIR . 'cat001'); if ( count($error_lines) < 2 ) { // no action needed endscript(); } $error_lines = array_reverse( array_filter( $error_lines, 'trim' ) ); $last_fail = new DateTime( trim( substr($error_lines[0], 0, 15) ), new DateTimeZone(R_TIMEZONE) ); $last_fail2 = new DateTime( trim( substr($error_lines[1], 0, 15) ), new DateTimeZone(R_TIMEZONE) ); $last_check_pure = file_get_contents( RESTARTER_DIR . 'info_file' ); $last_check = new DateTime(); $last_check->setTimezone( new DateTimeZone(R_TIMEZONE) ); $last_check->setTimestamp( $last_check_pure ); if (in_array('force',$argv)) { echo 'Forced restart!' . PHP_EOL; } if ( in_array('force',$argv) || ( $last_fail > $last_check && $last_fail2 > $last_check ) ) { echo date('d-m-Y H:i.s') . ' - fails detected restarting!' . PHP_EOL; $foscam = new FoscamCamera( FOSCAM_IP, FOSCAM_PORT, FOSCAM_LOGIN, FOSCAM_PASS ); if ( $foscam->restart() ) { echo 'Restarted. Sleeping for ' . SLEEP_TIME . ' seconds...'; sleep( SLEEP_TIME ); echo 'OK' . PHP_EOL; } else { echo 'Cannot restart camera, check camera IP, PORT, LOGIN and PASSWORD. Provided user must have rights to reboot camera.' . PHP_EOL; endscript(false); } } endscript();
2. Modify first five params to fit Your needs, and save file
3. Make it executable
# chmod +x /bin/foscam_restarter.php
4. OPTIONAL You can check if its working properly with (it will restart your camera!!)
# foscam_restarter.php force
5. Make log file for restarter
# touch /var/log/foscam_restarter.log
6. Add script to crontab
# echo "*/1 * * * * root /bin/foscam_restarter.php >> /var/log/foscam_restarter.log" > /etc/cron.d/foscam_restarter # echo "" >> /etc/cron.d/foscam_restarter
7. Restart cron
# service cron restart
8. If You have logrotate on zoneminder logs, modify logrotate script - add cleanup of /tmp/foscam_restarter/info_file (defined as INFO_FILE) For example, script for logrotate with restarter support:
/var/log/zm/*.log { daily missingok rotate 31 compress delaycompress sharedscripts postrotate /usr/bin/zmpkg.pl logrot 2> /dev/null > /dev/null || true /bin/rm -f /tmp/foscam_restarter/info_file endscript }
9. Probably its done, unless You have other custom modifications.