poll_database.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. function show_status($done, $total, $size = 30) {
  3. static $start_time;
  4. // if we go over our bound, just ignore it
  5. if ($done > $total) {
  6. return;
  7. }
  8. if (empty($start_time)) {
  9. $start_time = time();
  10. }
  11. $now = time();
  12. $perc = (float)($done / $total);
  13. $bar = floor($perc * $size);
  14. $status_bar = "\r[";
  15. $status_bar .= str_repeat("=", $bar);
  16. if ($bar < $size) {
  17. $status_bar .= ">";
  18. $status_bar .= str_repeat(" ", $size - $bar);
  19. }
  20. else {
  21. $status_bar .= "=";
  22. }
  23. $disp = number_format($perc * 100, 0);
  24. $status_bar .= "] $disp% $done/$total";
  25. $rate = ($now - $start_time) / $done;
  26. $left = $total - $done;
  27. $eta = round($rate * $left, 2);
  28. $elapsed = $now - $start_time;
  29. $status_bar .= " remaining: " . number_format($eta) . " sec. elapsed: " . number_format($elapsed) . " sec.";
  30. echo "$status_bar ";
  31. flush();
  32. // when done, send a newline
  33. if ($done == $total) {
  34. echo "\n";
  35. }
  36. }
  37. // include channel list
  38. //include "SDS_status_channels.php";
  39. $username = getenv("PHP_ADEI_USERNAME");
  40. $password = getenv("PHP_ADEI_PASSWORD");
  41. $context = stream_context_create(["http" => ["header" => "Authorization: Basic " . base64_encode("$username:$password") , "Connection: close\r\n", ], ]);
  42. $relationKatrinNumbers = array(
  43. array( // 22 rows: 2x11 electrode rings (inner+outer)
  44. "436-EHV-0-9006-0002", // WestRing 02 outer
  45. "436-EHV-0-9106-0002", // WestRing 02 inner
  46. "436-EHV-0-9005-0002", // WestRing 03 outer
  47. "436-EHV-0-9105-0002", // WestRing 03 inner
  48. "436-EHV-0-9004-0002", // WestRing 04 outer
  49. "436-EHV-0-9104-0002", // WestRing 04 inner
  50. "436-EHV-0-9003-0002", // WestRing 05 outer
  51. "436-EHV-0-9103-0002", // WestRing 05 inner
  52. "436-EHV-0-9002-0002", // WestRing 06 outer
  53. "436-EHV-0-9102-0002", // WestRing 06 inner
  54. "436-EHV-0-9001-0002", // WestRing 07-11 outer
  55. "436-EHV-0-9101-0002", // WestRing 07-11 inner
  56. "436-EHV-0-9012-0002", // WestRing 12 outer
  57. "436-EHV-0-9112-0002", // WestRing 12 inner
  58. "436-EHV-0-9013-0002", // WestRing 13 outer
  59. "436-EHV-0-9113-0002", // WestRing 13 inner
  60. "436-EHV-0-9014-0002", // WestRing 14 outer
  61. "436-EHV-0-9114-0002", // WestRing 14 inner
  62. "436-EHV-0-9015-0002", // WestRing 15 outer
  63. "436-EHV-0-9115-0002", // WestRing 15 inner
  64. "436-EHV-0-9016-0002", // WestRing 16 outer
  65. "436-EHV-0-9116-0002", // WestRing 16 inner
  66. // ^............ means this is a HV relation number (9 = west)
  67. // ^........... means outer (=0) or inner (=1) layer
  68. // ^.......... means source (=0) or detector (=1) side
  69. // ^......... means ring index (0..6), counted from central segment
  70. ) ,
  71. array( // 22 rows: 2x11 electrode rings (inner+outer)
  72. "436-EHV-0-8006-0002", // EastRing 02 outer
  73. "436-EHV-0-8106-0002", // EastRing 02 inner
  74. "436-EHV-0-8005-0002", // EastRing 03 outer
  75. "436-EHV-0-8105-0002", // EastRing 03 inner
  76. "436-EHV-0-8004-0002", // EastRing 04 outer
  77. "436-EHV-0-8104-0002", // EastRing 04 inner
  78. "436-EHV-0-8003-0002", // EastRing 05 outer
  79. "436-EHV-0-8103-0002", // EastRing 05 inner
  80. "436-EHV-0-8002-0002", // EastRing 06 outer
  81. "436-EHV-0-8102-0002", // EastRing 06 inner
  82. "436-EHV-0-8001-0002", // EastRing 07-11 outer
  83. "436-EHV-0-8101-0002", // EastRing 07-11 inner
  84. "436-EHV-0-8012-0002", // EastRing 12 outer
  85. "436-EHV-0-8112-0002", // EastRing 12 inner
  86. "436-EHV-0-8013-0002", // EastRing 13 outer
  87. "436-EHV-0-8113-0002", // EastRing 13 inner
  88. "436-EHV-0-8014-0002", // EastRing 14 outer
  89. "436-EHV-0-8114-0002", // EastRing 14 inner
  90. "436-EHV-0-8015-0002", // EastRing 15 outer
  91. "436-EHV-0-8115-0002", // EastRing 15 inner
  92. "436-EHV-0-8016-0002", // EastRing 16 outer
  93. "436-EHV-0-8116-0002", // EastRing 16 inner
  94. // ^............ means this is a HV relation number (8 = east)
  95. // ^........... means outer (=0) or inner (=1) layer
  96. // ^.......... means source (=0) or detector (=1) side
  97. // ^......... means ring index (0..6), counted from central segment
  98. ) ,
  99. );
  100. $relationTargetNumbers = array(
  101. array( // IE offset channels West side
  102. // 22 rows: 3x7 offset channels + IE common
  103. "436-EHV-0-0003-0102", // WestCh 01
  104. "436-EHV-0-0003-0202", // WestCh 02
  105. "436-EHV-0-0003-0302", // WestCh 03
  106. "436-EHV-0-0003-0402", // WestCh 04
  107. "436-EHV-0-0003-0502", // WestCh 05
  108. "436-EHV-0-0003-0602", // WestCh 06
  109. "436-EHV-0-0003-0702", // WestCh 07
  110. "436-EHV-0-0005-0102", // WestCh 08
  111. "436-EHV-0-0005-0202", // WestCh 09
  112. "436-EHV-0-0005-0302", // WestCh 10
  113. "436-EHV-0-0005-0402", // WestCh 11
  114. "436-EHV-0-0005-0502", // WestCh 12
  115. "436-EHV-0-0005-0602", // WestCh 13
  116. "436-EHV-0-0005-0702", // WestCh 14
  117. "436-EHV-0-0007-0102", // WestCh 15
  118. "436-EHV-0-0007-0202", // WestCh 16
  119. "436-EHV-0-0007-0302", // WestCh 17
  120. "436-EHV-0-0007-0402", // WestCh 18
  121. "436-EHV-0-0007-0502", // WestCh 19
  122. "436-EHV-0-0007-0602", // WestCh 20
  123. "436-EHV-0-0007-0702", // WestCh 21
  124. "436-EHV-0-1003-0002"
  125. // IE common
  126. ) ,
  127. array( // IE offset channels East side
  128. // 22 rows: 3x7 offset channels + IE common
  129. "436-EHV-0-0004-0102", // EastCh 01
  130. "436-EHV-0-0004-0202", // EastCh 02
  131. "436-EHV-0-0004-0302", // EastCh 03
  132. "436-EHV-0-0004-0402", // EastCh 04
  133. "436-EHV-0-0004-0502", // EastCh 05
  134. "436-EHV-0-0004-0602", // EastCh 06
  135. "436-EHV-0-0004-0702", // EastCh 07
  136. "436-EHV-0-0006-0102", // EastCh 08
  137. "436-EHV-0-0006-0202", // EastCh 09
  138. "436-EHV-0-0006-0302", // EastCh 10
  139. "436-EHV-0-0006-0402", // EastCh 11
  140. "436-EHV-0-0006-0502", // EastCh 12
  141. "436-EHV-0-0006-0602", // EastCh 13
  142. "436-EHV-0-0006-0702", // EastCh 14
  143. "436-EHV-0-0008-0102", // EastCh 15
  144. "436-EHV-0-0008-0202", // EastCh 16
  145. "436-EHV-0-0008-0302", // EastCh 17
  146. "436-EHV-0-0008-0402", // EastCh 18
  147. "436-EHV-0-0008-0502", // EastCh 19
  148. "436-EHV-0-0008-0602", // EastCh 20
  149. "436-EHV-0-0008-0702", // EastCh 21
  150. "436-EHV-0-1003-0002"
  151. // IE common
  152. ) ,
  153. );
  154. function flatten(array $array) {
  155. $return = array();
  156. array_walk_recursive($array, function ($a) use (&$return) {
  157. $return[] = $a;
  158. });
  159. return $return;
  160. }
  161. // set time zone to local German time
  162. date_default_timezone_set("Europe/Berlin");
  163. $counter = 0;
  164. $calltime = time();
  165. do {
  166. $errorcount = 0;
  167. $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>";
  168. $starttime = time();
  169. echo "Polling database." . PHP_EOL;
  170. /// TODO (1) get current relations for HV rings (KATRIN number mapping)
  171. /// TODO (2) write channel mapping to file used by monitor ("patchpanel/data/activeIEconfig.txt")
  172. /// => (a) the file contains one line per IE channel (44 total: 11 rings x inner/outer x west/east)
  173. /// => (b) each line contains the power supply index (values 0 .. 21; 22 = through IE)
  174. /// TODO (3) resolve ADEI channels for each relation (?)
  175. /// TODO (4) update ADEIchannels array (?)
  176. // new kdb.kaas.kit.edu added and optimized readout parameters
  177. $fileip = file_get_contents("https://kdb.kaas.kit.edu/kdb-api.fcgi/json?relations&kn=" . join(",", flatten($relationKatrinNumbers)) . "&only_recent=1", false, $context);
  178. //show_status($id+1,sizeof($adeiChannel));
  179. if (substr($fileip, 0, 5) == "ERROR") {
  180. $errorcount++;
  181. $errormessage = $errormessage . "Error querying database<br>";
  182. }
  183. if ($errorcount > 0) {
  184. echo $errormessage;
  185. }
  186. $json = json_decode($fileip, true);
  187. var_dump($json);
  188. $patchpanelMap = array();
  189. if ($json && $json["result"]) {
  190. foreach ($json["result"]["Relation"] as $kn => $data) {
  191. $target = $data["AssociateNumber"];
  192. $patchpanelMap[$kn] = $target;
  193. echo "\t" . $kn . " => " . $target . PHP_EOL;
  194. }
  195. }
  196. // increse loop counter
  197. $counter++;
  198. $stoptime = time();
  199. $wait_till = $starttime + 11;
  200. if ($stoptime - $starttime > 10) {
  201. $errormessage .= "ERROR: delayed readout " . ($stoptime - $starttime) . " s ";
  202. $errorcount++;
  203. }
  204. echo "Writing patchpanel database data to file." . PHP_EOL;
  205. $strFileContent = "";
  206. for ($eastwest = 0;$eastwest < 2;$eastwest++) {
  207. // loop over each half of the array
  208. foreach ($relationKatrinNumbers[$eastwest] as $kn) {
  209. $index = 22; // default to IE common
  210. if (array_key_exists($kn, $patchpanelMap)) {
  211. $target = $patchpanelMap[$kn];
  212. $index = array_search($target, $relationTargetNumbers[$eastwest]);
  213. }
  214. $strFileContent .= $index . "\n";
  215. }
  216. }
  217. // write ADEI data to file
  218. if (file_exists("patchpanel/data/activeIEconfig.txt")) {
  219. rename("patchpanel/data/activeIEconfig.txt", "patchpanel/data/activeIEconfig.txt.bak");
  220. }
  221. $file_w = fopen("patchpanel/data/activeIEconfig.txt", "w+");
  222. //sleep(5);
  223. fwrite($file_w, $strFileContent);
  224. fclose($file_w);
  225. // write log file
  226. $log_message = "";
  227. $log_message = date(DATE_RFC850) . ": ADEI data polled: loop " . $counter;
  228. $log_message .= ", poll time " . ($stoptime - $starttime) . " s";
  229. if ($stoptime - $starttime > 10) {
  230. $log_message .= ", delayed readout";
  231. }
  232. $log_message .= PHP_EOL;
  233. $file_log = fopen("logs/poll_adei_data.log", "a");
  234. fwrite($file_log, $log_message);
  235. fclose($file_log);
  236. //echo "Loop ".$counter." done.".PHP_EOL;
  237. if ($wait_till <= $stoptime) {
  238. sleep(3);
  239. }
  240. else {
  241. time_sleep_until($wait_till);
  242. }
  243. } while ($counter < 10 && time() < $calltime + 51);
  244. //echo "All loops done.".PHP_EOL;
  245. ?>