#!/s/perl/bin/perl -w

###############################################################################
#
#  sense.pl 
#
#  read all data from HA7Net 1-wire device and put in database
#
#    note: 
#     If the project increases in scope, more fields will need
#     to be added to the database, such as the IP address that the
#     sensors are located on
#
################################################################################


################################################################################
# Tim Czerwonka, timc@cs.wisc.edu
# $Id: 1wiresense.pl,v 1.2 2007/11/14 16:00:16 timc Exp $
#
# $Log: 1wiresense.pl,v $
# Revision 1.2  2007/11/14 16:00:16  timc
# added path to perl lib for HA7net hardware
#
# Revision 1.1  2007/11/14 15:59:37  timc
# Initial revision
#
# Revision 1.2  2007/11/13 22:15:00  timc
# updated rcs comment
#
################################################################################


use Data::Dumper;

use CSL::Pg;
use CSL::DBs;
use DBI;
my $dbh = DBI->connect($CSL::DBs::csdb) or die $DBI::errstr; 
$dbh->do("set search_path to sensornet");

use lib "/s/lab/src/1wire/Hardware-1Wire-HA7Net-1.01";
use lib::Hardware::1Wire::HA7Net;



################################################################################
# functions
################################################################################



# get_sensor_location -- return xyz location of specified device id
sub get_sensor_location{
    local ($device) = @_;

    my $query = $dbh->prepare("SELECT x, y, z
                               FROM sensors
                               WHERE id = ?");

    $query->execute($device);
    $data = $query->fetchrow_hashref;

    my $x = $data->{x};
    my $y = $data->{y};
    my $z = $data->{z};

    return ($x, $y, $z);
}



# store_sensor_data -- store the sensor data in the database
sub store_sensor_data{
    local ($sensor) = @_;

    #get everything together here
    ($id, $temp) = $sensor->temperature;
    ($x, $y, $z) = get_sensor_location($id);

    $db_update = $dbh->prepare("INSERT into data (id, x, y, z, time, value, unit, location)
                            values (?, ?, ?, ?, current_timestamp, ?, ?, ?)");

    $db_update->execute($id, $x, $y, $z, $temp, "C", "2360");


    return 0;
}


################################################################################
# main 
################################################################################

my @sensor_list;
my @ha7net_addresses;
my $busmaster;
my $pagecontent;
my $debug = 0;


# parse command line arguments (room number? debug?)
# * none right now


# get list of HA7net IP addresses
# could be a function call here, but not necessary now
@ha7net_addresses = "192.168.0.250";


# read all devices, store data
foreach $device (@ha7net_addresses) {
    $busmaster = new Hardware::1Wire::HA7Net ("http://$device");

    for $s ($busmaster->sensors) {
	if ($s->isa("ds1820")) {
	    if ($debug) {print join " ", "DS1820", $s->temperature, "\n";}
	    store_sensor_data($s);
        }
	elsif ($s->isa("ds18b20")) {
	    if ($debug) {print join " ", "DS18B20", $s->temperature, "\n";}
	    store_sensor_data($s);
        }
	elsif ($s->isa("hmp2001s")) {
	    #print join " ", "HMP2001S", $s->value, "\n";
	    die "Never-before-seen sensor type: ", $s->type, "\n";
        }
	else {
	    die "Unrecognized sensor type: ", $s->type, "\n";
        }
    } #end for
} #end foreach

exit 0;

