2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-07 12:58:13 +00:00

30_MilightBridge/98_ping: Use Blocking.pm so ping check does not hang main thread.

30_MilightBridge: Add support for tcp mode on bridge


git-svn-id: https://svn.fhem.de/fhem/trunk@10939 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
mattwire 2016-02-25 22:30:46 +00:00
parent 15f5acafd1
commit b273969d5b
3 changed files with 201 additions and 84 deletions

View File

@ -1,5 +1,8 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it. # Do not insert empty lines here, update check depends on it.
- feature: 30_MilightBridge: Support tcp bridge.
- bugfix: 30_MilightBridge/98_ping: Use Blocking.pm for ping checks so
it does not block main thread.
- feature 49_SSCAM: functions for cam-livestream added - feature 49_SSCAM: functions for cam-livestream added
- bugfix: 73_km200.pm: Bugfix InternalTimer; Improvement Log Level - bugfix: 73_km200.pm: Bugfix InternalTimer; Improvement Log Level
- bugfix: pre-coomit-hook test - bugfix: pre-coomit-hook test

View File

@ -27,6 +27,7 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Blocking;
use IO::Handle; use IO::Handle;
use IO::Socket; use IO::Socket;
@ -47,7 +48,7 @@ sub MilightBridge_Initialize($)
$hash->{UndefFn} = "MilightBridge_Undefine"; $hash->{UndefFn} = "MilightBridge_Undefine";
$hash->{NotifyFn} = "MilightBridge_Notify"; $hash->{NotifyFn} = "MilightBridge_Notify";
$hash->{AttrFn} = "MilightBridge_Attr"; $hash->{AttrFn} = "MilightBridge_Attr";
$hash->{AttrList} = "port sendInterval disable:0,1 tcpPing:1 checkInterval ".$readingFnAttributes; $hash->{AttrList} = "port protocol sendInterval disable:0,1 tcpPing:1 checkInterval ".$readingFnAttributes;
return undef; return undef;
} }
@ -73,12 +74,13 @@ sub MilightBridge_Define($$)
$attr{$name}{"port"} = "8899" if (!defined($attr{$name}{"port"})); $attr{$name}{"port"} = "8899" if (!defined($attr{$name}{"port"}));
$hash->{PORT} = $attr{$name}{"port"}; $hash->{PORT} = $attr{$name}{"port"};
$attr{$name}{"protocol"} = "udp" if (!defined($attr{$name}{"protocol"}));
# Create local socket # Create local socket
my $sock = IO::Socket::INET-> new ( my $sock = IO::Socket::INET-> new (
PeerPort => 48899, PeerPort => 48899,
Blocking => 0, Blocking => 0,
Proto => 'udp', Proto => $attr{$name}{"protocol"}) or return "can't bind: $@";
Broadcast => 1) or return "can't bind: $@";
my $select = IO::Select->new($sock); my $select = IO::Select->new($sock);
$hash->{SOCKET} = $sock; $hash->{SOCKET} = $sock;
$hash->{SELECT} = $select; $hash->{SELECT} = $select;
@ -98,13 +100,15 @@ sub MilightBridge_Define($$)
$attr{$name}{"event-on-change-reading"} = "state" if (!defined($attr{$name}{"event-on-change-reading"})); $attr{$name}{"event-on-change-reading"} = "state" if (!defined($attr{$name}{"event-on-change-reading"}));
$attr{$name}{"checkInterval"} = 10 if (!defined($attr{$name}{"checkInterval"})); $attr{$name}{"checkInterval"} = 10 if (!defined($attr{$name}{"checkInterval"}));
delete $hash->{helper}{RUNNING_PID};
readingsSingleUpdate($hash, "state", "Initialized", 1); readingsSingleUpdate($hash, "state", "Initialized", 1);
# Set state # Set state
$hash->{SENDFAIL} = 0; $hash->{SENDFAIL} = 0;
# Get initial bridge state # Get initial bridge state
MilightBridge_State($hash); MilightBridge_SetNextTimer($hash);
return undef; return undef;
} }
@ -115,7 +119,7 @@ sub MilightBridge_Undefine($$)
{ {
my ($hash,$arg) = @_; my ($hash,$arg) = @_;
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID}));
return undef; return undef;
} }
@ -150,6 +154,7 @@ sub MilightBridge_Attr($$$$) {
return "checkInterval is required in s (default: 10, min: 0)"; return "checkInterval is required in s (default: 10, min: 0)";
} }
readingsSingleUpdate($hash, "state", "Initialized", 1); readingsSingleUpdate($hash, "state", "Initialized", 1);
MilightBridge_SetNextTimer($hash);
} }
elsif ($attribute eq "port") elsif ($attribute eq "port")
{ {
@ -164,6 +169,19 @@ sub MilightBridge_Attr($$$$) {
$hash->{PORT} = $attr{$name}{"port"}; $hash->{PORT} = $attr{$name}{"port"};
} }
} }
elsif ($attribute eq "protocol")
{
if (($value eq "tcp" || $value eq "udp"))
{
$attr{$name}{"protocol"} = $value;
return "You need to restart fhem or modify to enable new protocol.";
}
else
{
return "protocol must be one of 'tcp|udp'";
}
}
# Handle "disable" attribute by opening/closing connection to device # Handle "disable" attribute by opening/closing connection to device
elsif ($attribute eq "disable") elsif ($attribute eq "disable")
{ {
@ -199,46 +217,89 @@ sub MilightBridge_Notify($$)
} }
##################################### #####################################
# Update readings to show status of bridge # Set next timer for ping check
sub MilightBridge_State(@) sub MilightBridge_SetNextTimer($)
{
my ($hash) = @_;
# Check state every X seconds
RemoveInternalTimer($hash);
InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, "checkInterval", "10"), "MilightBridge_DoPingStart", $hash, 0);
}
#####################################
# Prepare and start the blocking call in new thread
sub MilightBridge_DoPingStart($)
{ {
# Update Bridge state
my ($hash) = @_; my ($hash) = @_;
if (AttrVal($hash->{NAME}, "checkInterval", "10") == 0) return undef if (IsDisabled($hash->{NAME}));
{
Log3 ( $hash, 5, "$hash->{NAME}_State: Bridge status disabled"); my $timeout = 2;
return undef; my $mode = 'udp';
$mode = 'tcp' if(defined($attr{$hash->{NAME}}{tcpPing}));
my $arg = $hash->{NAME}."|".$hash->{HOST}."|".$mode."|".$timeout;
my $blockingFn = "MilightBridge_DoPing";
my $finishFn = "MilightBridge_DoPingDone";
my $abortFn = "MilightBridge_DoPingAbort";
if (!(exists($hash->{helper}{RUNNING_PID}))) {
$hash->{helper}{RUNNING_PID} =
BlockingCall($blockingFn, $arg, $finishFn, $timeout, $abortFn, $hash);
} else {
Log3 $hash, 3, "$hash->{NAME} Blocking Call running no new started";
MilightBridge_SetNextTimer($hash);
} }
}
Log3 ( $hash, 5, "$hash->{NAME}_State: Checking Bridge Status"); #####################################
# BlockingCall DoPing in separate thread
sub MilightBridge_DoPing(@)
{
my ($string) = @_;
my ($name, $host, $mode, $timeout) = split("\\|", $string);
Log3 ($name, 5, $name."_DoPing: Executing ping");
# Do a ping check to see if bridge is reachable
# check via ping # check via ping
my $pingstatus = "unreachable";
my $p; my $p;
if(defined($attr{$hash->{NAME}}{tcpPing})) $p = Net::Ping->new($mode);
{ my $result = $p->ping($host, $timeout);
$p = Net::Ping->new('tcp');
}
else
{
$p = Net::Ping->new('udp');
}
my $alive = $p->ping($hash->{HOST}, 2);
$p->close(); $p->close();
$pingstatus = "ok" if $alive;
$result="" if !(defined($result));
return "$name|$result";
}
#####################################
# Ping thread completed
sub MilightBridge_DoPingDone($)
{
my ($string) = @_;
my ($name, $result) = split("\\|", $string);
my $hash = $defs{$name};
my $status = "ok";
$status = "unreachable" if !($result);
# Update readings # Update readings
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
readingsBulkUpdate($hash, "state", $pingstatus); readingsBulkUpdate($hash, "state", $status);
readingsBulkUpdate( $hash, "sendFail", $hash->{SENDFAIL}); readingsBulkUpdate( $hash, "sendFail", $hash->{SENDFAIL});
readingsEndUpdate($hash, 1); readingsEndUpdate($hash, 1);
# Check state every X seconds delete($hash->{helper}{RUNNING_PID});
InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, "checkInterval", "10"), "MilightBridge_State", $hash, 0); MilightBridge_SetNextTimer($hash);
}
return undef; #####################################
# Ping thread timeout
sub MilightBridge_DoPingAbort($)
{
my ($hash) = @_;
delete($hash->{helper}{RUNNING_PID});
Log3 $hash->{NAME}, 3, "BlockingCall for ".$hash->{NAME}." was aborted";
MilightBridge_SetNextTimer($hash);
} }
##################################### #####################################
@ -429,6 +490,10 @@ sub MilightBridge_CmdQueue_Send(@)
<b>port</b><br/> <b>port</b><br/>
Default: 8899. Older bridges (V2) used port 50000 so change this value if you have an old bridge. Default: 8899. Older bridges (V2) used port 50000 so change this value if you have an old bridge.
</li> </li>
<li>
<b>protocol</b><br/>
Default: udp. Change to tcp if you have enabled tcp mode on your bridge.
</li>
<li> <li>
<b>tcpPing</b><br/> <b>tcpPing</b><br/>
If this attribute is defined, ping will use TCP instead of UDP. If this attribute is defined, ping will use TCP instead of UDP.

View File

@ -27,11 +27,7 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Blocking;
#use IO::Handle;
#use IO::Socket;
#use IO::Select;
#use Time::HiRes;
use Net::Ping; use Net::Ping;
sub ping_Initialize($) sub ping_Initialize($)
@ -62,6 +58,9 @@ sub ping_Define($$)
$hash->{MODE} = lc($mode); $hash->{MODE} = lc($mode);
$hash->{TIMEOUT} = $timeout; $hash->{TIMEOUT} = $timeout;
$hash->{FAILCOUNT} = 0; $hash->{FAILCOUNT} = 0;
delete $hash->{helper}{RUNNING_PID};
readingsSingleUpdate($hash, "state", "Initialized", 1); readingsSingleUpdate($hash, "state", "Initialized", 1);
return "ERROR: mode must be one of tcp,udp,icmp" if ($hash->{MODE} !~ "tcp|udp|icmp"); return "ERROR: mode must be one of tcp,udp,icmp" if ($hash->{MODE} !~ "tcp|udp|icmp");
@ -70,7 +69,7 @@ sub ping_Define($$)
$attr{$name}{"checkInterval"} = 10 if (!defined($attr{$name}{"checkInterval"})); $attr{$name}{"checkInterval"} = 10 if (!defined($attr{$name}{"checkInterval"}));
$attr{$name}{"event-on-change-reading"} = "state" if (!defined($attr{$name}{"event-on-change-reading"})); $attr{$name}{"event-on-change-reading"} = "state" if (!defined($attr{$name}{"event-on-change-reading"}));
ping_State($hash); ping_SetNextTimer($hash);
return undef; return undef;
} }
@ -81,7 +80,7 @@ sub ping_Undefine($$)
{ {
my ($hash,$arg) = @_; my ($hash,$arg) = @_;
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID}));
return undef; return undef;
} }
@ -122,24 +121,67 @@ sub ping_Attr($$$$) {
} }
##################################### #####################################
# Perform a ping and set state to result # Set next timer for ping check
sub ping_State(@) sub ping_SetNextTimer($)
{
my ($hash) = @_;
# Check state every X seconds
RemoveInternalTimer($hash);
InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, "checkInterval", "10"), "ping_Start", $hash, 0);
}
#####################################
# Prepare and start the blocking call in new thread
sub ping_Start($)
{ {
# Update Bridge state
my ($hash) = @_; my ($hash) = @_;
return undef if (IsDisabled($hash->{NAME})); return undef if (IsDisabled($hash->{NAME}));
Log3 ( $hash, 5, "$hash->{NAME}_State: Executing ping"); my $timeout = $hash->{TIMEOUT};
my $arg = $hash->{NAME}."|".$hash->{HOST}."|".$hash->{MODE}."|".$hash->{TIMEOUT};
my $blockingFn = "ping_DoPing";
my $finishFn = "ping_DoPingDone";
my $abortFn = "ping_DoPingAbort";
if (!(exists($hash->{helper}{RUNNING_PID}))) {
$hash->{helper}{RUNNING_PID} =
BlockingCall($blockingFn, $arg, $finishFn, $timeout, $abortFn, $hash);
} else {
Log3 $hash, 3, "$hash->{NAME} Blocking Call running no new started";
ping_SetNextTimer($hash);
}
}
#####################################
# BlockingCall DoPing in separate thread
sub ping_DoPing(@)
{
my ($string) = @_;
my ($name, $host, $mode, $timeout) = split("\\|", $string);
Log3 ($name, 5, $name."_DoPing: Executing ping");
# check via ping # check via ping
my $p; my $p;
$p = Net::Ping->new($hash->{MODE}); $p = Net::Ping->new($mode);
my $alive = $p->ping($hash->{HOST}, $hash->{TIMEOUT}); my $result = $p->ping($host, $timeout);
$p->close(); $p->close();
if ($alive) { $result="" if !(defined($result));
return "$name|$result";
}
#####################################
# Ping thread completed
sub ping_DoPingDone($)
{
my ($string) = @_;
my ($name, $result) = split("\\|", $string);
my $hash = $defs{$name};
if ($result) {
# State is ok # State is ok
$hash->{FAILCOUNT} = 0; $hash->{FAILCOUNT} = 0;
readingsSingleUpdate($hash, "state", "ok", 1); readingsSingleUpdate($hash, "state", "ok", 1);
@ -151,11 +193,18 @@ sub ping_State(@)
} }
} }
# Check state every X seconds delete($hash->{helper}{RUNNING_PID});
RemoveInternalTimer($hash); ping_SetNextTimer($hash);
InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, "checkInterval", "10"), "ping_State", $hash, 0); }
return undef; #####################################
# Ping thread timeout
sub ping_DoPingAbort($)
{
my ($hash) = @_;
delete($hash->{helper}{RUNNING_PID});
Log3 $hash->{NAME}, 3, "BlockingCall for ".$hash->{NAME}." was aborted";
ping_SetNextTimer($hash);
} }
1; 1;