Linux premium92.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64 Path : /home/bajcrwht/api.bajcodigital.com/update-sensor/ |
Current File : /home/bajcrwht/api.bajcodigital.com/update-sensor/index.php |
<?php $postData = $_REQUEST; // Convert the parameters to a JSON string $postDataJson = json_encode($postData, JSON_PRETTY_PRINT); // Define the file path and name /* $filePath = 'api_requests/'; $fileName = 'request_' . date('Y-m-d_H-i-s') . '.txt'; // Ensure the directory exists if (!is_dir($filePath)) { mkdir($filePath, 0777, true); } // Save the JSON string to a text file file_put_contents($filePath . $fileName, $postDataJson); header('Content-Type: application/json');*/ $servername = "localhost"; $username = "bajcrwht_bajcodigital"; $password = "se8CNal!k{tV"; $dbname = "bajcrwht_bajcodigital"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die(json_encode([ 'success' => false, 'message' => 'Database connection failed: ' . $conn->connect_error, 'data' => [] ])); } // Function to handle the API response function sendResponse($success, $message, $data = [], $statusCode = 200) { http_response_code($statusCode); echo json_encode([ 'success' => $success, 'message' => $message, 'data' => $data ]); exit(); } $requestMethod = $_SERVER["REQUEST_METHOD"]; $rex_c = isset($_REQUEST['REX-S']) ? $_REQUEST['REX-S'] : null; $pulse_time = isset($_REQUEST['pulse_time']) ? $_REQUEST['pulse_time'] : null; $ph = isset($_REQUEST['R_pH']) ? $_REQUEST['R_pH'] : null; $ph_temp = isset($_REQUEST['ph_temp']) ? $_REQUEST['ph_temp'] : null; $tds = isset($_REQUEST['R_TDS']) ? $_REQUEST['R_TDS'] : null; $tds_temp = isset($_REQUEST['R_TDS_Temp']) ? $_REQUEST['R_TDS_Temp'] : null; $turbidity = isset($_REQUEST['R_Turbidity']) ? $_REQUEST['R_Turbidity'] : null; // $turbidity_temp = isset($_REQUEST['turbidity_temp']) ? $_REQUEST['turbidity_temp'] : null; $chlorine = isset($_REQUEST['C_Chlorine']) ? $_REQUEST['C_Chlorine']/10 : null; // $chlorine_temp = isset($_REQUEST['chlorine_temp']) ? $_REQUEST['chlorine_temp'] : null; //remove $ph_clear = isset($_REQUEST['C_pH']) ? $_REQUEST['C_pH'] : null; $ph_tempclear = isset($_REQUEST['C_pH_Temp']) ? $_REQUEST['C_pH_Temp'] : null; $tds_clear = isset($_REQUEST['C_TDS']) ? $_REQUEST['C_TDS'] : null; $tds_tempclear = isset($_REQUEST['C_TDS_Temp']) ? $_REQUEST['C_TDS_Temp'] : null; $turbidity_clear = isset($_REQUEST['C_Turbidity']) ? $_REQUEST['C_Turbidity'] : null; // $turbidity_tempclear = isset($_REQUEST['turbidity_tempclear']) ? $_REQUEST['turbidity_tempclear'] : null; //remove $device_id = isset($_REQUEST['IMEI']) ? $_REQUEST['IMEI'] : null; // Validate required fields if (!$device_id) { sendResponse(false, 'Device code is required'); } $deviceQuery = "SELECT id FROM devices WHERE device_code = '$device_id' LIMIT 1"; $deviceResult = $conn->query($deviceQuery); if ($deviceResult->num_rows > 0) { $device = $deviceResult->fetch_assoc(); $deviceId = $device['id']; $timestamp = date('Y-m-d H:i:s'); // Insert into clearwater table $insertQuery = "INSERT INTO clearwaters (rex_c,pulse_time,ph,ph_temp,tds,tds_temp,turbidity,turbidity_temp,chlorine,chlorine_temp,ph_clear,ph_tempclear,tds_clear,tds_tempclear,turbidity_clear,turbidity_tempclear,device_id, created_at, updated_at) VALUES ('$rex_c','$pulse_time','$ph','$ph_temp','$tds','$tds_temp','$turbidity','$turbidity_temp','$chlorine','$chlorine_temp','$ph_clear','$ph_tempclear','$tds_clear','$tds_tempclear','$turbidity_clear','$turbidity_tempclear','$deviceId', '$timestamp', '$timestamp')"; if ($conn->query($insertQuery) === TRUE) { sendResponse(true, 'Clearwater created successfully', [ 'id' => $conn->insert_id ]); } else { sendResponse(false, 'Error: ' . $conn->error); } } else { sendResponse(false, 'Device not found'); } $conn->close(); ?>