#!/usr/bin/perl -W

# DigiTemp MySQL logging script
# Copyright 2002 by Brian C. Lane <bcl@brianlane.com>
# All Rights Reserved
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
#
# -------------------------[ HISTORY ]-------------------------------------
# 01/08/2004  The storage definition should have been decimal(6,2) instead
# bcl         of decimal(3,2). 
#             See http://www.mysql.com/doc/en/Numeric_types.html for a 
#             good description of how decimal(a,b) works.
#
# 08/18/2002  Putting together this MySQL logging script for the new 
# bcl         release of DigiTemp.
#
# -------------------------------------------------------------------------
#CREATE table digitemp (
#  dtKey int(11) NOT NULL auto_increment,
#  time timestamp NOT NULL,
#  kbin decimal(6,2) NOT NULL,
#  kbut decimal(6,2) NOT NULL,
#  gvuf decimal(6,2) NOT NULL,
#  gvur decimal(6,2) NOT NULL,
#  rf decimal(6,2) NOT NULL,
#  rr decimal(6,2) NOT NULL,
#  vv decimal(6,2) NOT NULL,
#  gvnf decimal(6,2) NOT NULL,
#  gvnr decimal(6,2) NOT NULL,
#  ute decimal(6,2) NOT NULL,
#  PRIMARY KEY (dtKey),
#  KEY time_key (time)
#);
#
# GRANT SELECT,INSERT ON digitemp.* TO vp_logger@localhost
# IDENTIFIED BY 'TopSekRet';
#
# -------------------------------------------------------------------------
use DBI;


# Database info
my $db_name     = "databasnamn";
my $db_user     = "användare";
my $db_pass     = "lösen"; 

# The DigiTemp Configuration file to use  
my $digitemp_rcfile = "/etc/digitemp.conf";  
my $digitemp_binary = "/usr/bin/digitemp_DS9097";  
  
my $debug = 0;  
my $i =0; 
my $array; 
  
# Connect to the database  
my $dbh = DBI->connect("dbi:mysql:$db_name","$db_user","$db_pass")  
          or die "I cannot connect to dbi:mysql:$db_name as $db_user - $DBI::errstr\n";  
   
# Gather information from DigiTemp  
# Read the output from digitemp  
# Output in form SerialNumber<SPACE>Temperature in Celcius   
open( DIGITEMP, "$digitemp_binary -q -a -o\"%.2C\" -c $digitemp_rcfile |" );  
 
while( <DIGITEMP> )   
{  
  print "$_\n" if($debug);  
  chomp;  
  $array->[$i] = $_; 
  $i += 1; 
} 
close( DIGITEMP );  
 
$vv = $array->[0];
$ute = $array->[1];
$rr = $array->[2];
$gvur = $array->[3];
$kbin = $array->[4];
$gvnr = $array->[5];
$gvuf = $array->[6];
$gvnf = $array->[7];
$kbut = $array->[8];
$rf = $array->[9];
 
my $sql="INSERT INTO digitemp SET kbin='$kbin', kbut='$kbut', gvuf='$gvuf', gvur='$gvur', rf='$rf', rr='$rr', vv='$vv', gvnf='$gvnf'
, gvnr='$gvnr', ute='$ute'";  
 
print "SQL: $sql\n" if($debug);  
 
$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";  
 
$dbh->disconnect;