<?php

ini_set('display_errors', true);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

error_reporting(E_ALL);
error_reporting(E_ALL);
error_reporting(-1);
ini_set('error_reporting', E_ALL);
if ( !isset( $_GET["action"] ) ) {
	exit( "no data" );
}

$acc = $_GET["action"];


if ( $acc == "getData" ) {

	$sensor_id = $_GET["sensor_id"]
	;
	$from_t = $_GET["from"];
	$to_t = $_GET["to"];

	$conn = pg_pconnect("host=10.8.0.90 dbname=MorTracker user=postgres");

	//$beginOfDay = strtotime("today", $timestamp);
	//$endOfDay   = strtotime("tomorrow", $beginOfDay) - 1;
	$result = pg_query($conn, "SELECT dest_tracker_id, timestamp_from, timestamp_to  FROM public.\"Records\" where source_tracker_id = $sensor_id and timestamp_from > $from_t and timestamp_from < $to_t order by timestamp_from ASC" );

	$arr = pg_fetch_all($result);
	if ( empty($arr) ) {
		echo "{}";
	}else{
		echo json_encode( $arr );
		}
}else if ( $acc == "getCoin" ) {

	$sensor_id = $_GET["sensor_id"]
	;
	$from_t = $_GET["from"];
	$to_t = $_GET["to"];

	$conn = pg_pconnect("host=10.8.0.90 dbname=MagicCoin user=postgres");

	//$beginOfDay = strtotime("today", $timestamp);
	//$endOfDay   = strtotime("tomorrow", $beginOfDay) - 1;
	$result = pg_query($conn, "SELECT MAX( timestamp ), sensor_id  FROM public.\"Measurments\" GROUP BY sensor_id" );

	$arr = pg_fetch_all($result);

	echo json_encode( $arr );
}else if ( $acc == "getDevices" ) {

	$sensor_id = $_GET["sensor_id"]
	;
	$from_t = $_GET["from"];
	$to_t = $_GET["to"];

	$conn = pg_pconnect("host=10.8.0.90 dbname=MagicCoin user=postgres");

	//$beginOfDay = strtotime("today", $timestamp);
	//$endOfDay   = strtotime("tomorrow", $beginOfDay) - 1;
	$result = pg_query($conn, "SELECT MAX( timestamp ), bts_id  FROM public.\"Measurments\" GROUP BY bts_id" );

	$arr = pg_fetch_all($result);

	echo json_encode( $arr );
}





?>