70_ZoneMinder: fetching Event-Details via API after event received

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@18501 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
delmar 2019-02-05 20:39:21 +00:00
parent d7a77f82cb
commit fc41d396aa
2 changed files with 96 additions and 11 deletions

View File

@ -238,6 +238,8 @@ sub ZoneMinder_SimpleGet {
$apiParam->{header} .= "Cookie: " . $hash->{HTTPCookies}; $apiParam->{header} .= "Cookie: " . $hash->{HTTPCookies};
} }
Log3 $name, 4, "ZoneMinder ($name) SimpleGet calling $url with callback $callback";
HttpUtils_NonblockingGet($apiParam); HttpUtils_NonblockingGet($apiParam);
} }
@ -336,13 +338,13 @@ sub ZoneMinder_GetFromJson {
# Log3 $name, 5, "json: $config"; # Log3 $name, 5, "json: $config";
my $searchLength = length($searchString); my $searchLength = length($searchString);
my $startIdx = index($config, $searchString); my $startIdx = index($config, $searchString);
Log3 $name, 5, "$searchString found at $startIdx"; Log3 $name, 5, "ZoneMinder ($name) - $searchString found at $startIdx";
$startIdx += $searchLength; $startIdx += $searchLength;
my $endIdx = index($config, $endChar, $startIdx); my $endIdx = index($config, $endChar, $startIdx);
my $frame = $endIdx - $startIdx; my $frame = $endIdx - $startIdx;
my $searchResult = substr $config, $startIdx, $frame; my $searchResult = substr $config, $startIdx, $frame;
Log3 $name, 5, "looking for $searchString - length: $searchLength. start: $startIdx. end: $endIdx. result: $searchResult"; Log3 $name, 5, "ZoneMinder ($name) - looking for $searchString - length: $searchLength. start: $startIdx. end: $endIdx. result: $searchResult";
return $searchResult; return $searchResult;
} }
@ -360,7 +362,7 @@ sub ZoneMinder_API_UpdateMonitors_Callback {
if ( $monitorId =~ /^[0-9]+$/ ) { if ( $monitorId =~ /^[0-9]+$/ ) {
ZoneMinder_UpdateMonitorAttributes($hash, $monitorData, $monitorId); ZoneMinder_UpdateMonitorAttributes($hash, $monitorData, $monitorId);
} else { } else {
Log3 $name, 0, "Invalid monitorId: $monitorId" unless ('itors' eq $monitorId); Log3 $name, 0, "ZoneMinder ($name) - Invalid monitorId: $monitorId" unless ('itors' eq $monitorId);
} }
} }
@ -413,36 +415,50 @@ sub ZoneMinder_GetCookies {
sub ZoneMinder_Write { sub ZoneMinder_Write {
my ( $hash, $arguments) = @_; my ( $hash, $arguments) = @_;
my $name = $hash->{NAME};
my $method = $arguments->{method}; my $method = $arguments->{method};
if ($method eq 'changeMonitorFunction') { if ($method eq 'changeMonitorFunction') {
my $zmMonitorId = $arguments->{zmMonitorId}; my $zmMonitorId = $arguments->{zmMonitorId};
my $zmFunction = $arguments->{zmFunction}; my $zmFunction = $arguments->{zmFunction};
Log3 $hash->{NAME}, 4, "method: $method, monitorId:$zmMonitorId, Function:$zmFunction"; Log3 $name, 4, "ZoneMinder ($name) method: $method, monitorId:$zmMonitorId, Function:$zmFunction";
return ZoneMinder_API_ChangeMonitorState($hash, $zmMonitorId, $zmFunction, undef); return ZoneMinder_API_ChangeMonitorState($hash, $zmMonitorId, $zmFunction, undef);
} elsif ($method eq 'changeMonitorEnabled') { } elsif ($method eq 'changeMonitorEnabled') {
my $zmMonitorId = $arguments->{zmMonitorId}; my $zmMonitorId = $arguments->{zmMonitorId};
my $zmEnabled = $arguments->{zmEnabled}; my $zmEnabled = $arguments->{zmEnabled};
Log3 $hash->{NAME}, 4, "method: $method, monitorId:$zmMonitorId, Enabled:$zmEnabled"; Log3 $name, 4, "ZoneMinder ($name) method: $method, monitorId:$zmMonitorId, Enabled:$zmEnabled";
return ZoneMinder_API_ChangeMonitorState($hash, $zmMonitorId, undef, $zmEnabled); return ZoneMinder_API_ChangeMonitorState($hash, $zmMonitorId, undef, $zmEnabled);
} elsif ($method eq 'changeMonitorAlarm') { } elsif ($method eq 'changeMonitorAlarm') {
my $zmMonitorId = $arguments->{zmMonitorId}; my $zmMonitorId = $arguments->{zmMonitorId};
my $zmAlarm = $arguments->{zmAlarm}; my $zmAlarm = $arguments->{zmAlarm};
Log3 $hash->{NAME}, 4, "method: $method, monitorId:$zmMonitorId, Alarm:$zmAlarm"; Log3 $name, 4, "ZoneMinder ($name) method: $method, monitorId:$zmMonitorId, Alarm:$zmAlarm";
return ZoneMinder_Trigger_ChangeAlarmState($hash, $zmMonitorId, $zmAlarm); return ZoneMinder_Trigger_ChangeAlarmState($hash, $zmMonitorId, $zmAlarm);
} elsif ($method eq 'changeMonitorText') { } elsif ($method eq 'changeMonitorText') {
my $zmMonitorId = $arguments->{zmMonitorId}; my $zmMonitorId = $arguments->{zmMonitorId};
my $zmText = $arguments->{text}; my $zmText = $arguments->{text};
Log3 $hash->{NAME}, 4, "method: $method, monitorId:$zmMonitorId, Text:$zmText"; Log3 $name, 4, "ZoneMinder ($name) method: $method, monitorId:$zmMonitorId, Text:$zmText";
return ZoneMinder_Trigger_ChangeText($hash, $zmMonitorId, $zmText); return ZoneMinder_Trigger_ChangeText($hash, $zmMonitorId, $zmText);
} elsif ($method eq 'queryEventDetails') {
my $zmApiUrl = ZoneMinder_getZmApiUrl($hash);
if ( not defined($zmApiUrl) ) {
return undef;
}
my $zmMonitorId = $arguments->{zmMonitorId};
my $zmEventId = $arguments->{zmEventId};
Log3 $name, 4, "ZoneMinder ($name) method: $method, monitorId:$zmMonitorId, EventId:$zmEventId";
ZoneMinder_SimpleGet($hash, "$zmApiUrl/events/$zmEventId.json", \&ZoneMinder_API_QueryEventDetails_Callback);
return undef;
} }
return undef; return undef;
@ -503,6 +519,35 @@ sub ZoneMinder_API_ChangeMonitorState_Callback {
return undef; return undef;
} }
sub ZoneMinder_API_QueryEventDetails_Callback {
my ($param, $err, $data) = @_;
my $hash = $param->{hash};
my $name = $hash->{NAME};
my $zmMonitorId = ZoneMinder_GetConfigValueByKey($hash, $data, 'MonitorId');
my $zmEventId = ZoneMinder_GetConfigValueByKey($hash, $data, 'Id');
my $zmNotes = ZoneMinder_GetConfigValueByKey($hash, $data, 'Notes');
# my $logDevHash = $modules{ZM_Monitor}{defptr}{$name.'_'.$zmMonitorId};
Log3 $name, 4, "ZoneMinder ($name) - QueryEventDetails_Callback zmMonitorId: $zmMonitorId, zmEventId: $zmEventId, zmNotes: $zmNotes";
Dispatch($hash, "eventDetails:$zmMonitorId|$zmEventId|$zmNotes", undef);
# foreach my $monitorData (@monitors) {
# my $monitorId = ZoneMinder_GetConfigValueByKey($hash, $monitorData, 'Id');
# if ( $monitorId =~ /^[0-9]+$/ ) {
# my $dispatchResult = Dispatch($hash, "createMonitor:$monitorId", undef);
# }
# }
# my $zmApiUrl = ZoneMinder_getZmApiUrl($hash);
# ZoneMinder_SimpleGet($hash, "$zmApiUrl/monitors.json", \&ZoneMinder_API_UpdateMonitors_Callback);
return undef;
}
sub ZoneMinder_Trigger_ChangeAlarmState { sub ZoneMinder_Trigger_ChangeAlarmState {
my ( $hash, $zmMonitorId, $zmAlarm ) = @_; my ( $hash, $zmMonitorId, $zmAlarm ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};

View File

@ -255,6 +255,8 @@ sub ZM_Monitor_Parse {
my $msgType = $msg[0]; my $msgType = $msg[0];
if ($msgType eq 'event') { if ($msgType eq 'event') {
return ZM_Monitor_handleEvent($io_hash, $msg[1]); return ZM_Monitor_handleEvent($io_hash, $msg[1]);
} elsif ($msgType eq 'eventDetails') {
return ZM_Monitor_handleZoneUpdate($io_hash, $msg[1]);
} elsif ($msgType eq 'createMonitor') { } elsif ($msgType eq 'createMonitor') {
return ZM_Monitor_handleMonitorCreation($io_hash, $msg[1]); return ZM_Monitor_handleMonitorCreation($io_hash, $msg[1]);
} elsif ($msgType eq 'monitor') { } elsif ($msgType eq 'monitor') {
@ -277,10 +279,13 @@ sub ZM_Monitor_handleEvent {
my $eventId = $msgTokens[3]; my $eventId = $msgTokens[3];
my $logDevAddress = $ioName.'_'.$zmMonitorId; my $logDevAddress = $ioName.'_'.$zmMonitorId;
Log3 $io_hash, 5, "Handling event for logical device $logDevAddress"; Log3 $io_hash, 5, "ZM_Monitor Handling event for logical device $logDevAddress";
# wenn bereits eine Gerätedefinition existiert (via Definition Pointer aus Define-Funktion) # wenn bereits eine Gerätedefinition existiert (via Definition Pointer aus Define-Funktion)
if(my $hash = $modules{ZM_Monitor}{defptr}{$logDevAddress}) { if(my $hash = $modules{ZM_Monitor}{defptr}{$logDevAddress}) {
Log3 $hash, 5, "Logical device $logDevAddress found. Writing readings"; my $name = $hash->{NAME};
Log3 $hash, 5, "ZM_Monitor ($name) Logical device $logDevAddress found. Writing readings";
ZM_Monitor_queryEventDetails($hash, $eventId);
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
ZM_Monitor_createEventStreamUrl($hash, $eventId); ZM_Monitor_createEventStreamUrl($hash, $eventId);
@ -294,19 +299,54 @@ sub ZM_Monitor_handleEvent {
readingsBulkUpdate($hash, "alert", $alertState, 1); readingsBulkUpdate($hash, "alert", $alertState, 1);
readingsBulkUpdate($hash, "lastEventTimestamp", $eventTs); readingsBulkUpdate($hash, "lastEventTimestamp", $eventTs);
readingsBulkUpdate($hash, "lastEventId", $eventId); readingsBulkUpdate($hash, "lastEventId", $eventId);
readingsBulkUpdate($hash, "lastEventNotes", '');
readingsEndUpdate($hash, 1); readingsEndUpdate($hash, 1);
Log3 $hash, 5, "Writing readings done. Now returning log dev name: $hash->{NAME}"; Log3 $hash, 5, "ZM_Monitor ($name) Writing readings done. Now returning log dev name: $hash->{NAME}";
# Rückgabe des Gerätenamens, für welches die Nachricht bestimmt ist. # Rückgabe des Gerätenamens, für welches die Nachricht bestimmt ist.
return $hash->{NAME}; return $hash->{NAME};
} else { } else {
# Keine Gerätedefinition verfügbar. Daher Vorschlag define-Befehl: <NAME> <MODULNAME> <ADDRESSE> # Keine Gerätedefinition verfügbar. Daher Vorschlag define-Befehl: <NAME> <MODULNAME> <ADDRESSE>
my $autocreate = "UNDEFINED ZM_Monitor_$logDevAddress ZM_Monitor $zmMonitorId"; my $autocreate = "UNDEFINED ZM_Monitor_$logDevAddress ZM_Monitor $zmMonitorId";
Log3 $io_hash, 5, "logical device with address $logDevAddress not found. returning autocreate: $autocreate"; Log3 $io_hash, 5, "ZM_Monitor logical device with address $logDevAddress not found. returning autocreate: $autocreate";
return $autocreate; return $autocreate;
} }
} }
sub ZM_Monitor_queryEventDetails {
my ( $hash, $eventId ) = @_;
my $arguments = {
method => 'queryEventDetails',
zmMonitorId => $hash->{helper}{ZM_MONITOR_ID},
zmEventId => $eventId
};
my $result = IOWrite($hash, $arguments);
}
sub ZM_Monitor_handleZoneUpdate {
my ( $io_hash, $message ) = @_;
my $ioName = $io_hash->{NAME};
my @msgTokens = split(/\|/, $message);
my $zmMonitorId = $msgTokens[0];
my $zmEventId = $msgTokens[1];
my $zmNotes = $msgTokens[2];
my $logDevAddress = $ioName.'_'.$zmMonitorId;
if(my $hash = $modules{ZM_Monitor}{defptr}{$logDevAddress}) {
readingsSingleUpdate($hash, 'lastEventNotes', $zmNotes, 1);
return $hash->{NAME}
} else {
# Keine Gerätedefinition verfügbar. Daher Vorschlag define-Befehl: <NAME> <MODULNAME> <ADDRESSE>
my $autocreate = "UNDEFINED ZM_Monitor_$logDevAddress ZM_Monitor $zmMonitorId";
Log3 $io_hash, 5, "ZM_Monitor logical device with address $logDevAddress not found. returning autocreate: $autocreate";
return $autocreate;
}
}
#for now, this is nearly a duplicate of writing the streamUrl reading. #for now, this is nearly a duplicate of writing the streamUrl reading.
#will need some love to make better use of existing code. #will need some love to make better use of existing code.
sub ZM_Monitor_createEventStreamUrl { sub ZM_Monitor_createEventStreamUrl {