<?php

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
 */

// require_once './connection_smart_farm.php';
require_once './Connection_getlark.php';

//if (!isset($_POST) == "") {

$input = file_get_contents('php://input');
$data = json_decode($input, true);
// echo 'Data Recieve';
print_r($data);

// Print out the structure for testing
//var_dump($data);

//echo $sensor_data->type;
//}

$user_id = 2;
$farm_id = 2;
$sensor_id = 5;
$flow_meter_id = 2;
$device_mac = $data['device'];
$water_level = $data['data']['D1'];
$water_flow = $data['data']['D2'];
$accumulative_water = $data['data']['D3'];
$cumulative_traffic = $data['data']['D4'];
$analog_output = $data['data']['D5'];
$temperature = $data['data']['D6'];

date_default_timezone_set('Asia/Kolkata');

// Get the current date and time
$currentDateTime = date('Y-m-d H:i:s');
$created_date = $currentDateTime;


if($device_mac!=""){
            
            $query1 = " insert into tbl_flow_meter_data (user_id,flow_meter_id,water_level,water_flow,accumulative_water,cumulative_traffic,analog_output,temperature,created_date)"
        . " values ('$user_id','$flow_meter_id','$water_level','$water_flow','$accumulative_water','$cumulative_traffic','$analog_output','$temperature','$created_date') ";

    try {
        if ($con) {
    
            if (mysqli_query($con, $query1)) {
                echo 'Data Store';
            } else {
                echo "Error:01";
            }
        } else {
            echo "Error:01";
        }
    } catch (Exception $ex) {
        echo "Error:01";
    }
}else{
    echo "Error:Blank";
}
?>