Subversion Repositories HomeAutomation

Rev

Rev 518 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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