Svdrp-php

Aus VDR Wiki
(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
Zeile 1: Zeile 1:
 
<pre>
 
<pre>
<?php
+
<?php
+
 
// SVDRP is a class do communicate with a vdr via svdrp
+
// SVDRP is a class do communicate with a vdr via svdrp
class SVDRP
+
class SVDRP
{
+
{
var $cfgServer;
+
var $cfgServer;
var $cfgPort;  
+
var $cfgPort;  
var $cfgTimeOut;
+
var $cfgTimeOut;
+
 
var $handle;
+
var $handle;
var $debug;
+
var $debug;
+
function SVDRP($server = "localhost", $port=2001, $timeout = 30, $debug = 1)
+
function SVDRP($server = "localhost", $port=2001, $timeout = 30, $debug = 0)
{
+
{
$this->cfgServer = $server;
+
$this->cfgServer = $server;
$this->cfgPort = $port;
+
$this->cfgPort = $port;
$this->cfgTimeOut = $timeout;
+
$this->cfgTimeOut = $timeout;
$this->debug = $debug;
+
$this->debug = $debug;
$this->handle = 0;
+
$this->handle = 0;
}
+
}
+
 
function DebugMessage($msg)
+
function DebugMessage($msg)
{
+
{
if($this->debug) echo ($msg);
+
if($this->debug) echo ($msg);
}
+
}
+
 
function Connect()
+
function Connect()
{
+
{
if($this->handle) Disconnect();
+
if($this->handle) Disconnect();
$errno = 0;
+
$errno = 0;
$errstr = "";
+
$errstr = "";
$this->handle = fsockopen($this->cfgServer, $this->cfgPort, &$errno, &$errstr, $this->cfgTimeOut);
+
$this->handle = fsockopen($this->cfgServer, $this->cfgPort, &$errno, &$errstr, $this->cfgTimeOut);
+
 
if(!$this->handle)
+
if(!$this->handle)
{
+
{
$this->DebugMessage("error $errno: $errstr");
+
$this->DebugMessage("error $errno: $errstr");
return false;
+
return false;
}
+
}
+
$this->DebugMessage("handle: $this->handle<br>\n");
+
$this->DebugMessage("handle: $this->handle<br>\n");
+
 
+
$input = fgets($this->handle,128);
+
$input = fgets($this->handle,128);
+
if(!preg_match("/^220 /", $input) || $input == "")
+
if(!preg_match("/^220 /", $input) || $input == "")
{
+
{
$this->DebugMessage("wrong welcome message: '$input'<br>\n");
+
$this->DebugMessage("wrong welcome message: '$input'<br>\n");
$this->Disconnect();
+
$this->Disconnect();
return false;
+
return false;
}
+
}
+
+
$this->DebugMessage("Welcome message: $input<br><br>\n");
+
$this->DebugMessage("Welcome message: $input<br><br>\n");
+
return true;
+
return true;
}
+
}
+
 
function Command($cmd)
+
function Command($cmd)
{
+
{
if(!$this->handle) return false;
+
if(!$this->handle) return false;
+
$ret = array();
+
$ret = array();
+
$this->DebugMessage("Kommando $cmd<br><pr"."e>");
+
$this->DebugMessage("Kommando $cmd<br><pr"."e>");
fputs($this->handle, $cmd . "\n");
+
fputs($this->handle, $cmd . "\n");
+
$s = "";
while($s = fgets($this->handle,512))
+
$nline = 0;
{
+
while($s .= fgets($this->handle,2048))
+
{
                          $this->DebugMessage($s);
+
$nline++;
preg_match("/^(\\d{3})(.)(.*)$/", $s, $data);
+
$number = $data[1];
+
                        $this->DebugMessage($s);
// TODO: Fehlernummer bearbeiten
+
if(!preg_match("/^(\\d{3})([ -])(.*)$/", $s, $data))
$ret[] = $data[3];
+
{
  if($data[2] != "-") break;  
+
continue;
   
+
}
}
+
$this->DebugMessage("</p"."re>");
+
return $ret;
+
}
+
+
$number = $data[1];
function ListChannels($numberorname="")
+
// TODO: Fehlernummer bearbeiten
{
+
$ret[] = $data[3];
if(!$this->handle) return false;
+
if($data[2] != "-" && $nline == 1) $ret =  $data[3] ;
$channels = array();
+
  if($data[2] != "-") break;
$lines = $this->Command("LSTC$numberorname");
+
$s = "";
if(!$lines) return false;
+
foreach($lines as $a => $l)
+
}
{
+
$a = split(":", $l);
+
$this->DebugMessage("</pr"."e>");
$name = $a[0];
+
return $ret;
$freq = $a[1];
+
}
$b = split(";", $name);
+
 
$name = $b[0];
+
function ListChannels($numberorname="")
if(!isset($b[1])) $b[1] = $name;
+
{
$group = $b[1];
+
if(!$this->handle) return false;
+
$channels = array();
+
$lines = $this->Command("LSTC$numberorname");
$c["name"] = $name;
+
if(!$lines) return false;
$c["group"] = $group;
+
foreach($lines as $a => $l)
$c["frequency"] = $freq;
+
{
+
$a = split(":", $l);
$channels[] = $c;
+
$name = $a[0];
+
$freq = $a[1];
+
$b = split(";", $name);
}
+
$name = $b[0];
return $channels;
+
if(!isset($b[1])) $b[1] = $name;
}
+
$group = $b[1];
+
 
function Help()
+
{
+
$c["name"] = $name;
return $this->Command("HELP");
+
$c["group"] = $group;
}
+
$c["frequency"] = $freq;
function Disconnect()
+
{
+
$channels[] = $c;
if(!$this->handle) return;
+
$this->Command("quit");
+
+
}
fclose($this->handle);
+
return $channels;
$this->handle = 0;
+
}
$this->DebugMessage("disconnected");
+
}
+
function Help()
}
+
{
   
+
return $this->Command("HELP");
  // Small Example
+
}
$a = new SVDRP();
+
function Disconnect()
$a->Connect();
+
{
print_r($a->Help());
+
if(!$this->handle) return;
print_r($a->ListChannels());
+
$this->Command("QUIT");
$a->Disconnect();
+
?>
+
fclose($this->handle);
 +
$this->handle = 0;
 +
$this->DebugMessage("disconnected");
 +
}
 +
function ClearEpg()
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("CLRE");
 +
return true;
 +
}
 +
function SwitchUp()
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("CHAN +");
 +
return true;
 +
}
 +
function SwitchDown()
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("CHAN -");
 +
return true;
 +
}
 +
function SwitchChannel($channel)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("CHAN $channel");
 +
return true;
 +
}
 +
function DeleteChannel($id)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("DELC $id");
 +
return true;
 +
}
 +
function DeleteRecord($id)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("DELR $id");
 +
return true;
 +
}
 +
function DeleteTimer($id)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("DELT $id");
 +
return true;
 +
}
 +
function GrabImage($filename, $type="jpeg", $quality="", $width="", $height)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("GRAB $filename $type $quality $width $height");
 +
return true;
 +
}
 +
function HitKey($key)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("HITK $key");
 +
return true;
 +
}
 +
function GetKeys()
 +
{
 +
if(!$this->handle) return false;
 +
 
 +
$lines = $this->Command("HITK");
 +
$keys = array();
 +
foreach($lines as $l)
 +
{
 +
if(!preg_match("/^ {4}(.*)$/", $l, $m)) continue;
 +
$keys[] = $m[1];
 +
}
 +
 +
return $keys;
 +
}
 +
function ListEPG($channel="", $time="")
 +
{
 +
if(!$this->handle) return false;
 +
$lines = $this->Command("LSTE");
 +
 +
$epg = array ();
 +
$channel = array();
 +
$event = array();
 +
 +
$channelname = "";
 +
foreach($lines as $l)
 +
{
 +
preg_match("/^(.)\\s*(.*)$/", $l, $m);
 +
$type = $m[1];
 +
$text = $m[2];
 +
switch($type)
 +
{
 +
case 'C': // Channel
 +
$channeldata = $text;
 +
 +
break;
 +
case 'E': // new Event
 +
sscanf($text, "%u %ld %d %X", $event["EventID"], $event["StartTime"], $event["Duration"], $event["TableID"]);
 +
 +
break;
 +
case 'T': // Title
 +
$event["Title"] = $text;
 +
break;
 +
case 'S': // Short text
 +
$event["Shottext"] = $text;
 +
break;
 +
case 'D': // Description
 +
$event["Desc"] = $text;
 +
break;
 +
case 'V': // VPS
 +
$event["VPS"] = $text;
 +
break;
 +
case 'e': // Event end
 +
$channel[] = $event;
 +
$event = array();
 +
 +
break;
 +
case 'c': // Channel end
 +
$epg[$channelname] = $channel;
 +
$channel = array();
 +
 +
break;
 +
}
 +
 
 +
}
 +
$channel[] = $event;
 +
 +
$epg[$channelname] = $channel;
 +
 +
 +
return $epg;
 +
}
 +
 
 +
function Message($msg)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("MESG $msg");
 +
return true;
 +
}
 +
 
 +
 +
// Volume commands
 +
function ToggleMute()
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("VOLU mute");
 +
return true;
 +
}
 +
function VolumeUp()
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("VOLU +");
 +
return true;
 +
}
 +
function VolumeDown()
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("VOLU -");
 +
return true;
 +
}
 +
function SetVolume($v)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("VOLU $v");
 +
return true;
 +
}
 +
function GetVolume()
 +
{
 +
if(!$this->handle) return false;
 +
$v = $this->Command("VOLU");
 +
if($v == "Audio is mute") return 0;
 +
if(!preg_match("/Audio volume is (.*)/", $v, $m)) return false;
 +
 +
return $m[1];
 +
}
 +
function GetDiskStat()
 +
{
 +
if(!$this->handle) return false;
 +
$stat = $this->Command("STAT DISK");
 +
sscanf($stat, "%dMB %dMB %d%%", $FreeMUsedMB, $FreeMB, $Percent);
 +
$ret["FreeMB + UsedMB"] = $FreeMUsedMB;
 +
$ret["FreeMB"] = $FreeMB;
 +
$ret["UsedMB"] = $FreeMUsedMB - $FreeMB;
 +
$ret["Percent"] = $Percent;
 +
return $ret;
 +
}
 +
function StartScan()
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("SCAN");
 +
return true;
 +
}
 +
function MoveChannel($number, $to)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("MOVC $number $to");
 +
return true;
 +
 +
}
 +
function MoveTimer($number, $to)
 +
{
 +
if(!$this->handle) return false;
 +
$this->Command("MOVT $number $to");
 +
return true;
 +
}
 +
 +
function ListRecords($n)
 +
{
 +
if(!$this->handle) return false;
 +
return $this->Command("LSTR $n");
 +
}
 +
 
 +
//TODO: Implement following commands:
 +
/*
 +
 +
LSTR LSTT  MESG  MODC  MODT
 +
NEWC NEWT  NEXT
 +
PUTE  UPDT 
 +
*/
 +
}
 +
 
 +
// Small Example
 +
echo "<pr"."e>";
 +
$a = new SVDRP();
 +
$a->Connect();
 +
print_r($a->Help());
 +
print_r($a->ListChannels());
 +
$a->GetKeys();
 +
print_r($a->GetVolume());
 +
print_r($a->GetDiskStat());
 +
$a->Disconnect();
 +
?>
 +
 
 
</pre>
 
</pre>
  
 
[[Kategorie:svdrp-anwendungen]]
 
[[Kategorie:svdrp-anwendungen]]

Version vom 11. Dezember 2004, 17:39 Uhr

<?php

// SVDRP is a class do communicate with a vdr via svdrp
class SVDRP
{
	var $cfgServer;
	var $cfgPort; 
	var $cfgTimeOut;

	var $handle;
	var $debug;
	
	function SVDRP($server = "localhost", $port=2001, $timeout = 30, $debug = 0)
	{	
		$this->cfgServer = $server;
		$this->cfgPort = $port;
		$this->cfgTimeOut = $timeout;	
		$this->debug = $debug;
		$this->handle = 0;
	} 	

	function DebugMessage($msg)
	{
		if($this->debug) echo ($msg);
	}

	function Connect()
	{
		if($this->handle) Disconnect();
		$errno = 0;
		$errstr = "";
		$this->handle = fsockopen($this->cfgServer, $this->cfgPort, &$errno, &$errstr, $this->cfgTimeOut);

		if(!$this->handle)
		{
			$this->DebugMessage("error $errno: $errstr");
			return false;
		}
		
		$this->DebugMessage("handle: $this->handle<br>\n");

		
		$input = fgets($this->handle,128);
		
		if(!preg_match("/^220 /", $input) || $input == "")
		{
			$this->DebugMessage("wrong welcome message: '$input'<br>\n");
			$this->Disconnect();
			return false;
		}
		
		
		$this->DebugMessage("Welcome message: $input<br><br>\n");
		
		return true;
	}

	function Command($cmd)
	{
		if(!$this->handle) return false;
		
		$ret = array();
		
		$this->DebugMessage("Kommando $cmd<br><pr"."e>");
		fputs($this->handle, $cmd . "\n");
		$s = "";
		$nline = 0;
		while($s .= fgets($this->handle,2048))
		{
			$nline++;
			 
                         $this->DebugMessage($s);
			 if(!preg_match("/^(\\d{3})([ -])(.*)$/", $s, $data))
			 {
			 	continue;
			 }
			 
			 
			 	
			 
			 $number = $data[1];
			 // TODO: Fehlernummer bearbeiten
			 $ret[] = $data[3];
			 if($data[2] != "-" && $nline == 1) $ret =  $data[3] ;
 			 if($data[2] != "-") break; 
			 $s = "";
			 
		}
		
		$this->DebugMessage("</pr"."e>");
		return $ret;
	}

	function ListChannels($numberorname="")
	{
		if(!$this->handle) return false;
		$channels = array();	
		$lines = $this->Command("LSTC$numberorname");
		if(!$lines) return false;
		foreach($lines as $a => $l)
		{
			$a = split(":", $l);
			$name = $a[0];
			$freq = $a[1];
			$b = split(";", $name);
			$name = $b[0];
			if(!isset($b[1])) $b[1] = $name;
			$group = $b[1];

			
			$c["name"] = $name;
			$c["group"] = $group;
			$c["frequency"] = $freq;
	
			$channels[] = $c;
		
			
		}
		return $channels;
	}
	
	function Help()
	{
		return $this->Command("HELP");
	}
	function Disconnect()
	{
		if(!$this->handle) return;
		$this->Command("QUIT");
		
		fclose($this->handle);
		$this->handle = 0;
		$this->DebugMessage("disconnected");
	}
	function ClearEpg()
	{
		if(!$this->handle) return false;
		$this->Command("CLRE");
		return true;
	}
	function SwitchUp()
	{
		if(!$this->handle) return false;
		$this->Command("CHAN +");
		return true;
	}
	function SwitchDown()
	{
		if(!$this->handle) return false;
		$this->Command("CHAN -");
		return true;
	}
	function SwitchChannel($channel)
	{
		if(!$this->handle) return false;
		$this->Command("CHAN $channel");
		return true;
	}
	function DeleteChannel($id)
	{
		if(!$this->handle) return false;
		$this->Command("DELC $id");
		return true;
	}
	function DeleteRecord($id)
	{
		if(!$this->handle) return false;
		$this->Command("DELR $id");
		return true;
	}
	function DeleteTimer($id)
	{
		if(!$this->handle) return false;
		$this->Command("DELT $id");
		return true;
	}
	function GrabImage($filename, $type="jpeg", $quality="", $width="", $height)
	{
		if(!$this->handle) return false;
		$this->Command("GRAB $filename $type $quality $width $height");
		return true;
	}
	function HitKey($key)
	{
		if(!$this->handle) return false;
		$this->Command("HITK $key");
		return true;
	}
	function GetKeys()
	{
		if(!$this->handle) return false;

		$lines = $this->Command("HITK");
		$keys = array();
		foreach($lines as $l)
		{
			if(!preg_match("/^ {4}(.*)$/", $l, $m)) continue;
			$keys[] = $m[1];
		}
		
		return $keys;
	}
	function ListEPG($channel="", $time="")
	{
		if(!$this->handle) return false;
		$lines = $this->Command("LSTE");
		 
		$epg = array ();
		$channel = array();
		$event = array();
		
		$channelname = "";
		foreach($lines as $l)
		{
			preg_match("/^(.)\\s*(.*)$/", $l, $m);
			$type = $m[1];
			$text = $m[2];
			switch($type)
			{
			case 'C': // Channel
				$channeldata = $text;
				
				break;
			case 'E': // new Event
				sscanf($text, "%u %ld %d %X", $event["EventID"], $event["StartTime"], $event["Duration"], $event["TableID"]);
								
				break;
			case 'T': // Title
				$event["Title"] = $text;
				break;
			case 'S': // Short text
				$event["Shottext"] = $text;
				break;
			case 'D': // Description
				$event["Desc"] = $text;
				break;
			case 'V': // VPS
				$event["VPS"] = $text;
				break;
			case 'e': // Event end
				$channel[] = $event;
				$event = array();
				 
				break;
			case 'c': // Channel end
				$epg[$channelname] = $channel;
				$channel = array();
				
				break;
			}

		}
		$channel[] = $event;
		 
		$epg[$channelname] = $channel;
		
		
		return $epg;
	}

	function Message($msg)
	{
		if(!$this->handle) return false;
		$this->Command("MESG $msg");
		return true;
	}

	
	// Volume commands
	function ToggleMute()
	{
		if(!$this->handle) return false;
		$this->Command("VOLU mute");
		return true;
	}
	function VolumeUp()
	{
		if(!$this->handle) return false;
		$this->Command("VOLU +");
		return true;
	}
	function VolumeDown()
	{
		if(!$this->handle) return false;
		$this->Command("VOLU -");
		return true;
	}
	function SetVolume($v)
	{
		if(!$this->handle) return false;
		$this->Command("VOLU $v");
		return true;
	}
	function GetVolume()
	{
		if(!$this->handle) return false;
		$v = $this->Command("VOLU");
		if($v == "Audio is mute") return 0;
		if(!preg_match("/Audio volume is (.*)/", $v, $m)) return false;
		
		return $m[1];
	}
	function GetDiskStat()
	{
		if(!$this->handle) return false;
		$stat = $this->Command("STAT DISK");
		sscanf($stat, "%dMB %dMB %d%%", $FreeMUsedMB, $FreeMB, $Percent); 
		$ret["FreeMB + UsedMB"] = $FreeMUsedMB;
		$ret["FreeMB"] = $FreeMB;
		$ret["UsedMB"] = $FreeMUsedMB - $FreeMB;
		$ret["Percent"] = $Percent;
		return $ret;
	}
	function StartScan()
	{
		if(!$this->handle) return false;
		$this->Command("SCAN");
		return true;
	}
	function MoveChannel($number, $to)
	{
		if(!$this->handle) return false;
		$this->Command("MOVC $number $to");
		return true;
		
	}
	function MoveTimer($number, $to)
	{
		if(!$this->handle) return false;
		$this->Command("MOVT $number $to");
		return true;
	}
	
	function ListRecords($n)
	{
		if(!$this->handle) return false;
		return $this->Command("LSTR $n");
	}

	//TODO: Implement following commands:
	/*
	
	LSTR  LSTT  MESG  MODC  MODT
	NEWC  NEWT  NEXT
	PUTE  UPDT   
	*/
}

// Small Example
echo "<pr"."e>";
$a = new SVDRP();
$a->Connect();
print_r($a->Help());
print_r($a->ListChannels());
$a->GetKeys();
print_r($a->GetVolume());
print_r($a->GetDiskStat());
$a->Disconnect();
?>