############################################################################## # # 70_STV.pm # # a module to send messages or commands to a Samsung TV # for example a LE40B650 # # written 2012 by Gabriel Bentele > # # $Id$ # # Version = 1.1 # ############################################################################## # # define STV # # set # # where is one of mute, volume, call, sms, date # examples: # set mute on # set volume 20 # set call Peter 1111111 Klaus 222222Peter 1111111 Klaus 222222 # set sms Peter 1111111 Klaus 222222 das ist der text # set date 2012-12-10 18:07:04 Peter 11111 Bier 2012-12-11 23:59:20 Paulaner # ############################################################################## package main; use IO::Socket::INET; my @gets = ('dummy'); sub STV_Initialize($) { my ($hash) = @_; $hash->{DefFn} = "STV_Define"; $hash->{StateFn} = "STV_SetState"; $hash->{SetFn} = "STV_Set"; $hash->{AttrList} = "loglevel:0,1,2,3,4,5"; } sub STV_SetState($$$$) { my ($hash, $tim, $vt, $val) = @_; $val = $1 if($val =~ m/^(.*) \d+$/); return "Undefined value $val" if(!defined($it_c2b{$val})); return undef; } sub STV_Define($$) { my ($hash, $def) = @_; my @args = split("[ \t]+", $def); if (int(@args) < 2) { return "energy_Define: too much arguments. Usage:\n" . "define STV "; } $hash->{Host} = $args[2]; $hash->{STATE} = 'Initialized'; Log 3, "sub define2 with host: $hash->{Host}"; return undef; } sub connection($$) { my $tmp = shift ; Log 4, "connection message: $tmp"; my $TV = shift; my $buffer = ""; my $tmp2 = ""; my $sock = new IO::Socket::INET ( PeerAddr => $TV, PeerPort => '52235', Proto => 'tcp', Timout => 5 ); if (defined ($sock)){ print $sock $tmp; my $buff =""; while ((read $sock, $buff, 1) > 0){ $buffer .= $buff; } @tmp2 = split (/\n/,$buffer); Log 3, "$TV response: $tmp2[0]"; Log 4, "$TV buffer response: $buffer"; $sock->close(); Log 4, "$TV: socket closed"; }else{ Log 4, "$TV: not able to close socket"; } } sub STV_Set($@) { my ($hash, @a) = @_; my $name = $hash->{NAME}; my $TV = $hash->{Host}; my $count = @a; my $arg = lc($a[1]); # mute volume my $cont1 = ucfirst($arg); # Mute my $cont2 = ""; my $cont3 = ""; my $cont4 = ""; my $cont5 = ""; my $cont6 = ""; my $cont7 = ""; my $cont8 = ""; my $cont9 = ""; if (defined $a[2]) { $cont2 = $a[2]} if (defined $a[3]) { $cont3 = $a[3]} if (defined $a[4]) { $cont4 = $a[4]} if (defined $a[5]) { $cont5 = $a[5]} if (defined $a[6]) { $cont6 = $a[6]} if (defined $a[7]) { $cont7 = $a[7]} if (defined $a[8]) { $cont8 = $a[8]} if (defined $a[9]) { $cont9 = $a[9]} my $callsoap = ""; my $message = ""; my $head = ""; my $kind = 0; my $size = ""; my $body = ""; if ( $arg eq "mute" ) { $kind = 1; if ( $cont2 eq "off" ){ $cont2 = 0 ; }else { $cont2 = 1 ; } } if ( $arg eq "volume") { if ( $cont2 > 0 and $cont2 < 100 ){ $kind = 1; }else { Log 3, "$name Volume: not correct"; $kind = 0; } } if ( $arg eq "call") { $kind = 2; } if ( $arg eq "sms") { $kind = 3; for my $i (6..$count){ $body .= $a[$i]; $body .= " "; } } if ( $arg eq "date") { $kind = 4; for my $i (10..$count){ $body .= $a[$i]; $body .= " "; } } if ( $kind eq 1){ $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "0\r\n"; $callsoap .= "$cont2\r\n"; $callsoap .= "Master\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $size = length($callsoap); $head .= "POST /upnp/control/RenderingControl1 HTTP/1.1\r\n"; $head .= "Content-Type: text/xml; charset=\"utf-8\"\r\n"; $head .= "SOAPACTION: \"SoapAction:urn:schemas-upnp-org:service:RenderingControl:1#Set$cont1\"\r\n"; $head .= "Cache-Control: no-cache\r\n"; $head .= "Host: $TV:52235\r\n"; $head .= "Content-Length: $size\r\n"; $head .= "Connection: Close\r\n"; $head .= "\r\n"; $message .= $head; $message .= $callsoap; } my $calldate=`date +"%Y-%m-%d"`; chomp($calldate); my $calltime=`date +"%H:%M:%S"`; chomp($calltime); if ( $kind eq 2 ){ # CALL $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "text/xml\r\n"; $callsoap .= "1334799348\r\n"; $callsoap .= "\r\n"; $callsoap .= "<Category>Incoming Call</Category>\r\n"; $callsoap .= "<DisplayType>Maximum</DisplayType>\r\n"; $callsoap .= "<CallTime>\r\n"; $callsoap .= "<Date>$calldate</Date>\r\n"; $callsoap .= "<Time>$calltime</Time>\r\n"; $callsoap .= "</CallTime>\r\n"; $callsoap .= "<Callee>\r\n"; $callsoap .= "<Name>An: $cont4</Name>\r\n"; $callsoap .= "<Number>Nr: $cont5</Number>\r\n"; $callsoap .= "</Callee>\r\n"; $callsoap .= "<Caller>\r\n"; $callsoap .= "<Name>Von: $cont2</Name>\r\n"; $callsoap .= "<Number>Nr: $cont3</Number>\r\n"; $callsoap .= "</Caller>\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $size = length($callsoap); $head .= "POST /PMR/control/MessageBoxService HTTP/1.1\r\n"; $head .= "Content-Type: text/xml; charset=\"utf-8\"\r\n"; $head .= "SOAPACTION: \"urn:samsung.com:service:MessageBoxService:1#AddMessage\"\r\n"; $head .= "Cache-Control: no-cache\r\n"; $head .= "Host: $TV:52235\r\n"; $head .= "Content-Length: $size\r\n"; $head .= "Connection: Close\r\n"; $head .= "\r\n"; $message .= $head; $message .= $callsoap; } if ( $kind eq 3 ){ # SMS $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "text/xml\r\n"; $callsoap .= "1334799348\r\n"; $callsoap .= "\r\n"; $callsoap .= "<Category>SMS</Category>\r\n"; $callsoap .= "<DisplayType>Maximum</DisplayType>\r\n"; $callsoap .= "<ReceiveTime>\r\n"; $callsoap .= "<Date>$calldate</Date>\r\n"; $callsoap .= "<Time>$calltime</Time>\r\n"; $callsoap .= "</ReceiveTime>\r\n"; $callsoap .= "<Receiver>\r\n"; $callsoap .= "<Name>An: $cont4</Name>\r\n"; $callsoap .= "<Number>Nr: $cont5</Number>\r\n"; $callsoap .= "</Receiver>\r\n"; $callsoap .= "<Sender>\r\n"; $callsoap .= "<Name>Von: $cont2</Name>\r\n"; $callsoap .= "<Number>Nr: $cont3</Number>\r\n"; $callsoap .= "</Sender>\r\n"; $callsoap .= "<Body>Inhalt: $body</Body>\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $size = length($callsoap); $head .= "POST /PMR/control/MessageBoxService HTTP/1.1\r\n"; $head .= "Content-Type: text/xml; charset=\"utf-8\"\r\n"; $head .= "SOAPACTION: \"urn:samsung.com:service:MessageBoxService:1#AddMessage\"\r\n"; $head .= "Cache-Control: no-cache\r\n"; $head .= "Host: $TV:52235\r\n"; $head .= "Content-Length: $size\r\n"; $head .= "Connection: Close\r\n"; $head .= "\r\n"; $message .= $head; $message .= $callsoap; } if ( $kind eq 4 ){ # Termin $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "text/xml\r\n"; $callsoap .= "1334799348\r\n"; $callsoap .= "\r\n"; $callsoap .= "<Category>Schedule Reminder</Category>\r\n"; $callsoap .= "<DisplayType>Maximum</DisplayType>\r\n"; $callsoap .= "<StartTime>\r\n"; $callsoap .= "<Date>$cont2</Date>\r\n"; $callsoap .= "<Time>$cont3</Time>\r\n"; $callsoap .= "</StartTime>\r\n"; $callsoap .= "<Owner>\r\n"; $callsoap .= "<Name>Fr: $cont4</Name>\r\n"; $callsoap .= "<Number>Nr: $cont5</Number>\r\n"; $callsoap .= "</Owner>\r\n"; $callsoap .= "<Subject>Betreff: $cont6</Subject>\r\n"; $callsoap .= "<EndTime>\r\n"; $callsoap .= "<Date>$cont7</Date>\r\n"; $callsoap .= "<Time>$cont8</Time>\r\n"; $callsoap .= "</EndTime>\r\n"; $callsoap .= "<Location>Ort: $cont9</Location>\r\n"; $callsoap .= "<Body>Inhalt: $body</Body>\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $callsoap .= "\r\n"; $size = length($callsoap); $head .= "POST /PMR/control/MessageBoxService HTTP/1.1\r\n"; $head .= "Content-Type: text/xml; charset=\"utf-8\"\r\n"; $head .= "SOAPACTION: \"urn:samsung.com:service:MessageBoxService:1#AddMessage\"\r\n"; $head .= "Cache-Control: no-cache\r\n"; $head .= "Host: $TV:52235\r\n"; $head .= "Content-Length: $size\r\n"; $head .= "Connection: Close\r\n"; $head .= "\r\n"; $message .= $head; $message .= $callsoap; } if ( $kind ne 0 ){ connection($message, $TV); }else{ return "Unknown argument $name, choose one of mute volume call sms date"; } }