Browse Source

Add feature to pull patchpanel relations from KaLi database

Jan Behrens 2 years ago
parent
commit
aa95777525
2 changed files with 289 additions and 0 deletions
  1. 1 0
      poll_data.sh
  2. 288 0
      poll_database.php

+ 1 - 0
poll_data.sh

@@ -2,3 +2,4 @@
 
 php poll_adei_data.php
 php poll_HV_data.php
+php poll_database.php

+ 288 - 0
poll_database.php

@@ -0,0 +1,288 @@
+<?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";
+        }
+    }
+
+    // include channel list
+    //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", ], ]);
+
+    $relationKatrinNumbers = array(
+        array( // 22 rows: 2x11 electrode rings (inner+outer)
+            "436-EHV-0-9006-0002", // WestRing 02 outer
+            "436-EHV-0-9106-0002", // WestRing 02 inner
+            "436-EHV-0-9005-0002", // WestRing 03 outer
+            "436-EHV-0-9105-0002", // WestRing 03 inner
+            "436-EHV-0-9004-0002", // WestRing 04 outer
+            "436-EHV-0-9104-0002", // WestRing 04 inner
+            "436-EHV-0-9003-0002", // WestRing 05 outer
+            "436-EHV-0-9103-0002", // WestRing 05 inner
+            "436-EHV-0-9002-0002", // WestRing 06 outer
+            "436-EHV-0-9102-0002", // WestRing 06 inner
+            "436-EHV-0-9001-0002", // WestRing 07-11 outer
+            "436-EHV-0-9101-0002", // WestRing 07-11 inner
+            "436-EHV-0-9012-0002", // WestRing 12 outer
+            "436-EHV-0-9112-0002", // WestRing 12 inner
+            "436-EHV-0-9013-0002", // WestRing 13 outer
+            "436-EHV-0-9113-0002", // WestRing 13 inner
+            "436-EHV-0-9014-0002", // WestRing 14 outer
+            "436-EHV-0-9114-0002", // WestRing 14 inner
+            "436-EHV-0-9015-0002", // WestRing 15 outer
+            "436-EHV-0-9115-0002", // WestRing 15 inner
+            "436-EHV-0-9016-0002", // WestRing 16 outer
+            "436-EHV-0-9116-0002", // WestRing 16 inner
+            //         ^............ means this is a HV relation number (9 = west)
+            //          ^........... means outer (=0) or inner (=1) layer
+            //           ^.......... means source (=0) or detector (=1) side
+            //            ^......... means ring index (0..6), counted from central segment
+
+        ) ,
+        array( // 22 rows: 2x11 electrode rings (inner+outer)
+            "436-EHV-0-8006-0002", // EastRing 02 outer
+            "436-EHV-0-8106-0002", // EastRing 02 inner
+            "436-EHV-0-8005-0002", // EastRing 03 outer
+            "436-EHV-0-8105-0002", // EastRing 03 inner
+            "436-EHV-0-8004-0002", // EastRing 04 outer
+            "436-EHV-0-8104-0002", // EastRing 04 inner
+            "436-EHV-0-8003-0002", // EastRing 05 outer
+            "436-EHV-0-8103-0002", // EastRing 05 inner
+            "436-EHV-0-8002-0002", // EastRing 06 outer
+            "436-EHV-0-8102-0002", // EastRing 06 inner
+            "436-EHV-0-8001-0002", // EastRing 07-11 outer
+            "436-EHV-0-8101-0002", // EastRing 07-11 inner
+            "436-EHV-0-8012-0002", // EastRing 12 outer
+            "436-EHV-0-8112-0002", // EastRing 12 inner
+            "436-EHV-0-8013-0002", // EastRing 13 outer
+            "436-EHV-0-8113-0002", // EastRing 13 inner
+            "436-EHV-0-8014-0002", // EastRing 14 outer
+            "436-EHV-0-8114-0002", // EastRing 14 inner
+            "436-EHV-0-8015-0002", // EastRing 15 outer
+            "436-EHV-0-8115-0002", // EastRing 15 inner
+            "436-EHV-0-8016-0002", // EastRing 16 outer
+            "436-EHV-0-8116-0002", // EastRing 16 inner
+            //         ^............ means this is a HV relation number (8 = east)
+            //          ^........... means outer (=0) or inner (=1) layer
+            //           ^.......... means source (=0) or detector (=1) side
+            //            ^......... means ring index (0..6), counted from central segment
+
+        ) ,
+    );
+
+    $relationTargetNumbers = array(
+        array( // IE offset channels West side
+            // 22 rows: 3x7 offset channels + IE common
+            "436-EHV-0-0003-0102", // WestCh 01
+            "436-EHV-0-0003-0202", // WestCh 02
+            "436-EHV-0-0003-0302", // WestCh 03
+            "436-EHV-0-0003-0402", // WestCh 04
+            "436-EHV-0-0003-0502", // WestCh 05
+            "436-EHV-0-0003-0602", // WestCh 06
+            "436-EHV-0-0003-0702", // WestCh 07
+            "436-EHV-0-0005-0102", // WestCh 08
+            "436-EHV-0-0005-0202", // WestCh 09
+            "436-EHV-0-0005-0302", // WestCh 10
+            "436-EHV-0-0005-0402", // WestCh 11
+            "436-EHV-0-0005-0502", // WestCh 12
+            "436-EHV-0-0005-0602", // WestCh 13
+            "436-EHV-0-0005-0702", // WestCh 14
+            "436-EHV-0-0007-0102", // WestCh 15
+            "436-EHV-0-0007-0202", // WestCh 16
+            "436-EHV-0-0007-0302", // WestCh 17
+            "436-EHV-0-0007-0402", // WestCh 18
+            "436-EHV-0-0007-0502", // WestCh 19
+            "436-EHV-0-0007-0602", // WestCh 20
+            "436-EHV-0-0007-0702", // WestCh 21
+            "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 01
+            "436-EHV-0-0004-0202", // EastCh 02
+            "436-EHV-0-0004-0302", // EastCh 03
+            "436-EHV-0-0004-0402", // EastCh 04
+            "436-EHV-0-0004-0502", // EastCh 05
+            "436-EHV-0-0004-0602", // EastCh 06
+            "436-EHV-0-0004-0702", // EastCh 07
+            "436-EHV-0-0006-0102", // EastCh 08
+            "436-EHV-0-0006-0202", // EastCh 09
+            "436-EHV-0-0006-0302", // EastCh 10
+            "436-EHV-0-0006-0402", // EastCh 11
+            "436-EHV-0-0006-0502", // EastCh 12
+            "436-EHV-0-0006-0602", // EastCh 13
+            "436-EHV-0-0006-0702", // EastCh 14
+            "436-EHV-0-0008-0102", // EastCh 15
+            "436-EHV-0-0008-0202", // EastCh 16
+            "436-EHV-0-0008-0302", // EastCh 17
+            "436-EHV-0-0008-0402", // EastCh 18
+            "436-EHV-0-0008-0502", // EastCh 19
+            "436-EHV-0-0008-0602", // EastCh 20
+            "436-EHV-0-0008-0702", // EastCh 21
+            "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");
+
+    $counter = 0;
+    $calltime = time();
+    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();
+
+        echo "Polling database." . PHP_EOL;
+
+        /// 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 (?)
+        // 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);
+
+        //show_status($id+1,sizeof($adeiChannel));
+        if (substr($fileip, 0, 5) == "ERROR") {
+            $errorcount++;
+            $errormessage = $errormessage . "Error querying database<br>";
+        }
+
+        if ($errorcount > 0) {
+            echo $errormessage;
+        }
+
+        $json = json_decode($fileip, true);
+        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 = "";
+
+        for ($eastwest = 0;$eastwest < 2;$eastwest++) {
+            // loop over each half of the array
+            foreach ($relationKatrinNumbers[$eastwest] as $kn) {
+                $index = 22; // default to IE common
+                if (array_key_exists($kn, $patchpanelMap)) {
+                    $target = $patchpanelMap[$kn];
+                    $index = array_search($target, $relationTargetNumbers[$eastwest]);
+                }
+                $strFileContent .= $index . "\n";
+            }
+        }
+
+        // write ADEI data to file
+        if (file_exists("patchpanel/data/activeIEconfig.txt")) {
+            rename("patchpanel/data/activeIEconfig.txt", "patchpanel/data/activeIEconfig.txt.bak");
+        }
+
+        $file_w = fopen("patchpanel/data/activeIEconfig.txt", "w+");
+        //sleep(5);
+        fwrite($file_w, $strFileContent);
+        fclose($file_w);
+
+        // write log file
+        $log_message = "";
+        $log_message = date(DATE_RFC850) . ": ADEI data 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_adei_data.log", "a");
+        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);
+        }
+    } while ($counter < 10 && time() < $calltime + 51);
+
+    //echo "All loops done.".PHP_EOL;
+
+?>