Subversion Repositories HomeAutomation

Rev

Rev 518 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #!/usr/bin/perl -w
  2. use IO::Socket;
  3. use integer;
  4. use threads;
  5.  
  6. #for sending time msg: set to 1, else set to 0
  7. $sendtime = 0;
  8.  
  9. #for printing timestamps on printed messages: set to 1, else set to 0
  10. $timestamp = 1;
  11.  
  12. #define filters for id of messages, passed messages will be printed on stdout
  13. @acceptmask_ones =  (0x00080000, 0x00240000, 0x00040000, 0x00280000);
  14. @acceptmask_zeros = (0xfff70000, 0xffdb0000, 0xfffb0000, 0xffd70000);
  15. @denymask_ones = ();
  16. @denymask_zeros = ();
  17.  
  18. @input = ();
  19. @output = ();
  20.  
  21. #define makros, when an input sting is sent on can this perl-script will send the corresponding output string
  22.  
  23. # Remote button 1 => desktoplamp toggle
  24. push(@input , "PKT 0409000a 1 0 00 00 05 01");
  25. push(@output, "PKT 08008c00 1 0 03");
  26.  
  27. # Remote button 4 down => blinds darker
  28. push(@input , "PKT 0409000a 1 0 00 00 05 04");
  29. push(@output, "PKT 08010200 1 0 00 03");
  30.  
  31. # Remote button 4 up => blinds stop
  32. push(@input , "PKT 0409000a 1 0 0f 00 05 04");
  33. push(@output, "PKT 08010200 1 0 00 04");
  34.  
  35. # Remote button 6 down => blinds lighter
  36. push(@input , "PKT 0409000a 1 0 00 00 05 06");
  37. push(@output, "PKT 08010200 1 0 80 03");
  38.  
  39. # Remote button 6 up => blinds stop
  40. push(@input , "PKT 0409000a 1 0 0f 00 05 06");
  41. push(@output, "PKT 08010200 1 0 00 04");
  42.  
  43.  
  44. $remote = IO::Socket::INET->new(
  45.                     Proto    => "tcp",
  46.                     PeerAddr => "localhost",
  47.                     PeerPort => "1200",
  48.                 )
  49.                or die "cannot connect to port 1200 at localhost";
  50.  
  51. if ($sendtime == 1) {
  52.     $thr = threads->new(\&sendTime);
  53.     $thr->detach;
  54. }
  55.  
  56. $acceptmask_ones = @acceptmask_ones;
  57. $acceptmask_zeros = @acceptmask_zeros;
  58. if ($acceptmask_ones != $acceptmask_zeros) {
  59.     print "Acceptmasks must be same length\n";
  60.     exit;
  61. }
  62.  
  63. $denymask_ones = @denymask_ones;
  64. $denymask_zeros = @denymask_zeros;
  65. if ($denymask_ones != $denymask_zeros) {
  66.     print "Denymasks must be same length\n";
  67.     exit;
  68. }
  69.  
  70. #get the lengths of the makro arrays:
  71. $input = @input;
  72. $output = @output;
  73. if ($input != $output) {
  74.     print "Input and output makros must be same length\n";
  75.     exit;
  76. }
  77.  
  78. #read line from socket (blocking)
  79. while ( $line = <$remote> ) {
  80.     if (length($line) > 12) {
  81.         $id = hex(substr($line, 4, 8));
  82.        
  83.         #macro function
  84.         for ($i = 0; $i < $input; $i++) {
  85.             if ($input[$i] eq substr($line, 0, length($input[$i]))) {
  86.                 print $remote $output[$i];
  87.             }
  88.         }
  89.        
  90.         $printed = 0;
  91.         $denied = 0;
  92.         #filter function
  93.         for ($i = 0; $i < $acceptmask_ones && !$printed && !$denied; $i++) {
  94.             if ((($id & $acceptmask_ones[$i])==$acceptmask_ones[$i]) && (($id & $acceptmask_zeros[$i])==0)) {
  95.                 #check if denied by $denymask
  96.                 for ($j = 0; $j < $denymask_ones; $j++) {
  97.                     if ((($id & $denymask_ones[$j])==$denymask_ones[$j]) && (($id & $denymask_zeros[$j])==0)) {
  98.                         #this message fits the denymask
  99.                         $denied = 1;
  100.                     }
  101.                    
  102.                 }
  103.                
  104.                 if (!$denied) {
  105.                     #print timestamp
  106.                     if ($timestamp == 1) {
  107.                         print localtime()." ";
  108.                     }
  109.                     print $line;
  110.                    
  111.                     $printed = 1;
  112.                 }
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118. sub sendTime {
  119.     $remote = IO::Socket::INET->new(
  120.                     Proto    => "tcp",
  121.                     PeerAddr => "localhost",
  122.                     PeerPort => "1200",
  123.                 )
  124.                or die "cannot connect to port 1200 at localhost";
  125.    
  126.     while (1) {
  127.         select(undef, undef, undef, 0.5);
  128.         print $remote "PKT 00000000 1 0";
  129.     }
  130. }
  131.