<?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');
print($input);
// $data=$input;
$data = json_decode($input, true); // No `true` argument means it returns an object
// echo 'data '.$data;

echo 'd1 '.$data['data']['D1'];
// echo 'Data Recieve';
// print_r($data);

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

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

function modbus_registers_to_float($reg1, $reg2) {
    // Combine the two 16-bit registers into a single 32-bit integer
    $combined = ($reg1 << 16) | $reg2; // reg1 is the high word, reg2 is the low word

    // Convert the 32-bit integer to a 32-bit float
    // Pack the integer as a 32-bit big-endian value, then unpack it as a float
    $floatVal = unpack('G', pack('N', $combined))[1];
    return $floatVal;
}

// Modbus registers
// $reg1 = 15954;
$reg2 = 0x0000;

// Conversion
// $floatVal = modbus_registers_to_float($reg1, $reg2);

// $user_id = 1;
// $farm_id = 2;
// $sensor_id = 1;
// $flow_meter_id = 1;
$device_mac = $data['device'];
$water_level = modbus_registers_to_float($data['data']['D1'], $reg2);
$water_flow = modbus_registers_to_float($data['data']['D3'], $reg2);
$accumulative_water = modbus_registers_to_float($data['data']['D6'], $reg2);
$cumulative_traffic = modbus_registers_to_float($data['data']['D7'], $reg2);
$analog_output = modbus_registers_to_float($data['data']['D9'], $reg2);
$temperature = modbus_registers_to_float($data['data']['D10'], $reg2);

// $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!=""){
            
            if($device_mac=="e8:31:cd:2a:c2:50"){
                $user_id = 1;
                $flow_meter_id = 1;
                $water_level=$water_level+0.1925; // For Kadod 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') ";
            }else if($device_mac=="08:d1:f9:a0:ae:a0"){
                $user_id = 2;
                $flow_meter_id = 2;
                $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') ";
            }
            

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