update
This commit is contained in:
@@ -4,10 +4,11 @@ header("Content-Type: application/json");
|
||||
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
|
||||
header("Pragma: no-cache");
|
||||
|
||||
$database_path = "/var/www/moduleair_pro_4g/sqlite/sensors.db";
|
||||
|
||||
$type=$_GET['type'];
|
||||
|
||||
if ($type == "get_npm_sqlite_data") {
|
||||
$database_path = "/var/www/moduleair_pro_4g/sqlite/sensors.db";
|
||||
//echo "Getting data from sqlite database";
|
||||
try {
|
||||
$db = new PDO("sqlite:$database_path");
|
||||
@@ -25,6 +26,193 @@ if ($type == "get_npm_sqlite_data") {
|
||||
}
|
||||
}
|
||||
|
||||
//GETING data from config_table (SQLite DB)
|
||||
if ($type == "get_config_sqlite") {
|
||||
try {
|
||||
$db = new PDO("sqlite:$database_path");
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// Get all main configuration entries
|
||||
$config_query = $db->query("SELECT key, value, type FROM config_table");
|
||||
$config_data = $config_query->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// Convert data types according to their 'type' field
|
||||
$result = [];
|
||||
foreach ($config_data as $item) {
|
||||
$key = $item['key'];
|
||||
$value = $item['value'];
|
||||
$type = $item['type'];
|
||||
|
||||
// Convert value based on its type
|
||||
switch ($type) {
|
||||
case 'bool':
|
||||
$parsed_value = ($value == '1' || $value == 'true') ? true : false;
|
||||
break;
|
||||
case 'int':
|
||||
$parsed_value = intval($value);
|
||||
break;
|
||||
case 'float':
|
||||
$parsed_value = floatval($value);
|
||||
break;
|
||||
default:
|
||||
$parsed_value = $value;
|
||||
}
|
||||
|
||||
$result[$key] = $parsed_value;
|
||||
}
|
||||
|
||||
// Return JSON response
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($result, JSON_PRETTY_PRINT);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
// Return error as JSON
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
//GETING data from config_scrips_table (SQLite DB)
|
||||
if ($type == "get_config_scripts_sqlite") {
|
||||
|
||||
try {
|
||||
$db = new PDO("sqlite:$database_path");
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// Get all main configuration entries
|
||||
$config_query = $db->query("SELECT * FROM config_scripts_table");
|
||||
$config_data = $config_query->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// Convert data types according to their 'type' field
|
||||
$result = [];
|
||||
foreach ($config_data as $item) {
|
||||
$script_path = $item['script_path'];
|
||||
$enabled = $item['enabled'];
|
||||
|
||||
// Convert the enabled field to a proper boolean
|
||||
$result[$script_path] = ($enabled == 1);
|
||||
}
|
||||
|
||||
// Return JSON response
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
// Return error as JSON
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//UPDATING the config_table from SQLite DB
|
||||
if ($type == "update_config_sqlite") {
|
||||
$param = $_GET['param'] ?? null;
|
||||
$value = $_GET['value'] ?? null;
|
||||
|
||||
if ($param === null || $value === null) {
|
||||
echo json_encode(["error" => "Missing parameter or value"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = new PDO("sqlite:$database_path");
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// First, check if parameter exists and get its type
|
||||
$checkStmt = $db->prepare("SELECT type FROM config_table WHERE key = :param");
|
||||
$checkStmt->bindParam(':param', $param);
|
||||
$checkStmt->execute();
|
||||
$result = $checkStmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($result) {
|
||||
// Parameter exists, determine type and update
|
||||
$type = $result['type'];
|
||||
|
||||
// Convert value according to type if needed
|
||||
$convertedValue = $value;
|
||||
if ($type == "bool") {
|
||||
// Convert various boolean representations to 0/1
|
||||
$convertedValue = (filter_var($value, FILTER_VALIDATE_BOOLEAN)) ? "1" : "0";
|
||||
} elseif ($type == "int") {
|
||||
$convertedValue = (string)intval($value);
|
||||
} elseif ($type == "float") {
|
||||
$convertedValue = (string)floatval($value);
|
||||
}
|
||||
|
||||
// Update the value
|
||||
$updateStmt = $db->prepare("UPDATE config_table SET value = :value WHERE key = :param");
|
||||
$updateStmt->bindParam(':value', $convertedValue);
|
||||
$updateStmt->bindParam(':param', $param);
|
||||
$updateStmt->execute();
|
||||
|
||||
echo json_encode([
|
||||
"success" => true,
|
||||
"message" => "Configuration updated successfully",
|
||||
"param" => $param,
|
||||
"value" => $convertedValue,
|
||||
"type" => $type
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
"error" => "Parameter not found in configuration",
|
||||
"param" => $param
|
||||
]);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(["error" => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
//UPDATING the config_scripts table from SQLite DB
|
||||
if ($type == "update_config_scripts_sqlite") {
|
||||
$script_path = $_GET['param'] ?? null;
|
||||
$enabled = $_GET['value'] ?? null;
|
||||
|
||||
if ($script_path === null || $enabled === null) {
|
||||
echo json_encode(["error" => "Missing parameter or value"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = new PDO("sqlite:$database_path");
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// First, check if parameter exists and get its type
|
||||
$checkStmt = $db->prepare("SELECT enabled FROM config_scripts_table WHERE script_path = :script_path");
|
||||
$checkStmt->bindParam(':script_path', $script_path);
|
||||
$checkStmt->execute();
|
||||
$result = $checkStmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($result) {
|
||||
// Convert enabled value to 0 or 1
|
||||
$enabledValue = (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) ? 1 : 0;
|
||||
|
||||
// Update the enabled status
|
||||
$updateStmt = $db->prepare("UPDATE config_scripts_table SET enabled = :enabled WHERE script_path = :script_path");
|
||||
$updateStmt->bindParam(':enabled', $enabledValue, PDO::PARAM_INT);
|
||||
$updateStmt->bindParam(':script_path', $script_path);
|
||||
$updateStmt->execute();
|
||||
|
||||
echo json_encode([
|
||||
"success" => true,
|
||||
"message" => "Script configuration updated successfully",
|
||||
"script_path" => $script_path,
|
||||
"enabled" => (bool)$enabledValue
|
||||
], JSON_UNESCAPED_SLASHES); // Prevent escaping forward slashes
|
||||
} else {
|
||||
echo json_encode([
|
||||
"error" => "Script path not found in configuration",
|
||||
"script_path" => $script_path
|
||||
], JSON_UNESCAPED_SLASHES); // Prevent escaping forward slashes
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(["error" => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//old JSON way
|
||||
if ($type == "update_config") {
|
||||
echo "updating....";
|
||||
$param=$_GET['param'];
|
||||
|
||||
Reference in New Issue
Block a user