<?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");

$registeraddress = $_POST['fregisteraddress'];
$mbusfunction =    $_POST['fmbusfunction'];
$swapmode = $_POST['fswapmode'];
$vartype = $_POST['fvartype'];
$varmultiplier = $_POST['fvarmultiplier'];
$vardef = $_POST['fvardef'];
$recordactiontype = $_POST['frecordactiontype'];
$recordno = $_POST['frecordno'];
$devicerecordno = $_POST['fdevicerecordno'];

$sql = <<<EOF
      SELECT kno,comm_type from tblmbusdevices where kno=$devicerecordno;
EOF;

$ret = $db->query($sql);
$row = $ret->fetchArray(SQLITE3_ASSOC);
$mbusdevice_comm_type = $row['comm_type'];

if ($mbusdevice_comm_type > 1) {
   $mbusfunction = ord($mbusfunction);
}

if ($recordactiontype == 0) {
   $sql = <<<EOF
     INSERT INTO tblmbusvars (dkno, reg_addr, mbus_function,swap_mode,reg_var_type,reg_multiplier,reg_def)
     VALUES ($devicerecordno, $registeraddress, $mbusfunction, $swapmode, $vartype,$varmultiplier,'$vardef');
EOF;
   $ret = $db->query($sql);
} else if ($recordactiontype == 1) {
   $sql = <<<EOF
      UPDATE tblmbusvars
      SET
      reg_addr = $registeraddress,
      mbus_function = $mbusfunction,
      swap_mode = $swapmode,
      reg_var_type = $vartype,
      reg_multiplier = $varmultiplier,
      reg_def = '$vardef'
      WHERE kno=$recordno;
EOF;
   $ret = $db->query($sql);
} else if ($recordactiontype == 2) {
   $sql = <<<EOF
     DELETE FROM tblmbusvars
     WHERE kno=$recordno;
EOF;
   $ret = $db->query($sql);
}

$db->close();
?>

