From 999d14ae15aab07054914f3148534c774b576302 Mon Sep 17 00:00:00 2001 From: delmar <> Date: Sun, 7 Mar 2021 21:57:20 +0000 Subject: [PATCH] 49_IPCAM: now unblocking and removed attribute 'loglevel' git-svn-id: https://svn.fhem.de/fhem/trunk@23910 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/49_IPCAM.pm | 245 ++++++++++++++++++++++++++++-------------- 2 files changed, 163 insertions(+), 83 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index e1c78c886..f2ca95261 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: 49_IPCAM: now unblocking and removed attribute 'loglevel' - change: 76_SMAInverter: change the etoday management - change: 48_MieleAtHome: add readings to show token-refresh-count - change: 93_DbRep: attribute sqlCmdVars is now working with sqlCmdBlocking diff --git a/fhem/FHEM/49_IPCAM.pm b/fhem/FHEM/49_IPCAM.pm index 5f9f9bb8f..7cde2cc5e 100644 --- a/fhem/FHEM/49_IPCAM.pm +++ b/fhem/FHEM/49_IPCAM.pm @@ -37,6 +37,7 @@ IPCAM_Initialize($$) $hash->{UndefFn} = "IPCAM::Undef"; $hash->{GetFn} = "IPCAM::Get"; $hash->{SetFn} = "IPCAM::Set"; +# $hash->{FW_detailFn} = "IPCAM::DetailFn"; $hash->{AttrList} = "basicauth delay credentials path pathCmd pathPanTilt query snapshots storage timestamp:0,1 ". "cmdPanLeft cmdPanRight cmdTiltUp cmdTiltDown cmdStep ". "cmdPos01 cmdPos02 cmdPos03 cmdPos04 cmdPos05 cmdPos06 cmdPos07 cmdPos08 ". @@ -44,7 +45,7 @@ IPCAM_Initialize($$) "cmd01 cmd02 cmd03 cmd04 cmd05 cmd06 cmd07 cmd08 ". "cmd09 cmd10 cmd11 cmd12 cmd13 cmd14 cmd15 ". "model do_not_notify:1,0 showtime:1,0 scheme:http,https ". - "loglevel:0,1,2,3,4,5,6 disable:0,1 ". + "disable:0,1 ". $readingFnAttributes; } @@ -54,10 +55,6 @@ use warnings; use SetExtensions; use GPUtils qw(:all); -sub getSnapshot($); -sub guessFileFormat($); -sub getScheme($); - my %gets = ( "image" => "", "last" => "", @@ -89,9 +86,10 @@ BEGIN { gettimeofday attr TimeNow - GetFileFromURLQuiet + HttpUtils_NonblockingGet SetExtensions AttrTemplate_Set + urlEncode )) }; @@ -262,13 +260,44 @@ Set($$$@) { } Log3 $name, 4, "IPCAM ($name) - set $cmd requesting $camURI"; - my $camret = GetFileFromURLQuiet($camURI); - Log3 $name, 5, "IPCAM ($name) - return:$camret"; + #my $camret = GetFileFromURLQuiet($camURI); + SendCommand( $hash, $camURI ); + #Log3 $name, 5, "IPCAM ($name) - return:$camret"; } return undef; } +sub SendCommand { + my ($hash, $camUrl) = @_; + my $name = $hash->{NAME}; + + Log3 $name, 3, "IPCAM ($name) - sending command: $camUrl"; + my $apiParam = { + url => $camUrl, + method => "GET", + callback => \&IPCAM::SendCommand_Callback, + hash => $hash + }; + HttpUtils_NonblockingGet($apiParam); + + return undef; +} + +sub SendCommand_Callback { + my ($param, $err, $data) = @_; + my $hash = $param->{hash}; + my $name = $hash->{NAME}; + + if($err ne "") { + Log3 $name, 0, "IPCAM ($name) - error while sending command ".$param->{url}." - $err"; + + } elsif($data ne "") { + Log3 $name, 3, "IPCAM ($name) - command response: $data"; + } + +} + ##################################### sub Get($@) { @@ -356,19 +385,8 @@ getSnapshot($) { my $camPath; my $camQuery; my $camCredentials; - my $imageFile; - my $imageFormat; - my $lastSnapshot; - my $snapshot; - my $dateTime; my $modpath = $attr{global}{modpath}; - my $seq = int(defined($hash->{SEQ}) ? $hash->{SEQ} : 0); - my $seqImages = int(AttrVal($name,'snapshots',1)); - my $seqF; - my $seqL = length($seqImages); - my $storage = AttrVal($name,'storage',"$modpath/www/snapshots"); my $basicauth = AttrVal($name,'basicauth', undef); - my $timestamp; #if(!$storage) { # RemoveInternalTimer($hash); @@ -403,76 +421,124 @@ getSnapshot($) { $camURI =~ s/{PASSWORD}/$credentials{$name}{password}/; } } - - $dateTime = TimeNow(); - $timestamp = $dateTime; - $timestamp =~ s/ /_/g; - $timestamp =~ s/(:|-)//g; + $camURI = $camURI; Log3 $name, 3, "IPCAM ($name) - getSnapshot URI: $camURI"; - $snapshot = GetFileFromURLQuiet($camURI); +# while ($camURI =~ m/(\[.*:.*\])/) { +# Log3 $name, 3, "IPCAM ($name) - found reading: $1"; +# } + + GetSnapshot($hash, $camURI); # Log3 $name, 5, "IPCAM ($name) - getSnapshot snapshot: $snapshot"; - $imageFormat = guessFileFormat(\$snapshot); - - my @imageTypes = qw(JPEG PNG GIF TIFF BMP ICO PPM XPM XBM SVG); - - if( ! grep { $_ eq "$imageFormat"} @imageTypes) { - Log3 $name, 1, "IPCAM ($name) - Wrong or not supported image format: $imageFormat"; - RemoveInternalTimer($hash); - return undef; - } - - Log3 $name, 4, "IPCAM ($name) - Image Format: $imageFormat"; - - readingsBeginUpdate($hash); - if($seq < $seqImages) { - $seq++; - $seqF = sprintf("%0${seqL}d",$seq); - $imageFormat = "JPG" if($imageFormat eq "JPEG"); - - $lastSnapshot = $name."_snapshot.".lc($imageFormat); - if(defined($attr{$name}{timestamp}) && $attr{$name}{timestamp} == 1) { - $imageFile = $name."_".$timestamp.".".lc($imageFormat); - } else { - $imageFile = $name."_snapshot_".$seqF.".".lc($imageFormat); - } - if(!open(FH, ">$storage/$lastSnapshot")) { - Log3 $name, 0, "IPCAM ($name) - Can't write $storage/$lastSnapshot: $!"; - RemoveInternalTimer($hash); - readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1)); - return undef; - } - print FH $snapshot; - close(FH); - Log3 $name, 4, "IPCAM ($name) - snapshot $storage/$lastSnapshot written."; - if(!open(FH, ">$storage/$imageFile")) { - Log3 $name, 0, "IPCAM ($name) - Can't write $storage/$imageFile: $!"; - RemoveInternalTimer($hash); - readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1)); - return undef; - } - print FH $snapshot; - close(FH); - Log3 $name, 4, "IPCAM ($name) - snapshot $storage/$imageFile written."; - readingsBulkUpdate($hash,"last",$lastSnapshot, 1); - $hash->{STATE} = "last: $dateTime"; - $hash->{READINGS}{"snapshot$seqF"}{TIME} = $dateTime; - $hash->{READINGS}{"snapshot$seqF"}{VAL} = $imageFile; - } - - Log3 $name, 4, "IPCAM ($name) - image: $imageFile"; - - if($seq == $seqImages) { - readingsBulkUpdate($hash,"snapshots",$seq,1 ); - $seq = 0; - } - readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1)); - $hash->{SEQ} = $seq; - return undef; } +sub GetSnapshot { + my ($hash, $camUrl) = @_; + my $name = $hash->{NAME}; + + my $apiParam = { + url => $camUrl, + method => "GET", + callback => \&IPCAM::GetSnapshot_Callback, + hash => $hash + }; + HttpUtils_NonblockingGet($apiParam); + + return undef; +} + +sub GetSnapshot_Callback { + my ($param, $err, $snapshot) = @_; + my $hash = $param->{hash}; + my $name = $hash->{NAME}; + + if($err ne "") { + Log3 $name, 0, "IPCAM ($name) - error while getting snapshot ".$param->{url}." - $err"; + + } elsif($snapshot ne "") { +# Log3 $name, 3, "IPCAM ($name) - snapshot response: $data"; + + my $imageFormat = guessFileFormat(\$snapshot); + + my @imageTypes = qw(JPEG PNG GIF TIFF BMP ICO PPM XPM XBM SVG); + + if( ! grep { $_ eq "$imageFormat"} @imageTypes) { + Log3 $name, 1, "IPCAM ($name) - Wrong or not supported image format: $imageFormat"; + RemoveInternalTimer($hash); + return undef; + } + + Log3 $name, 3, "IPCAM ($name) - Snapshot Image Format: $imageFormat"; + + readingsBeginUpdate($hash); + + my $seq = int(defined($hash->{SEQ}) ? $hash->{SEQ} : 0); + my $seqImages = int(AttrVal($name,'snapshots',1)); + + if($seq < $seqImages) { + $seq++; + + my $seqL = length($seqImages); + my $seqF = sprintf("%0${seqL}d", $seq); + $imageFormat = "JPG" if($imageFormat eq "JPEG"); + + my $lastSnapshot = $name."_snapshot.".lc($imageFormat); + my $dateTime = TimeNow(); + my $timestamp = $dateTime; + $timestamp =~ s/ /_/g; + $timestamp =~ s/(:|-)//g; + + my $imageFile; + if(defined($attr{$name}{timestamp}) && $attr{$name}{timestamp} == 1) { + $imageFile = $name."_".$timestamp.".".lc($imageFormat); + } else { + $imageFile = $name."_snapshot_".$seqF.".".lc($imageFormat); + } + + my $modpath = $attr{global}{modpath}; + my $storage = AttrVal($name,'storage',"$modpath/www/snapshots"); + if(!open(FH, ">$storage/$lastSnapshot")) { + Log3 $name, 0, "IPCAM ($name) - Can't write $storage/$lastSnapshot: $!"; + RemoveInternalTimer($hash); + readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1)); + return undef; + } + print FH $snapshot; + close(FH); + + Log3 $name, 4, "IPCAM ($name) - snapshot $storage/$lastSnapshot written."; + if(!open(FH, ">$storage/$imageFile")) { + Log3 $name, 0, "IPCAM ($name) - Can't write $storage/$imageFile: $!"; + RemoveInternalTimer($hash); + readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1)); + return undef; + } + print FH $snapshot; + close(FH); + + Log3 $name, 4, "IPCAM ($name) - snapshot $storage/$imageFile written."; + readingsBulkUpdate($hash, "last", $lastSnapshot, 1); + $hash->{STATE} = "last: $dateTime"; + $hash->{READINGS}{"snapshot$seqF"}{TIME} = $dateTime; + $hash->{READINGS}{"snapshot$seqF"}{VAL} = $imageFile; + + Log3 $name, 4, "IPCAM ($name) - image: $imageFile"; + } + + + if($seq == $seqImages) { + readingsBulkUpdate($hash, "snapshots", $seq, 1); + $seq = 0; + } + readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1)); + $hash->{SEQ} = $seq; + } +} + + + ##################################### sub getScheme($) { @@ -510,6 +576,20 @@ guessFileFormat($) { return "unknown"; } +sub +DetailFn { + my ( $FW_wname, $deviceName, $FW_room ) = @_; + + my $hash = $main::defs{$deviceName}; + my $name = $hash->{NAME}; + + my $scheme = getScheme($hash); + my $ip = $hash->{AUTHORITY}; + my $cameraUri = "$scheme://$ip"; + + return "
Go to Camera Web UI
"; +} + # vim: ts=2:et 1; @@ -744,7 +824,6 @@ guessFileFormat($) {
  • disable
  • do_not_notify
  • -
  • loglevel
  • readingFnAttributes
  • path