2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-25 22:09:21 +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 $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 <name> VIESSMANN <port> <command_config> [<interval>]
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};
@ -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,9 +373,10 @@ 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) {
#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();
@ -398,9 +392,8 @@ VCONTROL_Read($)
sleep(5);
return "";
}
}
else {
if(!defined($mybuf) || length($mybuf) == 0) {
#TCP Connection
my $dev = $hash->{DeviceName};
Log3 $name, 3,"VCONTROL: LAN device $dev disconnected, waiting to reappear";
DevIo_Disconnected($hash);
@ -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) {
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;
if ($TCP_CONNECTION==0) {
###USB
if (index($dev, ':') == -1) {
$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);
}
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,13 +909,14 @@ VCONTROL_ReInit($)
#Opening USB Device
Log3($name, 3, "VCONTROL ReInit VCONTROL device $dev");
my $ret = undef;
my $po;
if ($TCP_CONNECTION==0) {
###USB
my $po;
if ($^O=~/Win/) {
require Win32::SerialPort;
$po = new Win32::SerialPort ($dev);
} else {
}
else {
require Device::SerialPort;
$po = new Device::SerialPort ($dev);
}
@ -929,14 +925,15 @@ VCONTROL_ReInit($)
Log3($name, 3, $msg) if($hash->{MOBILE});
return $msg if(!$hash->{MOBILE});
$readyfnlist{"$name.$dev"} = $hash;
return "";
return undef;
}
Log3($name, 3, "VCONTROL opened VCONTROL device $dev");
$hash->{USBDev} = $po;
if( $^O =~ /Win/ ) {
$readyfnlist{"$name.$dev"} = $hash;
} else {
}
else {
$hash->{FD} = $po->FILENO;
delete($readyfnlist{"$name.$dev"});
$selectlist{"$name.$dev"} = $hash;
@ -944,10 +941,13 @@ VCONTROL_ReInit($)
#Initialize to be able to receive data
VCONTROL_DoInit($hash, $po);
return $ret;
}
else {
###TCP COnnection
DevIo_OpenDev($hash, 0, "VCONTROL_DoInit");
}
}
#####################################
#####################################
## Load Config