forked from ZoneMinder/zoneminder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmonvif-trigger.pl.in
693 lines (582 loc) · 19.6 KB
/
zmonvif-trigger.pl.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#!@PERL_EXECUTABLE@ -w
#
# ==========================================================================
#
# ZoneMinder ONVIF Event Watcher Script
# Copyright (C) Jan M. Hochstein
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
# This module contains the implementation of the ONVIF event watcher
#
## SETUP:
## chmod g+rw /dev/shm/zm*
## chgrp users /dev/shm/zm*
## systemctl stop iptables
## DEBUG:
## hexdump -x -s 0 -n 600 /dev/shm/zm.mmap.1
## rm -rf /srv/www/html/zm/events/[...]
##
use strict;
use bytes;
use Carp;
use Data::Dump qw( dump );
use DBI;
use Getopt::Long qw(:config no_auto_abbrev no_ignore_case bundling);
use IO::Select;
use POSIX qw( EINTR );
use Time::HiRes qw( usleep );
use SOAP::Lite; # +trace;
use SOAP::Transport::HTTP;
use Data::UUID;
use ZoneMinder;
require ONVIF::Client;
require WSNotification::Elements::Header;
require WSNotification::Interfaces::WSBaseNotificationSender::NotificationProducerPort;
require WSNotification::Interfaces::WSBaseNotificationSender::SubscriptionManagerPort;
require WSNotification::Types::TopicExpressionType;
# ========================================================================
# Constants
# in seconds
use constant SUBSCRIPTION_RENEW_INTERVAL => 60; #3600;
use constant SUBSCRIPTION_RENEW_EARLY => 5; # 60;
use constant MONITOR_RELOAD_INTERVAL => 3600;
# ========================================================================
# Globals
my $verbose = 0;
my $script;
my $daemon_pid;
my $monitor_reload_time = 0;
# this does not work on all architectures
my @EXTRA_SOCK_OPTS = (
'ReuseAddr' => '1',
# 'ReusePort' => '1',
# 'Blocking' => '0',
);
# =========================================================================
# signal handling
sub handler { # 1st argument is signal name
my ($sig) = @_;
# Error("Caught a SIG$sig --daemon_pid = $daemon_pid shutting down");
Info("Caught a SIG$sig ;;; daemon_pid = $daemon_pid");
if ( $daemon_pid != 0 ) {
Info( "killing child $daemon_pid \n") ;
kill(15 ,$daemon_pid) ;
Info(" sent kill signal to child , now waitpid...") ;
waitpid($daemon_pid, 0);
confess();
}
Info("Exiting ..") ;
exit(0);
}
$SIG{INT} = \&handler;
$SIG{HUP} = \&handler;
$SIG{QUIT} = \&handler;
$SIG{TERM} = \&handler;
$SIG{__DIE__} = \&handler;
# =========================================================================
# Debug
#use Data::Hexdumper qw(hexdump);
#use Devel::Peek;
sub dump_mapped {
my ($monitor) = @_;
if ( !zmMemVerify($monitor) ) {
print "Error: Mapped memory not accessible\n";
}
my $mmap = $monitor->{MMap};
print 'Size: '.$ZoneMinder::Memory::mem_size ."\n";
printf("Mapped at %x\n", $monitor->{MMapAddr});
# Dump($mmap);
if ( $mmap && $$mmap ) {
#print hexdump(
#data => $$mmap, # what to dump
#output_format => ' %4a : %S %S %S %S %S %S %S %S : %d',
## start_position => 336, # start at this offset ...
#end_position => 400 # ... and end at this offset
#);
}
}
#push @EXPORT, qw(dump_mapped);
# =========================================================================
# internal methods
sub xs_duration {
use integer;
my ($seconds) = @_;
my $s = $seconds % 60;
$seconds /= 60;
my $m = $seconds % 60;
$seconds /= 60;
my $h = $seconds % 24;
$seconds /= 24;
my $d = $seconds;
my $str;
if($d > 0) {
$str = "P". $d;
}
else {
$str = "P";
}
$str = $str . "T";
if($h > 0) {
$str = $str . $h . "H";
}
if($m > 0) {
$str = $str . $m . "M";
}
if($s > 0) {
$str = $str . $s . "S";
}
return $str;
}
# =========================================================================
### ZoneMinder integration
## make this a singleton ?
{
package _ZoneMinder;
use strict;
use bytes;
use base qw(Class::Std::Fast);
use DBI;
use Encode qw(decode encode);
use Time::HiRes qw( usleep );
use ZoneMinder;
# my %monitors = ();
my $dbh;
my %monitors_of; # :ATTR(:name<monitors> :default<{}>);
sub init {
$dbh = zmDbConnect();
}
sub monitors {
my ($self) = @_;
return $monitors_of{ident $self}?%{ $monitors_of{ident $self} }:undef;
}
sub set_monitors {
my ($self, %monitors_par) = @_;
$monitors_of{ident $self} = \%monitors_par;
}
## TODO: remember to refresh this in all threads (daemon is forked)
sub loadMonitors {
my ($self) = @_;
Info("Loading monitors");
$monitor_reload_time = time();
my %new_monitors = ();
my $sql = "SELECT * FROM Monitors WHERE Capturing != 'None' AND ONVIF_URL != ''";
my $sth = $dbh->prepare_cached($sql) or Fatal("Can't prepare '$sql': ".$dbh->errstr());
my $res = $sth->execute() or Fatal("Can't execute: ".$sth->errstr());
while ( my $monitor = $sth->fetchrow_hashref() ) {
if ( !defined $script ) {
if ( !zmMemVerify($monitor) ) { # Check shared memory ok
zmMemInvalidate($monitor);
next if !zmMemVerify($monitor);
}
}
Info('Monitor URL: '.$monitor->{ONVIF_URL});
Info("In parent process ;;; child PID = $daemon_pid");
## set up ONVIF client for monitor
next if ! $monitor->{ONVIF_URL};
my $soap_version;
if ( $monitor->{ONVIF_Options} =~ /SOAP1([12])/ ) {
$soap_version = "1.$1";
} else {
$soap_version = '1.1';
}
my $client = ONVIF::Client->new( {
url_svc_device => $monitor->{ONVIF_URL},
soap_version => $soap_version } );
if ( $monitor->{ONVIF_Username} ) {
$client->set_credentials($monitor->{ONVIF_Username}, $monitor->{ONVIF_Password}, 0);
}
$client->create_services();
$monitor->{onvif_client} = $client;
$new_monitors{$monitor->{Id}} = $monitor;
} # end foreach monitor
$self->set_monitors(%new_monitors);
} # end foreach db monitor
sub freeMonitors {
my ($self) = @_;
my %monitors = $self->monitors();
foreach my $monitor ( values %monitors ) {
# Free up any used memory handle
zmMemInvalidate($monitor);
}
}
sub eventOn {
my ($self, $monitorId, $score, $cause, $text, $showtext) = @_;
# Info( "Trigger '$trigger'\n" );
Info("On: $monitorId, $score, $cause, $text, $showtext");
my %monitors = $self->monitors();
my $monitor = $monitors{$monitorId};
if ( defined $script ) {
# eval {
system($script, 'On', $monitor->{Name}, $monitor->{Path}, $cause);
# this goes to "stopped" in ffmpeg when executed from shell - why?
# }
} else {
# encode() ensures that no utf-8 is written to mmap'ed memory.
zmTriggerEventOn($monitor, $score, encode('utf-8', $cause), encode('utf-8', $text));
zmTriggerShowtext($monitor, encode('utf-8', $showtext)) if defined($showtext);
# main::dump_mapped($monitor);
}
}
sub eventOff {
my ($self, $monitorId, $score, $cause, $text, $showtext) = @_;
Info("Off: $monitorId, $score, $cause, $text, $showtext");
my %monitors = $self->monitors();
my $monitor = $monitors{$monitorId};
if ( defined $script ) {
# eval {
system($script, 'Off', $monitor->{Name}, $monitor->{Path}, $cause);
# }
} else {
my $last_event = zmGetLastEvent($monitor);
zmTriggerEventOff($monitor);
# encode() ensures that no utf-8 is written to mmap'ed memory.
zmTriggerShowtext($monitor, encode('utf-8', $showtext) ) if defined($showtext);
# Info( "Trigger '$trigger'\n" );
# Wait til it's finished
while ( zmInAlarm($monitor) && ($last_event == zmGetLastEvent($monitor)) ) {
# Tenth of a second
usleep(100000);
}
zmTriggerEventCancel($monitor);
# main::dump_mapped($monitor);
}
}
} # end package _ZoneMinder
# =========================================================================
### (experimental) send email
sub send_picture_email {
# 'ffmpeg -i "rtsp://admin:[email protected]:554/Streaming/Channels/1?transportmode=mcast&profile=Profile_1" -y -frames 1 -vf scale=1024:-1 /tmp/pic2.jpg'
}
# =========================================================================
### Consumer for Notify messages
$SOAP::Constants::DO_NOT_CHECK_MUSTUNDERSTAND = 1;
## make this a singleton ?
{
package _Consumer;
use strict;
use bytes;
use base qw(Class::Std::Fast SOAP::Server::Parameters);
use ZoneMinder;
my $zm;
sub BUILD {
my ($self, $ident, $arg_ref) = @_;
# $zm_of{$ident} = check_name( $arg_ref->{zm} );
$zm = $arg_ref->{zm};
}
#
# called on http://docs.oasis-open.org/wsn/bw-2/NotificationConsumer/Notify
#
sub Notify {
my ($self, $unknown, $som) = @_;
Debug('### Notify');
my $req = $som->context->request;
# Data::Dump::dump($req);
my $id = 0;
if ( $req->uri->path =~ m|/ref_(.*)/| ) {
$id = $1;
} else {
Warning('Unknown URL '.$req->uri->path.' called by event');
return ();
}
# Data::Dump::dump($som);
my $action = $som->valueof('/Envelope/Header/Action');
Debug(' Action = '.$action);
my $msg = $som->match('/Envelope/Body/Notify/NotificationMessage');
my $topic = $msg->valueof('Topic');
my $msg2 = $msg->match('Message/Message');
# Data::Dump::dump($msg2->current());
my $time = $msg2->dataof()->attr->{'UtcTime'};
my (%source, %data);
foreach my $item ($msg2->dataof('Source/SimpleItem')) {
$source{$item->attr->{Name}} = $item->attr->{Value};
# print $item->attr->{Name} ."=>". $item->attr->{Value} ."\n";
}
foreach my $item ($msg2->dataof('Data/SimpleItem')) {
$data{$item->attr->{Name}} = $item->attr->{Value};
}
Debug("Ref=$id, Topic=$topic, $time, Rule=$source{Rule}, isMotion=$data{IsMotion}");
if ( lc($data{IsMotion}) eq 'true' ) {
$zm->eventOn($id, 100, $source{Rule}, $time);
} elsif ( lc($data{IsMotion}) eq 'false' ) {
$zm->eventOff($id, 100, $source{Rule}, $time);
}
return ();
}
} # end Consumer
# =========================================================================
sub daemon_main {
my ($daemon) = @_;
# $daemon->handle();
# improve responsiveness with multiple clients (cameras)
my $d = $daemon->{_daemon};
my $select = IO::Select->new();
$select->add($d);
while ($select->count() ) {
my @ready = $select->can_read(); # blocks
foreach my $connection (@ready) {
if ( $connection == $d ) {
# on the daemon accept and add the connection
my $client = $connection->accept();
$select->add($client);
} else {
# it's a client connection
my $request = $connection->get_request();
if ( $request ) {
# process the request (taken from SOAP::Transport::HTTP::Daemon->handle() )
$daemon->request($request);
$daemon->SOAP::Transport::HTTP::Server::handle();
eval {
local $SIG{PIPE} = sub { print("SIGPIPE\n") }; # die?
$connection->send_response( $daemon->response );
};
if ( $@ && $@ !~ /^SIGPIPE/ ) {
print($@); # die?
}
} else {
# connection was closed by the client
$select->remove($connection);
$connection->close(); # is this necessary?
}
} # end if new connection or existing
} # end foreach connection
} # end while select->count
} # end sub daemon_main
sub start_daemon {
my ($localip, $localport, $zm) = @_;
### daemon
my $daemon = SOAP::Transport::HTTP::Daemon->new(
LocalAddr => $localip,
LocalPort => $localport,
# 'deserializer' => $deserializer,
@EXTRA_SOCK_OPTS
);
## handling
# we only handle one method
$daemon->on_dispatch( sub {
return ( 'http://docs.oasis-open.org/wsn/bw-2/NotificationConsumer', 'Notify' );
});
$daemon_pid = fork();
die "fork() failed: $!" unless defined $daemon_pid;
if ( $daemon_pid ) {
# this is a new process --> use new name and log file
$0 = $0.' [http-daemon]';
logInit(id => 'zmonvif-trigger-httpd');
logSetSignal();
# $zm is copied and the mmap'ed regions still exist
my $consumer = _Consumer->new({zm => $zm});
$daemon->dispatch_with({
# "http://docs.oasis-open.org/wsn/bw-2" => $consumer,
'http://docs.oasis-open.org/wsn/bw-2/NotificationConsumer' => $consumer,
});
daemon_main($daemon);
} else {
return $daemon;
}
} # end sub start_daemon
require WSNotification::Elements::Subscribe;
require WSNotification::Types::EndpointReferenceType;
#require WSNotification::Types::ReferenceParametersType;
#require WSNotification::Elements::Metadata;
require WSNotification::Types::FilterType;
require WSNotification::Elements::TopicExpression;
require WSNotification::Elements::MessageContent;
require WSNotification::Types::AbsoluteOrRelativeTimeType;
require WSNotification::Types::AttributedURIType;
sub subscribe {
my ($client, $localaddr, $topic_str, $duration, $ref_id) = @_;
# for debugging:
# $client->get_endpoint('events')->no_dispatch(1);
my $result = $client->get_endpoint('events')->Subscribe( {
ConsumerReference => { # WSNotification::Types::EndpointReferenceType
Address => { value => 'http://' . $localaddr . '/ref_'. $ref_id . '/' },
# ReferenceParameters => { # WSNotification::Types::ReferenceParametersType
# },
# Metadata => { # WSNotification::Types::MetadataType
# },
},
Filter => { # WSNotification::Types::FilterType
TopicExpression => { # WSNotification::Types::TopicExpressionType
xmlattr => {
Dialect => "http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet",
},
value => $topic_str,
},
# MessageContent => { # WSNotification::Types::QueryExpressionType
# },
},
InitialTerminationTime => xs_duration($duration), # AbsoluteOrRelativeTimeType
# SubscriptionPolicy => {
# },
},,
);
die $result if not $result;
### build Subscription Manager
my $submgr_addr = $result->get_SubscriptionReference()->get_Address()->get_value();
Info("Subscription Manager at $submgr_addr");
my $serializer = $client->service('device', 'ep')->get_serializer();
my $submgr_svc = WSNotification::Interfaces::WSBaseNotificationSender::SubscriptionManagerPort->new({
serializer => $serializer,
proxy => $submgr_addr,
},
);
return $submgr_svc;
} # end sub subscribe
sub unsubscribe {
my ($submgr_svc) = @_;
$submgr_svc->Unsubscribe( { },, );
}
sub renew {
my ($submgr_svc, $duration) = @_;
my $endpoint=$submgr_svc->get_endpoint();
my $uuid_gen = Data::UUID->new();
my $uuid = $uuid_gen->create_str();
my $result = $submgr_svc->Renew( {
TerminationTime => xs_duration($duration), # AbsoluteOrRelativeTimeType
},
WSNotification::Elements::Header->new({
Action => { value => 'http://docs.oasis-open.org/wsn/bw-2/SubscriptionManager/RenewRequest' },
MessageID => { value => "urn:uuid:$uuid" },
To => { value => "$endpoint" },
# ReplyTo => { Address => { value => "http://www.w3.org/2005/08/addressing/anonymous" }},
})
);
die $result if not $result;
}
sub events {
my ($localip, $localport) = @_;
my $zm = _ZoneMinder->new();
$zm->init();
$zm->loadMonitors(); # call before fork()
my %monitors = $zm->monitors();
my $monitor_count = scalar keys(%monitors);
if ( $monitor_count == 0 ) {
Warning('No active ONVIF monitors found. Exiting');
return;
}
Debug("Found $monitor_count active ONVIF monitors");
Info('ONVIF Trigger daemon starting');
if ( !defined $localip ) {
#$localip = '192.168.0.2';
#$localport = '0';
}
# re-use local address/port
# @LWP::Protocol::http::EXTRA_SOCK_OPTS =
*LWP::Protocol::http::_extra_sock_opts = sub {
# print "### extra_sock_opts ########################################\n";
@EXTRA_SOCK_OPTS;
};
#*LWP::Protocol::http::_check_sock = sub
#{
# my($self, $req, $sock) = @_;
# print "### check_sock ########################################\n";
# dump($sock);
#};
my $daemon = start_daemon($localip, $localport, $zm);
my $port = $daemon->url;
$port =~ s|^.*:||;
$port =~ s|/.*$||;
my $localaddr = $localip . ':' . $port;
Info('Daemon uses local address '.$localaddr);
# This value is passed as the LocalAddr argument to IO::Socket::INET.
my $transport = SOAP::Transport::HTTP::Client->new(
# 'local_address' => $localaddr ); ## REUSE port
local_address => $localip );
foreach my $monitor (values(%monitors)) {
my $client = $monitor->{onvif_client};
my $event_svc = $client->get_endpoint('events');
$event_svc->set_transport($transport);
# print "Sending from local address " .
# $event_svc->get_transport()->local_address . "\n";
my $submgr_svc = subscribe(
$client, $localaddr, 'tns1:RuleEngine//.',
SUBSCRIPTION_RENEW_INTERVAL, $monitor->{Id});
if ( !$submgr_svc ) {
Warning('Subscription failed for monitor #'.$monitor->{Id});
next;
}
$monitor->{submgr_svc} = $submgr_svc;
} # end foreach monitor
while (1) {
Info('Sleeping for ' . (SUBSCRIPTION_RENEW_INTERVAL - SUBSCRIPTION_RENEW_EARLY).' seconds');
sleep(SUBSCRIPTION_RENEW_INTERVAL - SUBSCRIPTION_RENEW_EARLY);
Info('Renewal');
my %monitors = $zm->monitors();
foreach my $monitor (values(%monitors)) {
if ( defined $monitor->{submgr_svc} ) {
renew($monitor->{submgr_svc}, SUBSCRIPTION_RENEW_INTERVAL + SUBSCRIPTION_RENEW_EARLY);
}
}
};
Info('ONVIF Trigger daemon exited');
%monitors = $zm->monitors();
foreach my $monitor (values(%monitors)) {
if ( defined $monitor->{submgr_svc} ) {
unsubscribe($monitor->{submgr_svc});
}
}
} # end sub events
# ========================================================================
# options processing
sub HELP_MESSAGE {
my ($fh, $pkg, $ver, $opts) = @_;
print $fh "Usage: " . __FILE__ . " <parameters>\n";
print $fh <<EOF
Parameters:
-v - increase verbosity
-l|local-addr - listen on address (host[:port])
-s|script - run script instead of generating ZM events
EOF
}
# ========================================================================
# MAIN
my ($localaddr, $localip, $localport);
# canonicalize command name
my $command = $0;
$0 = $command;
print("Init Log $command\n");
logInit(id=>'zm_onvif-trigger');
logSetSignal();
if ( !GetOptions(
'local-addr|l=s' => \$localaddr,
'script|s=s' => \$script,
'verbose|v=s' => \$verbose,
)) {
HELP_MESSAGE(\*STDOUT);
exit(1);
}
if ( defined $localaddr ) {
if ( $localaddr =~ /(.*):(.*)/ ) {
($localip, $localport) = ($1, $2);
} else {
$localip = $localaddr;
$localport = '0';
}
}
events($localip, $localport);
1;
__END__
=head1 NAME
zmonvif-trigger.pl - ZoneMinder ONVIF trigger daemon
=head1 SYNOPSIS
zmonfig-trigger.pl [-v] [-s] [-l=]
=head1 DESCRIPTION
=head1 OPTIONS
local-addr - local address to bind to
script|s=s - script to run
verbose|v=s - increase verbosity
=cut