<?php
  $dbaudrates = array(0 => 300, 1 => 600,2 => 1200,3 => 2400,4 => 4800,5 => 9600,6 => 14400,7 => 19200,8 => 38400,9 => 57600,10 => 115200);
  $ddatabits = array(0 => 7, 1 => 8);
  $dparity = array(0 => "NONE", 1 => "EVEN",2 => "ODD");
  $dstopbits = array(0 => "1", 1 => "1.5",2 => "2");

   class MyDB extends SQLite3 {
      function __construct() {
         $this->open('/kirac/db/sungateway.db');
      }
   }

   $db = new MyDB();
   if(!$db) {
      echo $db->lastErrorMsg();
   }

   $sql =<<<EOF
      SELECT * from tblrsconfig;
EOF;

   $ret = $db->query($sql);

   $row = $ret->fetchArray(SQLITE3_ASSOC);

   if (!$row) {
     $sql =<<<EOF
     INSERT INTO tblrsconfig (baudrate, databits, parity,stopbits,timeout,scanrate,queryinterval)
     VALUES (0, 0, 0,0,500,50,240);
EOF;
     $ret = $db->query($sql);

     $sql =<<<EOF
        SELECT * from tblrsconfig;
EOF;

     $ret = $db->query($sql);

     $row = $ret->fetchArray(SQLITE3_ASSOC);
   }

   if($row ) {

     //baudrate
     echo '<div class="row"><div class="col-25"><label for="fbaudrate">Baudrate</label></div><div class="col-75"><select name="fbaudrate">';
     for ($i=0;$i<11;$i++) {
        echo '<option value="'. $i .'"';
        if ($row['baudrate']==$i) {
          echo 'selected>'. $dbaudrates[$i] .'</option>';
        }else {
          echo '>'. $dbaudrates[$i] .'</option>';
        }
     }
     echo '</select></div></div>';

     //databits
     echo '<div class="row"><div class="col-25"><label for="fdatabit">Data Bit</label></div><div class="col-75"><select name="fdatabit">';
     for ($i=0;$i<2;$i++) {
        echo '<option value="'. $i .'"';
        if ($row['databits']==$i) {
          echo 'selected>'. $ddatabits[$i] .'</option>';
        }else {
          echo '>'. $ddatabits[$i] .'</option>';
        }
     }
     echo '</select></div></div>';

     //parity
     echo '<div class="row"><div class="col-25"><label for="fparity">Parity</label></div><div class="col-75"><select name="fparity">';
     for ($i=0;$i<3;$i++) {
        echo '<option value="'. $i .'"';
        if ($row['parity']==$i) {
          echo 'selected>'. $dparity[$i] .'</option>';
        }else {
          echo '>'. $dparity[$i] .'</option>';
        }
     }
     echo '</select></div></div>';

     //stopbits
     echo '<div class="row"><div class="col-25"><label for="fstopbit">Stop Bit</label></div><div class="col-75"><select name="fstopbit">';
     for ($i=0;$i<3;$i++) {
        echo '<option value="'. $i .'"';
        if ($row['stopbits']==$i) {
          echo 'selected>'. $dstopbits[$i] .'</option>';
        }else {
          echo '>'. $dstopbits[$i] .'</option>';
        }
     }
     echo '</select></div></div>';

     //timeout
     echo '<div class="row"><div class="col-25"><label for="ftimeout">Register okuma cevap bekleme zaman aşımı (ms)</label></div><div class="col-75"><input type="text" name="ftimeout" value="'. $row['timeout'] .'"></div></div>';

     //scanrate
     echo '<div class="row"><div class="col-25"><label for="fscanrate">2 register okuma arası bekleme (ms)</label></div><div class="col-75"><input type="text" name="fscanrate" value="'. $row['scanrate'] .'"></div></div>';

     //query interval
     echo '<div class="row"><div class="col-25"><label for="fqueryinterval">Yeniden okuma periyodu (sn)</label></div><div class="col-75"><input type="text" name="fqueryinterval" value="'. $row['queryinterval'] .'"></div></div>';

   }
   $db->close();
?>
