mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-12-01 04:38:35 -05:00
Merge pull request #6732 from singhharsh1708/fix/i-trigger-notify-batch
fix: handle every message of a batched onvif notify in zmonvif-trigger
This commit is contained in:
2 files changed
+119
-13
No files matched your search
@@ -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
|
||||
|
||||
@@ -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{<wsnt:NotificationMessage><wsnt:Topic Dialect="http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet">$topic</wsnt:Topic>}
|
||||
.qq{<wsnt:Message><tt:Message UtcTime="$time"><tt:Source><tt:SimpleItem Name="Rule" Value="$rule"/></tt:Source>}
|
||||
.qq{<tt:Data><tt:SimpleItem Name="$name" Value="$value"/></tt:Data></tt:Message></wsnt:Message></wsnt:NotificationMessage>};
|
||||
}
|
||||
|
||||
sub notify {
|
||||
my @messages = @_;
|
||||
my $xml = '<?xml version="1.0"?><s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"'
|
||||
.' xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:tt="http://www.onvif.org/ver10/schema"'
|
||||
.' xmlns:wsa="http://www.w3.org/2005/08/addressing">'
|
||||
.'<s:Header><wsa:Action>http://docs.oasis-open.org/wsn/bw-2/NotificationConsumer/Notify</wsa:Action></s:Header>'
|
||||
."<s:Body><wsnt:Notify>\n ".join("\n ", @messages)."\n</wsnt:Notify></s:Body></s:Envelope>";
|
||||
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);
|
||||
Reference in new issue
Block a user