Difference between revisions of "Foscam FI9803EP"

From ZoneMinder Wiki
Jump to navigationJump to search
(→‎Auto reboot camera when it fails: unharmful wrong date (I instead of i))
Line 53: Line 53:
<?php
<?php
set_time_limit(0);
set_time_limit(0);
/* MODIFY THIS PARAMETERS TO FIT YOU NEEDS */
/* 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', 'CAMERA_IP');
define('FOSCAM_IP', '10.0.0.11');
define('FOSCAM_PORT', 'CAMERA_PORT');
define('FOSCAM_PORT', '88');
define('FOSCAM_LOGIN', 'ADMIN RIGHTS USER');
define('FOSCAM_LOGIN', 'adm');
define('FOSCAM_PASS', 'PASSWORD');
define('FOSCAM_PASS', 'admin123');
define('RESTARTER_DIR', '/var/foscam_restarter/');
define('R_TIMEZONE', 'Europe/Warsaw');


/* ITS NOT NEEDED TO MODIFY THIS LINES UNLESS YOU KNOW WHAT ARE YOU DOING */
/* 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('INFO_FILE', '/tmp/foscam_restarter/info_file');
define('SLEEP_TIME', '120'); // sleep time in seconds
define('SLEEP_TIME', '120'); // sleep time in seconds


/* DO NOT MODIFY BELOW THIS LINE UNLESS YOU KNOW WHAT ARE YOU DOING */
/* lock file, only one instance of this script can be run */
function endscript() {
if ( is_file( LOCK_FILE ) ) {
  unlink( 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 restartCamera() {
  private function doCommand( $command, $params = array() ) {
   echo 'Sending restart command...';
    
  system('curl -s "http://' . FOSCAM_IP . ':' . FOSCAM_PORT . '/cgi-bin/CGIProxy.fcgi?cmd=rebootSystem&usr=' . FOSCAM_LOGIN . '&pwd=' . FOSCAM_PASS . '"');
      $url = $this->getCgiUrl();
  echo 'Sleeping for ' . SLEEP_TIME . ' seconds...';
      $url.= '?cmd=' . $command;
  sleep( SLEEP_TIME );
      $url.= '&usr=' . $this->login . '&pwd=' . $this->password;
  echo 'OK' . PHP_EOL;
 
}
      foreach ($params as $param_key => $param_val) {
        $url.='&' . $param_key . '=' . $param_val;
      }
 
      return shell_exec('curl -s "' . $url . '"');


function getLastFail() {
   }
   $last_fail = 'XXX';


   system('mkdir -p /tmp/foscam_restarter');
   private function getCgiUrl() {
  system('cat ' . ZM_LOG_FILE . ' | grep ' . FOSCAM_IP . ' | grep ERR | grep "Unable to open input" > /tmp/foscam_restarter/cat001');
      return 'http://' . $this->ip . ':' . $this->port . '/cgi-bin/CGIProxy.fcgi';
  $error_lines = file('/tmp/foscam_restarter/cat001');
  if ( count($error_lines) == 0 ) {
      return $last_fail;
   }
   }
  $error_lines = array_reverse( array_filter( $error_lines, 'trim' ) );
  $last_fail = trim(substr($error_lines[0], 0, 15));
  return $last_fail;


}
}


if ( is_file( LOCK_FILE ) ) {
function endscript( $success = true ) {
  if ( $success ) {
      file_put_contents( RESTARTER_DIR . 'info_file', time() );
  }
  unlink( LOCK_FILE );
   exit();
   exit();
}
}
touch( LOCK_FILE );


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 );
}
}


$last_fail = getLastFail();
system('mkdir -p ' . RESTARTER_DIR);


if ( !is_file( INFO_FILE ) ) {
if ( !is_file( RESTARTER_DIR . 'info_file' ) ) {
   file_put_contents( INFO_FILE, 'XXX' );
   /* first run, save run time and wait for next check */
  $loaded_fail = 'XXX';
  endscript();
}


} else {
system('cat ' . ZM_LOG_FILE . ' | grep ' . FOSCAM_IP . ' | grep ERR | grep "Unable to open input" > ' . RESTARTER_DIR . 'cat001');
  $loaded_fail = file_get_contents( INFO_FILE );
$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 camera reset by force fail!' . PHP_EOL;
   echo 'Forced restart!' . PHP_EOL;
  $loaded_fail = 'YYY';
}
}


if ( $loaded_fail != $last_fail ) {
if ( in_array('force',$argv) || ( $last_fail > $last_check && $last_fail2 > $last_check ) ) {
   echo date('d-M-Y H:i.s') . PHP_EOL;
   echo date('d-m-Y H:i.s') . ' - fails detected restarting!' . PHP_EOL;
   echo 'Camera fail detected (' . $last_fail . '), restarting camera...' . PHP_EOL;
   $foscam = new FoscamCamera( FOSCAM_IP, FOSCAM_PORT, FOSCAM_LOGIN, FOSCAM_PASS );
   restartCamera();
  if ( $foscam->restart() ) {
  $last_fail = getLastFail();
      echo 'Restarted. Sleeping for ' . SLEEP_TIME . ' seconds...';
   file_put_contents( INFO_FILE, $last_fail );
      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

  1. 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.