2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-10 09:16:53 +00:00

FritzBoxUtils.pm: fhemNc added, for the FB_WLANswitch function (Forum #27145)

git-svn-id: https://svn.fhem.de/fhem/trunk@6574 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2014-09-19 17:32:48 +00:00
parent e5a43457f9
commit 8d1a67591e
2 changed files with 33 additions and 7 deletions

View File

@ -217,6 +217,26 @@ IsInt($)
defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
}
# Small NC replacement: fhemNc("ip:port", "text", waitForReturn);
sub
fhemNc($$$)
{
my ($addr, $txt, $waitForReturn) = @_;
my $client = IO::Socket::INET->new(PeerAddr => $addr);
return "Can't connect to $addr\n" if(!$client);
syswrite($client, $txt);
return "" if(!$waitForReturn);
my ($ret, $buf) = ("", "");
shutdown($client, 1);
alarm(5);
while(sysread($client, $buf, 256) > 0) {
$ret .= $buf;
}
alarm(0);
close($client);
return $ret;
}
1;
=pod
@ -310,6 +330,12 @@ myUtils_Initialize($$)
to the number of seconds since 1970. Optimized for repeated use of similar
timestamps.</li></br>
<li><b>fhemNc("host:port", "textToSend", waitForReturn)</b><br>
sends textToSend to host:port, and if waitForReturn is set, then read
the answer (wait up to 5 seconds) and return it. Intended as small
nc replacement.
</li></br>
</ul>
</ul>
=end html

View File

@ -99,18 +99,18 @@ sub
FB_WLANswitch($) {
my $cmd = shift;
my $ret = "";
if ($cmd =~ m"on"i) { # on or ON
$ret .= "ATD: " . `echo "ATD#96*1*" | nc 127.0.0.1 1011` ;
if ($cmd =~ m/on/i) { # on or ON
$ret .= "ATD:".fhemNc("127.0.0.1:1011", "ATD#96*1*\n", 1);
sleep 1 ;
$ret .= " ATH: " . `echo "ATH" | nc 127.0.0.1 1011` ;
$ret .= " ATH:".fhemNc("127.0.0.1:1011", "ATH\n", 1);
}
if ($cmd =~ m"off"i) { # off or OFF
$ret .= "ATD: " . `echo "ATD#96*0*" | nc 127.0.0.1 1011` ;
if ($cmd =~ m/off/i) { # off or OFF
$ret .= "ATD:".fhemNc("127.0.0.1:1011", "ATD#96*0*\n", 1);
sleep 1 ;
$ret .= " ATH: " . `echo "ATH" | nc 127.0.0.1 1011` ;
$ret .= " ATH:".fhemNc("127.0.0.1:1011", "ATH\n", 1);
}
$ret =~ s,[\r\n]*,,g; # remove CR from return-string
Log 4, "FB_WLANswitch($cmd) returned: $ret";
Log 3, "FB_WLANswitch($cmd) returned: $ret";
}
1;