| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- ################################################################
- #
- # Copyright notice
- #
- # (c) 2011 Sacha Gloor (sacha@imp.ch)
- #
- # This script 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.
- #
- # The GNU General Public License can be found at
- # http://www.gnu.org/copyleft/gpl.html.
- # A copy is found in the textfile GPL.txt and important notices to the license
- # from the author is found in LICENSE.txt distributed with these scripts.
- #
- # This script 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.
- #
- # This copyright notice MUST APPEAR in all copies of the script!
- #
- ################################################################
- # $Id: 23_WEBIO_12DIGITAL.pm 2076 2012-11-04 13:49:43Z rudolfkoenig $
- ##############################################
- package main;
- use strict;
- use warnings;
- use Data::Dumper;
- use IO::Socket;
- #use LWP::UserAgent;
- #use HTTP::Request;
- sub
- WEBIO_12DIGITAL_Initialize($)
- {
- my ($hash) = @_;
- $hash->{SetFn} = "WEBIO_12DIGITAL_Set";
- $hash->{DefFn} = "WEBIO_12DIGITAL_Define";
- $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6";
- }
- ###################################
- sub
- WEBIO_12DIGITAL_Set($@)
- {
- my ($hash, @a) = @_;
- return "no set value specified" if(int(@a) < 2);
- return "Unknown argument $a[1], choose one of on off on-for-timer" if($a[1] eq "?");
- my $v = $a[1];
- my $v2= "";
- if(defined($a[2])) { $v2=$a[2]; }
- RemoveInternalTimer("WEBIO_12DIGITAL_on_timeout");
- if($v eq "on-for-timer")
- {
- InternalTimer(gettimeofday()+$v2, "WEBIO_12DIGITAL_on_timeout",$hash, 0);
- # on-for-timer is now a on.
- $v="on";
- }
- WEBIO_12DIGITAL_execute($hash->{DEF},$v);
- Log GetLogLevel($a[0],2), "WEBIO_12DIGITAL set @a";
- $hash->{CHANGED}[0] = $v;
- $hash->{STATE} = $v;
- $hash->{READINGS}{state}{TIME} = TimeNow();
- $hash->{READINGS}{state}{VAL} = $v;
- DoTrigger($hash->{NAME}, undef);
- return undef;
- }
- sub
- WEBIO_12DIGITAL_on_timeout($)
- {
- my ($hash) = @_;
- my @a;
- $a[0]=$hash->{NAME};
- $a[1]="off";
- WEBIO_12DIGITAL_Set($hash,@a);
- return undef;
- }
- ###################################
- sub
- WEBIO_12DIGITAL_execute($@)
- {
- my ($target,$cmd) = @_;
- my $URL='';
- my $v='';
- my $err_log='';
- if($cmd eq "on") { $v="ON"; }
- else { $v="OFF"; }
- my @a = split("[ \t][ \t]*", $target);
- my $sock = new IO::Socket::INET (
- PeerAddr => $a[0],
- PeerPort => '80',
- Proto => 'tcp',
- Timeout => "3",
- );
- $err_log = "Could not create socket: $!\n" unless $sock;
- if($err_log ne "") { return undef; }
- print $sock "GET /outputaccess".$a[1]."?PW=&State=".$v."\n";
- close($sock);
- return undef;
- }
- sub
- WEBIO_12DIGITAL_Define($$)
- {
- my ($hash, $def) = @_;
- my $name=$hash->{NAME};
- my @a = split("[ \t][ \t]*", $def);
- my $host = $a[2];
- my $host_port = $a[3];
- my $delay=$a[4];
- $attr{$name}{delay}=$delay if $delay;
- return "Wrong syntax: use define <name> WEBIO_12DIGITAL <ip-address> <port-nr> <poll-delay>" if(int(@a) != 5);
- $hash->{Host} = $host;
- $hash->{Host_Port} = $host_port;
-
- InternalTimer(gettimeofday()+$delay, "WEBIO_12DIGITAL_GetStatus", $hash, 0);
-
- return undef;
- }
- #####################################
- sub
- WEBIO_12DIGITAL_GetStatus($)
- {
- my ($hash) = @_;
- my $err_log='';
- my $line;
- my $state;
- my $name = $hash->{NAME};
- my $host = $hash->{Host};
- my $delay=$attr{$name}{delay}||300;
- InternalTimer(gettimeofday()+$delay, "WEBIO_12DIGITAL_GetStatus", $hash, 0);
- if(!defined($hash->{Host_Port})) { return(""); }
- my $host_port = $hash->{Host_Port};
- my $sock = new IO::Socket::INET (
- PeerAddr => $host,
- PeerPort => '80',
- Proto => 'tcp',
- Timeout => "3",
- );
- $err_log = "Could not create socket: $!\n" unless $sock;
- if($err_log ne "")
- {
- Log GetLogLevel($name,2), "WEBIO_12DIGITAL ".$err_log;
- return("");
- }
- print $sock "GET /output".$host_port."?PW=&\n";
- $line = <$sock>;
- close($sock);
- if($line =~ /ON/) { $state="on"; } else { $state="off"; }
- if($hash->{STATE} ne $state)
- {
- Log 4, "WEBIO_12DIGITAL_GetStatus: $host_port ".$hash->{STATE}." -> ".$state;
- $hash->{STATE} = $state;
- $hash->{CHANGED}[0] = $state;
- $hash->{READINGS}{state}{TIME} = TimeNow();
- $hash->{READINGS}{state}{VAL} = $state;
- DoTrigger($name, undef) if($init_done);
- }
- }
- 1;
- =pod
- =begin html
- <a name="WEBIO_12DIGITAL"></a>
- <h3>WEBIO_12DIGITAL</h3>
- <ul>
- Note: this module needs the HTTP::Request and LWP::UserAgent perl modules.
- <br><br>
- <a name="WEBIO_12DIGITALdefine"></a>
- <b>Define</b>
- <ul>
- <code>define <name> WEBIO_12DIGITAL <ip-address> <outputport> <delay></code>
- <br><br>
- Defines an Web-IO-Digital device (Box with up to 12 digital in/outputs, www.wut.de) via ip address. The status of the device is also pooled (delay interval).<br><br>
- Examples:
- <ul>
- <code>define motor1 WEBIO_12DIGITAL 192.168.8.200 1 60</code><br>
- </ul>
- </ul>
- <br>
- <a name="WEBIO_12DIGITALset"></a>
- <b>Set </b>
- <ul>
- <code>set <name> <value></code>
- <br><br>
- where <code>value</code> is one of:<br>
- <pre>
- on off
- </pre>
- Examples:
- <ul>
- <code>set motor1 on</code><br>
- </ul>
- <br>
- </ul>
- </ul>
- =end html
- =cut
|