From 14029547f641ae1d485a19f314441c9d3b3615b1 Mon Sep 17 00:00:00 2001 From: delmar <> Date: Sat, 15 May 2021 10:18:07 +0000 Subject: [PATCH] 49_IPCAM: allow zero for snapshots attribute, and some cleanup. git-svn-id: https://svn.fhem.de/fhem/trunk@24444 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/49_IPCAM.pm | 80 +++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index bee3e8a26..0a3422c49 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: allow zero for snapshots attribute, and some cleanup. - change: 93_DbLog: some minor changes, e.g. PID Info in Log Output - change: doif.js: tolerate dots in element id, cleanup - feature: 89_FULLY: Implemented some new commands diff --git a/fhem/FHEM/49_IPCAM.pm b/fhem/FHEM/49_IPCAM.pm index 45ddb9824..f9d8f3524 100644 --- a/fhem/FHEM/49_IPCAM.pm +++ b/fhem/FHEM/49_IPCAM.pm @@ -381,10 +381,13 @@ Get($@) { } } + + InternalTimer(gettimeofday(), "IPCAM::RequestSnapshot", $hash); + $hash->{READINGS}{snapshots}{VAL} = 0; - for (my $i=0;$i<$seqImages;$i++) { - InternalTimer(gettimeofday()+$seqWait, "IPCAM::RequestSnapshot", $hash); + for (my $i=1;$i<$seqImages;$i++) { $seqWait = $seqWait + $seqDelay; + InternalTimer(gettimeofday()+$seqWait, "IPCAM::RequestSnapshot", $hash); } return undef; @@ -395,7 +398,7 @@ Get($@) { RequestSnapshotWithCallback($hash,$callbackCommand); - return undef; + return undef; } elsif(defined($hash->{READINGS}{$arg})) { @@ -517,7 +520,7 @@ sub RequestSnapshot_Callback { my $imageFormat = guessFileFormat($name, \$snapshot); - my @imageTypes = qw(JPEG PNG GIF TIFF BMP ICO PPM XPM XBM SVG); + my @imageTypes = qw(jpg 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"; @@ -549,9 +552,25 @@ sub RequestSnapshot_Callback { } Log3 $name, 3, "IPCAM ($name) - Snapshot Image Format: $imageFormat"; - readingsBeginUpdate($hash); + my $lastSnapshot = $name."_snapshot.".$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); + + my $dateTime = TimeNow(); + $hash->{STATE} = "last: $dateTime"; + readingsBulkUpdate($hash, "last", $lastSnapshot, 1); + Log3 $name, 4, "IPCAM ($name) - snapshot $storage/$lastSnapshot written."; + my $seq = int(defined($hash->{SEQ}) ? $hash->{SEQ} : 0); my $seqImages = int(AttrVal($name,'snapshots',1)); @@ -560,33 +579,19 @@ sub RequestSnapshot_Callback { 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); + my $useTimestamp = AttrVal($name, 'timestamp', 0); + if($useTimestamp == 1) { + $imageFile = $name."_".$timestamp.".".$imageFormat; } else { - $imageFile = $name."_snapshot_".$seqF.".".lc($imageFormat); + $imageFile = $name."_snapshot_".$seqF.".".$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); @@ -597,8 +602,7 @@ sub RequestSnapshot_Callback { close(FH); Log3 $name, 4, "IPCAM ($name) - snapshot $storage/$imageFile written."; - readingsBulkUpdate($hash, "last", $lastSnapshot, 1); - $hash->{STATE} = "last: $dateTime"; + readingsBulkUpdate($hash, "snapshot$seqF", $imageFile, 1); Log3 $name, 4, "IPCAM ($name) - image: $imageFile"; @@ -658,20 +662,20 @@ guessFileFormat($$) { return "error while reading source image: $!" if(!$reading); local($_) = $srcHeader; - return "JPEG" if /^\xFF\xD8/; - return "PNG" if /^\x89PNG\x0d\x0a\x1a\x0a/; - return "GIF" if /^GIF8[79]a/; - return "TIFF" if /^MM\x00\x2a/; - return "TIFF" if /^II\x2a\x00/; - return "BMP" if /^BM/; - return "ICO" if /^\000\000\001\000/; - return "PPM" if /^P[1-6]/; - return "XPM" if /(^\/\* XPM \*\/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)/; - return "XBM" if /^(?:\/\*.*\*\/\n)?#define\s/; + return "jpg" if /^\xFF\xD8/; + return "png" if /^\x89PNG\x0d\x0a\x1a\x0a/; + return "gif" if /^GIF8[79]a/; + return "tiff" if /^MM\x00\x2a/; + return "tiff" if /^II\x2a\x00/; + return "bmp" if /^BM/; + return "ico" if /^\000\000\001\000/; + return "ppm" if /^P[1-6]/; + return "xpm" if /(^\/\* XPM \*\/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)/; + return "xbm" if /^(?:\/\*.*\*\/\n)?#define\s/; if (int(AttrVal($name, 'handleAnyXmlAsSvg', 0)) == 1) { - return "SVG" if /^(<\?xml|[\012\015\t ]*)?\r?\n?)?\r?\n?