From afc4ec72fef8bc60365ae7a6220b7460484d0e86 Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Wed, 16 Nov 2011 07:45:41 +0000 Subject: [PATCH] Sample program git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@1111 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- contrib/00_TAHR.pm | 153 ++++++++++++++++++++++++++++++++++++++++++ contrib/fhemupdate.pl | 2 +- 2 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 contrib/00_TAHR.pm diff --git a/contrib/00_TAHR.pm b/contrib/00_TAHR.pm new file mode 100644 index 000000000..4b460078e --- /dev/null +++ b/contrib/00_TAHR.pm @@ -0,0 +1,153 @@ +############################################### +# Sample fhem module, one-level approach, controlling a single device like a +# directly attached heating regulator. +# The alternative is a two level approach, where a physical device like a CUL +# is a bridge to a large number of logical devices (like FS20 actors, S300 +# sensors, etc) + +package main; + +use strict; +use warnings; +use Time::HiRes qw(gettimeofday); + +sub TAHR_Read($); +sub TAHR_Ready($); +sub TAHR_setbits($$); +sub TAHR_SetReading($$$$); + +my %tahr_sets = ( + "ww_soll" => "0C07656565%02x6565", + "ww_betriebsart" => "0C0E%02x6565656565", +); + + +sub +TAHR_Initialize($) +{ + my ($hash) = @_; + + require "$attr{global}{modpath}/FHEM/DevIo.pm"; + + $hash->{ReadFn} = "TAHR_Read"; + $hash->{ReadyFn} = "TAHR_Ready"; + $hash->{DefFn} = "TAHR_Define"; + $hash->{UndefFn} = "TAHR_Undef"; + $hash->{SetFn} = "TAHR_Set"; + $hash->{AttrList}= "do_not_notify:1,0 loglevel:0,1,2,3,4,5,6"; +} + +##################################### +sub +TAHR_Define($$) +{ + my ($hash, $def) = @_; + my @a = split("[ \t][ \t]*", $def); + + return "wrong syntax: define TAHR [devicename|none]" + if(@a != 3); + + DevIo_CloseDev($hash); + my $name = $a[0]; + my $dev = $a[2]; + + if($dev eq "none") { + Log 1, "TAHR device is none, commands will be echoed only"; + return undef; + } + + $hash->{DeviceName} = $dev; + my $ret = DevIo_OpenDev($hash, 0, "TAHR_Poll"); + return $ret; +} + + +##################################### +sub +TAHR_Undef($$) +{ + my ($hash, $arg) = @_; + DevIo_CloseDev($hash); + RemoveInternalTimer($hash); + return undef; +} + +##################################### +sub +TAHR_Set($@) +{ + my ($hash, @a) = @_; + + return "\"set TAHR\" needs at least an argument" if(@a < 2); + + my $cmd = $tahr_sets{$a[1]}; + return "Unknown argument $a[1], choose one of " . + join(" ", sort keys %tahr_sets) if(!defined($cmd)); + # FIXME + DevIo_SimpleWrite($hash, $cmd); + return undef; +} + + +##################################### +# called from the global loop, when the select for hash->{FD} reports data +sub +TAHR_Read($) +{ + my ($hash) = @_; + my $name = $hash->{NAME}; + my ($data, $crc); + + my $buf = DevIo_SimpleRead($hash); + return "" if(!defined($buf)); + + $buf = unpack('H*', $buf); + Log 5, "RAW: $buf"; + + ###################################### + # Analyze the data + my $tn = TimeNow(); + my ($key, $val) = ("key", "val"); + + # FIXME + TAHR_SetReading($hash, $tn, $key, $val); +} + +##################################### +sub +TAHR_Ready($) +{ + my ($hash) = @_; + + return DevIo_OpenDev($hash, 1, undef) + if($hash->{STATE} eq "disconnected"); + + # This is relevant for windows/USB only + my $po = $hash->{USBDev}; + my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status; + return ($InBytes>0); +} + +sub +TAHR_Poll($) +{ + my ($hash) = @_; + return if($hash->{STATE} eq "disconnected"); + # FIXME + DevIo_SimpleWrite($hash, "02"); # Request data + InternalTimer(gettimeofday()+5, "TAHR_Poll", $hash, 0); +} + +sub +TAHR_SetReading($$$$) +{ + my ($hash,$tn,$key,$val) = @_; + my $name = $hash->{NAME}; + Log GetLogLevel($name,4), "$name: $key $val"; + $hash->{READINGS}{$key}{TIME} = $tn; + $hash->{READINGS}{$key}{VAL} = $val; + DoTrigger($name, "$key: $val"); +} + + +1; diff --git a/contrib/fhemupdate.pl b/contrib/fhemupdate.pl index 1f1d3e151..2002e9fbb 100755 --- a/contrib/fhemupdate.pl +++ b/contrib/fhemupdate.pl @@ -53,7 +53,7 @@ if(open FH, "UPLOAD/filetimes.txt") { chdir("UPLOAD"); open FH, ">filetimes.txt" || die "Can't open filetimes.txt: $!\n"; open FTP, ">script.txt" || die "Can't open script.txt: $!\n"; -print FTP "cd fhem.de/fhemupdate\n"; +print FTP "cd fhem/fhemupdate\n"; print FTP "put filetimes.txt\n"; print FTP "pas\n"; # Without passive only 28 files can be transferred my $cnt;