From 9a1104130b9ac36ee95a2480b2de4962512da278 Mon Sep 17 00:00:00 2001 From: delmar <> Date: Wed, 18 Sep 2019 02:16:07 +0000 Subject: [PATCH] 70_ZoneMinder: improved contents of state (thx Florian_GT) git-svn-id: https://svn.fhem.de/fhem/trunk@20186 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/70_ZoneMinder.pm | 43 ++++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index afca14865..e8664013f 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. +- change: 70_ZoneMinder: improved contents of state (thx Florian_GT) - change: 95_Dashboard: attr dashboard_activetab is now working as designed, commandref revised, internal code review - change: 73_AutoShuttersControl: change timelimit for restart initial diff --git a/fhem/FHEM/70_ZoneMinder.pm b/fhem/FHEM/70_ZoneMinder.pm index 1067a15d9..9219bdb41 100755 --- a/fhem/FHEM/70_ZoneMinder.pm +++ b/fhem/FHEM/70_ZoneMinder.pm @@ -96,11 +96,34 @@ sub ZoneMinder_Define { DevIo_CloseDev($hash) if (DevIo_IsOpen($hash)); DevIo_OpenDev($hash, 0, undef); + my $triggerPortState = $hash->{STATE}; + ZoneMinder_updateState( $hash, $triggerPortState, 'n/a' ); + ZoneMinder_afterInitialized($hash); return undef; } +sub ZoneMinder_updateState { + my ( $hash, $triggerPortState, $apiState ) = @_; + + if ( defined( $triggerPortState ) ) { + $hash->{helper}{ZM_TRIGGER_STATE} = $triggerPortState; + } else { + $triggerPortState = $hash->{helper}{ZM_TRIGGER_STATE}; + } + + if ( defined( $apiState ) ) { + $hash->{helper}{ZM_API_STATE} = $apiState; + } else { + $apiState = $hash->{helper}{ZM_API_STATE}; + } + + readingsSingleUpdate( $hash, 'state', "Trigger-Port: $triggerPortState, API: $apiState", 0 ); + + return undef; +} + sub ZoneMinder_afterInitialized { my ($hash) = @_; @@ -188,12 +211,17 @@ sub ZoneMinder_API_Login_Callback { $hash->{APILoginStatus} = $param->{code}; Log3 $name, 3, "ZoneMinder ($name) - login status: $hash->{APILoginStatus}"; + my $apiState = undef; if($err ne "") { Log3 $name, 0, "error while requesting ".$param->{url}." - $err"; $hash->{APILoginError} = $err; + $apiState = 'error'; + } elsif($data ne "") { if ($data =~ m/Invalid username or password/) { $hash->{APILoginError} = "Invalid username or password."; + $apiState = 'login failed'; + } else { delete($defs{$name}{APILoginError}); @@ -206,9 +234,12 @@ sub ZoneMinder_API_Login_Callback { ZoneMinder_SimpleGet($hash, "$zmApiUrl/host/getVersion.json", \&ZoneMinder_API_ReadHostInfo_Callback); ZoneMinder_SimpleGet($hash, "$zmApiUrl/configs.json", \&ZoneMinder_API_ReadConfig_Callback); ZoneMinder_API_getLoad($hash); + + $apiState = 'opened'; } } } + ZoneMinder_updateState( $hash, undef, $apiState ); RemoveInternalTimer($hash, "ZoneMinder_API_Login"); my $interval = AttrVal($name, 'loginInterval', 3600); @@ -716,16 +747,10 @@ sub ZoneMinder_Ready { my ( $hash ) = @_; my $name = $hash->{NAME}; - if ( $hash->{STATE} eq "disconnected" ) { - return DevIo_OpenDev($hash, 1, undef ); #if success, $err is undef - } + ZoneMinder_updateState( $hash, 'disappeared', undef ); + + return DevIo_OpenDev($hash, 1, undef ); #if success, $err is undef - # This is relevant for Windows/USB only - if(defined($hash->{USBDev})) { - my $po = $hash->{USBDev}; - my ( $BlockingFlags, $InBytes, $OutBytes, $ErrorFlags ) = $po->status; - return ( $InBytes > 0 ); - } } 1;