<?php
require_once './Connection_getlark.php';

$json = file_get_contents('php://input');
// Decode the JSON string into an associative array
$dataArray = json_decode($json, true);

$ts_chec=0;
// Check if decoding was successful
if ($dataArray !== null) {
    // Loop through each item in the decoded array
    foreach ($dataArray as $entry) {
        // Accessing top-level fields
        // echo "Device: " . $entry['device'] . "<br>";
        // echo "Timestamp: " . $entry['ts'] . "<br>";
        // echo "RSSI: " . $entry['rssi'] . "<br>";
        // echo "JSON Count: " . $entry['json_count1'] . "<br>";
        // echo "Status: " . $entry['status'] . "<br>";

        // // Accessing nested "data" array
        // echo "Slave ID: " . $entry['data']['slave_id'] . "<br>";
        // echo "Function Code: " . $entry['data']['function_code'] . "<br>";
        // echo "D6: " . $entry['data']['D6'] . "<br>";
        // echo "D8: " . $entry['data']['D8'] . "<br>";
        // echo "D10: " . $entry['data']['D10'] . "<br>";
        // echo "D12: " . $entry['data']['D12'] . "<br>";
        // echo "D14: " . $entry['data']['D14'] . "<br>";
        // echo "D16: " . $entry['data']['D16'] . "<br>";
        // echo "D18: " . $entry['data']['D18'] . "<br>";
        // echo "<br>";
        
        $device_mac = $entry['device'];
        
        $water_level = $entry['data']['D769'];
        // $water_level1 = $entry['data']['D771'];
        
        $water_level = 4.19 - $entry['data']['D775'];
        
        $water_flow = $entry['data']['D773'];
        $accumulative_water = $entry['data']['D775'];
        $cumulative_traffic = $entry['data']['D777'];
        // $analog_output = $entry['data']['D9'];
        // $temperature = $entry['data']['D10'];
        
        
        
        date_default_timezone_set('Asia/Kolkata');
        // Get the current date and time
        $currentDateTime = date('Y-m-d H:i:s');
        $created_date_save = $currentDateTime;
        
        $timestamp =$entry['ts']; // Given UTC timestamp
        
        // if($ts_chec!=$timestamp){

            // Create a DateTime object in UTC
            $datetime = new DateTime("@$timestamp", new DateTimeZone("UTC"));
            
            // Convert to IST (Indian Standard Time)
            $datetime->setTimezone(new DateTimeZone("Asia/Kolkata"));
            
            // Display the date and time in IST
            $created_date=$datetime->format("Y-m-d H:i:s");
            
            
            if(trim($device_mac)=="08:d1:f9:a2:af:20"){
                
                $user_id = 3;
                $flow_meter_id = 5;
                $water_level=$water_level; // For Vyara Canal Measuremwnt
                $query1 = " insert into tbl_flow_meter_data (user_id,flow_meter_id,water_level,water_flow,accumulative_water,cumulative_traffic,analog_output,temperature,device_mac,created_date)"
                . " values ('$user_id','$flow_meter_id','$water_level','$water_flow','$accumulative_water','$cumulative_traffic','$analog_output','$temperature','$device_mac','$created_date') ";
         
                
            //             $query1 = " insert into air_quality (wind_speed,sulfur_dioxide,nitrogen_dioxide,carbon_dioxide,PM2_5,PM10,temperature,humidity,atmospheric_pressure,rainfall,mac_id,recorded_at,created_date)"
            // . " values ('$wind_speed','$so2','$no2','$co2','$PM2_5','$PM10','$temperature','$humidity','$atm','$rain','$device_mac','$created_date','$created_date_save') ";
                try {
                    if ($con) {
                
                        if (mysqli_query($con, $query1)) {
                            echo 'Data Store';
                            $ts_chec=$timestamp;
                        } else {
                            echo "Error:01";
                        }
                    } else {
                        echo "Error:02";
                    }
                } catch (Exception $ex) {
                    echo "Error:03";
                }
                
            }else{
                echo "Error:Mac Not Match";
            }
        // }else{
        //     echo "Error:Same Date Time";
        // }
    }
} else {
    echo "Failed to decode JSON data.";
}
?>