8 use POSIX qw(strftime);
9 use Data::Dump qw(dump);
13 my $filter = 'bakaceva';
14 $filter = $ARGV[0] if @ARGV;
16 my $alert_config = "alert-$filter.txt";
17 die "no $alert_config config" unless -e $alert_config;
20 open(my $n, '<', 'subvision-numbers.txt');
23 my ( $number, $pn, $imei, $name ) = split(/ /,$_);
24 next if $name !~ m/$filter/;
25 $pn2name->{$pn} = $number;
29 strftime("%Y-%m-%d %H:%M:%S ", localtime(time()));
32 my $dbh = DBI->connect("dbi:Pg:dbname=zc","dpavlin","", { RaiseError => 1, AutoCommit => 1 });
34 $dbh->do("LISTEN zc");
35 print ts,"LISTEN zc for $filter ", join(' ', keys %$pn2name), "\n";
38 my $fd = $dbh->func("getfd");
39 my $sel = IO::Select->new($fd);
41 my $sth = $dbh->prepare( "select * from sub_$filter where pn = ? order by time desc limit 1" );
46 my $notify = $dbh->func("pg_notifies");
48 #warn "# notify = ",dump($notify);
49 my ($relname, $pid, $payload) = @$notify;
50 #print ts,"$relname from PID $pid $payload\n";
51 my ($pn, $_id) = split(/,/, $payload);
52 if ( exists $pn2name->{$pn} ) {
54 my $row = $sth->fetchrow_hashref;
55 #warn "# row = ",dump($row);
56 # FIXME hard-coded 0.1
57 if ( abs( $row->{x_axis_angle} ) > 0.1 || abs( $row->{y_axis_angle} ) > 0.1 ) {
58 if ( ! -e "alert/$pn" ) {
59 print ts,"ALERT $pn\n";
61 open(my $fh, '>', "alert/$pn");
66 my ( undef, $time ) = split(/ /,$row->{time});
67 $time =~ s{\..+$}{}; # remove decimal time
68 my $name = $pn2name->{$pn};
70 open(my $cfg, '<', $alert_config);
73 $msg =~ s/\Q{{name}}\E/$name/g;
74 $msg =~ s/\Q{{time}}\E/$time/g;
77 # consume email <email> number
81 my $number = $1 if $email =~ s{\s+0([0-9]+)$}{};
82 $number = '+385' . $number;
84 print ts,"ALERT $email\n";
86 open(my $s, '|-', '/usr/sbin/sendmail -t');
87 print $s "From: Senzori Alert <alert\@senzori.subvision.hr>\nTo: $email\nSubject: alert $name $time\n\n$msg\n\n" . dump($row) . "\n.\n";
90 my $cmd = "ssh -p 22022 -i /home/dpavlin/.ssh/sendsms_rsa asterisk\@192.168.44.212 $number $msg";
95 print ts,"SKIP ALERT $pn already sent\n";