Control IP Cameras with PHP

The Script


/*
 Script By Lennar Kivistik 2012-12-15 v.01beta Deluxe :)
*/
class Camera
{
 private $adress;
 private $auth;
 private $username;
 private $password;
 private $positions;
 public $speed;

 function __construct()
 {
 $this->adress = "http://1.2.3.4:8080";
 $this->auth ="CAMPWD";
 $this->wait_until_next = 4;
 $this->Debug = 1;
 
 /*
 Preset 1 to 32 starts from 31 to 93, & time in seconds on each preset.
 Where 30 is the set preset, same for 32,34,36,38 etc.. 
 */
 $this->positions = @array('31','33','35','37'); 
 }

 public function Activate_Camera_Macro($times)
 {
 for($x=1; $x<=$times; $x++)
 { 
 echo ($this->Debug == 1 ? "This is loop nr: $x \n" : NULL); // Debug
 
 $this->y=0;
 
 foreach($this->positions as $pos)
 { 
 $this->Go_To_preset($pos, $this->wait_until_next);
 }
 }
 }

 public function Go_To_preset($position, $time)
 {
 $this->y=$this->y+1;
 
 echo ($this->Debug == 1 ? "$this->y. Position $position for $time sec. \n" : NULL); // Debug
 
 $this->Send_CMD_To_Cam("decoder_control.cgi?command=$position");
 
 sleep($time);
 }
 
 public function Set_Speed($speed)
 {
 $this->Send_CMD_To_Cam("set_misc.cgi?ptz_patrol_rate=$speed");
 }
 
 public function Toggle_Alarm($toggle, $sensitivity)
 {
 $this->Send_CMD_To_Cam("set_alarm.cgi?&motion_armed=$toggle&motion_sensitivity=$sensitivity");
 }
 
 public function Toggle_Alarm_On_Sound($toggle,$sensitivity)
 {
 $this->Send_CMD_To_Cam("set_alarm.cgi?sounddetect_armed=$toggle&sounddetect_sensitivity=$sensitivity");
 
 }
 private function Send_CMD_To_Cam($command)
 {
 $ch = curl_init("$this->adress/$command&$this->auth");
 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
 curl_exec($ch);
 }
}


$cam = new Camera;

$cam->Set_Speed(1);

$cam->Toggle_Alarm(0,6);

$cam->Toggle_Alarm_On_Sound(0,5);

$cam->Activate_Camera_Macro(1);

$cam->Go_To_preset(39, 1);

$cam->Go_To_preset(31, 3);

Posted on: Thursday 11 June 2015