<?php
// Connecting, selecting database

$dbconn = pg_connect("host=localhost dbname=nws user=nws password=nws")
    or die('Could not connect: ' . pg_last_error());


//select camid,url,description from cams where cwa = 'RLX' and method = 'rtsp' and active = true AND (EXTRACT(EPOCH FROM (current_timestamp - lastsuccess ))/60) < (interval + 20) and lat > %s and lat < %s and lon > %s and lon < %s order by lastsuccess desc limit 25


//no gets, curent point outage info
//error_reporting(-1); // reports all errors
//ini_set("display_errors", "1"); // shows all errors
//ini_set("log_errors", 1);
//ini_set("error_log", "php-error.log");


//county/state max

if (isset($_GET['cams'])) {
if($_GET['lat1']) {
$lat1 = pg_escape_string($_GET['lat1']);
if($_GET['lon1']) {
$lon1 = pg_escape_string($_GET['lon1']);
if($_GET['lat2']) {
$lat2 = pg_escape_string($_GET['lat2']);
if($_GET['lon2']) {
$lon2 = pg_escape_string($_GET['lon2']);
if($_GET['elevbottom']) {
$elevbottom = pg_escape_string($_GET['elevbottom']);
if($_GET['elevtop']) {
$elevtop = pg_escape_string($_GET['elevtop']);


$result = pg_query_params($dbconn,
"select camid,url,description from cams where method = 'rtsp' and active = true and cwa = 'RLX' and elevation > $5 and elevation < $6 and (EXTRACT(EPOCH FROM (current_timestamp - lastsuccess ))/60) < (interval + 20) and lat < $1 and lat > $2 and lon < $3 and lon > $4 order by elevation desc",
array($lat1,$lat2,$lon1,$lon2,$elevbottom,$elevtop)) or die('Query failed: ' . pg_last_error());



//$result = pg_query_params($dbconn,
//select distinct on (county,state) max(outage),county,state from (select distinct on (county,state,update) county,state,sum(outages) as outage, update as time, sum(served) as served from countyoutages where update > '2023-04-01' and update < '2023-04-02' and cwa = 'RLX' group by county,state,update) as potato group by county,state;
//"select camid,url,description from cams where method = 'rtsp' and active = true $1 '162.210.14.137' and (EXTRACT(EPOCH FROM (current_timestamp - lastsuccess ))/60) < (interval + 20) and lat < $2 and lat > $3 and lon < $4 and lon > $5",
//array($res,$lat1,$lat2,$lon1,$lon2)) or die('Query failed: ' . pg_last_error());
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    $array[] = $line;
}
echo json_encode($array);
pg_free_result($result);
pg_close($dbconn);

}}}}}}}

if (isset($_GET['camstatic'])) {

if($_GET['lat1']) {
$lat1 = pg_escape_string($_GET['lat1']);
if($_GET['lon1']) {
$lon1 = pg_escape_string($_GET['lon1']);
if($_GET['radius']) {
$radius = pg_escape_string($_GET['radius']);
$rad = $radius / 70;

$lat1 = floatval($lat1);
$lon1 = floatval($lon1);
$radius = floatval($rad);
$query = "select * from cams where method = 'rtsp' and active = true and cwa = 'RLX' and (EXTRACT(EPOCH FROM (current_timestamp - lastsuccess ))/60) < (interval + 20) and st_dwithin(geom, ST_SetSRID(ST_Point(" . strval($lon1) . ", " . strval($lat1) . "), 4326)," . strval($radius) . ") order by elevation desc";



$result = pg_query($dbconn,$query) or die('Query failed: ' . pg_last_error());

while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    $array[] = $line;
}
echo json_encode($array);
pg_free_result($result);
pg_close($dbconn);



}}}}



if (isset($_GET['camdb'])) {
$result = pg_query($dbconn, 
"SELECT COUNT(*) FROM camdb") or die('Query failed: ' . pg_last_error());
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    $array[] = $line;
}
echo json_encode($array);
pg_free_result($result);
pg_close($dbconn);
}






?>
