##############################################
# $Id$
package main;

# From peterp
# Lacrosse TX3-TH thermo/hygro sensor

use strict;
use warnings;

sub
CUL_TX_Initialize($)
{
  my ($hash) = @_;

  $hash->{Match}     = "^TX..........";        # Need TX to avoid FHTTK
  $hash->{DefFn}     = "CUL_TX_Define";
  $hash->{UndefFn}   = "CUL_TX_Undef";
  $hash->{ParseFn}   = "CUL_TX_Parse";
  $hash->{AttrList}  = "IODev do_not_notify:1,0 ignore:0,1 " .
                        "showtime:1,0 loglevel:0,1,2,3,4,5,6";
}

#############################
sub
CUL_TX_Define($$)
{
  my ($hash, $def) = @_;
  my @a = split("[ \t][ \t]*", $def);

  return "wrong syntax: define <name> CUL_TX <code> [corr] [minsecs]"
        if(int(@a) < 3 || int(@a) > 5);

  $hash->{CODE} = $a[2];
  $hash->{corr} = ((int(@a) > 3) ? $a[3] : 0);
  $hash->{minsecs} = ((int(@a) > 4) ? $a[4] : 0);
  $hash->{lastT} =  0;
  $hash->{lastH} =  0;

  $modules{CUL_TX}{defptr}{$a[2]} = $hash;
  $hash->{STATE} = "Defined";
  Log 4, "CUL_TX defined  $a[0] $a[2]";

  return undef;
}

#####################################
sub
CUL_TX_Undef($$)
{
  my ($hash, $name) = @_;
  delete($modules{CUL_TX}{defptr}{$hash->{CODE}})
     if(defined($hash->{CODE}) &&
        defined($modules{CUL_TX}{defptr}{$hash->{CODE}}));
  return undef;
}

###################################
sub
CUL_TX_Parse($$)
{
  my ($hash, $msg) = @_;
  $msg = substr($msg, 1);
  # Msg format: TXTHHXYZXY, see http://www.f6fbb.org/domo/sensors/tx3_th.php
  my @a = split("", $msg);
  my $id2 = hex($a[4]) & 1; #meaning unknown
  my $id3 = (hex($a[3])<<3) + (hex($a[4])>>1);

  if($a[5] ne $a[8] || $a[6] ne $a[9]) {
    Log 4, "CUL_TX $id3 ($msg) data error";
    return "";
  }

  my $def = $modules{CUL_TX}{defptr}{$id3};
  if(!$def) {
    Log 2, "CUL_TX Unknown device $id3, please define it";
    return "UNDEFINED CUL_TX_$id3 CUL_TX $id3" if(!$def);
  }
  my $now = time();

  my $name = $def->{NAME};

  my $ll4 = GetLogLevel($name,4);
  Log $ll4, "CUL_TX $name $id3 ($msg)";

  my ($msgtype, $val, $changedTxt);
  my $valraw = ($a[5].$a[6].".".$a[7]);
  my $type = $a[2];
  if($type eq "0") {
    if($now - $def->{lastT} < $def->{minsecs} ) {
      return ""; 
    }
    $def->{lastT} = $now;
    $msgtype = "temperature";
    $val = sprintf("%2.1f", ($valraw - 50 + $def->{corr}) );
    Log $ll4, "CUL_TX $msgtype $name $id3 T: $val F: $id2";
    $changedTxt = "temperature: $val";

  } elsif ($type eq "E") {
    if($now - $def->{lastH} < $def->{minsecs} ) {
      return ""; 
    }
    $def->{lastH} = $now;
    $msgtype = "humidity";
    $val = $valraw;
    Log $ll4, "CUL_TX $msgtype $name $id3 H: $val F: $id2";
    $changedTxt = "humidity: $val";

  } else {
    my $ll2 = GetLogLevel($name,4);
    Log $ll2, "CUL_TX $type $name $id3 ($msg) unknown type";
    return "";

  }


  my $state="";
  my $t = ReadingsVal($name, "temperature", undef);
  my $h = ReadingsVal($name, "humidity", undef);
  if(defined($t) && defined($h)) {
    $state="T: $t H: $h";

  } elsif(defined($t)) {
    $state="T: $t";

  } elsif(defined($h)) {
    $state="H: $h";

  }

  my $tn = TimeNow();
  $def->{STATE} = $state;
  $def->{READINGS}{state}{TIME} = $tn;
  $def->{READINGS}{state}{VAL} = $state;
  $def->{CHANGED}[0] = $changedTxt;

  $def->{READINGS}{$msgtype}{VAL} = $val;
  $def->{READINGS}{$msgtype}{TIME} = $tn;

  DoTrigger($name, undef) if($init_done);
  return $name;
}

1;

