From 51adaf1d9e0f47964c565390aee6be120dc528d8 Mon Sep 17 00:00:00 2001
From: pahenning <>
Date: Tue, 16 Oct 2012 18:15:47 +0000
Subject: [PATCH] git-svn-id: https://svn.fhem.de/fhem/trunk@1980
 2b470e98-0d58-463d-a4d8-8e2adae1ed80

---
 fhem/contrib/1-Wire/00_OWX.pm      | 29 ++++++++--------------
 fhem/contrib/1-Wire/21_OWCOUNT.pm  | 39 +++++++++++++++++++-----------
 fhem/contrib/1-Wire/21_OWSWITCH.pm |  8 +++++-
 fhem/contrib/1-Wire/21_OWTHERM.pm  | 18 ++++++++------
 4 files changed, 52 insertions(+), 42 deletions(-)

diff --git a/fhem/contrib/1-Wire/00_OWX.pm b/fhem/contrib/1-Wire/00_OWX.pm
index 1bb365195..57a564f3e 100644
--- a/fhem/contrib/1-Wire/00_OWX.pm
+++ b/fhem/contrib/1-Wire/00_OWX.pm
@@ -12,7 +12,7 @@
 # Internally these interfaces are vastly different, read the corresponding Wiki pages 
 # http://fhemwiki.de/wiki/Interfaces_f%C3%BCr_1-Wire
 #
-# Version 2.20 - October, 2012
+# Version 2.22 - October, 2012
 #
 # Prof. Dr. Peter A. Henning, 2012
 #
@@ -151,7 +151,7 @@ sub OWX_Define ($$) {
   }
   
   #-- check syntax
-  Log 1,"OWX: Warning - Some parameter(s) ignored, must be define <name> OWX <serial-device>|<cuno-device>"
+  Log 1,"OWX: Warning - Some parameter(s) ignored, must be define <name> OWX <serial-device>|<cuno/coc-device>"
      if(int(@a) > 3);
   #-- If this line contains 3 parameters, it is the bus master definition
   my $dev = $a[2];
@@ -161,7 +161,7 @@ sub OWX_Define ($$) {
   #-- Dummy 1-Wire ROM identifier
   $hash->{ROM_ID} = "FF";
 
-  #-- First step: check if we have a directly connected serial interface or a CUNO attached
+  #-- First step: check if we have a directly connected serial interface or a CUNO/COC attached
   #   (mod suggested by T.Faust)
   if ( $dev =~ m/\/dev\/.*/ ){
     #-- TODO: what should we do when the specified device name contains @ already ?
@@ -386,6 +386,7 @@ sub OWX_CRC ($) {
 
 sub OWX_CRC8 ($$) {
   my ($string,$crc) = @_;
+  my $crc0=ord($crc);
   my $crc8=0;  
   my @strhex;
 
@@ -393,9 +394,9 @@ sub OWX_CRC8 ($$) {
     $strhex[$i]=ord(substr($string,$i,1));
     $crc8 = $crc8_table[ $crc8 ^ $strhex[$i] ];
   }
-    
+   
   if( defined($crc) ){
-    if ( $crc = $crc8 ){
+    if ( $crc0 == $crc8 ){
       return 1;
     }else{
       return 0;
@@ -445,17 +446,6 @@ sub OWX_DOCRC16($$) {
   return ($crc);
 }
 
-#//-------------------------------------------------------------------#
-
-#Aufruf der Funktion im Programm:
-
-#{
-#//...
-#  unsigned int DEVICE_CRC16=0;
-#  DEVICE_CRC16 = calcCRC16r (DEVICE_CRC16,chr,0xA001);
-#//...
-#}
-
 ########################################################################################
 # 
 # OWX_Detect - Detect 1-Wire interface 
@@ -610,6 +600,7 @@ sub OWX_Discover ($) {
     #-- sleeping for some time
     select(undef,undef,undef,3);
     CUL_SimpleWrite($owx_hwdevice, "Oc");
+     select(undef,undef,undef,0.5);
     my $ob = OWX_SimpleRead($owx_hwdevice);
     if( $ob ){
       foreach my $dx (split(/\n/,$ob)){
@@ -1956,10 +1947,10 @@ sub OWX_Receive_CUNO ($$) {
     }elsif( length($ob) == 20 ){
       $numread++;
     }else{
-      Log 1,"OWX: Received unexpected number of ".length($ob)." bytes from CUNO";
+      Log 1,"OWX: Received unexpected number of ".length($ob)." bytes from CUNO/COC";
     } 
   }
-  Log 3, "OWX: Receive from CUNO $numread bytes = $res2"
+  Log 3, "OWX: Receive from CUNO/COC $numread bytes = $res2"
      if( $owx_debug > 1);
   
   return($res);
@@ -2010,7 +2001,7 @@ sub OWX_Send_CUNO ($$) {
     $res2.=sprintf "0x%1x%1x ",$j,$k;
     CUL_SimpleWrite($owx_hwdevice, $res);
   } 
-  Log 3,"OWX: Send to CUNO $res2"
+  Log 3,"OWX: Send to CUNO/COC $res2"
      if( $owx_debug > 1);
 }
 
diff --git a/fhem/contrib/1-Wire/21_OWCOUNT.pm b/fhem/contrib/1-Wire/21_OWCOUNT.pm
index 46564e2b5..031676493 100644
--- a/fhem/contrib/1-Wire/21_OWCOUNT.pm
+++ b/fhem/contrib/1-Wire/21_OWCOUNT.pm
@@ -14,7 +14,7 @@
 #
 # Prof. Dr. Peter A. Henning, 2012
 # 
-# Version 2.18 - September, 2012
+# Version 2.22 - September, 2012
 #   
 # Setup bus device in fhem.cfg as
 #
@@ -44,6 +44,8 @@
 # Additional attributes are defined in fhem.cfg, in some cases per channel, where <channel>=A,B
 # Note: attributes are read only during initialization procedure - later changes are not used.
 #
+# attr <name> event on-change/on-update = when to write an event (default= on-update)
+#
 # attr <name> UnitInReading = whether the physical unit is written into the reading = 1 (default) or 0
 # attr <name> <channel>Name <string>|<string> = name for the channel | a type description for the measured value
 # attr <name> <channel>Unit <string>|<string> = unit of measurement for this channel | its abbreviation 
@@ -142,7 +144,8 @@ sub OWCOUNT_Initialize ($) {
   $hash->{SetFn}   = "OWCOUNT_Set";
   
   #-- see header for attributes
-  my $attlist = "IODev do_not_notify:0,1 showtime:0,1 model:DS2423 loglevel:0,1,2,3,4,5 UnitInReading:0,1";
+  my $attlist = "IODev do_not_notify:0,1 showtime:0,1 model:DS2423 loglevel:0,1,2,3,4,5 UnitInReading:0,1 ".
+    "event:on-update,on-change";
   for( my $i=0;$i<int(@owg_fixed);$i++ ){
     $attlist .= " ".$owg_fixed[$i]."Name";
     $attlist .= " ".$owg_fixed[$i]."Offset";
@@ -257,7 +260,7 @@ sub OWCOUNT_InitializeDevice($) {
     my $unit = defined($attr{$name}{$owg_fixed[$i]."Unit"})  ? $attr{$name}{$owg_fixed[$i]."Unit"} : "counts|cts";
     my @unarr= split(/\|/,$unit);
     if( int(@unarr)!=2 ){
-      Log 1, "OWCOUNT: Incomplete channel unit specification $unit. Better use $unit|<abbreviation>";
+      Log 1, "OWCOUNT: Incomplete channel unit specification $unit. Better use <long unit desc>|$unit";
       push(@unarr,"");  
     }
     
@@ -694,12 +697,18 @@ sub OWCOUNT_GetValues($) {
     return "OWCOUNT: Could not get values from device $name";
   }
   $hash->{PRESENT} = 1; 
-  $value=OWCOUNT_FormatValues($hash);
-  #--logging
-  Log 5, $value;
-  $hash->{CHANGED}[0] = $value;
   
-  DoTrigger($name, undef);
+   #-- old state, new state
+  my $oldval = $hash->{STATE};
+  $value=OWCOUNT_FormatValues($hash);
+  my $newval =  $hash->{STATE};
+   #--logging depends on setting of the event-attribute
+  Log 5, $value;
+  my $ev = defined($attr{$name}{"event"})  ? $attr{$name}{"event"} : "on-update";  
+  if( ($ev eq "on-update") || (($ev eq "on-change") && ($newval ne $oldval)) ){
+     $hash->{CHANGED}[0] = $value;
+     DoTrigger($name, undef);
+  } 
   
   return undef;
 }
@@ -909,11 +918,13 @@ sub OWXCOUNT_GetPage($$) {
   OWX_Reset($master);
 
   #-- process results
-  if( length($res) < 54){
-    Log 1, "OWXCOUNT: warning, have received ".length($res)." bytes in three steps";
-    #return "OWXCOUNT: warning, have received ".length($res)." bytes in three steps";
-  }
-  #Log 1, "OWXCOUNT: warning, have received ".length($res)." bytes in three steps";
+  @data=split(//,$res);
+  return "OWXCOUNT: invalid data length, ".length($res)." bytes in three steps"
+     if( length($res) < 54);
+  #return "invalid data"
+  #  if (ord($data[17])<=0); 
+  #return "invalid CRC"
+  #  if (OWX_CRC8(substr($res,10,8),$data[18])==0);  
   
   #-- first 12 byte are 9 ROM ID +3 command, next 32 are memory
   #-- memory part, treated as string
@@ -927,7 +938,7 @@ sub OWXCOUNT_GetPage($$) {
     }
   
     #-- first ignore memory and only use counter (Fehler gefunden von jamesgo)
-    my $value = (ord($data[3])<<32) + (ord($data[2])<<16) +(ord($data[1])<<8) + ord($data[0]);       
+    my $value = (ord($data[3])<<24) + (ord($data[2])<<16) +(ord($data[1])<<8) + ord($data[0]);       
    
     if( $page == 14) {
       $owg_val[0] = $value;
diff --git a/fhem/contrib/1-Wire/21_OWSWITCH.pm b/fhem/contrib/1-Wire/21_OWSWITCH.pm
index c80ea2a87..156f20900 100644
--- a/fhem/contrib/1-Wire/21_OWSWITCH.pm
+++ b/fhem/contrib/1-Wire/21_OWSWITCH.pm
@@ -17,7 +17,7 @@
 #
 # Prof. Dr. Peter A. Henning, 2012
 # 
-# Version 2.18 - September, 2012
+# Version 2.22 - September, 2012
 #   
 # Setup bus device in fhem.cfg as
 #
@@ -719,6 +719,12 @@ sub OWXSWITCH_GetState($) {
   
   #-- process results
   @data=split(//,substr($res,10));
+  #return "invalid data length"
+  #  if (@data != 22); 
+  #return "invalid data"
+  #  if (ord($data[17])<=0); 
+  #return "invalid CRC"
+  #  if (OWX_CRC8(substr($res,10,8),$data[18])==0);  
   
   #-- reset the bus
   OWX_Reset($master);
diff --git a/fhem/contrib/1-Wire/21_OWTHERM.pm b/fhem/contrib/1-Wire/21_OWTHERM.pm
index 106546b29..1b6d4258c 100755
--- a/fhem/contrib/1-Wire/21_OWTHERM.pm
+++ b/fhem/contrib/1-Wire/21_OWTHERM.pm
@@ -15,7 +15,7 @@
 # Prof. Dr. Peter A. Henning, 2012
 # Martin Fischer, 2011
 # 
-# Version 2.20 - October, 2012
+# Version 2.22 - October, 2012
 #   
 # Setup bus device in fhem.cfg as
 #
@@ -469,12 +469,14 @@ sub OWTHERM_GetValues($@) {
   }elsif( $interface eq "OWFS" ){
     $ret = OWFSTHERM_GetValues($hash);
   }else{
-    return "OWTHERM: GetValues with wrong IODev type $interface";
+    Log 3, "OWTHERM: GetValues with wrong IODev type $interface";
+    return 1;
   }
 
   #-- process results
   if( defined($ret)  ){
-    return "OWTHERM: Could not get values from device $name, reason $ret";
+    Log 3, "OWTHERM: Could not get values from device $name, reason $ret";
+    return 1;
   }
   $hash->{PRESENT} = 1; 
 
@@ -677,7 +679,7 @@ sub OWXTHERM_GetValues($) {
     OWX_Reset($master);
     #-- issue the match ROM command \x55 and the start conversion command
     if( OWX_Complex($master,$owx_dev,"\x44",0) eq 0 ){
-      return "OWXTHERM: Device $owx_dev not accessible";
+      return "$owx_dev not accessible";
     } 
     #-- conversion needs some 950 ms - but we may also do it in shorter time !
     select(undef,undef,undef,1.0);
@@ -691,7 +693,7 @@ sub OWXTHERM_GetValues($) {
   #Log 1,"OWXTHERM: data length from reading device is ".length($res)." bytes";
   #-- process results
   if( $res eq 0 ){
-    return "OWXTHERM: Device $owx_dev not accessible in 2nd step"; 
+    return "$owx_dev not accessible in 2nd step"; 
   }
   
   #if (length($res) == 10){
@@ -708,11 +710,11 @@ sub OWXTHERM_GetValues($) {
   #$res="000000000".$res
   #  if(length($res)==10);
   my  @data=split(//,$res);
-  return "OWTHERM: invalid data length, ".int(@data)." bytes"
+  return "invalid data length, ".int(@data)." bytes"
     if (@data != 19); 
-  return "OWXTHERM: invalid data"
+  return "invalid data"
     if (ord($data[17])<=0); 
-  return "OWXTHERM: invalid CRC"
+  return "invalid CRC"
     if (OWX_CRC8(substr($res,10,8),$data[18])==0);
   
   #-- this must be different for the different device types