2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-13 17:26:34 +00:00

Wake on Lan module from Matthias K.

git-svn-id: https://svn.fhem.de/fhem/trunk@1201 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2012-01-22 08:50:17 +00:00
parent 7ef6fc9937
commit 214345e0eb
3 changed files with 205 additions and 0 deletions

151
fhem/FHEM/98_WOL.pm Normal file
View File

@ -0,0 +1,151 @@
##############################################
# $Id: 99_WOL.pm 1116 2012-01-21 15:01:34Z matthiasklass $
package main;
use strict;
use warnings;
use IO::Socket;
use Time::HiRes qw(gettimeofday);
sub
WOL_Initialize($)
{
my ($hash) = @_;
$hash->{SetFn} = "WOL_Set";
$hash->{DefFn} = "WOL_Define";
$hash->{UndefFn} = "WOL_Undef";
$hash->{AttrList} = "loglevel:0,1,2,3,4,5,6";
}
###################################
sub
WOL_Set($@)
{
my ($hash, @a) = @_;
return "no set value specified" if(int(@a) < 2);
return "Unknown argument $a[1], choose one of refresh on" if($a[1] eq "?");
my $name = shift @a;
my $v = join(" ", @a);
Log GetLogLevel($name,2), "WOL set $name $v";
my $mac = $hash->{MAC};
if($v eq "on")
{
eval {
#for(my $i = 1; $i <= 3; $i++) {
wake($mac);
#}
};
if ($@){
### catch block
Log GetLogLevel($name,2), "WOL error: $@";
};
Log GetLogLevel($name,2), "WOL waking $name ($mac)";
} elsif ($v eq "refresh")
{
WOL_GetUpdate($hash);
} else
{
return "unknown argument $v, choose one of refresh, on";
}
$hash->{CHANGED}[0] = $v;
$hash->{STATE} = $v;
$hash->{READINGS}{state}{TIME} = TimeNow();
$hash->{READINGS}{state}{VAL} = $v;
return undef;
}
sub
WOL_Define($$)
{
my ($hash, $def) = @_;
my @a = split("[ \t][ \t]*", $def);
my $u = "wrong syntax: define <name> WOL MAC_ADRESS IP";
return $u if(int(@a) < 4);
$hash->{MAC} = $a[2];
$hash->{IP} = $a[3];
$hash->{INTERVAL} = 600;
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "WOL_GetUpdate", $hash, 0);
return undef;
}
sub WOL_Undef($$) {
my ($hash, $arg) = @_;
RemoveInternalTimer($hash);
return undef;
}
sub WOL_GetUpdate($)
{
my ($hash) = @_;
my $ip = $hash->{IP};
if (system("ping -q -c 1 $ip > /dev/null") == 0)
{
$hash->{READINGS}{state}{VAL} = "on";
$hash->{READINGS}{isRunning}{VAL} = "true";
} else
{
$hash->{READINGS}{state}{VAL} = "off";
$hash->{READINGS}{isRunning}{VAL} = "false";
}
$hash->{READINGS}{state}{TIME} = TimeNow();
$hash->{READINGS}{isRunning}{TIME} = TimeNow();
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "WOL_GetUpdate", $hash, 0);
}
sub wake($)
{
my ($mac) = @_;
Log GetLogLevel("WOL",2), "trying to wake $mac";
my $response = `/usr/bin/ether-wake $mac`;
Log GetLogLevel("WOL",4), "trying etherwake with response: $response";
wol_by_udp($mac);
Log GetLogLevel("WOL",4), "trying direct socket via UDP";
}
# method to wake via lan, taken from Net::Wake package
sub wol_by_udp {
my ($mac_addr, $host, $port) = @_;
# use the discard service if $port not passed in
if (! defined $host) { $host = '255.255.255.255' }
if (! defined $port || $port !~ /^\d+$/ ) { $port = 9 }
my $sock = new IO::Socket::INET(Proto=>'udp') or die "socket : $!";
die "Can't create WOL socket" if(!$sock);
my $ip_addr = inet_aton($host);
my $sock_addr = sockaddr_in($port, $ip_addr);
$mac_addr =~ s/://g;
my $packet = pack('C6H*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, $mac_addr x 16);
setsockopt($sock, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
send($sock, $packet, 0, $sock_addr) or die "send : $!";
close ($sock);
return 1;
}
1;

View File

@ -123,6 +123,7 @@
<a href="#WS300">WS300</a> &nbsp;
<a href="#WS3600">WS3600</a> &nbsp;
<a href="#Weather">Weather</a> &nbsp;
<a href="#WOL">WOL</a> &nbsp;
<a href="#X10">X10</a> &nbsp;
<a href="#xxLG7000">xxLG7000</a> &nbsp;
@ -3880,6 +3881,53 @@ A line ending with \ will be concatenated with the next one, so long lines
<br>
</ul>
<a name="WOL"></a>
<h3>WOL</h3>
<ul>
<a name="WOLdefine"></a>
<b>Define</b>
<ul>
<code>define &lt;name&gt; WOL &lt;MAC&gt; &lt;IP&gt;
&lt;unitcode&gt;</code>
<br><br>
Defines a WOL device via its MAC and IP address.<br><br>
Example:
<ul>
<code>define computer1 WOL 72:11:AC:4D:37:13 192.168.0.24</code><br>
</ul>
Notes:
<ul>
<li>Module uses <code>ether-wake</code> on FritzBoxes.</li>
<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>
</ul>
</ul>
<a name="WOLset"></a>
<b>Set </b>
<ul>
<code>set &lt;name&gt; &lt;value&gt;</code>
<br><br>
where <code>value</code> is one of:<br>
<pre>
refresh # checks whether the computer is currently running
on # sends a magic packet to the defined MAC address
</pre>
Examples:
<ul>
<code>set computer1 on</code><br>
<code>set computer1 refresh</code><br>
</ul>
</ul>
<a name="WOLget"></a>
<b>Get</b> <ul>N/A</ul><br>
<a name="WOLattr"></a>
<b>Attributes</b> <ul>N/A</ul><br>
</ul>
<a name="X10"></a>
<h3>X10</h3>
<ul>
@ -8286,6 +8334,8 @@ The .gnuplot file consists of 3 parts:
plots of the lineytype "lines" will have an additional starting and
ending segment, so that filling is done correctly.<br>
See the SVG spec for details of this CSS file.
Note: if you plan to use this attribute, you have to specify it for all
the lines (attribute-blocks) in the plot command.
</li>
<li>lw &lt;linewidth&gt;<br>
Sets the stroke-width style of the line. This attribute is deprecated,

View File

@ -107,6 +107,10 @@
<a href="http://www.dhs-computertechnik.de/support-iphone.html">
dhs-computertechnik</a> or
<a href="http://code.google.com/p/phyfhem/">phyfhem</a>
<br><br>
Android frontends:
<a href="http://andFHEM.klass.li">AndFHEM</a> (native app)
</ul>