Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use Device::SerialPort qw( :PARAM :STAT 0.07 );
- use XML::Simple;
- use DBI;
- ########################################################################
- # configure parameters here
- ########################################################################
- $dbhost = "localhost";
- $dbname = "tinplh";
- $dbuser = "tinplh_user";
- $dbpass = "tinplh_pass";
- $provider = "Current Cost";
- @elements = ('CC-TEMP', 'CC-WATTS');
- $port = "/dev/currentcost";
- $log_activity = 1; # set to 0 or 1 to insert into activity log
- ########################################################################
- # open database and retrieve the sensor ids for this provider
- $dbh = DBI->connect("dbi:mysql:$dbname;$dbhost", $dbuser, $dbpass) or die "Can not connect: " . $DBI::errstr;
- $sql = "SELECT s.name, s.id FROM sensors s, sensortypes t WHERE s.sensortype = t.id and t.provider = '$provider'";
- $sth = $dbh->prepare($sql) or die "Can not prepare: " . $dbh->errstr();
- $sth->execute() or die "Can not execute: " . $sth->errstr();
- $sensor_hash = ();
- while (@data = $sth->fetchrow_array()) {
- die "sensor lookup failed" unless $data[0];
- $sensor_hash->{$data[0]} = $data[1];
- }
- $sth->finish();
- # open the serial port and set the baudrate for it
- $ob = Device::SerialPort->new($port) or die "Can not open port $port\n";
- $ob->baudrate(57600);
- $ob->write_settings;
- $ob->close;
- $backend = 'XML::Parser';
- $ENV{XML_SIMPLE_PREFERRED_PARSER} = $backend;
- open(SERIAL, "<$port");
- # loop forever
- while (1) {
- sleep(2);
- # read a line from the serial port
- while ($line = <SERIAL>) {
- chop($line);
- # if the message is invalid, continue with the next record
- $isValid = (index($line, "<msg>") != -1);
- if (!$isValid) { last; }
- # force XML::Simple to see this as a string not as a file
- $line = "<fakeTag>$line</fakeTag>";
- $nref = XMLin($line, forcearray => 0);
- $ref = $nref->{msg};
- $isHistoric = (index($line, "<hist>") != -1);
- if (!$isHistoric) {
- # $ccname = $ref->{src}; # source and software version
- # $dsb = 0 + $ref->{dsb}; # days since birth, ie days run
- # $recordTime = $ref->{time}; # 24 hour clock time as displayed
- $temp = $ref->{tmpr}; # temperature as displayed
- $ch1watts = 0 + $ref->{ch1}->{watts}; # sensor channel 1 watts
- # $sensor = 0 + $ref->{sensor}; # appliance number as displayed
- # $id = $ref->{id}; # radio id received from the sensor
- # $type = 0 + $ref->{type}; # sensor type: "1" = electricity
- $num{'CC-WATTS'} = $ch1watts;
- $num{'CC-TEMP'} = $temp;
- # insert data into db
- foreach $element (@elements) {
- $value = $num{$element};
- $sensorid = $sensor_hash->{$element};
- if (!defined $sensorid) { $sensorid = 0; }
- if ($sensorid > 0)
- {
- if ($log_activity) {
- # insert activity record
- $sql = "INSERT INTO activity VALUES (DEFAULT, $sensorid, '$value', NULL)";
- $sth = $dbh->prepare($sql) or die "Can not prepare: " . $dbh->errstr();
- $sth->execute() or die "Can not execute: " . $sth->errstr();
- $sth->finish();
- }
- # update status record
- $sql = "INSERT INTO status (sensor, value, tstamp) VALUES ($sensorid, '$value', NULL) " .
- "ON DUPLICATE KEY UPDATE value='$value', tstamp=NOW()";
- $sth = $dbh->prepare($sql) or die "Can not prepare: " . $dbh->errstr();
- $sth->execute() or die "Can not execute: " . $sth->errstr();
- $sth->finish();
- }
- }
- }
- }
- }
- close(SERIAL);
- # close database
- $dbh->disconnect;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement