added a parameter to choose the rule for waking up the host EW|UDP

added an other parameter to send the magic packet repeatedly, by keeping a NAS alive(Buffalo).


git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@4056 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
dietmar63 2013-10-16 20:51:25 +00:00
parent 1c42d93fe1
commit d2bb3ae660
2 changed files with 236 additions and 111 deletions

View File

@ -25,6 +25,10 @@
# define t4 RandomTimer *23:01:40 Zirkulation 23:02:40 100; attr t4 verbose 5; # define t4 RandomTimer *23:01:40 Zirkulation 23:02:40 100; attr t4 verbose 5;
# #
############################################################################## ##############################################################################
# 10.09.2013 Svenson : disable direct if attribute changed, add state disabled;
# randomtimer run every day if attribut runonce 0 (default is 1)
#
##############################################################################
package main; package main;
use strict; use strict;
@ -38,7 +42,8 @@ sub RandomTimer_Initialize($)
$hash->{DefFn} = "RandomTimer_Define"; $hash->{DefFn} = "RandomTimer_Define";
$hash->{UndefFn} = "RandomTimer_Undef"; $hash->{UndefFn} = "RandomTimer_Undef";
$hash->{AttrList} = "onCmd offCmd switchmode disable:0,1 disableCond ". $hash->{AttrFn} = "RandomTimer_Attr";
$hash->{AttrList} = "onCmd offCmd switchmode disable:0,1 disableCond runonce:0,1 ".
$readingFnAttributes; $readingFnAttributes;
} }
# #
@ -187,6 +192,9 @@ sub RandomTimer_Exec($)
my $midnight = $now1 + 24*3600 - (3600*$hour + 60*$min + $sec); my $midnight = $now1 + 24*3600 - (3600*$hour + 60*$min + $sec);
Log3 $hash, 4, "[".$hash->{NAME}. "]"." Next Timer ".strftime("%d.%m.%Y %H:%M:%S",localtime($midnight)); Log3 $hash, 4, "[".$hash->{NAME}. "]"." Next Timer ".strftime("%d.%m.%Y %H:%M:%S",localtime($midnight));
InternalTimer($midnight, "RandomTimer_ExecRepeater_verzoegert", $hash, 0); InternalTimer($midnight, "RandomTimer_ExecRepeater_verzoegert", $hash, 0);
} else {
$hash->{COMMAND} = "off";
$hash->{STATE} = "disabled";
} }
return; return;
} }
@ -211,10 +219,14 @@ sub RandomTimer_Exec($)
$hash->{STATE} = "off"; $hash->{STATE} = "off";
fhem ("set $hash->{DEVICE} $hash->{COMMAND}"); fhem ("set $hash->{DEVICE} $hash->{COMMAND}");
delete $hash->{ABSCHALTZEIT}; delete $hash->{ABSCHALTZEIT};
if ($hash->{REP} gt "") { if ($hash->{REP} gt "") {
RandomTimer_ExecRepeater($hash); RandomTimer_ExecRepeater($hash);
} else { } else {
fhem ("delete $hash->{NAME}"); if ( AttrVal($hash->{NAME}, "runonce", 1) == 1 )
{ fhem ("delete $hash->{NAME}") ;}
else
{ RandomTimer_ExecRepeater($hash);}
} }
} else { } else {
toggleDevice($hash); toggleDevice($hash);
@ -225,6 +237,25 @@ sub RandomTimer_Exec($)
# #
# #
# #
sub RandomTimer_Attr($$$) {
my ($cmd, $name, $attrName, $attrVal) = @_;
if( $attrName eq "disable" ) {
my $hash = $defs{$name};
if( $cmd eq "set" && $attrVal ne "0" ) {
$attr{$name}{$attrName} = 1;
$hash->{STATE} = "disabled";
RemoveInternalTimer($hash);
} else {
$attr{$name}{$attrName} = 0;
$hash->{STATE} = "off";
RandomTimer_ExecRepeater($hash);
}
}
}
#
#
#
sub RandomTimer_ExecRepeater_verzoegert($) sub RandomTimer_ExecRepeater_verzoegert($)
{ {
my ($hash) = @_; my ($hash) = @_;

View File

@ -1,5 +1,22 @@
############################################## # $Id
# $Id$ # erweitert um die Funktion nas_control Dietmar Ortmann $
#
# This file is part of fhem.
#
# Fhem is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Fhem is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fhem. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
package main; package main;
use strict; use strict;
@ -7,87 +24,105 @@ use warnings;
use IO::Socket; use IO::Socket;
use Time::HiRes qw(gettimeofday); use Time::HiRes qw(gettimeofday);
sub sub WOL_Initialize($)
WOL_Initialize($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{SetFn} = "WOL_Set"; $hash->{SetFn} = "WOL_Set";
$hash->{DefFn} = "WOL_Define"; $hash->{DefFn} = "WOL_Define";
$hash->{UndefFn} = "WOL_Undef"; $hash->{UndefFn} = "WOL_Undef";
$hash->{AttrList} = "shutdownCmd loglevel:0,1,2,3,4,5,6"; $hash->{AttrList} = "interval shutdownCmd ".
$readingFnAttributes;
} }
#
################################### #
sub #
WOL_Set($@) sub WOL_Set($@)
{ {
my ($hash, @a) = @_; my ($hash, @a) = @_;
return "no set value specified" if(int(@a) < 2); return "no set value specified" if(int(@a) < 2);
return "Unknown argument $a[1], choose one of refresh on" if($a[1] eq "?"); return "Unknown argument $a[1], choose one of on off refresh" if($a[1] eq "?");
my $name = shift @a; my $name = shift @a;
my $v = join(" ", @a); my $v = join(" ", @a);
my $logLevel = GetLogLevel($name,2);
Log $logLevel, "WOL set $name $v"; Log3 $hash, 3, "WOL set $name $v";
if($v eq "on") if ($v eq "on") {
{ $hash->{STATE} = $v;
eval { Log3 $hash, 3, "WOL waking $name with MAC $hash->{MAC} IP $hash->{IP} ";
for(my $i = 1; $i <= 3; $i++) {
wake($hash, $logLevel);
}
};
if ($@){
### catch block
Log $logLevel, "WOL error: $@";
};
Log $logLevel, "WOL waking $name";
} elsif ($v eq "refresh")
{
WOL_GetUpdate($hash);
} elsif ($v eq "off") { } elsif ($v eq "off") {
my $cmd = AttrVal($name, "shutdownCmd", ""); $hash->{STATE} = $v;
if ($cmd eq "") my $cmd = AttrVal($name, "shutdownCmd", "");
{ if ($cmd eq "") {
Log $logLevel, "No shutdown command given!"; Log3 $hash, 3, "No shutdown command given (see shutdownCmd attribute)!";
return "no shutdown command given (see shutdownCmd attribute)!" } else {
} qx ($cmd);
`$cmd`; }
} else } elsif ($v eq "refresh") {
{ ;
return "unknown argument $v, choose one of refresh, on";
} }
$hash->{CHANGED}[0] = $v; RemoveInternalTimer($hash);
$hash->{STATE} = $v; InternalTimer(gettimeofday()+5, "WOL_UpdateReadings", $hash, 0);
$hash->{READINGS}{state}{TIME} = TimeNow();
$hash->{READINGS}{state}{VAL} = $v;
if ($hash->{STATE} eq "on") {
InternalTimer(gettimeofday()+1, "WOL_GetUpdate", $hash, 0);
}
return undef; return undef;
} }
#
sub #
WOL_Define($$) #
sub WOL_Define($$)
{ {
my ($hash, $def) = @_; my ($hash, $def) = @_;
my @a = split("[ \t][ \t]*", $def); my @a = split("[ \t][ \t]*", $def);
my $u = "wrong syntax: define <name> WOL MAC_ADRESS IP"; my $u = "wrong syntax: define <name> WOL <MAC_ADRESS> <IP> <mode> <repeat> ";
return $u if(int(@a) < 4); return $u if(int(@a) < 4);
my $name = shift @a;
my $type = shift @a;
my $mac = shift @a;
my $ip = shift @a;
my $mode = shift @a;
my $repeat = shift @a;
$hash->{MAC} = $a[2]; $repeat = "000" if (!defined $repeat);
$hash->{IP} = $a[3]; $mode = "BOTH" if (!defined $mode);
$hash->{INTERVAL} = 600;
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "WOL_GetUpdate", $hash, 0); return "invalid MAC<$mac> - use HH:HH:HH:HH:HH"
if(!($mac =~ m/^([0-9a-f]{2}([:-]|$)){6}$/i ));
return "invalid IP<$ip> - use ddd.ddd.ddd.ddd"
if(!($ip =~ m/^([0-9]{1,3}([.-]|$)){4}$/i ));
return "invalid mode<$mode> - use EW|UDP|BOTH"
if(!($mode =~ m/^(BOTH|EW|UDP)$/));
return "invalid repeat<$repeat> - use 999"
if(!($repeat =~ m/^[0-9]{3,3}$/i));
$hash->{NAME} = $name;
$hash->{MAC} = $mac;
$hash->{IP} = $ip;
$hash->{REPEAT} = $repeat;
$hash->{MODE} = $mode;
$hash->{INTERVAL} = AttrVal($hash->{NAME}, "interval", 900);
RemoveInternalTimer($hash);
InternalTimer(gettimeofday()+5, "WOL_UpdateReadings", $hash, 0);
InternalTimer(gettimeofday()+30,"WOL_GetUpdate", $hash, 0);
readingsSingleUpdate($hash, "packet_via_EW", "none",0);
readingsSingleUpdate($hash, "packet_via_UDP", "none",0);
return undef; return undef;
} }
#
#
#
sub WOL_Undef($$) { sub WOL_Undef($$) {
my ($hash, $arg) = @_; my ($hash, $arg) = @_;
@ -95,112 +130,169 @@ sub WOL_Undef($$) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
return undef; return undef;
} }
#
#
#
sub WOL_UpdateReadings($)
{
my ($hash) = @_;
$hash->{INTERVAL} = AttrVal($hash->{NAME}, "interval", 900);
my $ip = $hash->{IP};
readingsBeginUpdate ($hash);
if (`ping -c 1 -w 2 $ip` =~ m/100%/) {
readingsBulkUpdate ($hash, "isRunning", "false");
} else {
readingsBulkUpdate ($hash, "isRunning", "true");
}
readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1));
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "WOL_UpdateReadings", $hash, 0);
}
#
#
#
sub WOL_GetUpdate($) sub WOL_GetUpdate($)
{ {
my ($hash) = @_; my ($hash) = @_;
my $ip = $hash->{IP}; if ($hash->{STATE} eq "on") {
#if (system("ping -q -c 1 $ip > /dev/null") == 0) wake($hash);
####
#changed 2013-07-27 by UliM
#based on Thread http://forum.fhem.de/index.php?t=msg&th=11823&goto=69801&rid=86#msg_69801
#if (`ping -c 1 $ip` =~ m/100/)
if (`ping -c 1 -w 1 $ip` =~ m/100%/)
{
$hash->{READINGS}{state}{VAL} = "off";
$hash->{READINGS}{isRunning}{VAL} = "false";
} else
{
$hash->{READINGS}{state}{VAL} = "on";
$hash->{READINGS}{isRunning}{VAL} = "true";
} }
$hash->{READINGS}{state}{TIME} = TimeNow();
$hash->{READINGS}{isRunning}{TIME} = TimeNow();
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "WOL_GetUpdate", $hash, 0); if ($hash->{REPEAT} > 0 && $hash->{STATE} eq "on" ) {
InternalTimer(gettimeofday()+$hash->{REPEAT}, "WOL_GetUpdate", $hash, 0);
}
} }
#
sub wake #
#
sub wake($)
{ {
my ($hash, $logLevel) = @_; my ($hash) = @_;
my $mac = $hash->{MAC}; my $name = $hash->{NAME};
my $mac = $hash->{MAC};
my $host = $hash->{IP};
Log $logLevel, "trying to wake $mac"; readingsBeginUpdate ($hash);
my $response = `/usr/bin/ether-wake $mac`; Log3 $hash, 3, "WOL keeping $name with MAC $mac IP $host busy";
Log $logLevel, "trying etherwake with response: $response";
wol_by_udp($mac); if ($hash->{MODE} eq "BOTH" || $hash->{MODE} eq "EW" ) {
Log $logLevel, "trying direct socket via UDP"; wol_by_ew ($mac);
readingsBulkUpdate ($hash, "packet_via_EW", $mac);
}
if ($hash->{MODE} eq "BOTH" || $hash->{MODE} eq "UDP" ) {
wol_by_udp ($hash, $mac, $host);
readingsBulkUpdate ($hash, "packet_via_UDP", $host);
}
readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1));
} }
# method to wake via lan, taken from Net::Wake package #
#
# method to wakevia lan, taken from Net::Wake package
sub wol_by_udp { sub wol_by_udp {
my ($mac_addr, $host, $port) = @_; my ($hash, $mac_addr, $host, $port) = @_;
# use the discard service if $port not passed in # use the discard service if $port not passed in
if (! defined $host) { $host = '255.255.255.255' } if (! defined $host) { $host = '255.255.255.255' }
if (! defined $port || $port !~ /^\d+$/ ) { $port = 9 } if (! defined $port || $port !~ /^\d+$/ ) { $port = 9 }
my $sock = new IO::Socket::INET(Proto=>'udp') or die "socket : $!"; my $sock = new IO::Socket::INET(Proto=>'udp') or die "socket : $!";
die "Can't create WOL socket" if(!$sock); if(!$sock) {
Log3 $hash, 1, "Can't create WOL socket";
return 1;
}
my $ip_addr = inet_aton($host); my $ip_addr = inet_aton($host);
my $sock_addr = sockaddr_in($port, $ip_addr); my $sock_addr = sockaddr_in($port, $ip_addr);
$mac_addr =~ s/://g; $mac_addr =~ s/://g;
my $packet = pack('C6H*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, $mac_addr x 16); my $packet = pack('C6H*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, $mac_addr x 16);
setsockopt($sock, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!"; setsockopt($sock, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
send($sock, $packet, 0, $sock_addr) or die "send : $!"; send($sock, $packet, 0, $sock_addr) or die "send : $!";
close ($sock); close ($sock);
return 1; return 1;
} }
#
#
# method to wakevia ether-wake
sub wol_by_ew {
my ($mac) = @_;
my $response = `/usr/bin/ether-wake $mac`;
return 1;
}
1; 1;
=pod =pod
=begin html =begin html
<a name="WOL"></a> <a name="WOL"></a>
<h3>WOL</h3> <h3>WOL</h3>
Defines a WOL device via its MAC and IP address.<br><br>
when sending the <b>on</b> command to a WOL device it wakes up the dependent device by sending a magic packet. When running in repeat mode the magic paket ist sent every n seconds to the device.
So, for example a Buffalo NAS can be kept awake.
<ul> <ul>
<a name="WOLdefine"></a> <a name="WOLdefine"></a>
<h4>Define</h4> <h4>Define</h4>
<ul> <ul>
<code>define &lt;name&gt; WOL &lt;MAC&gt; &lt;IP&gt; <code><b><font size="+1">define &lt;name&gt; WOL &lt;MAC&gt; &lt;IP&gt; [&lt;mode&gt; [&lt;repeat&gt;]]</font></b></code>
&lt;unitcode&gt;</code>
<br><br> <br><br>
Defines a WOL device via its MAC and IP address.<br><br> <dl>
<dt><b>MAC</b></dt>
<dd>MAC-Adress of the host</dd>
<dt><b>IP</b></dt>
<dd>IP-Adress of the host (or broadcast address of the local network if IP of the host is unknown)</dd>
<dt><b>mode <i>[EW|UDP]</i></b></dt>
<dd>EW: wakeup by <i>usr/bin/ether-wake</i> </dd>
<dd>UDP: wakeup by an implementation like <i>Net::Wake(CPAN)</i></dd>
</dl>
<br><br>
Example: <b><font size="+1">Examples</font></b>:
<ul> <ul>
<code>define computer1 WOL 72:11:AC:4D:37:13 192.168.0.24</code><br> <code>define computer1 WOL 72:11:AC:4D:37:13 192.168.0.24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switching only one time</code><br>
<code>define computer1 WOL 72:11:AC:4D:37:13 192.168.0.24 EW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; by ether-wake(linux command)</code><br>
<code>define computer1 WOL 72:11:AC:4D:37:13 192.168.0.24 BOTH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; by both methods</code><br>
<code>define computer1 WOL 72:11:AC:4D:37:13 192.168.0.24 UDP 200 &nbsp;&nbsp;&nbsp; in repeat mode<i><b>usr/bin/ether-wake</b></i> in repeatmode</code><br>
</ul> </ul>
Notes: <br><br>
<b><font size="+1">Notes</font></b>:
<ul> <ul>
<li>Module uses <code>ether-wake</code> on FritzBoxes.</li> Not every hardware is able to wake up other devices by default. Oftenly firewalls filter magic packets. Switch them first off.
<li>For other computers the WOL implementation of <a href="http://search.cpan.org/~clintdw/Net-Wake-0.02/lib/Net/Wake.pm">Net::Wake</a> is used</li> You may need a packet sniffer to check some malfunktion.
With this module you get two methods to do the job: see the mode parameter.
</ul> </ul>
</ul> </ul>
<a name="WOLset"></a> <a name="WOLset"></a>
<h4>Set </h4> <h4>Set </h4>
<ul> <ul>
<code>set &lt;name&gt; &lt;value&gt;</code> <code><b><font size="+1">set &lt;name&gt; &lt;value&gt;</font></b></code>
<br><br> <br><br>
where <code>value</code> is one of:<br> where <code>value</code> is one of:<br>
<pre> <pre>
refresh # checks whether the computer is currently running <b>refresh</b> # checks(by ping) whether the device is currently running
on # sends a magic packet to the defined MAC address <b>on</b> # sends a magic packet to the defined MAC address
<b>off</b> # stops sending magic packets and sends the <b>shutdownCmd</b>(see attributes)
</pre> </pre>
Examples: <b><font size="+1">Examples</font></b>:
<ul> <ul>
<code>set computer1 on</code><br> <code>set computer1 on</code><br>
<code>set computer1 off</code><br>
<code>set computer1 refresh</code><br> <code>set computer1 refresh</code><br>
</ul> </ul>
</ul> </ul>
@ -208,8 +300,10 @@ sub wol_by_udp {
<a name="WOLattr"></a> <a name="WOLattr"></a>
<h4>Attributes</h4> <h4>Attributes</h4>
<ul> <ul>
<li><a name="owtherm_stateAL"><code>attr &lt;name&gt; shutdownCmd &lt;string&gt;</code></a> <li><code>attr &lt;name&gt; shutdownCmd &lt;string&gt;</code>
<br />Custom command executed to shutdown a remote machine, i.e. <code>sh /path/to/some/shell/script.sh</code></li> <br>Custom command executed to shutdown a remote machine, i.e. <code>sh /path/to/some/shell/script.sh</code></li>
<li><code>attr &lt;name&gt; interval &lt;seconds&gt;</code></a>
<br>defines the time between two checks by a <i>ping</i> if state of &lt;name&gt is <i>on</i></li>
</ul> </ul>
</ul> </ul>