2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-28 11:01:59 +00:00

89_VCONTROL: LAN Reconnects corrected

git-svn-id: https://svn.fhem.de/fhem/trunk@11330 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
adamwit 2016-04-27 18:10:03 +00:00
parent c470eb1a3b
commit e6d6ba859b

View File

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