From c6718e153a13062a3e9afe47796dcc1e24b1b8d1 Mon Sep 17 00:00:00 2001 From: charlie71 <> Date: Sat, 21 Dec 2019 18:09:43 +0000 Subject: [PATCH] 44_S7:disable attribute added git-svn-id: https://svn.fhem.de/fhem/trunk@20795 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/44_S7.pm | 51 +++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/fhem/FHEM/44_S7.pm b/fhem/FHEM/44_S7.pm index edd619a27..622f22551 100644 --- a/fhem/FHEM/44_S7.pm +++ b/fhem/FHEM/44_S7.pm @@ -57,7 +57,7 @@ sub S7_Initialize($) { #S5_OK $hash->{SetFn} = "S7_Set"; $hash->{AttrFn} = "S7_Attr"; - $hash->{AttrList} = "MaxMessageLength Intervall receiveTimeoutMs " . $readingFnAttributes; + $hash->{AttrList} = "disable:0,1 MaxMessageLength Intervall receiveTimeoutMs " . $readingFnAttributes; # $hash->{AttrList} = join( " ", @areasconfig )." PLCTime"; } @@ -248,11 +248,11 @@ sub S7_Define($$) { # S5 OK $hash->{STATE} = "disconnected"; main::readingsSingleUpdate( $hash, "state", "disconnected", 1 ); - S7_connect($hash); - - InternalTimer( gettimeofday() + $hash->{Interval}, - "S7_GetUpdate", $hash, 0 ); - + if (!S7_isDisabled($hash)) { + S7_connect($hash); + InternalTimer( gettimeofday() + $hash->{Interval}, + "S7_GetUpdate", $hash, 0 ); + } return undef; } @@ -339,9 +339,18 @@ sub S7_Attr(@) { $hash->{S7PLCClient}->setRecvTimeout($hash->{receiveTimeoutMs}) if ( defined( $hash->{S7PLCClient} ) ); } + } elsif ($aName eq "disable") { + if ($aVal == 1 && $attr{$name}{disable}==0) { + #disconnection will be done by the update timer + + } elsif ($aVal == 0 && $attr{$name}{disable}==1) { + #reconnect + S7_reconnect($hash); + InternalTimer( gettimeofday() + $hash->{Interval} + 3, + "S7_GetUpdate", $hash, 0 ); + } } - - + ########### if ( $aName eq "WriteInputs-Config" @@ -787,16 +796,18 @@ sub S7_GetUpdate($) { my $res = S7_readFromPLC($hash); - if ( $res == 0 ) { - InternalTimer( gettimeofday() + $hash->{Interval}, - "S7_GetUpdate", $hash, 1 ); - } - else { + if (!S7_isDisabled($hash)) { + if ( $res == 0 ) { + InternalTimer( gettimeofday() + $hash->{Interval}, + "S7_GetUpdate", $hash, 1 ); + }else { - #an error has occoured --> 10sec break - InternalTimer( gettimeofday() + 10, "S7_GetUpdate", $hash, 1 ); + #an error has occoured --> 10sec break + InternalTimer( gettimeofday() + 10, "S7_GetUpdate", $hash, 1 ); + } + } else { + S7_disconnect($hash); } - } ##################################### @@ -1115,6 +1126,14 @@ sub S7_readFromPLC($) { #S5 OK return 0; } +sub S7_isDisabled($) { + my ($hash) = @_; + my $name = $hash->{NAME}; + + return $attr{$name}{disable} == 1 if (defined ($attr{$name}{disable})); + return 0; +} + 1;