<?php


function show_status($done, $total, $size=30) {

    static $start_time;

    // if we go over our bound, just ignore it
    if($done > $total) return;

    if(empty($start_time)) $start_time=time();
    $now = time();

    $perc=(double)($done/$total);

    $bar=floor($perc*$size);

    $status_bar="\r[";
    $status_bar.=str_repeat("=", $bar);
    if($bar<$size){
        $status_bar.=">";
        $status_bar.=str_repeat(" ", $size-$bar);
    } else {
        $status_bar.="=";
    }

    $disp=number_format($perc*100, 0);

    $status_bar.="] $disp%  $done/$total";

    $rate = ($now-$start_time)/$done;
    $left = $total - $done;
    $eta = round($rate * $left, 2);

    $elapsed = $now - $start_time;

    $status_bar.= " remaining: ".number_format($eta)." sec.  elapsed: ".number_format($elapsed)." sec.";

    echo "$status_bar  ";

    flush();

    // when done, send a newline
    if($done == $total) {
        echo "\n";
    }

}

// include channel list
include "SDS_status_channels.php";

$username = 'katrin';
$password = 'neutrino';
$context = stream_context_create(array(
		'http' => array(
			'header'  => "Authorization: Basic " . base64_encode("$username:$password"), "Connection: close\r\n"
		)
	));

$counter = 0;
$calltime = time();

        $errorcount = 0;
        $errormessage = '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>';

	$starttime = time();
	
        echo "Polling ADEI data.".PHP_EOL;

	for ($id = 0; $id < 1; $id++)
	{
		$adeiChannels = "";
		for ($id2 = 0; $id2 < sizeof($adeiChannel[$id]); $id2++)
		{
			$adeiChannels = $adeiChannels . $adeiChannel[$id][$id2] . ",";
		}
		$adeiChannels = substr($adeiChannels, 0, strlen($adeiChannels) - 1);


		// new adei-katrin.kaas.kit.edu added
                $fileip = file_get_contents("http://adei-katrin.kaas.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&rt=full&window=3600,-1&format=csv", false, $context);

		echo $fileip.PHP_EOL;
		
                //show_status($id+1,sizeof($adeiChannel));

                if (substr($fileip,0,5) == "ERROR")
                {$errorcount++; $errormessage = $errormessage . "Error loading ADEI channel " . $id . "<br>";}

		for ($id2 = sizeof($adeiChannel[$id]) - 1 ; $id2 > -1; $id2--)
		{
			$pos = strrpos($fileip, ",");
			$adeiValue[$id][$id2] = floatval(substr($fileip, $pos + 2));
			$fileip = substr($fileip, 0, $pos);
			//echo $id . " " . $id2 . " " . $adeiValue[$id][$id2] . PHP_EOL;
		}

	}


?>
