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

}



	// ########## SLOW CONTROL CHANNELS ##########

	$adeiChannel = array(
		array(
			"aircoils__aircoils_rep__0__2",   //EMCS currents
			"aircoils__aircoils_rep__0__3",
			"aircoils__aircoils_rep__0__4",
			"aircoils__aircoils_rep__0__5",
			"aircoils__aircoils_rep__0__6",
			"aircoils__aircoils_rep__0__7",
			"aircoils__aircoils_rep__0__8",
			"aircoils__aircoils_rep__0__9",
			"aircoils__aircoils_rep__0__10",
			"aircoils__aircoils_rep__0__11",
			"aircoils__aircoils_rep__0__12",
			"aircoils__aircoils_rep__0__13",
			"aircoils__aircoils_rep__0__14",
			"aircoils__aircoils_rep__0__15",
			"aircoils__aircoils_rep__0__16",
			"aircoils__aircoils_rep__0__17"
		),
		array(
			"aircoils__aircoils_rep__0__34",   //EMCS Status
			"aircoils__aircoils_rep__0__35",
			"aircoils__aircoils_rep__0__36",
			"aircoils__aircoils_rep__0__37",
			"aircoils__aircoils_rep__0__38",
			"aircoils__aircoils_rep__0__39",
			"aircoils__aircoils_rep__0__40",
			"aircoils__aircoils_rep__0__41",
			"aircoils__aircoils_rep__0__42",
			"aircoils__aircoils_rep__0__43",
			"aircoils__aircoils_rep__0__44",
			"aircoils__aircoils_rep__0__45",
			"aircoils__aircoils_rep__0__46",
			"aircoils__aircoils_rep__0__47",
			"aircoils__aircoils_rep__0__48",
			"aircoils__aircoils_rep__0__49"
		),
		array(
			"aircoils__aircoils_rep__0__86",   // LEM Status
			"aircoils__aircoils_rep__0__87",
			"aircoils__aircoils_rep__0__88",
			"aircoils__aircoils_rep__0__89",
			"aircoils__aircoils_rep__0__90",
			"aircoils__aircoils_rep__0__91",
			"aircoils__aircoils_rep__0__92",
			"aircoils__aircoils_rep__0__93",
			"aircoils__aircoils_rep__0__94",
			"aircoils__aircoils_rep__0__95",
			"aircoils__aircoils_rep__0__96",
			"aircoils__aircoils_rep__0__97",
			"aircoils__aircoils_rep__0__98",
			"aircoils__aircoils_rep__0__99",
			"aircoils__aircoils_rep__0__100",
			"aircoils__aircoils_rep__0__101",
                        "aircoils__aircoils_rep__0__102"   // HV dipole trigger status
		),
		array(
			"aircoils__aircoils_rep__0__82",  //PS1 + PS2 current 
			"aircoils__aircoils_rep__0__83",
			"aircoils__aircoils_rep__0__84",  //PS1 + PS2 Status
			"aircoils__aircoils_rep__0__85"
		),
		array(
			"fpd__katrin_rep__0__153",
			"fpd__katrin_rep__0__158"
		),
	        // Baffle vacuum  (index 5, former egun part)
		array(
                        "csms__ControlSystem_MS__432VAK_Kryoleitung__9"
		),
		array(
			"fpd__katrin_rep__0__43",
			"fpd__katrin_rep__0__66",
			"fpd__katrin_rep__0__80",
			"fpd__katrin_rep__0__99",
			"fpd__katrin_rep__0__112",
			"fpd__katrin_rep__0__113",
			"fpd__katrin_rep__0__116",
			"fpd__katrin_rep__0__117",
			"fpd__katrin_rep__0__118",
			"fpd__katrin_rep__0__60",
			"fpd__katrin_rep__0__62",
			"fpd__katrin_rep__0__114",
			"fpd__katrin_rep__0__45"
		),
		array(
			"csms__ControlSystem_MS__432VAK_PumpPort2__33",
			"csms__ControlSystem_MS__432VAK_PumpPort2__6",
			"csms__ControlSystem_MS__432VAK_PumpPort2__12",
			"csms__ControlSystem_MS__432VAK_PumpPort2__18",
			"csms__ControlSystem_MS__432VAK_PumpPort2__57",
			"csms__ControlSystem_MS__432VAK_PumpPort2__60",
			"csms__ControlSystem_MS__432VAK_PumpPort2__63",
			"csms__ControlSystem_MS__432VAK_PumpPort2__36"
		),
		array(
			"csms__ControlSystem_MS__432VAK_PumpPort3__24",
			"csms__ControlSystem_MS__432VAK_PumpPort3__6",
			"csms__ControlSystem_MS__432VAK_PumpPort3__12",
			"csms__ControlSystem_MS__432VAK_PumpPort3__18",
			"csms__ControlSystem_MS__432VAK_PumpPort3__42",
			"csms__ControlSystem_MS__432VAK_PumpPort3__45",
			"csms__ControlSystem_MS__432VAK_PumpPort3__48"
		),
		array(
			"csms__ControlSystem_MS__432VAK_Tank__6",  // RPM 106
			"csms__ControlSystem_MS__432VAK_Tank__9",  // VAI 105
			"csms__ControlSystem_MS__432VAK_Tank__0",  // RPI 0096
			"csms__ControlSystem_MS__432VAK_Tank__15",  
			"csms__ControlSystem_MS__432VAK_Tank__27",
			"csms__ControlSystem_MS__432VAK_Tank__30",
			"csms__ControlSystem_MS__432VAK_Tank__33",
			"csms__ControlSystem_MS__432VAK_Tank__30",
			"csms__ControlSystem_MS__432VAK_Tank__30",
			"csms__ControlSystem_MS__432VAK_Tank__30",
			"csms__ControlSystem_MS__432VAK_Tank__30"   
//                      "csms__ControlSystem_MS__432VAK_Tank__15",  
//			"csms__ControlSystem_MS__432VAK_Tank__27",
//			"csms__ControlSystem_MS__432VAK_Tank__30",
//			"csms__ControlSystem_MS__432VAK_Tank__33",
//			"csms__ControlSystem_MS__432VAK_Tank__39",
//			"csms__ControlSystem_MS__432VAK_Tank__45",
//			"csms__ControlSystem_MS__432VAK_Tank__48",
//			"csms__ControlSystem_MS__432VAK_Tank__36"
		),
                array(                                  //HV subsystem 
			"hv__katrin-hv_rep__0__39",    // MS vessel IST
			"hv__katrin-hv_rep__0__38",    // MS vessel SOLL
			"hv__katrin-hv_rep__0__87",    // MS IE Common electrode IST
			"hv__katrin-hv_rep__0__86",    // MS IE Common electrode SOLL
			"hv__katrin-hv_rep__0__71",    // MS IE dipole west IST
			"hv__katrin-hv_rep__0__70",    // MS IE dipole west SOLL
			"hv__katrin-hv_rep__0__79",    // MS IE dipole east IST
			"hv__katrin-hv_rep__0__78",    // MS IE dipole east SOLL
			"hv__katrin-hv_rep__0__63",
			"hv__katrin-hv_rep__0__62",
			"hv__katrin-hv_rep__0__55",
			"hv__katrin-hv_rep__0__54",
			"hv__katrin-hv_rep__0__132",   // dipole relay status 
			"hv__katrin-hv_rep__0__131",   // dipole relay status
                        "hv__katrin-hv_rep__0__95",    // PS vessel IST
			"hv__katrin-hv_rep__0__94",    // PS vessel SOLL
                        "hv__katrin-hv_rep__0__103",    // PS dipole west IST
			"hv__katrin-hv_rep__0__102",    // PS dipole west SOLL
                        "hv__katrin-hv_rep__0__111",    // PS dipole east IST
			"hv__katrin-hv_rep__0__110",    // PS dipole east SOLL
                        "hv__katrin-hv_rep__0__47",    // MS IE Precision Supply IST
			"hv__katrin-hv_rep__0__46",    // MS IE Precision Supply SOLL
		),
		array(
			"hv__katrin-hv_rep__1__3",
			"hv__katrin-hv_rep__1__10",
			"hv__katrin-hv_rep__1__17",
			"hv__katrin-hv_rep__1__24",
			"hv__katrin-hv_rep__1__31",
			"hv__katrin-hv_rep__1__38",
			"hv__katrin-hv_rep__1__45",
			"hv__katrin-hv_rep__1__52",
			"hv__katrin-hv_rep__1__59",
			"hv__katrin-hv_rep__1__66",
			"hv__katrin-hv_rep__1__73",
			"hv__katrin-hv_rep__1__80",
			"hv__katrin-hv_rep__1__87",
			"hv__katrin-hv_rep__1__94",
			"hv__katrin-hv_rep__1__101",
			"hv__katrin-hv_rep__1__108",
			"hv__katrin-hv_rep__1__115",
			"hv__katrin-hv_rep__1__122",
			"hv__katrin-hv_rep__1__129",
			"hv__katrin-hv_rep__1__136",
			"hv__katrin-hv_rep__1__143",
			"hv__katrin-hv_rep__1__150",
			"hv__katrin-hv_rep__1__157",
			"hv__katrin-hv_rep__1__164"
		),
		array(
			"hv__katrin-hv_rep__1__2",
			"hv__katrin-hv_rep__1__9",
			"hv__katrin-hv_rep__1__16",
			"hv__katrin-hv_rep__1__23",
			"hv__katrin-hv_rep__1__30",
			"hv__katrin-hv_rep__1__37",
			"hv__katrin-hv_rep__1__44",
			"hv__katrin-hv_rep__1__51",
			"hv__katrin-hv_rep__1__58",
			"hv__katrin-hv_rep__1__65",
			"hv__katrin-hv_rep__1__72",
			"hv__katrin-hv_rep__1__79",
			"hv__katrin-hv_rep__1__86",
			"hv__katrin-hv_rep__1__93",
			"hv__katrin-hv_rep__1__100",
			"hv__katrin-hv_rep__1__107",
			"hv__katrin-hv_rep__1__114",
			"hv__katrin-hv_rep__1__121",
			"hv__katrin-hv_rep__1__128",
			"hv__katrin-hv_rep__1__135",
			"hv__katrin-hv_rep__1__142",
			"hv__katrin-hv_rep__1__149",
			"hv__katrin-hv_rep__1__156",
			"hv__katrin-hv_rep__1__163"
		),
		array(
			"hv__katrin-hv_rep__2__3",
			"hv__katrin-hv_rep__2__10",
			"hv__katrin-hv_rep__2__17",
			"hv__katrin-hv_rep__2__24",
			"hv__katrin-hv_rep__2__31",
			"hv__katrin-hv_rep__2__38",
			"hv__katrin-hv_rep__2__45",
			"hv__katrin-hv_rep__2__52",
			"hv__katrin-hv_rep__2__59",
			"hv__katrin-hv_rep__2__66",
			"hv__katrin-hv_rep__2__73",
			"hv__katrin-hv_rep__2__80",
			"hv__katrin-hv_rep__2__87",
			"hv__katrin-hv_rep__2__94",
			"hv__katrin-hv_rep__2__101",
			"hv__katrin-hv_rep__2__108",
			"hv__katrin-hv_rep__2__115",
			"hv__katrin-hv_rep__2__122",
			"hv__katrin-hv_rep__2__129",
			"hv__katrin-hv_rep__2__136",
			"hv__katrin-hv_rep__2__143",
			"hv__katrin-hv_rep__2__150",
			"hv__katrin-hv_rep__2__157",
			"hv__katrin-hv_rep__2__164"
		),
		array(
			"hv__katrin-hv_rep__2__2",
			"hv__katrin-hv_rep__2__9",
			"hv__katrin-hv_rep__2__16",
			"hv__katrin-hv_rep__2__23",
			"hv__katrin-hv_rep__2__30",
			"hv__katrin-hv_rep__2__37",
			"hv__katrin-hv_rep__2__44",
			"hv__katrin-hv_rep__2__51",
			"hv__katrin-hv_rep__2__58",
			"hv__katrin-hv_rep__2__65",
			"hv__katrin-hv_rep__2__72",
			"hv__katrin-hv_rep__2__79",
			"hv__katrin-hv_rep__2__86",
			"hv__katrin-hv_rep__2__93",
			"hv__katrin-hv_rep__2__100",
			"hv__katrin-hv_rep__2__107",
			"hv__katrin-hv_rep__2__114",
			"hv__katrin-hv_rep__2__121",
			"hv__katrin-hv_rep__2__128",
			"hv__katrin-hv_rep__2__135",
			"hv__katrin-hv_rep__2__142",
			"hv__katrin-hv_rep__2__149",
			"hv__katrin-hv_rep__2__156",
			"hv__katrin-hv_rep__2__163"
		),
		array(
			"csms__ControlSystem_MS__430KRYO_Baffel__147",
			"csms__ControlSystem_MS__430KRYO_Baffel__159",
			"csms__ControlSystem_MS__430KRYO_Baffel__171",
			"csms__ControlSystem_MS__430KRYO_Baffel__183",
			"csms__ControlSystem_MS__430KRYO_Baffel__189",
			"csms__ControlSystem_MS__430KRYO_Baffel__195",
			"csms__ControlSystem_MS__430KRYO_Baffel__120",
			"csms__ControlSystem_MS__430KRYO_Baffel__123"
		),
		array(
			"precmm0__PrecMagnet__NorthRing__0",
			"precmm0__PrecMagnet__NorthRing__1",
			"precmm0__PrecMagnet__NorthRing__2",
			"precmm0__PrecMagnet__NorthRing__7",
			"precmm0__PrecMagnet__NorthRing__8",
			"precmm0__PrecMagnet__NorthRing__9",
			"precmm0__PrecMagnet__NorthRing__14",
			"precmm0__PrecMagnet__NorthRing__15",
			"precmm0__PrecMagnet__NorthRing__16",
			"precmm0__PrecMagnet__NorthRing__21",
			"precmm0__PrecMagnet__NorthRing__22",
			"precmm0__PrecMagnet__NorthRing__23"
		),
		array(
			"precmm0__PrecMagnet__MidRing__0",
			"precmm0__PrecMagnet__MidRing__1",
			"precmm0__PrecMagnet__MidRing__2",
			"precmm0__PrecMagnet__MidRing__7",
			"precmm0__PrecMagnet__MidRing__8",
			"precmm0__PrecMagnet__MidRing__9",
			"precmm0__PrecMagnet__MidRing__14",
			"precmm0__PrecMagnet__MidRing__15",
			"precmm0__PrecMagnet__MidRing__16",
			"precmm0__PrecMagnet__MidRing__21",
			"precmm0__PrecMagnet__MidRing__22",
			"precmm0__PrecMagnet__MidRing__23",
			"precmm0__PrecMagnet__MidRing__28",
			"precmm0__PrecMagnet__MidRing__29",
			"precmm0__PrecMagnet__MidRing__30",
			"precmm0__PrecMagnet__MidRing__35",
			"precmm0__PrecMagnet__MidRing__36",
			"precmm0__PrecMagnet__MidRing__37"
		),
		array(
			"precmm0__PrecMagnet__SouthRing__0",
			"precmm0__PrecMagnet__SouthRing__1",
			"precmm0__PrecMagnet__SouthRing__2",
			"precmm0__PrecMagnet__SouthRing__7",
			"precmm0__PrecMagnet__SouthRing__8",
			"precmm0__PrecMagnet__SouthRing__9",
			"precmm0__PrecMagnet__SouthRing__14",
			"precmm0__PrecMagnet__SouthRing__15",
			"precmm0__PrecMagnet__SouthRing__16",
			"precmm0__PrecMagnet__SouthRing__21",
			"precmm0__PrecMagnet__SouthRing__22",
			"precmm0__PrecMagnet__SouthRing__23"
		),
		array(
			"fpd__katrin_rep__0__198",
			"fpd__katrin_rep__0__199",
			"fpd__katrin_rep__0__200"
		),
		array(
			"csms__ControlSystem_MS__435HZ_Temperatur__27",
			"csms__ControlSystem_MS__435HZ_Temperatur__33",
			"csms__ControlSystem_MS__435HZ_Temperatur__42",
			"csms__ControlSystem_MS__435HZ_Temperatur__45"
		),
		array(
			"aircoils__aircoils_rep__1__2",
			"aircoils__aircoils_rep__1__3",
			"aircoils__aircoils_rep__1__4",
			"aircoils__aircoils_rep__1__5",
			"aircoils__aircoils_rep__1__6",
			"aircoils__aircoils_rep__1__7",
			"aircoils__aircoils_rep__1__8",
			"aircoils__aircoils_rep__1__9",
			"aircoils__aircoils_rep__1__10",
			"aircoils__aircoils_rep__1__11",
			"aircoils__aircoils_rep__1__12",
			"aircoils__aircoils_rep__1__13",
			"aircoils__aircoils_rep__1__14",
			"aircoils__aircoils_rep__1__15",
			"aircoils__aircoils_rep__1__16",
			"aircoils__aircoils_rep__1__17"
		),
		array(
			"aircoils__aircoils_rep__1__18",
			"aircoils__aircoils_rep__1__19",
			"aircoils__aircoils_rep__1__20",
			"aircoils__aircoils_rep__1__21",
			"aircoils__aircoils_rep__1__22",
			"aircoils__aircoils_rep__1__23",
			"aircoils__aircoils_rep__1__24",
			"aircoils__aircoils_rep__1__25",
			"aircoils__aircoils_rep__1__26",
			"aircoils__aircoils_rep__1__27",
			"aircoils__aircoils_rep__1__28",
			"aircoils__aircoils_rep__1__29",
			"aircoils__aircoils_rep__1__30",
			"aircoils__aircoils_rep__1__31",
			"aircoils__aircoils_rep__1__32",
			"aircoils__aircoils_rep__1__33"
		),
		array(
			"aircoils__aircoils_rep__1__57",
			"aircoils__aircoils_rep__1__58",
			"aircoils__aircoils_rep__1__43",
			"aircoils__aircoils_rep__1__44",
			"aircoils__aircoils_rep__1__45",
			"aircoils__aircoils_rep__1__46",
			"aircoils__aircoils_rep__1__47",
			"aircoils__aircoils_rep__1__48",
			"aircoils__aircoils_rep__1__49",
			"aircoils__aircoils_rep__1__50",
			"aircoils__aircoils_rep__1__51",
			"aircoils__aircoils_rep__1__52",
			"aircoils__aircoils_rep__1__53",
			"aircoils__aircoils_rep__1__54",
			"aircoils__aircoils_rep__1__55",
			"aircoils__aircoils_rep__1__56",
			"aircoils__aircoils_rep__1__38"
		),
                // CPS current (index 24)
                array(
                        "cscps__ControlSystem_CPS__320_MAG_Stromzufuhr__0"
                ) ,
                // PP2 TMP temps (index 25)
		array(
			"csms__ControlSystem_MS__435HZ_PumpPort2__78",
			"csms__ControlSystem_MS__435HZ_PumpPort2__81",
			"csms__ControlSystem_MS__435HZ_PumpPort2__84"
                ),
                // PP3 TMP temps (index 26)
		array(
			"csms__ControlSystem_MS__435HZ_PumpPort3__60",
			"csms__ControlSystem_MS__435HZ_PumpPort3__63",
			"csms__ControlSystem_MS__435HZ_PumpPort3__66"
                ),
                // PS vacuum system (index 27)
                array(
                        "csps__ControlSystem_PS__412VAK_PumpPort__27",  // CC1232
			"csps__ControlSystem_PS__412VAK_PumpPort__6",   // TMP 1122 speed
			"csps__ControlSystem_PS__412VAK_PumpPort__12",  // TMP 1123 speed
			"csps__ControlSystem_PS__412VAK_PumpPort__42",  // VAO 1313 ZUST
			"csps__ControlSystem_PS__412VAK_PumpPort__45",  // VAO 1314 ZUST
                        "csps__ControlSystem_PS__412VAK_PumpPort__21"   // Ex1221
                ),
                // MS PP1 vacuum (index 28)
		array(
			"csms__ControlSystem_MS__432VAK_PumpPort1__0"
                ),
                // PS Temperatures (index 29)
		array(
                         "csps__ControlSystem_PS__415HZ_Tank__78",
                         "csps__ControlSystem_PS__415HZ_Tank__84",
                         "csps__ControlSystem_PS__415HZ_Tank__90"
                ) ,
                // Voltage monitoring K35 etc. (index 30)
		array(
                         "hv__katrin-hv_rep__7__2",  //K35 divided voltage
                         "hv__katrin-hv_rep__7__5"  //K65 divided voltage
                ),
                // Interlock V4 and Detector Valve (index 31)
		array(
                         "csms__ControlSystem_MS__436HV_Interlook__6"  //CRT 5221 Freigabe 
                )
        );



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


echo "Include ADEI array from file.";
include 'ADEIvalues.php';
echo "Done.".PHP_EOL;


// Now read and test. $lines_read will contain an array like the original.
// $adeiValue = [];
// We use file function to read the file as an array of lines.

//$file_r = file('ADEIvalues.dat');

// Unpack all lines
//foreach ($file_r as $line) {
    // Format is i* because we may have more than 1 int in the line
    // If you changed format while packing, you will have to unpack with the
    // corresponding same format
//  $adeiValue[] = unpack('f*', $line);
  //var_dump($adeiValue);
//usleep(1000000);
//}

foreach ($adeiValue as $inner_array_value) {
    foreach ($inner_array_value as $num) {
        // Use of i format (integer). If you want to change format
        // according to the value of $num, you will have to save the
        // format too.
       echo $num . " ";
       usleep(10000);
}

    echo PHP_EOL;
}


echo "Done.".PHP_EOL;

?>
