From 59508b48e90f26ee1a3fd95a51dd2df4c5e9d282 Mon Sep 17 00:00:00 2001 From: neubert Date: Sun, 7 Dec 2008 20:11:54 +0000 Subject: [PATCH] - reworked 15_CUL_EM.pm to account for timer wraparounds, more readings added - speed gain at startup through disabled refreshvalues query to all FHTs at definition; if you want it back at a "set myFHT report1 255 report2 255" command to the config file. git-svn-id: https://fhem.svn.sourceforge.net/svnroot/fhem/trunk/fhem@282 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- CHANGED | 8 +++- FHEM/11_FHT.pm | 30 ++++++------- FHEM/15_CUL_EM.pm | 105 +++++++++++++++++++++++++++++++++++++++------- HISTORY | 6 +++ 4 files changed, 118 insertions(+), 31 deletions(-) diff --git a/CHANGED b/CHANGED index 47bf64d01..cd4816750 100644 --- a/CHANGED +++ b/CHANGED @@ -442,7 +442,7 @@ - bugfix: FHT short message warning - bugfix: rereadconfig crashes with active webpgm2 connections (2008-11-13) - bugfix: watchdog crash (2008-11-15) - - bugfix: Strange call for nonexistent MyCUL: ReadFn + - bugfix: Strange call for nonexistent MyCUL: ReadFn - feature: webpgm2: gplot output goes to /tmp/gnuplot.err - feature: devspec TYPE,DEF,STATE. e.g. list TYPE:FS20, set DEF:123 on - bugfix: at schedules 2 events after the DST change (fix not verified) @@ -450,3 +450,9 @@ - feature: CUL / CUL_EM / CUL_WS documentation - feature: do not block fhem when the CUR is disconnected - bugfix: correct correction factors for EMEM in 15_CUL_EM.pm + - feature: reworked 15_CUL_EM.pm to account for timer wraparounds, more + readings added + - feature: speed gain through disabled refreshvalues query to all FHTs at + definition; if you want it back at a "set myFHT report1 255 + report2 255" command to the config file. + diff --git a/FHEM/11_FHT.pm b/FHEM/11_FHT.pm index 2bb4e33fe..136295da9 100755 --- a/FHEM/11_FHT.pm +++ b/FHEM/11_FHT.pm @@ -95,14 +95,14 @@ my %cantset = ( my %priority = ( - "desired-temp"=> 1, - "mode" => 2, - "report1" => 3, + "desired-temp"=> 1, + "mode" => 2, + "report1" => 3, "report2" => 3, - "holiday1" => 4, - "holiday2" => 5, - "day-temp" => 6, - "night-temp" => 7, + "holiday1" => 4, + "holiday2" => 5, + "day-temp" => 6, + "night-temp" => 7, ); my %c2m = (0 => "auto", 1 => "manual", 2 => "holiday", 3 => "holiday_short"); @@ -274,7 +274,7 @@ FHT_Define($$) $a[2] = lc($a[2]); return "Define $a[0]: wrong CODE format: specify a 4 digit hex value" if($a[2] !~ m/^[a-f0-9][a-f0-9][a-f0-9][a-f0-9]$/i); - + $hash->{CODE} = $a[2]; $hash->{CODE} = $a[2]; @@ -283,8 +283,8 @@ FHT_Define($$) AssignIoPort($hash); - Log GetLogLevel($a[0],2),"Asking the FHT device $a[0]/$a[2] to send its data"; - FHT_Set($hash, ($a[0], "report1", "255", "report2", "255")); + #Log GetLogLevel($a[0],2),"Asking the FHT device $a[0]/$a[2] to send its data"; + #FHT_Set($hash, ($a[0], "report1", "255", "report2", "255")); return undef; } @@ -299,7 +299,7 @@ FHT_Undef($$) } ##################################### -sub +sub FHT_Parse($$) { my ($hash, $msg) = @_; @@ -368,7 +368,7 @@ FHT_Parse($$) if($sval =~ m/.6/) { $val = "$fv" } elsif($sval =~ m/.8/) { $val = "offset: $fv" } elsif($sval =~ m/.a/) { $val = "lime-protection" } - elsif($sval =~ m/.c/) { $val = "synctime" } + elsif($sval =~ m/.c/) { $val = "synctime" } elsif($sval =~ m/.e/) { $val = "test" } elsif($sval =~ m/.f/) { $val = "pair" } else { $val = "unknown_$sval: $fv" } @@ -415,7 +415,7 @@ FHT_Parse($$) ################################ # Softbuffer: delete confirmed commands if($confirm) { - my $found; + my $found; my $io = $def->{IODev}; foreach my $key (sort keys %{$io->{SOFTBUFFER}}) { my $h = $io->{SOFTBUFFER}{$key}; @@ -442,7 +442,7 @@ doSoftBuffer($) my ($io) = @_; my $now = gettimeofday(); - + my $count = 0; my $fhzbuflen = -999; foreach my $key (keys %{ $io->{SOFTBUFFER} }) { @@ -517,7 +517,7 @@ getFhtBuffer($) my $msg = FHZ_ReadAnswer($io, "fhtbuf", 1.0); if(!defined($msg)) { $msg= ""; } Log 5, "getFhtBuffer: $count $msg"; - + return hex(substr($msg, 16, 2)) if($msg && $msg =~ m/^[0-9A-F]+$/i); return 0 if($count++ >= 5); } diff --git a/FHEM/15_CUL_EM.pm b/FHEM/15_CUL_EM.pm index 0e9f0bac9..a599ad5d0 100755 --- a/FHEM/15_CUL_EM.pm +++ b/FHEM/15_CUL_EM.pm @@ -80,30 +80,105 @@ CUL_EM_Parse($$) my @a = split("", $msg); my $tpe = ($a[1].$a[2])+0; my $cde = ($a[3].$a[4])+0; - my $cnt = hex($a[5].$a[6]); - my $cum = hex($a[ 9].$a[10].$a[ 7].$a[ 8]); - my $lst = hex($a[13].$a[14].$a[11].$a[12]); - my $top = hex($a[17].$a[18].$a[15].$a[16]); + my $seqno = hex($a[5].$a[6]); + my $total_cnt = hex($a[ 9].$a[10].$a[ 7].$a[ 8]); + my $current_cnt = hex($a[13].$a[14].$a[11].$a[12]); + my $peak_cnt = hex($a[17].$a[18].$a[15].$a[16]); + + # these are the raw readings from the device my $val = sprintf("CNT: %d CUM: %d 5MIN: %d TOP: %d", - $cnt, $cum, $lst, $top); + $seqno, $total_cnt, $current_cnt, $peak_cnt); + + + # seqno = number of received datagram in sequence, runs from 2 to 255 + # total_cnt= total (cumulated) value in ticks as read from the device + # basis_cnt= correction to total (cumulated) value in ticks to account for + # counter wraparounds + # total = total (cumulated) value in device units + # current = current value (average over latest 5 minutes) in device units + # peak = maximum value in device units if($defptr{$cde}) { $hash = $defptr{$cde}; - my $corr1 = $hash->{corr1}; # EMEM power correction factor - my $corr2 = $hash->{corr2}; # EMEM energy correction factor - $cum *= $corr2; - $lst *= $corr1; - $top *= $corr1; - $val = sprintf("CNT: %d CUM: %0.3f 5MIN: %0.3f TOP: %0.3f", - $cnt, $cum, $lst, $top); + + + # count changes + my $c= 0; + + # set state to raw readings my $n = $hash->{NAME}; Log GetLogLevel($n,1), "CUL_EM $n: $val"; $hash->{STATE} = $val; + $hash->{CHANGED}[$c++] = $val; - $hash->{CHANGED}[0] = $val; - $hash->{STATE} = $val; - $hash->{READINGS}{state}{TIME} = TimeNow(); + + # + # calculate readings + # + + # current time + my $tn = TimeNow(); + + # update sequence number reading + $hash->{READINGS}{seqno}{TIME} = $tn; + $hash->{READINGS}{seqno}{VAL} = $seqno; + $hash->{CHANGED}[$c++] = "seqno: $seqno"; + + # update raw readings + $hash->{READINGS}{state}{TIME} = $tn; $hash->{READINGS}{state}{VAL} = $val; + $hash->{CHANGED}[$c++] = "state: $val"; + + # initialize total_cnt_last + my $total_cnt_last; + if(defined($hash->{READINGS}{total_cnt})) { + $total_cnt_last= $hash->{READINGS}{total_cnt}{VAL}; + } else { + $total_cnt_last= 0; + } + + # update total_cnt reading + $hash->{READINGS}{total_cnt}{TIME} = $tn; + $hash->{READINGS}{total_cnt}{VAL} = $total_cnt; + $hash->{CHANGED}[$c++] = "total_cnt: $total_cnt"; + + # initialize basis_cnt_last + my $basis_cnt_last; + if(defined($hash->{READINGS}{basis_cnt})) { + $basis_cnt_last= $hash->{READINGS}{basis}{VAL}; + } else { + $basis_cnt_last= 0; + } + + # correct counter wraparound + my $basis_cnt= $basis_cnt_last; + if($total_cnt< $total_cnt_last) { + $basis_cnt+= 65536; + # update basis_cnt + $hash->{READINGS}{basis}{TIME}= $tn; + $hash->{READINGS}{basis}{VAL} = $basis_cnt; + $hash->{CHANGED}[$c++] = "basis: $basis_cnt"; + } + + # + # translate into device units + # + my $corr1 = $hash->{corr1}; # EMEM power correction factor + my $corr2 = $hash->{corr2}; # EMEM energy correction factor + + my $total = ($basis_cnt+$total_cnt)*$corr2; + my $current = $current_cnt*$corr1; + my $peak = $peak_cnt*$corr1; + + $hash->{CHANGED}[$c++] = "total: $total"; + $hash->{READINGS}{total}{TIME} = $tn; + $hash->{READINGS}{total}{VAL} = $total; + $hash->{CHANGED}[$c++] = "current: $current"; + $hash->{READINGS}{current}{TIME} = $tn; + $hash->{READINGS}{current}{VAL} = $current; + $hash->{CHANGED}[$c++] = "peak: $peak"; + $hash->{READINGS}{peak}{TIME} = $tn; + $hash->{READINGS}{peak}{VAL} = $peak; return $hash->{NAME}; diff --git a/HISTORY b/HISTORY index 306b6312b..ec40aa211 100644 --- a/HISTORY +++ b/HISTORY @@ -343,3 +343,9 @@ Note: we have to sleep after disconnect for 5 seconds, else the Linux kernel sends us a SIGSEGV, and the USB device is gone till the next reboot. - the fhem CUL part documented + +- Sun Dec 7 21:09 (Boris) + - reworked 15_CUL_EM.pm to account for timer wraparounds, more readings added + - speed gain through disabled refreshvalues query to all FHTs at definition; + if you want it back at a "set myFHT report1 255 report2 255" command to the + config file.