Store Min Max

<?php

  class Serial
  {
    private $fd;

    public function open( $port )
    {
      exec( 'mode ' . $port . ' baud=115200 data=8 stop=1 parity=n' );
      $this->fd = dio_open( $port, O_RDWR );
      return $this->fd;
    }

    public function close()
    {
      dio_close( $this->fd );
    }

    public function readline()
    {
      if( $this->fd )
      {
        $line = "";
        while( ($ch = dio_read( $this->fd, 1 )) != "\n" )
        {
          if( $ch != "\r" )
            $line .= $ch;
        }
        return $line;
      }
    }
  }

  class Temperature_Database
  {
    public $stmt;
    public $mysqli;

    public function __construct()
    {
      global $mean;
      global $std_dev;
      global $min_temp;
      global $max_temp;

      $db_name = "temperature";

      //
      // This program can be called with optional arguments:
      //   --database=foo         the name of the database to use
      //   --new                  delete the old database first
      //

      $options = getopt( "", [ "database::", "new::" ] );

      $drop_it = FALSE;
      foreach( array_keys( $options ) as $key )
      {
        if( $key == "new" )
          $drop_it = TRUE;
        if( $key == "database" )
        {
          $db_name = $options[$key];
        }
      }

      print( "Using database $db_name\n" );

      //
      // Connect to the MySql server on localhost
      //
      $this->mysqli = new mysqli( 'localhost', 'root', '' )
        or die( 'Could not connect: ' . mysqli_error() );

      //
      // Optionally start a fresh database by dropping the old one
      //
      if( $drop_it )
      {
        print( "Droping database $db_name\n" );
        $this->mysqli->query( 'DROP DATABASE IF EXISTS ' . $db_name )
          or die( "Could not drop database '$db_name'" );
      }
        
      //
      // Create the database if it does not exist
      //
      $this->mysqli->query( 'CREATE DATABASE IF NOT EXISTS ' . $db_name )
        or die( "Could not create database '$db_name'" );

      //
      // Select the database
      //
      $this->mysqli->select_db( $db_name )
          or die( "Could not select database '$db_name'" );


      if( $drop_it )
        $this->mysqli->query("DROP TABLE IF EXISTS temps");

      //
      // Create a table of data if this is a new database
      //
      $create_table = "CREATE TABLE IF NOT EXISTS temps ("
        . " id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
        . " mean DOUBLE NOT NULL,"
        . " std_dev DOUBLE NOT NULL,"
        . " min_temp DOUBLE NOT NULL,"
        . " max_temp DOUBLE NOT NULL,"
        . " reg_date TIMESTAMP )";

      $this->mysqli->query( $create_table )
        or die( 'Create table failed: ' . $this->mysqli->error );

      //
      // Prepare our insert query
      //
      $this->stmt = $this->mysqli->prepare(
        "INSERT INTO temps( mean, std_dev, min_temp, max_temp ) VALUES( ?, ?, ?, ? )" )
          or die( 'Prepare failed: ' . $this->mysqli->error );

      //
      // And bind it
      //
      $this->stmt->bind_param( "dddd", $mean, $std_dev, $min_temp, $max_temp )
        or die( 'Bind_param failed: ' . $this->mysqli->error );
        
    }

    public function save_temp()
    {
      global $mean;
      global $std_dev;
      global $min_temp;
      global $max_temp;

      //
      // This will execute our previously prepared and bound insert query
      //
      $this->stmt->execute()
        or die( 'Insert failed: ' . $this->mysqli->error );

      print( "Mean: " . $mean . ", Standard Deviation: " . $std_dev . " Min: " . $min_temp . " Max: " . $max_temp . PHP_EOL );
    }
  }

  $serial = new Serial();
  $db = new Temperature_Database();

  if( $serial->open( "com3:" ) )
  {
    for( ;; )
    {
      $line = $serial->readline();
      $pieces = explode( ",", $line );
      $mean = $pieces[0];
      $std_dev = $pieces[1];
      $min_temp = $pieces[2];
      $max_temp = $pieces[3];
      $db->save_temp();
    }
    $serial->close();
  } 
  else
    print( "Can't open com3:" );
?>