9 Replies - 2681 Views - Last Post: 22 May 2012 - 08:35 AM

#1 smilykoch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 23-April 12

Help understanding and aid translating to PHP.

Posted 20 May 2012 - 11:17 AM

Hey guys.

Could anyone of you help me understanding this script as i have NO idea about Perl but i need the outcome of this script, also i would love to be able to translate it to PHP, as that's a language i understand and furtermore because i have all my other scripts for this particular project.

#!/usr/bin/perl
# upnp_tv_agent.pl

use warnings;
use strict;

use Net::UPnP::ControlPoint;

my $upnp = Net::UPnP::ControlPoint->new();
my @devs = $upnp->search();
my $tv;

foreach my $dev ( @devs ) 
{
   foreach my $serv ( $dev->getservicelist() )
   {
      if ( $serv->getservicetype() =~ /TVAgent/ )
      {
         $tv = $serv;
         last;
      }
   }
}

die "TVAgent not found\n" unless $tv;

my $res = action( 'GetCurrentMainTVChannel' );
print "[*] $res->{CurrentChannel}\n";

$res = action( 'GetWatchingInformation' );
print "[*] $res->{TVMode}\n";
print "[*] $res->{WatchingInformation}\n";


exit 0;

# -----------------------------------------------------------------------------

sub action
{
   my ( $act, $args ) = @_;

   my $r = $tv->postaction( $act, $args );

   unless ( 200 == $r->getstatuscode() )
   {
      die "Error $act: ". $r->getstatuscode()."\n";
   }

   my $res = $r->getargumentlist();
   unless ( 'OK' eq $res->{Result} )
   {
      die "Error $act response $res->{Result}\n";
   }

   return $res;
}

# -----------------------------------------------------------------------------

__END__



Thanks in advance for now.
Mathias Koch

Is This A Good Question/Topic? 0
  • +

Replies To: Help understanding and aid translating to PHP.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,852
  • Joined: 18-April 07

Re: Help understanding and aid translating to PHP.

Posted 20 May 2012 - 11:29 AM

Well the first part is looping through Universal Plug and Play devices. For each device it is looking through its services for one that is marked as "TVAgent".

Once found, it sets a variable $tv to this service it found. The second part then interacts with this service requesting the main tv channel and then asking for watching information.

From what I know there is no way to directly do this with PHP itself, but there are projects out there written in C that PHP can then use to interface with Plug and Play devices and get control points just like is being done there in Perl.

One such project is gupnp. You can check that out and perhaps get something that will work for you through PHP.

:)
Was This Post Helpful? 1
  • +
  • -

#3 smilykoch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 23-April 12

Re: Help understanding and aid translating to PHP.

Posted 20 May 2012 - 11:39 AM

Wow, that really helped..

actually i can use it in perl if it cant really be done through PHP.. but when i run the script through xampp i get following error message:

Quote

Can't locate Net/UPnP/ControlPoint.pm in @INC (@INC contains: C:/xampp/perl/site/lib/ C:/xampp/perl/lib C:/xampp/perl/site/lib . C:/xampp/apache) at C:/xampp/htdocs/XBMC/tv.pl line 7. BEGIN failed--compilation aborted at C:/xampp/htdocs/XBMC/tv.pl line 7. ,



What is wrong?

Thanks

is it a library im missing?
Was This Post Helpful? 0
  • +
  • -

#4 smilykoch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 23-April 12

Re: Help understanding and aid translating to PHP.

Posted 20 May 2012 - 01:31 PM

Okay.. i was able to install the missing UPnP module..

but now im getting the error:

Quote

Can't call method "postaction" on an undefined value at C:/xampp/htdocs/XBMC/tv.pl line 43. ,


Why ?

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Help understanding and aid translating to PHP.

Posted 21 May 2012 - 12:50 AM

View Postsmilykoch, on 20 May 2012 - 09:31 PM, said:

Okay.. i was able to install the missing UPnP module..

but now im getting the error:

Quote

Can't call method "postaction" on an undefined value at C:/xampp/htdocs/XBMC/tv.pl line 43. ,


Why ?


Line 43 is
my $r = $tv->postaction( $act, $args );
The error message indicates that $tv is undefined, so apparently it's failing to detect/connect to any TVAgent devices... which is extremely odd, given that the program should "die" (which means exactly what is sounds like) on line 25 if it fails to connect to a TVAgent.
Was This Post Helpful? 1
  • +
  • -

#6 smilykoch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 23-April 12

Re: Help understanding and aid translating to PHP.

Posted 21 May 2012 - 12:55 AM

Hmm.. i really have no idea on how to proceed here..

1. I dont know that much about Perl..
2. I dont know ANYTHING about UPnP :whatsthat:

Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 smilykoch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 23-April 12

Re: Help understanding and aid translating to PHP.

Posted 21 May 2012 - 01:39 AM

I made it to the next step!

if i run it through cmd i get this:

Quote

c:\xampp\htdocs\XBMC>perl tv.pl
[*] <?xml version="1.0" encoding="UTF-8"?><Channel><ChType>CANALDIGITALSAT</ChType><MajorCh>105</MajorCh><MinorCh>65534</MinorCh><PTC>571</PTC<ProgNum>3206</ProgNum></Channel>
Error GetWatchingInformation response NOTOK


And if i run the .pl script though my browser/webserver (XAMPP) i also get the error:

Quote

Error GetWatchingInformation response NOTOK ,



But why?
Was This Post Helpful? 1
  • +
  • -

#8 smilykoch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 23-April 12

Re: Help understanding and aid translating to PHP.

Posted 21 May 2012 - 02:06 AM

Wooohoo! now it works..

but it seems "kinda" slow? the script takes about 2 seconds from execution to return of values? is this UPnP's fault? or is there a way of decreasing this time?

Thanks
Was This Post Helpful? 0
  • +
  • -

#9 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Help understanding and aid translating to PHP.

Posted 22 May 2012 - 01:42 AM

View Postsmilykoch, on 21 May 2012 - 10:06 AM, said:

Wooohoo! now it works..

but it seems "kinda" slow? the script takes about 2 seconds from execution to return of values? is this UPnP's fault? or is there a way of decreasing this time?


I know absolutely nothing about UPnP either, so it might be possible to adjust some settings to speed it up a little, but, based on past experience with MythTV/Tivo/random other DVRs, I'm not at all surprised that each request to the tuner is taking a second to respond. Especially if it's an analog tuner, it's probably spending most of that time sampling the video signal to see what's there before reporting on it. A digital tuner might be able to provide that information a bit faster, or it might not...
Was This Post Helpful? 0
  • +
  • -

#10 smilykoch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 23-April 12

Re: Help understanding and aid translating to PHP.

Posted 22 May 2012 - 08:35 AM

it is a digital tuner, so i guess it might not :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1