$total) {
return;
}
if (empty($start_time)) {
$start_time = time();
}
$now = time();
$perc = (float)($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 = getenv("PHP_ADEI_USERNAME");
$password = getenv("PHP_ADEI_PASSWORD");
// ** Katrin numbers referring to IE electrode segments ** //
$relationKatrinNumbers = array(
array( // 22 rows: 2x11 electrode rings (inner+outer)
"434-EEL-3-0210-0002", // WestRing 02 outer
"434-EEL-3-0211-0002", // WestRing 02 inner
"434-EEL-3-0310-0002", // WestRing 03 outer
"434-EEL-3-0311-0002", // WestRing 03 inner
"434-EEL-3-0410-0002", // WestRing 04 outer
"434-EEL-3-0411-0002", // WestRing 04 inner
"434-EEL-3-0510-0002", // WestRing 05 outer
"434-EEL-3-0511-0002", // WestRing 05 inner
"434-EEL-3-0610-0002", // WestRing 06 outer
"434-EEL-3-0611-0002", // WestRing 06 inner
"434-EEL-3-0710-0002", // WestRing 07-11 outer
"434-EEL-3-0711-0002", // WestRing 07-11 inner
"434-EEL-3-1210-0002", // WestRing 12 outer
"434-EEL-3-1211-0002", // WestRing 12 inner
"434-EEL-3-1310-0002", // WestRing 13 outer
"434-EEL-3-1311-0002", // WestRing 13 inner
"434-EEL-3-1410-0002", // WestRing 14 outer
"434-EEL-3-1411-0002", // WestRing 14 inner
"434-EEL-3-1510-0002", // WestRing 15 outer
"434-EEL-3-1511-0002", // WestRing 15 inner
"434-EEL-3-1610-0002", // WestRing 16 outer
"434-EEL-3-1611-0002", // WestRing 16 inner
// ^^............ means ring index (02..16)
// ^........... means west (=1) or east (=2) dipole
// ^.......... means outer (=0) or inner (=1) layer
) ,
array( // 22 rows: 2x11 electrode rings (inner+outer)
"434-EEL-3-0220-0002", // EastRing 02 outer
"434-EEL-3-0221-0002", // EastRing 02 inner
"434-EEL-3-0320-0002", // EastRing 03 outer
"434-EEL-3-0321-0002", // EastRing 03 inner
"434-EEL-3-0420-0002", // EastRing 04 outer
"434-EEL-3-0421-0002", // EastRing 04 inner
"434-EEL-3-0520-0002", // EastRing 05 outer
"434-EEL-3-0521-0002", // EastRing 05 inner
"434-EEL-3-0620-0002", // EastRing 06 outer
"434-EEL-3-0621-0002", // EastRing 06 inner
"434-EEL-3-0720-0002", // EastRing 07-11 outer
"434-EEL-3-0721-0002", // EastRing 07-11 inner
"434-EEL-3-1220-0002", // EastRing 12 outer
"434-EEL-3-1221-0002", // EastRing 12 inner
"434-EEL-3-1320-0002", // EastRing 13 outer
"434-EEL-3-1321-0002", // EastRing 13 inner
"434-EEL-3-1420-0002", // EastRing 14 outer
"434-EEL-3-1421-0002", // EastRing 14 inner
"434-EEL-3-1520-0002", // EastRing 15 outer
"434-EEL-3-1521-0002", // EastRing 15 inner
"434-EEL-3-1620-0002", // EastRing 16 outer
"434-EEL-3-1621-0002", // EastRing 16 inner
// ^^............ means ring index (02..16)
// ^........... means west (=1) or east (=2) dipole
// ^.......... means outer (=0) or inner (=1) layer
) ,
);
// ** Katrin numbers referring to IE power supply channels ** //
$relationTargetNumbers = array(
array( // IE offset channels West side
// 22 rows: 3x7 offset channels + IE common
"436-EHV-0-0003-0102", // WestCh 00 / not used
"436-EHV-0-0003-0202", // WestCh 01 / not used
"436-EHV-0-0003-0302", // WestCh 02 / Patch 00
"436-EHV-0-0003-0402", // WestCh 03 / Patch 01
"436-EHV-0-0003-0502", // WestCh 04 / Patch 02
"436-EHV-0-0003-0602", // WestCh 05 / Patch 03
"436-EHV-0-0003-0702", // WestCh 06 / Patch 04
"436-EHV-0-0003-0802", // WestCh 07 / Patch 05
"436-EHV-0-0005-0102", // WestCh 08 / Patch 06
"436-EHV-0-0005-0202", // WestCh 09 / Patch 07
"436-EHV-0-0005-0302", // WestCh 10 / Patch 08
"436-EHV-0-0005-0402", // WestCh 11 / Patch 09
"436-EHV-0-0005-0502", // WestCh 12 / Patch 10
"436-EHV-0-0005-0602", // WestCh 13 / Patch 11
"436-EHV-0-0005-0702", // WestCh 14 / Patch 12
"436-EHV-0-0005-0802", // WestCh 15 / Patch 13
"436-EHV-0-0007-0102", // WestCh 16 / Patch 14
"436-EHV-0-0007-0202", // WestCh 16 / Patch 15
"436-EHV-0-0007-0302", // WestCh 17 / Patch 16
"436-EHV-0-0007-0402", // WestCh 18 / Patch 17
"436-EHV-0-0007-0502", // WestCh 19 / Patch 18
"436-EHV-0-0007-0602", // WestCh 20 / Patch 19
"436-EHV-0-0007-0702", // WestCh 21 / Patch 20
"436-EHV-0-0007-0802", // WestCh 22 / Patch 21
"436-EHV-0-0002-0101", // IE dipole west
"436-EHV-0-1003-0002", // IE common
) ,
array( // IE offset channels East side
// 22 rows: 3x7 offset channels + IE common
"436-EHV-0-0004-0102", // EastCh 00 / not used
"436-EHV-0-0004-0202", // EastCh 01 / not used
"436-EHV-0-0004-0302", // EastCh 02 / Patch 00
"436-EHV-0-0004-0402", // EastCh 03 / Patch 01
"436-EHV-0-0004-0502", // EastCh 04 / Patch 02
"436-EHV-0-0004-0602", // EastCh 05 / Patch 03
"436-EHV-0-0004-0702", // EastCh 06 / Patch 04
"436-EHV-0-0004-0802", // EastCh 07 / Patch 05
"436-EHV-0-0006-0102", // EastCh 08 / Patch 06
"436-EHV-0-0006-0202", // EastCh 09 / Patch 07
"436-EHV-0-0006-0302", // EastCh 10 / Patch 08
"436-EHV-0-0006-0402", // EastCh 11 / Patch 09
"436-EHV-0-0006-0502", // EastCh 12 / Patch 10
"436-EHV-0-0006-0602", // EastCh 13 / Patch 11
"436-EHV-0-0006-0702", // EastCh 14 / Patch 12
"436-EHV-0-0006-0802", // EastCh 15 / Patch 13
"436-EHV-0-0008-0102", // EastCh 16 / Patch 14
"436-EHV-0-0008-0202", // EastCh 16 / Patch 15
"436-EHV-0-0008-0302", // EastCh 17 / Patch 16
"436-EHV-0-0008-0402", // EastCh 18 / Patch 17
"436-EHV-0-0008-0502", // EastCh 19 / Patch 18
"436-EHV-0-0008-0602", // EastCh 20 / Patch 19
"436-EHV-0-0008-0702", // EastCh 21 / Patch 20
"436-EHV-0-0008-0802", // EastCh 22 / Patch 21
"436-EHV-0-0002-0102", // IE dipole east
"436-EHV-0-1003-0002", // IE common
) ,
);
function flatten(array $array) {
$return = array();
array_walk_recursive($array, function ($a) use (&$return) {
$return[] = $a;
});
return $return;
}
// set time zone to local German time
date_default_timezone_set("Europe/Berlin");
// build HTTP context
$context = stream_context_create([
'http' => [
'header' => "Authorization: Basic " . base64_encode("$username:$password") . "\r\n" .
"Connection: close" . "\r\n",
],
]);
$KDB_API_URL = "https://kdb-test.kaas.kit.edu/kdb-api.fcgi/json";
//$KDB_API_URL = "http://localhost:9999/json";
$counter = 0;
$calltime = time();
// do {
$errorcount = 0;
$errormessage = "
";
$starttime = time();
//echo "Polling database." . PHP_EOL;
// new kdb.kaas.kit.edu added and optimized readout parameters
$fileip = file_get_contents($KDB_API_URL . "?relations&kn=" . join(",", flatten($relationKatrinNumbers)) . "&only_recent=1", false, $context);
//show_status($id+1,sizeof($adeiChannel));
if (substr($fileip, 0, 5) == "ERROR") {
$errorcount++;
$errormessage = $errormessage . "Error querying database
";
}
$json = json_decode($fileip, true);
//echo "Database result (JSON):" . PHP_EOL;
//var_dump($json);
$patchpanelMap = array();
if ($json && $json["result"]) {
foreach ($json["result"]["Relation"] as $kn => $data) {
$target = $data["AssociateNumber"];
$patchpanelMap[$kn] = $target;
//echo "\t" . $kn . " => " . $target . PHP_EOL;
}
}
// increse loop counter
$counter++;
$stoptime = time();
// $wait_till = $starttime + 11;
if ($stoptime - $starttime > 10) {
$errormessage .= "ERROR: delayed readout " . ($stoptime - $starttime) . " s ";
$errorcount++;
}
//echo "Writing patchpanel database data to file." . PHP_EOL;
$strFileContent = "";
$fname = "patchpanel/data/activeIEconfig.txt";
$offset = 2;
assert( sizeof($relationKatrinNumbers) == 2 );
assert( sizeof($relationTargetNumbers) == 2 );
for ($eastwest = 0; $eastwest < 2; $eastwest++) {
// loop over each half of the array
assert( sizeof($relationKatrinNumbers[$eastwest]) == 22 );
assert( sizeof($relationTargetNumbers[$eastwest]) >= 22 );
foreach ($relationKatrinNumbers[$eastwest] as $kn) {
$index = 'IE'; // default to IE common
$target = "(default)";
if (array_key_exists($kn, $patchpanelMap)) {
$target = $patchpanelMap[$kn];
if ($target == end($relationTargetNumbers[$eastwest])) { // last entry = IE common
$index = 'IE';
}
else {
$index = array_search($target, $relationTargetNumbers[$eastwest]) + $offset;
}
}
echo "\t" . $kn . " => " . $target . " [" . $index . "]" . PHP_EOL;
$strFileContent .= $index . "\n";
}
}
// write ADEI data to file
if (file_exists($fname)) {
rename($fname, $fname . ".bak");
}
$file_w = fopen($fname, "w+");
//sleep(5);
if ($file_w) {
fwrite($file_w, $strFileContent);
fclose($file_w);
}
// write log file
$log_message = "";
$log_message = date(DATE_RFC850) . ": database polled: loop " . $counter;
$log_message .= ", poll time " . ($stoptime - $starttime) . " s";
if ($stoptime - $starttime > 10) {
$log_message .= ", delayed readout";
}
$log_message .= PHP_EOL;
$file_log = fopen("logs/poll_database.log", "a");
fwrite($file_log, $log_message);
fclose($file_log);
if ($errorcount > 0) {
echo $errormessage;
}
// //echo "Loop ".$counter." done.".PHP_EOL;
// if ($wait_till <= $stoptime) {
// sleep(3);
// }
// else {
// time_sleep_until($wait_till);
// }
// } while ($counter < 10 && time() < $calltime + 51);
//echo "All loops done.".PHP_EOL;
?>