From 4e77ae8c64acb1db81471dda9ff906be5b9c0fb4 Mon Sep 17 00:00:00 2001 From: rleins <> Date: Mon, 14 Mar 2016 20:57:40 +0000 Subject: [PATCH] MEDIAPORTAL: Added HeartbeatInterval and disable attribute git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@11067 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- FHEM/70_MEDIAPORTAL.pm | 178 ++++++++++++++++++++++++++--------------- 1 file changed, 114 insertions(+), 64 deletions(-) diff --git a/FHEM/70_MEDIAPORTAL.pm b/FHEM/70_MEDIAPORTAL.pm index ffaa75fd2..ae34e55bd 100644 --- a/FHEM/70_MEDIAPORTAL.pm +++ b/FHEM/70_MEDIAPORTAL.pm @@ -23,6 +23,9 @@ # ######################################################################################## # Changelog +# 14.03.2016 +# Es gibt nun ein Attribut "HeartbeatInterval", mit dem das Intervall für die Verbindungsprüfung festgelegt werden kann. Ein Wert von "0" deaktiviert die Prüfung. +# Es gibt nun das Attribut "disable", mit dem das Modul deaktiviert werden kann. # 08.02.2016 # Neuer MediaType "recording" hinzugefügt # 07.02.2016 @@ -58,7 +61,7 @@ use Data::Dumper; sub MEDIAPORTAL_Set($@); sub MEDIAPORTAL_Log($$$); -my $MEDIAPORTAL_Interval = 15; +my $MEDIAPORTAL_HeartbeatInterval = 15; ######################################################################################## # @@ -76,8 +79,8 @@ sub MEDIAPORTAL_Initialize($) { $hash->{SetFn} = 'MEDIAPORTAL_Set'; $hash->{DefFn} = 'MEDIAPORTAL_Define'; $hash->{UndefFn} = 'MEDIAPORTAL_Undef'; - #$hash->{AttrFn} = 'MEDIAPORTAL_Attr'; - $hash->{AttrList} = 'authmethod:none,userpassword,passcode,both username password generateNowPlayingUpdateEvents:1,0 macaddress '.$readingFnAttributes; + $hash->{AttrFn} = 'MEDIAPORTAL_Attribute'; + $hash->{AttrList} = 'authmethod:none,userpassword,passcode,both username password HeartbeatInterval generateNowPlayingUpdateEvents:1,0 macaddress '.$readingFnAttributes; $hash->{STATE} = 'Initialized'; } @@ -105,7 +108,8 @@ sub MEDIAPORTAL_Define($$) { $hash->{DeviceName} = $dev; $hash->{STATE} = 'Disconnected'; - my $ret = DevIo_OpenDev($hash, 0, 'MEDIAPORTAL_DoInit'); + my $ret = undef; + $ret = DevIo_OpenDev($hash, 0, 'MEDIAPORTAL_DoInit') if AttrVal($hash->{NAME}, 'disable', 0); return $ret; } @@ -124,6 +128,93 @@ sub MEDIAPORTAL_Undef($$) { return undef; } +######################################################################################## +# +# MEDIAPORTAL_Attribute +# +######################################################################################## +sub MEDIAPORTAL_Attribute($@) { + my ($mode, $devName, $attrName, $attrValue) = @_; + my $hash = $defs{$devName}; + + my $disableChange = 0; + if($mode eq 'set') { + if ($attrName eq 'disable') { + if ($attrValue && AttrVal($devName, $attrName, 0) != 1) { + MEDIAPORTAL_Log($devName, 5, 'Neu-Disabled'); + $disableChange = 1; + } + + if (!$attrValue && AttrVal($devName, $attrName, 0) != 0) { + MEDIAPORTAL_Log($devName, 5, 'Neu-Enabled'); + $disableChange = 1; + } + } + } elsif ($mode eq 'del') { + if ($attrName eq 'disable') { + if (AttrVal($devName, $attrName, 0) != 0) { + MEDIAPORTAL_Log($devName, 5, 'Deleted-Disabled'); + $disableChange = 1; + $attrValue = 0; + } + } + } + + if ($disableChange) { + # Wenn die Verbindung beendet werden muss... + if ($attrValue) { + MEDIAPORTAL_Log $devName, 5, 'Call AttributeFn: Stop Connection...'; + DevIo_CloseDev($hash); + } + + # Wenn die Verbindung gestartet werden muss... + if (!$attrValue) { + MEDIAPORTAL_Log $devName, 5, 'Call AttributeFn: Start Connection...'; + DevIo_OpenDev($hash, 0, 'MEDIAPORTAL_DoInit'); + } + } + + return undef; +} + +######################################################################################## +# +# MEDIAPORTAL_DoInit +# +######################################################################################## +sub MEDIAPORTAL_DoInit($) { + my ($hash) = @_; + + $hash->{STATE} = 'Connecting...'; + $hash->{helper}{buffer} = ''; + $hash->{helper}{LastStatusTimestamp} = time(); + + # Versuch, die MAC-Adresse des Ziels selber herauszufinden... + if (AttrVal($hash->{NAME}, 'macaddress', '') eq '') { + my $newmac = MEDIAPORTAL_GetMAC($hash); + + if (defined($newmac)) { + CommandAttr(undef, $hash->{NAME}.' macaddress '.$newmac); + } + } + + RemoveInternalTimer($hash); + InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, 'HeartbeatInterval', $MEDIAPORTAL_HeartbeatInterval), 'MEDIAPORTAL_GetIntervalStatus', $hash, 0) if AttrVal($hash->{NAME}, 'HeartbeatInterval', $MEDIAPORTAL_HeartbeatInterval); + + return undef; +} + +######################################################################################## +# +# MEDIAPORTAL_Ready +# +######################################################################################## +sub MEDIAPORTAL_Ready($) { + my ($hash) = @_; + + return DevIo_OpenDev($hash, 1, 'MEDIAPORTAL_DoInit'); +} + ######################################################################################## # # MEDIAPORTAL_Get @@ -135,6 +226,8 @@ sub MEDIAPORTAL_Get($@) { my $cname = $a[1]; my $cmd = ''; + return 'Module disabled!' if AttrVal($hash->{NAME}, 'disable', 0); + if ($cname eq "status") { $cmd = "{\"Type\":\"requeststatus\"}\r\n"; } elsif ($cname eq "nowplaying") { @@ -169,20 +262,21 @@ sub MEDIAPORTAL_GetIntervalStatus($) { my ($hash) = @_; return undef if (ReadingsVal($hash->{NAME}, 'state', 'disconnected') eq 'disconnected'); + return undef if (!AttrVal($hash->{NAME}, 'HeartbeatInterval', $MEDIAPORTAL_HeartbeatInterval)); # Prüfen, wann der letzte Status zugestellt wurde... - if (time() - $hash->{helper}{LastStatusTimestamp} > (2 * $MEDIAPORTAL_Interval + 5)) { + if (time() - $hash->{helper}{LastStatusTimestamp} > (2 * $MEDIAPORTAL_HeartbeatInterval + 5)) { MEDIAPORTAL_Log $hash->{NAME}, 3, 'GetIntervalStatus hat festgestellt, dass Mediaportal sich seit '.(time() - $hash->{helper}{LastStatusTimestamp}).'s nicht zurückgemeldet hat. Die Verbindung wird neu aufgebaut!'; MEDIAPORTAL_Set($hash, ($hash->{NAME}, 'reconnect')); - InternalTimer(gettimeofday() + $MEDIAPORTAL_Interval, 'MEDIAPORTAL_GetIntervalStatus', $hash, 0); + InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, 'HeartbeatInterval', $MEDIAPORTAL_HeartbeatInterval), 'MEDIAPORTAL_GetIntervalStatus', $hash, 0); return undef; } # Status anfordern... MEDIAPORTAL_Get($hash, ($hash->{NAME}, 'status')); - InternalTimer(gettimeofday() + $MEDIAPORTAL_Interval, 'MEDIAPORTAL_GetIntervalStatus', $hash, 0); + InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, 'HeartbeatInterval', $MEDIAPORTAL_HeartbeatInterval), 'MEDIAPORTAL_GetIntervalStatus', $hash, 0); } ######################################################################################## @@ -213,6 +307,8 @@ sub MEDIAPORTAL_Set($@) { # Legacy Volume writing... $cname = 'Volume' if (lc($cname) eq 'volume'); + return 'Module disabled!' if AttrVal($hash->{NAME}, 'disable', 0); + if ($cname eq "command") { if (!MEDIAPORTAL_isInList($a[2], split(/ /, $mpcommands))) { return "Unknown command '$a[2]'. Supported commands are: $mpcommands"; @@ -311,7 +407,9 @@ sub MEDIAPORTAL_Read($) { $hash->{helper}{buffer} = ''; return undef; } - + + return undef if AttrVal($hash->{NAME}, 'disable', 0); + MEDIAPORTAL_Log $hash->{NAME}, 5, "RAW MSG: $buf"; # Zum Buffer hinzufügen @@ -472,62 +570,6 @@ sub MEDIAPORTAL_ProcessMessage($$) { } } -######################################################################################## -# -# MEDIAPORTAL_DoInit -# -######################################################################################## -sub MEDIAPORTAL_DoInit($) { - my ($hash) = @_; - - $hash->{STATE} = 'Connecting...'; - $hash->{helper}{buffer} = ''; - $hash->{helper}{LastStatusTimestamp} = time(); - - # Versuch, die MAC-Adresse des Ziels selber herauszufinden... - if (AttrVal($hash->{NAME}, 'macaddress', '') eq '') { - my $newmac = MEDIAPORTAL_GetMAC($hash); - - if (defined($newmac)) { - CommandAttr(undef, $hash->{NAME}.' macaddress '.$newmac); - } - } - - RemoveInternalTimer($hash); - InternalTimer(gettimeofday() + $MEDIAPORTAL_Interval, 'MEDIAPORTAL_GetIntervalStatus', $hash, 0); - - return undef; -} - -######################################################################################## -# -# MEDIAPORTAL_Ready -# -######################################################################################## -sub MEDIAPORTAL_Ready($) { - my ($hash) = @_; - - return DevIo_OpenDev($hash, 1, 'MEDIAPORTAL_DoInit'); -} - -######################################################################################## -# -# MEDIAPORTAL_Attr -# -######################################################################################## -#sub MEDIAPORTAL_Attr($@) { -# my (@a) = @_; -# -# my $hash = $defs{$a[1]}; -# my $name = $hash->{NAME}; -# -# MEDIAPORTAL_Log $hash->{NAME}, 5,"********** 1 $a[1] 2 $a[2]"; -# if($a[0] eq 'set') { -# } -# -# return undef; -#} - ######################################################################################## # # MEDIAPORTAL_GetMSG_identify @@ -768,8 +810,12 @@ sub MEDIAPORTAL_Log($$$) {
disable <value>
+generateNowPlayingUpdateEvents <value>
NowPlayingUpdate
-Events. If set, Fhem generates an event per second with the updated time-values for the current playing. Defaults to "0".HeartbeatInterval <interval>
+macaddress <address>
disable <value>
+generateNowPlayingUpdateEvents <value>
NowPlayingUpdate
-Events an- oder abgeschaltet werden. Wenn auf "1" gesetzt, generiert Fhem ein Event pro Sekunde mit den angepassten Zeitangaben. Standard ist "0".HeartbeatInterval <intervall>
+macaddress <address>