Subversion Repositories HomeAutomation

Rev

Rev 482 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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