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

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

   $db->exec("PRAGMA synchronous = OFF");

   $stationid = $_POST['fstationid'];
   $commtype = $_POST['fcommtype'];
   $swapmode = $_POST['fswapmode'];
   $deviceip = $_POST['fdeviceip'];
   $tcpport = $_POST['ftcpport'];
   $deviceid = $_POST['fdeviceid'];
   $devicedef = $_POST['fdevicedef'];
   $active = 0;
   if(isset($_POST['factive'])) {
   	$active = 1;
   }
   $recordactiontype = $_POST['frecordactiontype'];
   $recordno = $_POST['frecordno'];

   if ($tcpport<=0) {
      $tcpport = 0;
   }

   if ($recordactiontype==0) {
     $sql =<<<EOF
     INSERT INTO tblmbusdevices (station_id, device_id, device_def,comm_type,swap_mode,device_ip,tcp_port,active)
     VALUES ('$stationid', $deviceid, '$devicedef',$commtype,$swapmode,'$deviceip',$tcpport,$active);
EOF;
     $ret = $db->query($sql);
   }else if ($recordactiontype==1) {
     $sql =<<<EOF
     UPDATE tblmbusdevices
     SET
     station_id = '$stationid',
     device_id = $deviceid,
     device_def = '$devicedef',
     comm_type = $commtype,
     swap_mode = $swapmode,
     device_ip = '$deviceip',
     tcp_port = $tcpport,
     active = $active 
     WHERE kno=$recordno;
EOF;
     $ret = $db->query($sql);
   }else if ($recordactiontype==2) {
     $sql =<<<EOF
     DELETE FROM tblmbusdevices
     WHERE kno=$recordno;
EOF;
     $ret = $db->query($sql);

     $sql =<<<EOF
     DELETE FROM tblmbusvars
     WHERE dkno=$recordno;
EOF;
     $ret = $db->query($sql);
   }

   $db->close();
?>
