From e6d6ba859bd09c8318e856b39d67c74dd5f0299a Mon Sep 17 00:00:00 2001 From: adamwit <> Date: Wed, 27 Apr 2016 18:10:03 +0000 Subject: [PATCH] 89_VCONTROL: LAN Reconnects corrected git-svn-id: https://svn.fhem.de/fhem/trunk@11330 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/89_VCONTROL.pm | 266 +++++++++++++++++++-------------------- 1 file changed, 133 insertions(+), 133 deletions(-) diff --git a/fhem/FHEM/89_VCONTROL.pm b/fhem/FHEM/89_VCONTROL.pm index 9edfbde1b..31f3f7493 100644 --- a/fhem/FHEM/89_VCONTROL.pm +++ b/fhem/FHEM/89_VCONTROL.pm @@ -72,8 +72,8 @@ my %DayHash; my @mode = ("WW","RED","NORM","H+WW","H+WW FS","ABSCHALT"); my $temp_mode=0; -#define LAN Hardware (1) or USB device (0) -my $LAN_HW = 0; +#define TCP Connection (1) or USB device (0) +my $TCP_CONNECTION = 0; ###################################################################################### sub VCONTROL_1ByteUParse($$); @@ -105,8 +105,7 @@ sub VCONTROL_CmdConfig($); sub VCONTROL_ReInit($); -sub -VCONTROL_Initialize($) +sub VCONTROL_Initialize($) { my ($hash) = @_; @@ -127,8 +126,7 @@ VCONTROL_Initialize($) ##################################### # define VIESSMANN [] -sub -VCONTROL_Define($$) +sub VCONTROL_Define($$) { my ($hash, $def) = @_; my @a = split("[ \t][ \t]*", $def); @@ -140,16 +138,18 @@ VCONTROL_Define($$) return $msg; } - #Close Device to initialize properly - ###USB + #Determine if USB device or TCP Connection is used + if (index($a[2], ':') == -1) { + ###USB delete $hash->{USBDev}; delete $hash->{FD}; } else { - ###LAN Hardware found - $LAN_HW = 1; + ###TCP Connection + $TCP_CONNECTION = 1; } + #Close Device to initialize properly DevIo_CloseDev($hash); my $name = $a[0]; @@ -189,9 +189,9 @@ VCONTROL_Define($$) Log3($name, 3, "VCONTROL opening VCONTROL device $dev"); my $ret = undef; - ###USB - if (index($a[2], ':') == -1) { - + + if ($TCP_CONNECTION == 0) { + ###USB if ($^O=~/Win/) { require Win32::SerialPort; $po = new Win32::SerialPort ($dev); @@ -222,6 +222,7 @@ VCONTROL_Define($$) } else { + ###TCP Connection $ret = DevIo_OpenDev($hash, 0, "VCONTROL_DoInit"); } @@ -232,14 +233,12 @@ VCONTROL_Define($$) InternalTimer(gettimeofday()+1, "VCONTROL_Poll", $hash, 0); return $ret; - } ##################################### # Input is hexstring ## This function will not be used until now! -#sub -#VCONTROL_Write($$) +#sub VCONTROL_Write($$) #{ # my ($hash,$fn,$msg) = @_; # my $name = $hash->{NAME}; @@ -255,8 +254,7 @@ VCONTROL_Define($$) ##################################### -sub -VCONTROL_Undef($$) +sub VCONTROL_Undef($$) { my ($hash, $arg) = @_; my $name = $hash->{NAME}; @@ -277,8 +275,7 @@ VCONTROL_Undef($$) } ##################################### -sub -VCONTROL_Poll($) +sub VCONTROL_Poll($) { my $hash = shift; my $name = $hash->{NAME}; @@ -289,7 +286,7 @@ VCONTROL_Poll($) Log3 $name, 5, "VCONTROL: Poll disabled!"; } else - { $poll_now=POLL_ACTIVE; + { $poll_now = POLL_ACTIVE; if( AttrVal($name, "init_every_poll", 0 ) == 1 ) { VCONTROL_ReInit($hash); @@ -303,23 +300,21 @@ VCONTROL_Poll($) } ##################################### -sub -VCONTROL_Shutdown($) +sub VCONTROL_Shutdown($) { my ($hash) = @_; return undef; } ##################################### -sub -VCONTROL_SetState($$$$) +sub VCONTROL_SetState($$$$) { my ($hash, $tim, $vt, $val) = @_; return undef; } -sub -VCONTROL_Clear($) +##################################### +sub VCONTROL_Clear($) { my $hash = shift; my $buf; @@ -337,8 +332,7 @@ VCONTROL_Clear($) } ##################################### -sub -VCONTROL_DoInit($$) +sub VCONTROL_DoInit($$) { #Initialisation -> Send one 0x04 so the heating started to send 0x05 Synchronity-Bytes my ($hash,$po) = @_; @@ -368,8 +362,7 @@ VCONTROL_DoInit($$) ##################################### # called from the global loop, when the select for hash->{FD} reports data -sub -VCONTROL_Read($) +sub VCONTROL_Read($) { my ($hash) = @_; my $name = $hash->{NAME}; @@ -380,33 +373,33 @@ VCONTROL_Read($) #Read on Device my $mybuf = DevIo_SimpleRead($hash); - if ($LAN_HW == 0) { - #USB device is disconnected try to connect again if(!defined($mybuf) || length($mybuf) == 0) { - my $dev = $hash->{DeviceName}; - Log3 $name, 3,"VCONTROL: USB device $dev disconnected, waiting to reappear"; - #$hash->{USBDev}->close(); - DoTrigger($name, "DISCONNECTED"); - DevIo_Disconnected($hash); - delete($hash->{USBDev}); - delete($selectlist{"$name.$dev"}); - $readyfnlist{"$name.$dev"} = $hash; # Start polling - $hash->{STATE} = "disconnected"; + #Lost connection to the device + if ($TCP_CONNECTION == 0) { + #USB device + my $dev = $hash->{DeviceName}; + Log3 $name, 3,"VCONTROL: USB device $dev disconnected, waiting to reappear"; + #$hash->{USBDev}->close(); + DoTrigger($name, "DISCONNECTED"); + DevIo_Disconnected($hash); + delete($hash->{USBDev}); + delete($selectlist{"$name.$dev"}); + $readyfnlist{"$name.$dev"} = $hash; # Start polling + $hash->{STATE} = "disconnected"; - # Without the following sleep the open of the device causes a SIGSEGV, - # and following opens block infinitely. Only a reboot helps. - sleep(5); - return ""; - } - } - else { - if(!defined($mybuf) || length($mybuf) == 0) { - my $dev = $hash->{DeviceName}; - Log3 $name, 3,"VCONTROL: LAN device $dev disconnected, waiting to reappear"; - DevIo_Disconnected($hash); - $hash->{STATE} = "disconnected"; - return ""; - } + # Without the following sleep the open of the device causes a SIGSEGV, + # and following opens block infinitely. Only a reboot helps. + sleep(5); + return ""; + } + else { + #TCP Connection + my $dev = $hash->{DeviceName}; + Log3 $name, 3,"VCONTROL: LAN device $dev disconnected, waiting to reappear"; + DevIo_Disconnected($hash); + $hash->{STATE} = "disconnected"; + return ""; + } } #msg read on device @@ -437,7 +430,8 @@ VCONTROL_Read($) #exit if no poll period #exit if no set command send if ($poll_now == POLL_PAUSED && $send_now == NO_SEND ){ - return ""; } + return ""; + } my $sendbuf=""; #End of Poll Interval @@ -471,10 +465,18 @@ VCONTROL_Read($) my $closeAfterPoll = AttrVal($name, "closedev", "0"); ###if Attribut to close after poll -> close if ($closeAfterPoll == 1) { - delete $hash->{USBDev}; - delete $hash->{FD}; - Log3 $name, 3, "VCONTROL: USB device closed"; - } + if ($TCP_CONNECTION == 0) { + ###USB + delete $hash->{USBDev}; + delete $hash->{FD}; + Log3 $name, 3, "VCONTROL: USB device closed"; + } + else { + ###TCP Connection + DevIo_CloseDev($hash); + Log3 $name, 3, "VCONTROL: TCP connection closed"; + } + } return ""; }; @@ -493,7 +495,8 @@ VCONTROL_Read($) Log3 $name, 5, "VCONTROL: exit if buffer just filled with 0x05"; $hash->{PARTIAL} = ""; $read_now = READ_UNDEF; - return ""; } + return ""; + } #if one 05 received we can send command if ( length($hexline) == 2 && $hexline eq "05" && $read_now == READ_UNDEF) @@ -571,8 +574,7 @@ VCONTROL_Read($) } ##################################### -sub -VCONTROL_Parse($$$$) +sub VCONTROL_Parse($$$$) { my ($hash, $cmd, $hexline,$answer) = @_; @@ -723,8 +725,7 @@ VCONTROL_Parse($$$$) ##################################### -sub -VCONTROL_Ready($) +sub VCONTROL_Ready($) { my ($hash) = @_; my $dev = $hash->{DeviceName}; @@ -732,13 +733,14 @@ VCONTROL_Ready($) my $po; - ###USB - if (index($dev, ':') == -1) { + if ($TCP_CONNECTION==0) { + ###USB $po=$hash->{USBDev}; if(!$po) { # Looking for the device if ($^O=~/Win/) { $po = new Win32::SerialPort ($dev); - } else { + } + else { $po = new Device::SerialPort ($dev); } return undef if(!$po); @@ -749,39 +751,29 @@ VCONTROL_Ready($) $hash->{FD} = $po->FILENO; delete($readyfnlist{"$name.$dev"}); $selectlist{"$name.$dev"} = $hash; - } else { + } + else { $readyfnlist{"$name.$dev"} = $hash; } $hash->{PARTIAL} = ""; VCONTROL_DoInit($hash, $po); DoTrigger($name, "CONNECTED"); $hash->{STATE} = "connected"; - return undef; - } - } else { - $hash->{PARTIAL} = ""; - return DevIo_OpenDev($hash, 1, "VCONTROL_DoInit") - if($hash->{STATE} eq "disconnected"); - # This is relevant for windows/USB only - my $po = $hash->{USBDev}; - my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags); - if($po) { - ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status; - } - return ($InBytes && $InBytes>0); - } - - - # This is relevant for windows only - if (index($dev, ':') == -1) { - return undef if !$po; - my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags)=$po->status; - return ($InBytes>0); - } + } + return undef if !$po; + my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags)=$po->status; + return ($InBytes>0); + } + else { + ###TCP Connection + $hash->{PARTIAL} = ""; + return DevIo_OpenDev($hash, 1, "VCONTROL_DoInit") if($hash->{STATE} eq "disconnected"); + } } -sub -VCONTROL_Set($@) + +##################################### +sub VCONTROL_Set($@) { my ($hash, @a) = @_; my $pn = $hash->{NAME}; @@ -853,7 +845,6 @@ VCONTROL_Set($@) readingsBulkUpdate ($hash, $arg, $value); readingsEndUpdate ($hash, 1); } - } #else { @@ -862,6 +853,7 @@ VCONTROL_Set($@) return ""; } +##################################### sub VCONTROL_Get($@) { my ($hash, @a) = @_; return "no get value specified" if(@a < 2); @@ -882,7 +874,9 @@ sub VCONTROL_Get($@) { VCONTROL_CmdConfig($command_config_file); @cmd_list = @poll_cmd_list; } - else { $get_config_now = GET_CONFIG_ACTIVE; } + else { + $get_config_now = GET_CONFIG_ACTIVE; + } return ""; } @@ -892,15 +886,16 @@ sub VCONTROL_Get($@) { RemoveInternalTimer($hash); VCONTROL_Poll($hash); } - else { $get_timer_now = GET_TIMER_ACTIVE; } - - + else { + $get_timer_now = GET_TIMER_ACTIVE; + } + return ""; } -sub -VCONTROL_ReInit($) +##################################### +sub VCONTROL_ReInit($) { my $hash = shift; my $name = $hash->{NAME}; @@ -914,40 +909,45 @@ VCONTROL_ReInit($) #Opening USB Device Log3($name, 3, "VCONTROL ReInit VCONTROL device $dev"); - my $ret = undef; - my $po; - ###USB - if ($^O=~/Win/) { - require Win32::SerialPort; - $po = new Win32::SerialPort ($dev); - } else { - require Device::SerialPort; - $po = new Device::SerialPort ($dev); - } - if(!$po) { - my $msg = "Can't open $dev: $!"; - Log3($name, 3, $msg) if($hash->{MOBILE}); - return $msg if(!$hash->{MOBILE}); - $readyfnlist{"$name.$dev"} = $hash; - return ""; - } - Log3($name, 3, "VCONTROL opened VCONTROL device $dev"); + if ($TCP_CONNECTION==0) { + ###USB + my $po; + if ($^O=~/Win/) { + require Win32::SerialPort; + $po = new Win32::SerialPort ($dev); + } + else { + require Device::SerialPort; + $po = new Device::SerialPort ($dev); + } + if(!$po) { + my $msg = "Can't open $dev: $!"; + Log3($name, 3, $msg) if($hash->{MOBILE}); + return $msg if(!$hash->{MOBILE}); + $readyfnlist{"$name.$dev"} = $hash; + return undef; + } + Log3($name, 3, "VCONTROL opened VCONTROL device $dev"); - $hash->{USBDev} = $po; - if( $^O =~ /Win/ ) { - $readyfnlist{"$name.$dev"} = $hash; - } else { - $hash->{FD} = $po->FILENO; - delete($readyfnlist{"$name.$dev"}); - $selectlist{"$name.$dev"} = $hash; - } + $hash->{USBDev} = $po; + if( $^O =~ /Win/ ) { + $readyfnlist{"$name.$dev"} = $hash; + } + else { + $hash->{FD} = $po->FILENO; + delete($readyfnlist{"$name.$dev"}); + $selectlist{"$name.$dev"} = $hash; + } - #Initialize to be able to receive data - VCONTROL_DoInit($hash, $po); - - return $ret; - + #Initialize to be able to receive data + VCONTROL_DoInit($hash, $po); + } + else { + ###TCP COnnection + DevIo_OpenDev($hash, 0, "VCONTROL_DoInit"); + } } + ##################################### ##################################### ## Load Config @@ -1039,7 +1039,7 @@ sub VCONTROL_CmdConfig($) $write_idx++; } } - else{ + else { Log3 undef, 3, "VCONTROL: unknown command '$cfgarray[0]' in '$cmd_config_file'"; } }