Advertisement
Guest User

Untitled

a guest
May 11th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.74 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use Device::SerialPort qw( :PARAM :STAT 0.07 );
  4. use XML::Simple;
  5. use DBI;
  6.  
  7. ########################################################################
  8. # configure parameters here
  9. ########################################################################
  10.  
  11. $dbhost = "localhost";
  12. $dbname = "tinplh";
  13. $dbuser = "tinplh_user";
  14. $dbpass = "tinplh_pass";
  15.  
  16. $provider = "Current Cost";
  17. @elements = ('CC-TEMP', 'CC-WATTS');
  18.  
  19. $port = "/dev/currentcost";
  20.  
  21. $log_activity = 1; # set to 0 or 1 to insert into activity log
  22.  
  23. ########################################################################
  24.  
  25. # open database and retrieve the sensor ids for this provider
  26. $dbh = DBI->connect("dbi:mysql:$dbname;$dbhost", $dbuser, $dbpass) or die "Can not connect: " . $DBI::errstr;
  27. $sql = "SELECT s.name, s.id FROM sensors s, sensortypes t WHERE s.sensortype = t.id and t.provider = '$provider'";
  28. $sth = $dbh->prepare($sql) or die "Can not prepare: " . $dbh->errstr();
  29. $sth->execute() or die "Can not execute: " . $sth->errstr();
  30. $sensor_hash = ();
  31. while (@data = $sth->fetchrow_array()) {
  32.   die "sensor lookup failed" unless $data[0];
  33.   $sensor_hash->{$data[0]} = $data[1];
  34. }
  35. $sth->finish();
  36.  
  37. # open the serial port and set the baudrate for it
  38. $ob = Device::SerialPort->new($port) or die "Can not open port $port\n";
  39. $ob->baudrate(57600);
  40. $ob->write_settings;
  41. $ob->close;
  42.  
  43. $backend = 'XML::Parser';
  44. $ENV{XML_SIMPLE_PREFERRED_PARSER} = $backend;
  45.  
  46. open(SERIAL, "<$port");
  47.  
  48. # loop forever
  49. while (1) {
  50.  
  51.   sleep(2);
  52.  
  53.   # read a line from the serial port
  54.   while ($line = <SERIAL>) {
  55.     chop($line);
  56.  
  57.     # if the message is invalid, continue with the next record
  58.     $isValid = (index($line, "<msg>") != -1);
  59.     if (!$isValid) { last; }
  60.  
  61.     # force XML::Simple to see this as a string not as a file
  62.     $line = "<fakeTag>$line</fakeTag>";
  63.  
  64.     $nref = XMLin($line, forcearray => 0);
  65.     $ref = $nref->{msg};
  66.  
  67.     $isHistoric = (index($line, "<hist>") != -1);
  68.     if (!$isHistoric) {
  69.  
  70. #      $ccname     = $ref->{src};              # source and software version
  71. #      $dsb        = 0 + $ref->{dsb};          # days since birth, ie days run
  72. #      $recordTime = $ref->{time};             # 24 hour clock time as displayed
  73.       $temp       = $ref->{tmpr};             # temperature as displayed
  74.       $ch1watts   = 0 + $ref->{ch1}->{watts}; # sensor channel 1 watts
  75. #      $sensor     = 0 + $ref->{sensor};       # appliance number as displayed
  76. #      $id         = $ref->{id};               # radio id received from the sensor
  77. #      $type       = 0 + $ref->{type};         # sensor type: "1" = electricity
  78.  
  79.       $num{'CC-WATTS'} = $ch1watts;
  80.       $num{'CC-TEMP'} = $temp;
  81.  
  82.       # insert data into db
  83.       foreach $element (@elements) {
  84.  
  85.         $value = $num{$element};
  86.  
  87.         $sensorid = $sensor_hash->{$element};
  88.         if (!defined $sensorid) { $sensorid = 0; }
  89.  
  90.         if ($sensorid > 0)
  91.         {
  92.           if ($log_activity) {
  93.             # insert activity record
  94.             $sql = "INSERT INTO activity VALUES (DEFAULT, $sensorid, '$value', NULL)";
  95.             $sth = $dbh->prepare($sql) or die "Can not prepare: " . $dbh->errstr();
  96.             $sth->execute() or die "Can not execute: " . $sth->errstr();
  97.             $sth->finish();
  98.           }
  99.  
  100.           # update status record
  101.           $sql = "INSERT INTO status (sensor, value, tstamp) VALUES ($sensorid, '$value', NULL) " .
  102.                  "ON DUPLICATE KEY UPDATE value='$value', tstamp=NOW()";
  103.           $sth = $dbh->prepare($sql) or die "Can not prepare: " . $dbh->errstr();
  104.           $sth->execute() or die "Can not execute: " . $sth->errstr();
  105.           $sth->finish();
  106.         }
  107.       }
  108.     }
  109.   }
  110. }
  111.  
  112. close(SERIAL);
  113.  
  114. # close database
  115. $dbh->disconnect;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement