From 6387dce1df0773cad7ceee93d899aebae20c6ee2 Mon Sep 17 00:00:00 2001 From: ulimaass <> Date: Thu, 24 Apr 2014 20:26:06 +0000 Subject: [PATCH] New modules 10_Itach_IR and 88_Itach_IRDevice git-svn-id: https://svn.fhem.de/fhem/trunk@5636 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 3 + fhem/FHEM/10_Itach_IR.pm | 162 +++++++++++++++++++ fhem/FHEM/88_Itach_IRDevice.pm | 284 +++++++++++++++++++++++++++++++++ 3 files changed, 449 insertions(+) create mode 100644 fhem/FHEM/10_Itach_IR.pm create mode 100644 fhem/FHEM/88_Itach_IRDevice.pm diff --git a/fhem/CHANGED b/fhem/CHANGED index ee741de8c..40f51308c 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,6 +1,9 @@ # 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. - SVN + - added: new modules 10_Itach_IR and 88_Itach_IRDevice to + use Itach WF2IR or IP2IR to be used as universal + infrared remotecontrol - added: new module 51_I2C_TSL2561.pm (kaihs) - added: new module 02_FRAMEBUFFER.pm (kaihs) - feature: SYSMON: many FritzBox specific readings: diff --git a/fhem/FHEM/10_Itach_IR.pm b/fhem/FHEM/10_Itach_IR.pm new file mode 100644 index 000000000..eb7080560 --- /dev/null +++ b/fhem/FHEM/10_Itach_IR.pm @@ -0,0 +1,162 @@ +################################################################################ +# 10_Itach_IR +# $Id$ +# +################################################################################ +# +# Copyright notice +# +# (c) 2014 Copyright: Ulrich Maass +# +# 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 . +# +# Disclaimer: The Author takes no responsibility whatsoever +# for damages potentially done by this program. +# +################################################################################ +# +# This module serves as communication layer for 88_Itach_IRDevice +# +################################################################################ + +package main; +use strict; +use warnings; +use IO::Socket::INET; + +######################### +# Forward declaration +sub IIR_Define(); +sub IIR_Send($$); +sub IIR_Write($@); + + +##################################### +# Initialize module +sub +Itach_IR_Initialize($) +{ + my ($hash) = @_; + # provider +# $hash->{ReadFn} = "IIR_Write"; + $hash->{WriteFn} = "IIR_Write"; + $hash->{Clients} = ":Itach_IRDevice:"; + #consumer + $hash->{DefFn} = "IIR_Define"; + $hash->{AttrList} = "verbose:0,1,2,3,4,5,6 timeout"; +} + + +##################################### +# Initialize every new instance +sub +IIR_Define() +{ + my ($hash, $def) = @_; + my @args = split("[ \t]+", $def); + return "Usage: define Itach_IR " if($#args != 2); + + my ($name, $type, $host) = @args; + $hash->{STATE} = "Initialized"; + $hash->{IPADR} = $host if ($host); #format-check required + my $cmdret= CommandAttr(undef,"$name room ItachIR") if (!AttrVal($name,'room',undef)); + return undef; +} + + +##################################### +# Execute IOWrite-calls from clients +sub IIR_Write($@) { + my ($hash,$name,$cmd,$IRcode)= @_; + Log3 $name, 5, "IIR_Write called with $name,$cmd"; + my $newstate = $name.':'.$cmd; + readingsSingleUpdate($hash,"lastcommand",$newstate,0); + my $ret=IIR_Send($hash,$IRcode); + return undef; +} + +##################################### +# Send IR-code +sub +IIR_Send($$){ + my ($hash,$IR)=@_; + if (!$IR) { + Log3 $hash->{NAME}, 2, "Called without IR-code to send."; + return; + } + my $socket = new IO::Socket::INET ( + PeerHost => $hash->{IPADR}, + PeerPort => '4998', + Proto => 'tcp', + Timeout => AttrVal($hash->{NAME},'timeout','2.0'), + ); + # send Itach command + if ($socket) { + my @codes = split(';',$IR); + foreach my $IRcode (@codes) { + my $data = $IRcode."\r\n"; + $socket->send($data); + select(undef, undef, undef, 0.3); #pause 0.3 seconds + Log3 $hash->{NAME}, 5, 'Sent to '.$hash->{IPADR}.' : '.$IRcode; + } + $socket->close(); + readingsSingleUpdate($hash,"state",'Initialized',0) if ($hash->{STATE} ne 'Initialized'); + } else { + Log3 $hash->{NAME}, 1, 'Could not open socket with '.$hash->{IPADR}.' : '.$@; + readingsSingleUpdate($hash,"state",$@,0); + } + return undef; +} + +1; + + +=pod +=begin html + + +

Itach_IR

+
    + Defines a device representing a physical Itach IR. Serves as communication layer for Itach_IRDevice.
    + For more information, check the Wiki page.
    + +
    + Define +
      + define <name> Itach_IR <IP-address>
      + Example:
      + define Itach Itach_IR 192.168.1.2 +
    + +
    + Set
      N/A

    + +
    + Get
      N/A

    + +
    + Attributes +
      +
    • verbose
    • +
    • timeout
      + Can be used to change the timeout-value for tcp-communication. Default is 2.0.
    • +
    +
+ +=end html +=cut + + diff --git a/fhem/FHEM/88_Itach_IRDevice.pm b/fhem/FHEM/88_Itach_IRDevice.pm new file mode 100644 index 000000000..d47c9b938 --- /dev/null +++ b/fhem/FHEM/88_Itach_IRDevice.pm @@ -0,0 +1,284 @@ +############################################## +# 88_Itach_IRDevice +# $Id$ +# +################################################################ +# +# Copyright notice +# +# (c) 2014 Copyright: Ulrich Maass +# +# 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 . +# +# Disclaimer: The Author takes no responsibility whatsoever +# for damages potentially done by this program. +# +################################################################################ +# +# This module serves as frontend to 10_Itach_IR +# +################################################################################ + +package main; +use strict; +use warnings; + +######################### +# Forward declaration +sub IIRD_Define(); +sub IIRD_Attr(@); +sub IIRD_Set($@); +sub IIRD_Get($@); +sub IIRD_getDeviceCommands($); +sub IIRD_send($$$); +sub IIRD_getIRcode($$); +sub IIRD_clearIRcodes($@); + +##################################### +# Initialize module +sub +Itach_IRDevice_Initialize($) +{ + my ($hash) = @_; + $hash->{GetFn} = "IIRD_Get"; + $hash->{SetFn} = "IIRD_Set"; + $hash->{AttrFn} = "IIRD_Attr"; + $hash->{DefFn} = "IIRD_Define"; + $hash->{AttrList} = "verbose:0,1,2,3,4,5,6 IODev"; +} + + +##################################### +# Initialize every new instance +sub +IIRD_Define() { + my ($hash, $def) = @_; + my $ret; + my ($name, $type, $file, $iodev) = split("[ \t]+", $def); + return "Usage: define ItachIRDevice " if(!$file); + + #should check if file exists + + $hash->{STATE} = "Initialized"; + $hash->{FILE} = $file if ($file); + + AssignIoPort($hash,$iodev) if( !$hash->{IODev} ); + + if(defined($hash->{IODev}->{NAME})) { + Log3 $name, 4, "$name: I/O device is " . $hash->{IODev}->{NAME}; + } else { + my $ret = "$name: no I/O device"; + Log3 $name, 1, $ret; + } + + my $cmdret= CommandAttr(undef,"$name room ItachIR") if (!AttrVal($name,'room',undef)); + + return $ret; +} + + +##################################### +# Ensure .IRcodes is created from scratch after an attribute value has been changed +sub +IIRD_Attr(@) +{ + my @a = @_; + my $hash = $defs{$a[1]}; + delete $hash->{'.IRcodes'}; # reset IRcodes so they will be loaded anew + return; +} + +##################################### +# Digest set-commands +sub +IIRD_Set($@) +{ + my ($hash, @a) = @_; + my ($nam,$cmd,$par)=@a; + return if (!defined($cmd)); + if ($cmd eq '?') { + return "Unknown argument $cmd choose one of rereadIRfile seqsingle ". IIRD_getDeviceCommands($hash); + } elsif ($cmd eq 'rereadIRfile') { + IIRD_clearIRcodes($hash); + IIRD_getDeviceCommands($hash); + } else { + IIRD_send($hash,$cmd,$par); + } + return undef; +} + +##################################### +# Digest get-commands +sub +IIRD_Get($@) +{ + my ($hash, @a) = @_; + my $arg = (defined($a[1]) ? $a[1] : ""); #command + my $name = $hash->{NAME}; + + ## get htmlcode + if($arg eq "validcommands") { + my @vc = split(' ',IIRD_getDeviceCommands($hash)); + return join(' ', sort @vc); + } else { + return "Unknown argument $arg choose one of validcommands"; + } +} + +##################################### +# Send commands to ItachIR +sub + IIRD_send($$$) { + my ($hash,$cmd,$par)=@_; + my $IRcode; + my $name=$hash->{NAME}; + if ($cmd eq 'seqsingle') { + for (my $i=0;$i{'.IRcodes'} 2 hours after last use +} + +##################################### +# Get IRcode +sub +IIRD_getIRcode($$) { + my ($hash,$cmd)=@_; + my $nam=$hash->{NAME}; + my $scmd = $cmd; + $scmd =~ s/^0$/chr0/; + if (!$hash->{'.IRcodes'} || !$hash->{'.IRcodes'}{$scmd}) { + IIRD_getDeviceCommands($hash); + if (!$hash->{'.IRcodes'} || !$hash->{'.IRcodes'}{$scmd}) { + Log3 $nam, 3, "No IRcode defined for device $nam, command $cmd"; + return undef; + } + } + return $hash->{'.IRcodes'}{$scmd}; +} + +##################################### +## get all defined commands of device +sub +IIRD_getDeviceCommands($) { + my $hash = shift; + my $ret; + my $filename=$hash->{FILE}; + if (!$hash->{'.IRcodes'}) { + my $filename = AttrVal('global','modpath','.').'/'.$filename; + Log3 $hash->{NAME}, 5, "Reading $filename ..."; + # open IR-file + if (! open(IRCODES, "< $filename")) { + Log3 $hash->{NAME}, 1, "Cannot open file $filename : $!"; + } else { + # read commands from IR-file + my $line; + while (defined ($line = )) { + $line =~ m/^\[(.*)\]\s(.*)/; + $ret .= $1.' '; + my $scmd = $1; + my $IR = $2; + $scmd =~ s/^0$/chr0/; + $hash->{'.IRcodes'}{$scmd} = $IR; + next; + } + } + } else { + foreach my $cmd (keys %{$hash->{'.IRcodes'}}) { + $cmd =~ s/^chr0$/0/; + $ret .= $cmd . ' ' if (defined($cmd)); + } + } + $ret =~ s/ $//; #delete final blank + return $ret; +} + +##################################### +# Delete IRcodes from hash to save memory +sub +IIRD_clearIRcodes($@) { + my $hash = shift; + delete $hash->{'.IRcodes'}; + return undef; +} + +1; + + +=pod +=begin html + + +

Itach_IRDevice

+
    + Itach IR is a physical device that serves to emit Infrared (IR) commands, hence it is a general IR remote control that can be controlled via WLAN (WF2IR) or LAN (IP2IR).
    + Using the iLearn-Software that ships with every Itach IR, record the IR-squences per original remotecontrol-button and store all these IR-codes as an IR-config-file. + This IR-config-file can then be used directly with this module. All commands stored in the IR-config-file will be available immediately for use.
    + For more information, check the Wiki page.
    + +
    + Define +
      + define <name> Itach_IRDevice <IR-config-file>
      + Store the IR-config-file in the same directory where fhem.cfg resides.
      + Hint: define an Itach_IR device first!
      + Example:
      + define IR_mac Itach_IRDevice IR-codes-mac.txt
      + define IR_amp Itach_IRDevice IR-codes-media-amplifier.txt +
    + +
    + Set
    + + set <name> <command> [<parameter>]

    + The list of available commands depends on the content of the IR-config-file. +
    There are only two module specific commands: +
      +
    • set <name> rereadIRfile
      + For performance reasons, the IR-config-File is read into memory upon definition of the device. If you change the configuration within that file, use this set-command to read its content into fhem once again.
    • +
    • set <name> seqsingle <parameter>
      + Will send the digits of a sequence one after the other. Useful when you have a sequence of digits to be sent, e.g. 123. Each digit must be a valid command in your IR-config-file.
    • +
    + +
    + Get +
      +
    • get <name> validcommands
      + Lists the valid commands for this device according to your IR-config-file.
    • +
    + +
    + Attributes +
      +
    • verbose
    • +
    • IODev
      + Needs to be stated if more than one ItachIR-device is part of your fhem-configuration.
    • +
    +
+ +=end html +=cut + +