From 607f95210557ef2c6391e1e6bc7bd9955ce66b8c Mon Sep 17 00:00:00 2001 From: nasseeder1 Date: Wed, 27 Mar 2019 22:10:48 +0000 Subject: [PATCH] 49_SSCam: delay FHEM shutdown as long as sessions are not terminated, add Meta.pm support git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@19051 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- CHANGED | 2 + FHEM/49_SSCam.pm | 187 ++++++++++++++++++++++++++++++++++++++----- FHEM/49_SSCamSTRM.pm | 106 ++++++++++++++++++++++-- 3 files changed, 267 insertions(+), 28 deletions(-) diff --git a/CHANGED b/CHANGED index b918c1eec..798e17780 100644 --- a/CHANGED +++ b/CHANGED @@ -1,5 +1,7 @@ # 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_SSCam: delay FHEM shutdown as long as sessions are not + terminated, add Meta.pm support - feature: 30_HUEBridge: added schedules (by PPP01) - change: 74_GardenaSmartDevice: change on-for-timer time format - feature: 74_GardenaSmartDevice: add support for power plug and META diff --git a/FHEM/49_SSCam.pm b/FHEM/49_SSCam.pm index e1fc3344b..9b391d363 100644 --- a/FHEM/49_SSCam.pm +++ b/FHEM/49_SSCam.pm @@ -42,11 +42,15 @@ use MIME::Base64; use Time::HiRes; use HttpUtils; use Blocking; # für EMail-Versand -use Encode; +use Encode; +eval "use FHEM::Meta;1" or my $modMetaAbsent = 1; # no if $] >= 5.017011, warnings => 'experimental'; # Versions History intern our %SSCam_vNotesIntern = ( + "8.13.0" => "27.03.2019 add Meta.pm support ", + "8.12.0" => "25.03.2019 FHEM standard function X_DelayedShutdown implemented, delay FHEM shutdown as long as sessions ". + "are not terminated. ", "8.11.5" => "24.03.2019 fix possible overload Synology DS during shutdown restart ", "8.11.4" => "11.03.2019 make module ready for SVS version 8.2.3-5829 ", "8.11.3" => "08.03.2019 avoid possible JSON errors, fix fhem is hanging while restart or get snapinfo - Forum: #45671.msg915546.html#msg915546 ", @@ -138,6 +142,7 @@ our %SSCam_vNotesIntern = ( # Versions History extern our %SSCam_vNotesExtern = ( + "8.12.0" => "25.03.2019 Delay FHEM shutdown as long as sessions are not terminated, but not longer than global attribute \"maxShutdownDelay\". ", "8.11.0" => "25.02.2019 compatibility set to SVS version 8.2.3, Popup possible for streaming devices of type \"generic\", ". "support for \"genericStrmHtmlTag\" in streaming devices ", "8.10.0" => "15.02.2019 Possibility of send recordings by telegram is integrated as well as sending snapshots ", @@ -348,12 +353,13 @@ sub SSCam_TBotSendIt($$$$$$$;$$$); ################################################################ sub SSCam_Initialize($) { my ($hash) = @_; - $hash->{DefFn} = "SSCam_Define"; - $hash->{UndefFn} = "SSCam_Undef"; - $hash->{DeleteFn} = "SSCam_Delete"; - $hash->{SetFn} = "SSCam_Set"; - $hash->{GetFn} = "SSCam_Get"; - $hash->{AttrFn} = "SSCam_Attr"; + $hash->{DefFn} = "SSCam_Define"; + $hash->{UndefFn} = "SSCam_Undef"; + $hash->{DeleteFn} = "SSCam_Delete"; + $hash->{SetFn} = "SSCam_Set"; + $hash->{GetFn} = "SSCam_Get"; + $hash->{AttrFn} = "SSCam_Attr"; + $hash->{DelayedShutdownFn} = "SSCam_DelayedShutdown"; # Aufrufe aus FHEMWEB $hash->{FW_summaryFn} = "SSCam_FWsummaryFn"; $hash->{FW_detailFn} = "SSCam_FWdetailFn"; @@ -400,7 +406,9 @@ sub SSCam_Initialize($) { "webCmd ". $readingFnAttributes; -return undef; + eval { FHEM::Meta::InitMod( __FILE__, $hash ) }; # für Meta.pm (https://forum.fhem.de/index.php/topic,97589.0.html) + +return; } ################################################################ @@ -426,13 +434,13 @@ sub SSCam_Define($@) { my $serverport = $a[4] ? $a[4] : 5000; my $proto = $a[5] ? lc($a[5]) : "http"; - $hash->{SERVERADDR} = $serveraddr; - $hash->{SERVERPORT} = $serverport; - $hash->{CAMNAME} = $camname; - $hash->{VERSION} = (SSCam_sortVersion("desc",keys %SSCam_vNotesIntern))[0]; - $hash->{MODEL} = ($camname =~ m/^SVS$/i)?"SVS":"CAM"; # initial, CAM wird später ersetzt durch CamModel - $hash->{PROTOCOL} = $proto; - $hash->{COMPATIBILITY} = $compstat; # getestete SVS-version Kompatibilität + $hash->{SERVERADDR} = $serveraddr; + $hash->{SERVERPORT} = $serverport; + $hash->{CAMNAME} = $camname; + $hash->{MODEL} = ($camname =~ m/^SVS$/i)?"SVS":"CAM"; # initial, CAM wird später ersetzt durch CamModel + $hash->{PROTOCOL} = $proto; + $hash->{COMPATIBILITY} = $compstat; # getestete SVS-version Kompatibilität + $hash->{HELPER}{MODMETAABSENT} = 1 if($modMetaAbsent); # Modul Meta.pm nicht vorhanden # benötigte API's in $hash einfügen $hash->{HELPER}{APIINFO} = "SYNO.API.Info"; # Info-Seite für alle API's, einzige statische Seite ! @@ -472,6 +480,9 @@ sub SSCam_Define($@) { $hash->{HELPER}{SNAPLIMIT} = 0; # abgerufene Anzahl Snaps $hash->{HELPER}{TOTALCNT} = 0; # totale Anzahl Snaps + # Versionsinformationen setzen + SSCam_setVersionInfo($hash); + readingsBeginUpdate($hash); readingsBulkUpdate($hash,"PollState","Inactive"); # es ist keine Gerätepolling aktiv if(SSCam_IsModelCam($hash)) { @@ -492,16 +503,56 @@ sub SSCam_Define($@) { return undef; } +################################################################ +# Die Undef-Funktion wird aufgerufen wenn ein Gerät mit delete +# gelöscht wird oder bei der Abarbeitung des Befehls rereadcfg, +# der ebenfalls alle Geräte löscht und danach das +# Konfigurationsfile neu einliest. +# Funktion: typische Aufräumarbeiten wie das +# saubere Schließen von Verbindungen oder das Entfernen von +# internen Timern, sofern diese im Modul zum Pollen verwendet +# wurden. ################################################################ sub SSCam_Undef($$) { my ($hash, $arg) = @_; - #SSCam_logout($hash); + RemoveInternalTimer($hash); return undef; } -################################################################ +####################################################################################################### +# Mit der X_DelayedShutdown Funktion kann eine Definition das Stoppen von FHEM verzögern um asynchron +# hinter sich aufzuräumen. Dies kann z.B. der Verbindungsabbau mit dem physikalischen Gerät sein (z.B. +# Session beenden, Logout, etc.), welcher mehrfache Requests/Responses benötigt. +# Je nach Rückgabewert $delay_needed wird der Stopp von FHEM verzögert. +# Im Unterschied zur Shutdown-Funktion steht vor einem bevorstehenden Stopp von FHEM für einen +# User-konfigurierbaren Zeitraum (global-Attribut: maxShutdownDelay / Standard: 10 Sekunden) weiterhin +# die asynchrone FHEM Infrastruktur (DevIo/Read-Funktion und InternalTimer) zur Verfügung. +# Sobald alle nötigen Maßnahmen erledigt sind, muss der Abschluss mit CancelDelayedShutdown($name) an +# FHEM zurückgemeldet werden. +####################################################################################################### +sub SSCam_DelayedShutdown($) { + my ($hash) = @_; + my $name = $hash->{NAME}; + + Log3($name, 1, "$name - Quit session due to shutdown ..."); + $hash->{HELPER}{ACTIVE} = "on"; # keine weiteren Aktionen erlauben + SSCam_logout($hash); + +return 1; +} + +################################################################# +# Wenn ein Gerät in FHEM gelöscht wird, wird zuerst die Funktion +# X_Undef aufgerufen um offene Verbindungen zu schließen, +# anschließend wird die Funktion X_Delete aufgerufen. +# Funktion: Aufräumen von dauerhaften Daten, welche durch das +# Modul evtl. für dieses Gerät spezifisch erstellt worden sind. +# Es geht hier also eher darum, alle Spuren sowohl im laufenden +# FHEM-Prozess, als auch dauerhafte Daten bspw. im physikalischen +# Gerät zu löschen die mit dieser Gerätedefinition zu tun haben. +################################################################# sub SSCam_Delete($$) { my ($hash, $arg) = @_; my $index = $hash->{TYPE}."_".$hash->{NAME}."_credentials"; @@ -1893,7 +1944,7 @@ sub SSCam_initonboot ($) { InternalTimer(gettimeofday()+int(rand(30)), "SSCam_wdpollcaminfo", $hash, 0); } else { - InternalTimer(gettimeofday()+1, "SSCam_initonboot", $hash, 0); + InternalTimer(gettimeofday()+3, "SSCam_initonboot", $hash, 0); } return; } @@ -1912,7 +1963,7 @@ sub SSCam_versionCheck($) { my $cs = ReadingsVal($name, "compstate", "true"); if($cs eq "false") { Log3($name, 2, "$name - WARNING - The current/simulated SVS-version ".ReadingsVal($name, "SVSversion", ""). - " may be incompatible with SSCam version $hash->{VERSION}. ". + " may be incompatible with SSCam version $hash->{HELPER}{VERSION}. ". "For further information execute \"get $name versionNotes 4\"."); } @@ -6390,6 +6441,7 @@ sub SSCam_logout ($) { }; HttpUtils_NonblockingGet ($param); + } sub SSCam_logout_return ($) { @@ -6430,7 +6482,7 @@ sub SSCam_logout_return ($) { if ($success) { # die Logout-URL konnte erfolgreich aufgerufen werden - Log3($name, 4, "$name - Session of User $username has ended - SID: \"$sid\" has been deleted"); + Log3($name, 2, "$name - Session of User \"$username\" terminated - session ID \"$sid\" deleted"); } else { # Errorcode aus JSON ermitteln @@ -6448,6 +6500,7 @@ sub SSCam_logout_return ($) { # ausgeführte Funktion ist erledigt (auch wenn logout nicht erfolgreich), Freigabe Funktionstoken SSCam_delActiveToken($hash); + CancelDelayedShutdown($name); return; } @@ -8825,6 +8878,41 @@ sub SSCam_trim ($) { return ($str); } +############################################################################################# +# Versionierungen des Moduls setzen +# Die Verwendung von Meta.pm und Packages wird berücksichtigt +############################################################################################# +sub SSCam_setVersionInfo($) { + my ($hash) = @_; + my $name = $hash->{NAME}; + + my $v = (SSCam_sortVersion("desc",keys %SSCam_vNotesIntern))[0]; + my $type = $hash->{TYPE}; + $hash->{HELPER}{PACKAGE} = __PACKAGE__; + $hash->{HELPER}{VERSION} = $v; + + if($modules{$type}{META}{x_prereqs_src} && !$hash->{HELPER}{MODMETAABSENT}) { + # META-Daten sind vorhanden + $modules{$type}{META}{version} = "v".$v; # Version aus META.json überschreiben, Anzeige mit {Dumper $modules{SMAPortal}{META}} + if($modules{$type}{META}{x_version}) { # {x_version} ( nur gesetzt wenn $Id$ im Kopf komplett! vorhanden ) + $modules{$type}{META}{x_version} =~ s/1.1.1/$v/g; + } else { + $modules{$type}{META}{x_version} = $v; + } + return $@ unless (FHEM::Meta::SetInternals($hash)); # FVERSION wird gesetzt ( nur gesetzt wenn $Id$ im Kopf komplett! vorhanden ) + if(__PACKAGE__ eq "FHEM::$type" || __PACKAGE__ eq $type) { + # es wird mit Packages gearbeitet -> Perl übliche Modulversion setzen + # mit {->VERSION()} im FHEMWEB kann Modulversion abgefragt werden + use version 0.77; our $VERSION = FHEM::Meta::Get( $hash, 'version' ); + } + } else { + # herkömmliche Modulstruktur + $hash->{VERSION} = $v; + } + +return; +} + ############################################################################################# # Hint Hash EN ############################################################################################# @@ -12513,4 +12601,63 @@ attr <name> genericStrmHtmlTag <img $HTMLATTR =end html_DE + +=for :application/json;q=META.json 49_SSCam.pm +{ + "abstract": "Module to control cameras as well as other functions of the Synology Surveillance Station.", + "x_lang": { + "de": { + "abstract": "Modul zur Steuerung von Kameras und anderen Funktionen der Synology Surveillance Station." + } + }, + "keywords": [ + "camera", + "control", + "PTZ", + "Synology Surveillance Station", + "MJPEG", + "HLS", + "RTSP" + ], + "version": "v1.1.1", + "release_status": "stable", + "author": [ + "Heiko Maaz " + ], + "x_fhem_maintainer": [ + "DS_Starter" + ], + "x_fhem_maintainer_github": [ + "nasseeder1" + ], + "prereqs": { + "runtime": { + "requires": { + "FHEM": 5.00918799, + "perl": 5.014, + "POSIX": 0, + "JSON": 0, + "Data::Dumper": 0, + "MIME::Base64": 0, + "Time::HiRes": 0, + "HttpUtils": 0, + "Blocking": 0, + "Encode": 0 + }, + "recommends": { + "FHEM::Meta": 0 + }, + "suggests": { + } + } + }, + "resources": { + "x_wiki": { + "web": "https://wiki.fhem.de/wiki/SSCAM_-_Steuerung_von_Kameras_in_Synology_Surveillance_Station", + "title": "SSCAM - Steuerung von Kameras in Synology Surveillance Station" + } + } +} +=end :application/json;q=META.json + =cut diff --git a/FHEM/49_SSCamSTRM.pm b/FHEM/49_SSCamSTRM.pm index 2a2dd1aee..6eedb2c37 100644 --- a/FHEM/49_SSCamSTRM.pm +++ b/FHEM/49_SSCamSTRM.pm @@ -31,9 +31,11 @@ package main; use strict; use warnings; +eval "use FHEM::Meta;1" or my $modMetaAbsent = 1; # Versions History intern our %SSCamSTRM_vNotesIntern = ( + "2.5.0" => "27.03.2019 add Meta.pm support ", "2.4.0" => "24.02.2019 support for \"genericStrmHtmlTag\" in streaming device MODEL generic ", "2.3.0" => "04.02.2019 SSCamSTRM_Rename / SSCamSTRM_Copy added, Streaming device can now be renamed or copied ", "2.2.1" => "19.12.2018 commandref revised ", @@ -89,13 +91,13 @@ sub SSCamSTRM_Initialize($) { $hash->{FW_summaryFn} = "SSCamSTRM_FwFn"; $hash->{FW_detailFn} = "SSCamSTRM_FwFn"; $hash->{AttrFn} = "SSCamSTRM_Attr"; - $hash->{FW_hideDisplayName} = 1; # Forum 88667 + $hash->{FW_hideDisplayName} = 1; # Forum 88667 # $hash->{FW_addDetailToSummary} = 1; - # $hash->{FW_atPageEnd} = 1; # wenn 1 -> kein Longpoll ohne informid in HTML-Tag + # $hash->{FW_atPageEnd} = 1; # wenn 1 -> kein Longpoll ohne informid in HTML-Tag - #$data{FWEXT}{SSCAMSTRM}{SCRIPT} = "/pgm2/".$hlsjs if (!$data{FWEXT}{SSCAMSTRM}{SCRIPT}); + eval { FHEM::Meta::InitMod( __FILE__, $hash ) }; # für Meta.pm (https://forum.fhem.de/index.php/topic,97589.0.html) -return undef; +return; } ################################################################ @@ -109,10 +111,12 @@ sub SSCamSTRM_Define($$) { my $arg = (split("[()]",$link))[1]; $arg =~ s/'//g; - ($hash->{PARENT},$hash->{MODEL}) = ((split(",",$arg))[0],(split(",",$arg))[2]); + ($hash->{PARENT},$hash->{MODEL}) = ((split(",",$arg))[0],(split(",",$arg))[2]); + $hash->{HELPER}{MODMETAABSENT} = 1 if($modMetaAbsent); # Modul Meta.pm nicht vorhanden + $hash->{LINK} = $link; - $hash->{VERSION} = $hash->{VERSION} = (reverse sort(keys %SSCamSTRM_vNotesIntern))[0]; - $hash->{LINK} = $link; + # Versionsinformationen setzen + SSCamSTRM_setVersionInfo($hash); readingsSingleUpdate($hash,"state", "initialized", 1); # Init für "state" @@ -288,10 +292,45 @@ sub SSCamSTRM_refresh($) { return; } +############################################################################################# +# Versionierungen des Moduls setzen +# Die Verwendung von Meta.pm und Packages wird berücksichtigt +############################################################################################# +sub SSCamSTRM_setVersionInfo($) { + my ($hash) = @_; + my $name = $hash->{NAME}; + + my $v = (sortTopicNum("desc",keys %SSCamSTRM_vNotesIntern))[0]; + my $type = $hash->{TYPE}; + $hash->{HELPER}{PACKAGE} = __PACKAGE__; + $hash->{HELPER}{VERSION} = $v; + + if($modules{$type}{META}{x_prereqs_src} && !$hash->{HELPER}{MODMETAABSENT}) { + # META-Daten sind vorhanden + $modules{$type}{META}{version} = "v".$v; # Version aus META.json überschreiben, Anzeige mit {Dumper $modules{SMAPortal}{META}} + if($modules{$type}{META}{x_version}) { # {x_version} ( nur gesetzt wenn $Id$ im Kopf komplett! vorhanden ) + $modules{$type}{META}{x_version} =~ s/1.1.1/$v/g; + } else { + $modules{$type}{META}{x_version} = $v; + } + return $@ unless (FHEM::Meta::SetInternals($hash)); # FVERSION wird gesetzt ( nur gesetzt wenn $Id$ im Kopf komplett! vorhanden ) + if(__PACKAGE__ eq "FHEM::$type" || __PACKAGE__ eq $type) { + # es wird mit Packages gearbeitet -> Perl übliche Modulversion setzen + # mit {->VERSION()} im FHEMWEB kann Modulversion abgefragt werden + use version 0.77; our $VERSION = FHEM::Meta::Get( $hash, 'version' ); + } + } else { + # herkömmliche Modulstruktur + $hash->{VERSION} = $v; + } + +return; +} + 1; =pod -=item summary define a Streaming device by SSCam module +=item summary Definition of a streaming device by the SSCam module =item summary_DE Erstellung eines Streaming-Device durch das SSCam-Modul =begin html @@ -647,4 +686,55 @@ attr <name> genericStrmHtmlTag <img $HTMLATTR =end html_DE + +=for :application/json;q=META.json 49_SSCamSTRM.pm +{ + "abstract": "Definition of a streaming device by the SSCam module", + "x_lang": { + "de": { + "abstract": "Erstellung eines Streaming-Device durch das SSCam-Modul" + } + }, + "keywords": [ + "camera", + "streaming", + "PTZ", + "Synology Surveillance Station", + "MJPEG", + "HLS", + "RTSP" + ], + "version": "v1.1.1", + "release_status": "stable", + "author": [ + "Heiko Maaz " + ], + "x_fhem_maintainer": [ + "DS_Starter" + ], + "x_fhem_maintainer_github": [ + "nasseeder1" + ], + "prereqs": { + "runtime": { + "requires": { + "FHEM": 5.00918799, + "perl": 5.014 + }, + "recommends": { + "FHEM::Meta": 0 + }, + "suggests": { + } + } + }, + "resources": { + "x_wiki": { + "web": "https://wiki.fhem.de/wiki/SSCAM_-_Steuerung_von_Kameras_in_Synology_Surveillance_Station", + "title": "SSCAM - Steuerung von Kameras in Synology Surveillance Station" + } + } +} +=end :application/json;q=META.json + =cut