Perl RESTful url device toggle script

Posted on
Fri Feb 05, 2010 4:46 pm
chrisla23 offline
Posts: 97
Joined: Sep 16, 2009

Perl RESTful url device toggle script

I use this little script called from LIRC, which I trigger with a buttons on a harmony remote to a hacked appletv/ubuntu box. It toggles a device off and on. It used to have some ugly text manipulation to extract whether the device was on or not from the .txt URLs. Since the new version of Indigo supports XML, I re-wrote to use XML. I sort of think toggle should be baked into the server (isOn=2 ?) for URL access, but it works until then.

I thought maybe someone could use it, or as an example of perl/restful/xml.

Code: Select all
#!/usr/bin/perl

# toggle.pl 'light name in indigo'

use Net::HTTP;
use XML::Simple;

my $s = Net::HTTP->new(Host => "10.0.0.32:8176", KeepAlive => "TRUE") || die $@;
my $xml = new XML::Simple;

$s->write_request(GET => "/devices/$ARGV[0].xml", 'User-Agent' => "Mozilla/5.0");
my($code, $mess, %h) = $s->read_response_headers;


my $n = $s->read_entity_body($buf, 1024);

$data = $xml->XMLin("$buf");

   if ("$data->{'isOn'}" eq "True") {
   $s->write_request(GET => "/devices/$ARGV[0]?isOn=0&_method=put", 'User-Agent' => "Mozilla/5.0");
   }
   else {
   $s->write_request(GET => "/devices/$ARGV[0]?isOn=1&_method=put", 'User-Agent' => "Mozilla/5.0");
   }

Posted on
Fri Feb 05, 2010 5:47 pm
berkinet offline
User avatar
Posts: 3297
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Perl RESTful url device toggle script

Interesting. In your code it looks like you don't have a password set, or if you do, how did you deal with that?

In order to manage the authentication issues I ended up using a call to libcurl. It looks something like:
Code: Select all
use WWW::Curl::Easy;

my $curl= new WWW::Curl::Easy;
    $curl->setopt(CURLOPT_VERBOSE,0);
    $curl->setopt(CURLOPT_NOBODY,1);
    $curl->setopt(CURLOPT_TIMEOUT_MS,200);
    $curl->setopt(CURLOPT_HTTPAUTH,2);
    $curl->setopt(CURLOPT_USERNAME,$iuser);
    $curl->setopt(CURLOPT_PASSWORD,$ipass);
$curl or die "no http link :$!";
...
...
$site = "$iurl/variables/alarmStatus?value=Armed_STAY&_method=put";
$curl->setopt(CURLOPT_URL, $site);
$curl->perform;
It would be interesting to compare speeds between libcurl and the http mod. I initially used a system call to "curl" but testing showed that after 5 o6 6 calls the libcurl call was faster.

Posted on
Fri Feb 05, 2010 6:31 pm
chrisla23 offline
Posts: 97
Joined: Sep 16, 2009

Re: Perl RESTful url device toggle script

Always fun how there are a million ways to do the same thing. Indeed, no passwords, probably a touch crazy/lazy. In my old version, I did use curl partly via system calls.

Typical Old: `time`
real 0m1.992s
user 0m0.104s
sys 0m0.028s

Typical new:
real 0m0.310s
user 0m0.260s
sys 0m0.024s

I'd guess that the whole round trip from when I press the remote button to when the light turns off is maybe a 10th of a second more on top of that.

-Chris

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests