Browse Source

Merge branch 'jbehrens/patchpanel-relations'

* jbehrens/patchpanel-relations:
  add some transitions
  Fix patchpabel
  Patchpanel styling
  Add back-link to patchpanel page
  fixes + tuning, add author field
  remove sleeps
  make php scripts single-shot
  Cleanup
  Bugfixes for patchpanel <-> KDB
  add KDB checkbox, new validity/author fields
  Update KDB relations via new JSON API
Jan Behrens 1 year ago
parent
commit
88f73ab0f9
6 changed files with 549 additions and 79 deletions
  1. 8 8
      index.php
  2. 192 31
      patchpanel/index.php
  3. 3 3
      poll_HV_data.php
  4. 5 5
      poll_adei_data.php
  5. 38 32
      poll_database.php
  6. 303 0
      update_database.php

+ 8 - 8
index.php

@@ -1,12 +1,12 @@
 <html>
 <head>
 
-  
+
 <link rel="shortcut icon" href="pics/KATRINlogo.ico" />
 <link href="stylesheet.css" rel="stylesheet">
 
-  <link rel="preload" href="CorporateESC-Bold.woff" as="font" type="font/woff" crossorigin>
-  
+<link rel="preload" href="CorporateESC-Bold.woff" as="font" type="font/woff" crossorigin>
+
 <link rel="preload" href="CorporateESC-Bold.woff2" as="font" type="font/woff2" crossorigin>
 
 
@@ -161,7 +161,7 @@ else { echo '<meta http-equiv="refresh" content="120">'; $slowrefresh= true; }
 
 </head>
 <body style="background-color:white;">
-     
+
 <?php
       // set time zone to local German time
       date_default_timezone_set('Europe/Berlin');
@@ -187,14 +187,14 @@ else { echo '<meta http-equiv="refresh" content="120">'; $slowrefresh= true; }
       $loop = 0;
       $includeflag = 0;
       do {
-        $includeflag = include 'ADEIvalues.php'; 
+        $includeflag = include 'ADEIvalues.php';
         $loop++;
         if ($valuesincluded == 0) usleep(100000);
         }
       while (($loop < 21) && ($includeflag+$valuesincluded < 2));
       if ($valuesincluded == 0) $extraction_time = "Request timed out!".PHP_EOL;
       //echo $loop."  ".$includeflag."  ".$valuesincluded.PHP_EOL;
-      
+
 
 	// ########## TIME STAMP ##########
 //	echo '<font size="6" color="#FF0000"><b>Deprecated KATRIN Spectrometer and Detector Section Status Overview</b></font><br>';
@@ -214,7 +214,7 @@ else { echo '<meta http-equiv="refresh" content="120">'; $slowrefresh= true; }
 <div style="position:absolute; top:0px; left:0px">
 <img src="pics/bg.png">
 -->
-  
+
 <div style="position:absolute; top:150px; left:2px">
 <img src="pics/beamline_noMoS.png" >
 
@@ -247,6 +247,6 @@ else { echo '<meta http-equiv="refresh" content="120">'; $slowrefresh= true; }
 
   <br><br><br>
   <iframe width="1700" height="400" src="https://dataview.raspberryshake.org/#/embed/AM/R985B/00/EHZ"></iframe>
-  
+
 </body>
 </html>

+ 192 - 31
patchpanel/index.php

@@ -1,14 +1,117 @@
 <html>
 <head>
+<link rel="shortcut icon" href="../pics/KATRINlogo.ico" />
 <title>High-Voltage Patch-Panel Configuration</title>
 <style type="text/css">
 	body      { font-family:Arial; }
 	td        { text-align:center; }
+    td.left   { text-align:left; }
 	a:link    { text-decoration:none; }
 	a:visited { text-decoration:none; }
 	a:hover   { text-decoration:none; }
 	a:active  { text-decoration:none; }
 	a:focus   { text-decoration:none; }
+
+    td.even {
+        background-color: #C8C8C8;
+    }
+    td.odd {
+        background-color: #FFFFFF;
+    }
+
+    tr.highlight:hover,
+    tr.highlight:hover td {
+        background-color: #F0E68C !important;
+        transition: .1s;
+    }
+
+    .container  {
+      display: block;
+      position: relative;
+      margin: 5px;
+      margin-top: -9px;
+      padding-left: 17px;
+      cursor: default;
+      font-size: 22px;
+      -webkit-user-select: none;
+      -moz-user-select: none;
+      -ms-user-select: none;
+      user-select: none;
+    }
+
+    /* Hide the browser's default checkbox */
+    .container input {
+      position: absolute;
+      opacity: 0;
+      cursor: pointer;
+      height: 0;
+      width: 0;
+    }
+
+    /* Create a custom checkbox */
+    .checkmark {
+      position: absolute;
+      top: 0;
+      left: 0;
+      height: 12px;
+      width: 12px;
+      background-color: #eee;
+      border: 2px solid #ddd;
+      border-radius: 8px;
+      transition: .2s;
+    }
+
+    /* On mouse-over, add a grey background color */
+    .container:hover input ~ .checkmark {
+      background-color: #FF8C00;
+      border: 2px solid black;
+    }
+
+    /* On mouse-over, add a grey background color */
+    .container:hover input:checked ~ .checkmark {
+      background-color: #0000CD;
+      border: 2px solid #0000CD;
+    }
+
+    /* When the checkbox is checked, add a blue background */
+    .container input:checked ~ .checkmark {
+      background-color: #0000CD;
+      border: 2px solid #eee;
+    }
+
+    /* On mouse-over, add a grey background color */
+    .common .container:hover input:checked ~ .checkmark {
+      background-color: #CD0000;
+      border: 2px solid #CD0000;
+    }
+
+    .common .container input:checked ~ .checkmark {
+      background-color: #CD0000;
+      border: 2px solid #eee;
+    }
+
+    /* Create the checkmark/indicator (hidden when not checked) */
+    .checkmark:after {
+      content: "";
+      position: absolute;
+      display: none;
+    }
+
+    /* Show the checkmark when checked */
+    .container input:checked ~ .checkmark:after {
+      display: block;
+    }
+
+    /* Style the checkmark/indicator */
+    .container .checkmark:after {
+      left: 3px;
+      top: 3px;
+      width: 2px;
+      height: 2px;
+      border: solid white;
+      border-width: 1px 2px 3px 2px;
+      border-radius: 4px;
+    }
 </style>
 </head>
 <body>
@@ -33,7 +136,11 @@
 	{
 		// write TXT patch-panel configuration
 
-		$time = strtotime($_POST["validfrom"]);
+        $config = $_POST["configurationname"] != "" ? $_POST["configurationname"] : $_POST["configurationload"];
+        $author = $_POST["author"] != "" ? $_POST["author"] : "(anonymous)";
+
+        //$time = strtotime($_POST["validfrom"]);
+		$time = strtotime($_POST["validfrom-date"] . " " . $_POST["validfrom-time"]);
 		if ($time === FALSE) $filename = time() . ".txt"; else $filename = $time . ".txt";
 
 		$handle = fopen("data/" . $filename, "w");
@@ -42,7 +149,8 @@
 			$content = "";
 			for ($id = 0; $id < 44; $id++)
 			{
-				if ($_POST[$rings[$id]] == 22) $tempcontent = "IE"; else $tempcontent = floatval($_POST[$rings[$id]]) + $offset;
+				if ($_POST[$rings[$id]] == 22) $tempcontent = "IE";
+                else $tempcontent = floatval($_POST[$rings[$id]]) + $offset;
 				$content = $content . $tempcontent . "\r\n";
 			}
 			fwrite($handle, $content);
@@ -71,6 +179,20 @@
 				fclose($handle);
 			}
 		}
+
+        // update KDB database
+
+        if (isset($_POST["database"])) {
+            sleep(1);
+            //require '../update_database.php';
+            if ($time === FALSE) {
+                shell_exec('cd .. ; php ./update_database.php "' . $config . '" "' . $author . '"');
+            }
+            else {
+                shell_exec('cd .. ; php ./update_database.php "' . $config . '" "' . $author . '" "' . date('Y-m-d H:i:s', $time) . '"');
+            }
+        }
+
 	}
 
 ?>
@@ -90,6 +212,8 @@
 
     if ($_POST["configurationload"] == "(active configuration)")
 	{
+        shell_exec('cd .. ; php ./poll_database.php');
+
         $handle = fopen("data/activeIEconfig.txt", "r");
         if ($handle)
         {
@@ -171,9 +295,12 @@
 
 <form method="post" action="index.php" name="myform">
 
-
-
-<font size="6"><b>High-Voltage Patch-Panel Configuration</b></font>
+<font size="6">
+<a href="../index.php" title="Back to main page">
+<img style="margin-right: .5em; margin-bottom: -8px" src="../pics/katrin_logo_round.png" width="40px" height="40px">
+<a>
+<b>High-Voltage Patch-Panel Configuration</b>
+</font>
 
 
 
@@ -219,30 +346,48 @@
 
 	<tr bgcolor="#000000" height="1px"><td colspan="47"></td></tr>
 	<tr>
-		<td bgcolor="#C8C8C8" colspan="22"><b>W E S T</b></td>
-		<td colspan="3"><b>PATCH PANEL</b></td>
-		<td bgcolor="#C8C8C8" colspan="22"><b>E A S T</b></td>
+		<th bgcolor="#C8C8C8" colspan="22"><b>W E S T</b></th>
+		<th colspan="3"><b>PATCH PANEL</b></th>
+		<th bgcolor="#C8C8C8" colspan="22"><b>E A S T</b></th>
 	</tr>
 	<tr>
 		<?php
-			for ($id=2; $id<7; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '" colspan="2">0' . $id . '</td>'; }
+			for ($id=2; $id<7; $id++) {
+                if ($id % 2 == 1) $class = "odd"; else $class = "even";
+                echo '<td class="highlight ' . $class . '" colspan="2">0' . $id . '</td>';
+            }
 			echo '<td colspan="2">07-11</td>';
-			for ($id=12; $id<17; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '" colspan="2">' . $id . '</td>'; }
+			for ($id=12; $id<17; $id++) {
+                if ($id % 2 == 1) $class = "odd"; else $class = "even";
+                echo '<td class="highlight ' . $class . '" colspan="2">' . $id . '</td>';
+            }
 			echo '<td>&nbsp;ADEI&nbsp;</td>';
 			echo '<td>&nbsp;CH&nbsp;</td>';
 			echo '<td>&nbsp;ADEI&nbsp;</td>';
-			for ($id=2; $id<7; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '" colspan="2">0' . $id . '</td>'; }
+			for ($id=2; $id<7; $id++) {
+                if ($id % 2 == 1) $class = "odd"; else $class = "even";
+                echo '<td class="highlight ' . $class . '" colspan="2">0' . $id . '</td>';
+            }
 			echo '<td colspan="2">07-11</td>';
-			for ($id=12; $id<17; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '" colspan="2">' . $id . '</td>'; }
+			for ($id=12; $id<17; $id++) {
+                if ($id % 2 == 1) $class = "odd"; else $class = "even";
+                echo '<td class="highlight ' . $class . '" colspan="2">' . $id . '</td>';
+            }
 		?>
 	</tr>
 	<tr>
 		<?php
-			for ($id=0; $id<11; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '">o</td><td bgcolor="' . $color . '">i</td>'; }
+			for ($id=0; $id<11; $id++) {
+                if ($id % 2 == 1) $class = "odd"; else $class = "even";
+                echo '<td class="highlight ' . $class . '">o</td><td class="highlight ' . $class . '">i</td>';
+            }
 			echo '<td></td>';
 			echo '<td></td>';
 			echo '<td></td>';
-			for ($id=0; $id<11; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '">o</td><td bgcolor="' . $color . '">i</td>'; }
+			for ($id=0; $id<11; $id++) {
+                if ($id % 2 == 1) $class = "odd"; else $class = "even";
+                echo '<td class="highlight ' . $class . '">o</td><td class="highlight ' . $class . '">i</td>';
+            }
 		?>
 	</tr>
 	<?php
@@ -252,25 +397,27 @@
 
 			$text = str_pad($ch, 2, 0, STR_PAD_LEFT);
 
-			echo '<tr>';
+			echo '<tr class="highlight">';
 
 			for ($id=0; $id<22; $id++)
 			{
-				if ($id % 4 == 2 || $id % 4 == 3) $color = "#FFFFFF"; else $color = "#C8C8C8";
+				//if ($id % 4 == 2 || $id % 4 == 3) $color = "#FFFFFF"; else $color = "#C8C8C8";
+                if ($id % 4 == 2 || $id % 4 == 3) $class = "odd"; else $class = "even";
+                if ($ch == 22) $class .= " common";
 
 				$checked = "";
 				if ($check[$id] == $ch) $checked = 'checked="checked"';
-				echo '<td bgcolor="' . $color . '"><input type="radio" name="' . $rings[$id] . '" value="' . $ch . '" ' . $checked . '></td>';
+				echo '<td class="highlight ' . $class . '"><label class="container"><input type="radio" name="' . $rings[$id] . '" value="' . $ch . '" ' . $checked . '><span class="checkmark"></span></label></td>';
 			}
 
 			$value = $adeiValue[1][$ch];
 			if ($ch == 22) $value = $adeiValue[0][0] + $adeiValue[0][1];
 			echo '<td>' . sprintf('%.0F', $value) . '</td>';
 
-			echo '<td><b>';
+			echo '<th><b>';
 			if ($ch == 22) echo 'IE';
 			else echo $text;
-			echo '</b></td>';
+			echo '</b></th>';
 
 			$value = $adeiValue[2][$ch];
 			if ($ch == 22) $value = $adeiValue[0][0] + $adeiValue[0][2];
@@ -278,11 +425,13 @@
 
 			for ($id=22; $id<44; $id++)
 			{
-				if ($id % 4 == 0 || $id % 4 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8";
+				//if ($id % 4 == 0 || $id % 4 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8";
+                if ($id % 4 == 2 || $id % 4 == 3) $class = "even"; else $class = "odd";
+                if ($ch == 22) $class .= " common";
 
 				$checked = "";
 				if ($check[$id] == $ch) $checked = 'checked="checked"';
-				echo '<td bgcolor="' . $color . '"><input type="radio" name="' . $rings[$id] . '" value="' . $ch . '" ' . $checked . '></td>';
+				echo '<td class="highlight ' . $class . '"><label class="container"><input type="radio" name="' . $rings[$id] . '" value="' . $ch . '" ' . $checked . '><span class="checkmark"></span></label></td>';
 			}
 
 			echo '</tr>';
@@ -302,7 +451,9 @@
 	<tr>
 		<td><b>valid from</b></td>
 		<td>
-			<input type="text" name="validfrom" size="19" maxlength="19"> (YYYY-MM-DD HH:MM:SS)
+			<!-- <input type="text" name="validfrom" size="19" maxlength="19"> (YYYY-MM-DD HH:MM:SS) -->
+            <input type="date" name="validfrom-date">
+            <input type="time" name="validfrom-time">
 		</td>
 	</tr>
 </table>
@@ -315,25 +466,35 @@
 
 <table border="0" cellpadding="5" cellspacing="0" bgcolor="#C8C8C8">
 	<tr>
-		<td><b>elog entry</b></td>
-		<td>
-			<input type="checkbox" name="elog" value="yes"> write & submit
+		<td width="200px"><b>elog entry</b></td>
+		<td class="left">
+			<input type="checkbox" name="elog" value="yes"> submit to ELOG
+		</td>
+	</tr>
+    <tr>
+		<td><b>database entry</b></td>
+		<td class="left">
+            <input type="checkbox" name="database" value="yes"> submit to KDB
 		</td>
 	</tr>
 	<tr>
-		<td>author</td>
-		<td>
-			<select name="author" size="1">
+		<td>author<br/><sup>(click twice to open drop-down)</sup></td>
+		<td class="left">
+			<!--<select name="author" size="1">-->
+            <input type="text" list="authorlist" name="author" size="50"/>
+            <datalist id="authorlist">
 				<option>---</option>
-				<option>C. Rodenbeck</option>
+				<option>B. Bieringer</option>
+                <option>C. Rodenbeck</option>
 				<option>T. Thuemmler</option>
 				<option>S. Wuestling</option>
-			</select>
+            </datalist>
+			<!--</select>-->
 		</td>
 	</tr>
 	<tr>
 		<td>comments</td>
-		<td>
+		<td class="left">
 			<textarea name="comments" cols="50" rows="5"></textarea>
 		</td>
 	</tr>

+ 3 - 3
poll_HV_data.php

@@ -35,7 +35,7 @@
     $starttime = $calltime;
     $n = 0;
     //echo "started ".date("r", $starttime).PHP_EOL;
-    do {
+//    do {
         $n++;
         $currenttime = time();
         $delaytime = 10 - ($currenttime % 10);
@@ -211,8 +211,8 @@
         //echo $strFileContent.PHP_EOL;
         //echo $n." ".time().PHP_EOL;
 
-    }
-    while (time() < $starttime + 50);
+//    }
+//    while (time() < $starttime + 50);
 
     //echo "stopped".date("r", time()).PHP_EOL;
     //echo "Data extraction done.".PHP_EOL;

+ 5 - 5
poll_adei_data.php

@@ -64,7 +64,7 @@ date_default_timezone_set('Europe/Berlin');
 
 $counter = 0;
 $calltime = time();
-do {
+//do {
         $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>';
 
@@ -111,7 +111,7 @@ do {
         // increse loop counter
 	$counter ++;
 	$stoptime = time();
-	$wait_till = $starttime + 11;
+//	$wait_till = $starttime + 11;
 
 	if ($stoptime-$starttime > 10) {$errormessage .= "ERROR: delayed readout ".($stoptime-$starttime)." s "; $errorcount++;}
 	
@@ -143,10 +143,10 @@ do {
         
 	//echo "Loop ".$counter." done.".PHP_EOL;
 
-        if ($wait_till <= $stoptime) sleep(3);
-        else time_sleep_until($wait_till);
+//        if ($wait_till <= $stoptime) sleep(3);
+//        else time_sleep_until($wait_till);
 
-} while (($counter < 10) && (time() < $calltime + 51));
+//} while (($counter < 10) && (time() < $calltime + 51));
 
 //echo "All loops done.".PHP_EOL;
 

+ 38 - 32
poll_database.php

@@ -53,7 +53,6 @@
     //include "SDS_status_channels.php";
     $username = getenv("PHP_ADEI_USERNAME");
     $password = getenv("PHP_ADEI_PASSWORD");
-    $context = stream_context_create(["http" => ["header" => "Authorization: Basic " . base64_encode("$username:$password") , "Connection: close\r\n", ], ]);
 
     // ** Katrin numbers referring to IE electrode segments ** //
     $relationKatrinNumbers = array(
@@ -189,24 +188,28 @@
     // 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 {
+//    do {
         $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();
 
-        /// TODO (1) get current relations for HV rings (KATRIN number mapping)
-        /// TODO (2) write channel mapping to file used by monitor ("patchpanel/data/activeIEconfig.txt")
-        ///     => (a) the file contains one line per IE channel (44 total: 11 rings x inner/outer x west/east)
-        ///     => (b) each line contains the power supply index (values 0 .. 21; 22 = through IE)
-        /// TODO (3) resolve ADEI channels for each relation (?)
-        /// TODO (4) update ADEIchannels array (?)
-
         //echo "Polling database." . PHP_EOL;
         // new kdb.kaas.kit.edu added and optimized readout parameters
-        $fileip = file_get_contents("https://kdb.kaas.kit.edu/kdb-api.fcgi/json?relations&kn=" . join(",", flatten($relationKatrinNumbers)) . "&only_recent=1", false, $context);
+        $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") {
@@ -214,10 +217,6 @@
             $errormessage = $errormessage . "Error querying database<br>";
         }
 
-        if ($errorcount > 0) {
-            echo $errormessage;
-        }
-
         $json = json_decode($fileip, true);
         //echo "Database result (JSON):" . PHP_EOL;
         //var_dump($json);
@@ -235,7 +234,7 @@
         // increse loop counter
         $counter++;
         $stoptime = time();
-        $wait_till = $starttime + 11;
+//        $wait_till = $starttime + 11;
 
         if ($stoptime - $starttime > 10) {
             $errormessage .= "ERROR: delayed readout " . ($stoptime - $starttime) . " s ";
@@ -245,6 +244,9 @@
         //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++) {
@@ -252,17 +254,15 @@
             assert( sizeof($relationKatrinNumbers[$eastwest]) == 22 );
             assert( sizeof($relationTargetNumbers[$eastwest]) >= 22 );
             foreach ($relationKatrinNumbers[$eastwest] as $kn) {
-                $index = 22; // default to IE common
+                $index = 'IE'; // default to IE common
                 $target = "(default)";
                 if (array_key_exists($kn, $patchpanelMap)) {
                     $target = $patchpanelMap[$kn];
-                    if ($target == "436-EHV-0-1003-0002") {
+                    if ($target == end($relationTargetNumbers[$eastwest])) {  // last entry = IE common
                         $index = 'IE';
                     }
                     else {
-                        $index = array_search($target, $relationTargetNumbers[$eastwest]);
-                        if ($index > 22)
-                            $index = 22;
+                        $index = array_search($target, $relationTargetNumbers[$eastwest]) + $offset;
                     }
                 }
                 echo "\t" . $kn . " => " . $target . " [" . $index . "]" . PHP_EOL;
@@ -271,14 +271,16 @@
         }
 
         // write ADEI data to file
-        if (file_exists("patchpanel/data/activeIEconfig.txt")) {
-            rename("patchpanel/data/activeIEconfig.txt", "patchpanel/data/activeIEconfig.txt.bak");
+        if (file_exists($fname)) {
+            rename($fname, $fname . ".bak");
         }
 
-        $file_w = fopen("patchpanel/data/activeIEconfig.txt", "w+");
+        $file_w = fopen($fname, "w+");
         //sleep(5);
-        fwrite($file_w, $strFileContent);
-        fclose($file_w);
+        if ($file_w) {
+            fwrite($file_w, $strFileContent);
+            fclose($file_w);
+        }
 
         // write log file
         $log_message = "";
@@ -292,14 +294,18 @@
         fwrite($file_log, $log_message);
         fclose($file_log);
 
-        //echo "Loop ".$counter." done.".PHP_EOL;
-        if ($wait_till <= $stoptime) {
-            sleep(3);
-        }
-        else {
-            time_sleep_until($wait_till);
+        if ($errorcount > 0) {
+            echo $errormessage;
         }
-    } while ($counter < 10 && time() < $calltime + 51);
+
+//        //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;
 

+ 303 - 0
update_database.php

@@ -0,0 +1,303 @@
+<?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 = (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";
+        }
+    }
+
+    // get config name (optionally pass to script, 1st arg)
+    $config = !empty($argv[1]) ? $argv[1] : "";
+    $author = !empty($argv[2]) ? $argv[2] : "";
+
+    // get validity time (optionally pass to script, 2nd arg)
+    $ts = !empty($argv[3]) ? $argv[3] : date('Y-m-d H:i:s', $time);
+
+    // include channel list
+    //include "SDS_status_channels.php";
+    $username = getenv("PHP_ADEI_USERNAME");
+    $password = getenv("PHP_ADEI_PASSWORD");
+
+    // the user needs write access to KDB!
+    $kdb_username = getenv("PHP_KDB_USERNAME");
+    $kdb_password = getenv("PHP_KDB_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");
+
+    $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>";
+
+    $check = array( 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
+                    22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22 );
+
+    if ($config != "") {
+        $fname = "patchpanel/data/" . $config . ".conf";
+        $offset = 0;
+    }
+    else {
+        $fname = "patchpanel/data/activeIEconfig.txt";
+        $offset = -2;
+    }
+
+    // read ADEI data from file
+    if (! file_exists($fname)) {
+        $errorcount++;
+        $errormessage = $errormessage . "File " . $fname . " does not exist.<br>";
+    }
+
+    echo "Reading file: " . $fname . PHP_EOL;
+    $handle = fopen($fname, "r");
+    if ($handle) {
+        $id = 0;
+        while (($buffer = fgets($handle, 4096)) !== false)
+        {
+            $buffer = trim($buffer);
+            if ($buffer == 'IE')
+                $check[$id++] = 22;
+            else
+                $check[$id++] = intval($buffer) + $offset;
+        }
+        fclose($handle);
+    }
+
+    $json = array('relation' => array());
+
+    assert( sizeof($relationKatrinNumbers) == 2 );
+    assert( sizeof($relationTargetNumbers) == 2 );
+    $ch = 0;
+    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) {
+            $json['relation'][$kn] = array();
+            $json['relation'][$kn]['ValidityStart'] = $ts;
+            $json['relation'][$kn]['Comment'] = $config . " (" .  $author . ")";
+
+            $index = $check[$ch++];
+            assert($index >= 0 and $index <= 22);
+            if ($index >= 22) {
+                $target = end($relationTargetNumbers[$eastwest]);  // last entry = IE common
+            }
+            else {
+                $target = $relationTargetNumbers[$eastwest][$index];
+            }
+            $json['relation'][$kn]['AssociateNumber'] = $target;
+            echo "\t" . $kn . " => " . $target . " [" . $index . "]" . PHP_EOL;
+        }
+    }
+
+    //echo "Request payload (JSON):" . PHP_EOL;
+    //var_dump($json);
+
+    // build HTTP context
+    $context = stream_context_create([
+        'http' => [
+            'method' => 'POST',
+            'header' => "Authorization: Basic " . base64_encode("$kdb_username:$kdb_password") . "\r\n" .
+                        "Content-type: application/json" . "\r\n" .
+                        "Connection: close" . "\r\n",
+            'content' => json_encode($json),
+        ],
+    ]);
+
+    $KDB_API_URL = "https://kdb-test.kaas.kit.edu/kdb-api.fcgi/json/write";
+    //$KDB_API_URL = "http://localhost:9999/json/write";
+
+    echo "Updating database." . PHP_EOL;
+    $fileip = file_get_contents($KDB_API_URL, false, $context);
+
+    //echo $fileip . PHP_EOL;
+
+    //show_status($id+1,sizeof($adeiChannel));
+    if (substr($fileip, 0, 5) == "ERROR") {
+        $errorcount++;
+        $errormessage = $errormessage . "Error querying database<br>";
+    }
+
+    $json = json_decode($fileip, true);
+    //echo "Database result (JSON):" . PHP_EOL;
+    //var_dump($json);
+
+    if ($errorcount > 0) {
+        echo $errormessage;
+    }
+
+    //echo "All loops done.".PHP_EOL;
+
+    ?>