diff --git a/scripts/zmonvif-trigger.pl.in b/scripts/zmonvif-trigger.pl.in index ea32452913..a60b273585 100644 --- a/scripts/zmonvif-trigger.pl.in +++ b/scripts/zmonvif-trigger.pl.in @@ -353,21 +353,25 @@ $SOAP::Constants::DO_NOT_CHECK_MUSTUNDERSTAND = 1; # 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'}; + # A Notify can carry several messages; they are the first children of Notify + my $count = () = $som->dataof('/Envelope/Body/Notify/NotificationMessage'); + foreach my $i ( 1 .. $count ) { + my $msg = $som->match("/Envelope/Body/Notify/[$i]"); + 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"; + 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}; + } + main::handle_notification($zm, $id, $topic, $time, \%source, \%data); } - foreach my $item ($msg2->dataof('Data/SimpleItem')) { - $data{$item->attr->{Name}} = $item->attr->{Value}; - } - main::handle_notification($zm, $id, $topic, $time, \%source, \%data); return (); } } # end Consumer diff --git a/tests/perl/test_onvif_trigger_notify_batch.pl b/tests/perl/test_onvif_trigger_notify_batch.pl new file mode 100644 index 0000000000..cc11c87fb9 --- /dev/null +++ b/tests/perl/test_onvif_trigger_notify_batch.pl @@ -0,0 +1,102 @@ +#!/usr/bin/perl +# +# zmonvif-trigger's Notify handler must pass on every NotificationMessage of a +# Notify. Cameras batch messages; the handler read the first Topic and merged +# the Source and Data items of all messages, so an IsMotion=true followed by +# IsMotion=false in one Notify only turned the alarm off, and a rule's state +# was reported under another message's rule. +# +# Needs SOAP::Lite to build the SOAP::SOM; skips without it. +# +# Run as: perl tests/perl/test_onvif_trigger_notify_batch.pl +# +use strict; +use warnings; +use FindBin; + +if (!eval { require SOAP::Lite; 1 }) { + print "1..0 # SKIP SOAP::Lite does not load here\n"; + exit 0; +} + +my $file = "$FindBin::Bin/../../scripts/zmonvif-trigger.pl.in"; +open(my $fh, '<', $file) or die "Can't open $file: $!"; +my $src = do { local $/; <$fh> }; +close($fh); +$src =~ /^( sub Notify \{.*?^ \})/ms or die 'sub Notify not found'; +my $notify_sub = $1; + +my @calls; +sub handle_notification { + my ($zm, $id, $topic, $time, $source, $data) = @_; + push @calls, join('|', $id, $topic, $time, + join(',', map { "$_=$source->{$_}" } sort keys %$source), + join(',', map { "$_=$data->{$_}" } sort keys %$data)); +} + +{ + package _Consumer; + my $zm = 'zm'; + sub Debug {} + sub Warning {} + eval $notify_sub . "\n1;" or die $@; +} + +package FakeContext; +sub new { return bless { path => $_[1] }, $_[0]; } +sub request { return $_[0]; } +sub uri { return $_[0]; } +sub path { return $_[0]->{path}; } +package main; + +my ($passes, $failures) = (0, 0); +sub check { + my ($name, $got, $want) = @_; + if ($got eq $want) { + $passes++; print "ok - $name\n"; + } else { + $failures++; print "NOT OK - $name\n got: $got\n want: $want\n"; + } +} + +sub message { + my ($topic, $time, $rule, $name, $value) = @_; + return qq{$topic} + .qq{} + .qq{}; +} + +sub notify { + my @messages = @_; + my $xml = '' + .'http://docs.oasis-open.org/wsn/bw-2/NotificationConsumer/Notify' + ."\n ".join("\n ", @messages)."\n"; + my $som = SOAP::Deserializer->deserialize($xml); + $som->context(FakeContext->new('/ref_4/')); + @calls = (); + _Consumer->Notify(undef, $som); + return join("\n", @calls); +} + +check('one message', + notify(message('tns1:RuleEngine/CellMotionDetector/Motion', 'T1', 'MyMotion', 'IsMotion', 'true')), + '4|tns1:RuleEngine/CellMotionDetector/Motion|T1|Rule=MyMotion|IsMotion=true'); + +check('motion start and end in one Notify are both handled, in order', + notify( + message('tns1:RuleEngine/CellMotionDetector/Motion', 'T1', 'MyMotion', 'IsMotion', 'true'), + message('tns1:RuleEngine/CellMotionDetector/Motion', 'T2', 'MyMotion', 'IsMotion', 'false')), + "4|tns1:RuleEngine/CellMotionDetector/Motion|T1|Rule=MyMotion|IsMotion=true\n" + ."4|tns1:RuleEngine/CellMotionDetector/Motion|T2|Rule=MyMotion|IsMotion=false"); + +check('each message keeps its own topic, rule and data', + notify( + message('tns1:RuleEngine/CellMotionDetector/Motion', 'T1', 'MyMotion', 'IsMotion', 'true'), + message('tns1:RuleEngine/TamperDetector/Tamper', 'T2', 'MyTamper', 'IsTamper', 'false')), + "4|tns1:RuleEngine/CellMotionDetector/Motion|T1|Rule=MyMotion|IsMotion=true\n" + ."4|tns1:RuleEngine/TamperDetector/Tamper|T2|Rule=MyTamper|IsTamper=false"); + +print "\n$passes passed, $failures failed\n"; +exit($failures ? 1 : 0);