This code is from project: Raspberry pi and android simple remote control
server.xml
<homeControl> <B1 name="On/OFF"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 3 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B1> <B2 name="VolUp"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 1 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B2> <B3 name="VolDown"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 2 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B3> <B4 name="Mute"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 0 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B4> <B5 name="Play"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 4 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B5> <B6 name="Next"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 5 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B6> <B7 name="Prev"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 6 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B7> <B8 name="Stop"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 7 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B8> <B9 name="Portable"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 8 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B9> <B10 name="CD"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 9 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B10> <B11 name="USB"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 10 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B11> <B12 name="Tuner"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 11 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B12> <B13 name="PstPrev"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 12 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B13> <B14 name="PstNext"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 13 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B14> <B15 name="FldUp"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 14 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B15> <B16 name="FldDown"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 15 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B16> <B17 name="Shuffle"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 16 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B17> <B18 name="Repeat"> <get>echo "OFF"</get> <set> <on>i2cset -y 1 0x10 17 ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B18> <!-- B19 --> <!-- B20 --> <B21 name="Raspberry Halt"> <get>echo "OFF"</get> <set> <on>sudo halt ; echo "OFF"</on> <off>echo "OFF"</off> </set> </B21> <default name="None"> <get>echo "OFF"</get> </default> </homeControl>
server.pl
#!/usr/bin/perl use strict; use warnings; use IO::Socket; use XML::Simple; use Data::Dumper; $|++; # load xml configuration into hash '$hc' my $hc = XMLin('server.xml'); # create and bind socket to IP/PORT my $sock = new IO::Socket::INET ( LocalHost => '192.168.1.104', LocalPort => '8000', Proto => 'tcp', Listen => 1, Reuse => 1, ) or die "Could not create socket: $!\n"; # set sock options $sock->autoflush(1); while(1) { # if client arrives my $con = $sock->accept(); # if client send some data (parse line by line) while( defined ( my $data = <$con>) ) { # cut out carriage return and new line sign $data =~ s/\r|\n//g; print "\n" if ($data=~/GET PING/); # print on stdout what was received from android print "DEBUG: ", $data, "\n"; # split into variables my ($cmd, $mod, $obj, $val) = split(/ /, $data); # if it's PING, send PONG if ($cmd=~/GET/) { if ($mod=~/PING/) { print $con "PONG"; next; } } # button found in configuration (XML) found flag my $found = 0; # step through all defined butons foreach my $button ( keys $hc) { # $rec holds all info about desired button my $rec = $hc->{$button}; # match object (from android) with button name # (from XML config) if ($obj eq $button) { # set button found flag $found = 1; # if action is GET data, then if ($cmd=~/GET/) { # action is get name (description) if ($mod=~/NAME/) { # send name to socket (to android) print $con $rec->{name}; next; } # action is get state if ($mod=~/STATE/) { # run command from config (XML) and # fetch its result (not return code) to scket print $con `$rec->{get}`; next; } } # if actionis SET data from android if ($cmd=~/SET/) { # we'll be setting stae of the button if ($mod=~/STATE/) { # button-on request if ($val=~/true/) { # run command from config (XML) and # fetch its result (not return code) to scket print $con `$rec->{set}->{on}`; # button will change its state as is # in command result next; } # button-on request if ($val=~/false/) { # like above print $con `$rec->{set}->{off}`; next; } } } } } # button not found in config, so we fetch # default desciptin and states from config (XML) if ($found == 0) { if ($cmd=~/GET/) { if ($mod=~/NAME/) { print $con $hc->{default}->{name}; next; } if ($mod=~/STATE/) { print $con `$hc->{default}->{get}`; next; } } if ($cmd=~/SET/) { if ($mod=~/STATE/) { print $con `$hc->{default}->{get}`; next; } } } } } close($sock);
No comments:
Post a Comment