poll_database.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // ** Katrin numbers referring to IE electrode segments ** //
  42. $relationKatrinNumbers = array(
  43. array( // 22 rows: 2x11 electrode rings (inner+outer)
  44. "434-EEL-3-0210-0002", // WestRing 02 outer
  45. "434-EEL-3-0211-0002", // WestRing 02 inner
  46. "434-EEL-3-0310-0002", // WestRing 03 outer
  47. "434-EEL-3-0311-0002", // WestRing 03 inner
  48. "434-EEL-3-0410-0002", // WestRing 04 outer
  49. "434-EEL-3-0411-0002", // WestRing 04 inner
  50. "434-EEL-3-0510-0002", // WestRing 05 outer
  51. "434-EEL-3-0511-0002", // WestRing 05 inner
  52. "434-EEL-3-0610-0002", // WestRing 06 outer
  53. "434-EEL-3-0611-0002", // WestRing 06 inner
  54. "434-EEL-3-0710-0002", // WestRing 07-11 outer
  55. "434-EEL-3-0711-0002", // WestRing 07-11 inner
  56. "434-EEL-3-1210-0002", // WestRing 12 outer
  57. "434-EEL-3-1211-0002", // WestRing 12 inner
  58. "434-EEL-3-1310-0002", // WestRing 13 outer
  59. "434-EEL-3-1311-0002", // WestRing 13 inner
  60. "434-EEL-3-1410-0002", // WestRing 14 outer
  61. "434-EEL-3-1411-0002", // WestRing 14 inner
  62. "434-EEL-3-1510-0002", // WestRing 15 outer
  63. "434-EEL-3-1511-0002", // WestRing 15 inner
  64. "434-EEL-3-1610-0002", // WestRing 16 outer
  65. "434-EEL-3-1611-0002", // WestRing 16 inner
  66. // ^^............ means ring index (02..16)
  67. // ^........... means west (=1) or east (=2) dipole
  68. // ^.......... means outer (=0) or inner (=1) layer
  69. ) ,
  70. array( // 22 rows: 2x11 electrode rings (inner+outer)
  71. "434-EEL-3-0220-0002", // EastRing 02 outer
  72. "434-EEL-3-0221-0002", // EastRing 02 inner
  73. "434-EEL-3-0320-0002", // EastRing 03 outer
  74. "434-EEL-3-0321-0002", // EastRing 03 inner
  75. "434-EEL-3-0420-0002", // EastRing 04 outer
  76. "434-EEL-3-0421-0002", // EastRing 04 inner
  77. "434-EEL-3-0520-0002", // EastRing 05 outer
  78. "434-EEL-3-0521-0002", // EastRing 05 inner
  79. "434-EEL-3-0620-0002", // EastRing 06 outer
  80. "434-EEL-3-0621-0002", // EastRing 06 inner
  81. "434-EEL-3-0720-0002", // EastRing 07-11 outer
  82. "434-EEL-3-0721-0002", // EastRing 07-11 inner
  83. "434-EEL-3-1220-0002", // EastRing 12 outer
  84. "434-EEL-3-1221-0002", // EastRing 12 inner
  85. "434-EEL-3-1320-0002", // EastRing 13 outer
  86. "434-EEL-3-1321-0002", // EastRing 13 inner
  87. "434-EEL-3-1420-0002", // EastRing 14 outer
  88. "434-EEL-3-1421-0002", // EastRing 14 inner
  89. "434-EEL-3-1520-0002", // EastRing 15 outer
  90. "434-EEL-3-1521-0002", // EastRing 15 inner
  91. "434-EEL-3-1620-0002", // EastRing 16 outer
  92. "434-EEL-3-1621-0002", // EastRing 16 inner
  93. // ^^............ means ring index (02..16)
  94. // ^........... means west (=1) or east (=2) dipole
  95. // ^.......... means outer (=0) or inner (=1) layer
  96. ) ,
  97. );
  98. // ** Katrin numbers referring to IE power supply channels ** //
  99. $relationTargetNumbers = array(
  100. array( // IE offset channels West side
  101. // 22 rows: 3x7 offset channels + IE common
  102. "436-EHV-0-0003-0102", // WestCh 00 / not used
  103. "436-EHV-0-0003-0202", // WestCh 01 / not used
  104. "436-EHV-0-0003-0302", // WestCh 02 / Patch 00
  105. "436-EHV-0-0003-0402", // WestCh 03 / Patch 01
  106. "436-EHV-0-0003-0502", // WestCh 04 / Patch 02
  107. "436-EHV-0-0003-0602", // WestCh 05 / Patch 03
  108. "436-EHV-0-0003-0702", // WestCh 06 / Patch 04
  109. "436-EHV-0-0003-0802", // WestCh 07 / Patch 05
  110. "436-EHV-0-0005-0102", // WestCh 08 / Patch 06
  111. "436-EHV-0-0005-0202", // WestCh 09 / Patch 07
  112. "436-EHV-0-0005-0302", // WestCh 10 / Patch 08
  113. "436-EHV-0-0005-0402", // WestCh 11 / Patch 09
  114. "436-EHV-0-0005-0502", // WestCh 12 / Patch 10
  115. "436-EHV-0-0005-0602", // WestCh 13 / Patch 11
  116. "436-EHV-0-0005-0702", // WestCh 14 / Patch 12
  117. "436-EHV-0-0005-0802", // WestCh 15 / Patch 13
  118. "436-EHV-0-0007-0102", // WestCh 16 / Patch 14
  119. "436-EHV-0-0007-0202", // WestCh 16 / Patch 15
  120. "436-EHV-0-0007-0302", // WestCh 17 / Patch 16
  121. "436-EHV-0-0007-0402", // WestCh 18 / Patch 17
  122. "436-EHV-0-0007-0502", // WestCh 19 / Patch 18
  123. "436-EHV-0-0007-0602", // WestCh 20 / Patch 19
  124. "436-EHV-0-0007-0702", // WestCh 21 / Patch 20
  125. "436-EHV-0-0007-0802", // WestCh 22 / Patch 21
  126. "436-EHV-0-0002-0101", // IE dipole west
  127. "436-EHV-0-1003-0002", // IE common
  128. ) ,
  129. array( // IE offset channels East side
  130. // 22 rows: 3x7 offset channels + IE common
  131. "436-EHV-0-0004-0102", // EastCh 00 / not used
  132. "436-EHV-0-0004-0202", // EastCh 01 / not used
  133. "436-EHV-0-0004-0302", // EastCh 02 / Patch 00
  134. "436-EHV-0-0004-0402", // EastCh 03 / Patch 01
  135. "436-EHV-0-0004-0502", // EastCh 04 / Patch 02
  136. "436-EHV-0-0004-0602", // EastCh 05 / Patch 03
  137. "436-EHV-0-0004-0702", // EastCh 06 / Patch 04
  138. "436-EHV-0-0004-0802", // EastCh 07 / Patch 05
  139. "436-EHV-0-0006-0102", // EastCh 08 / Patch 06
  140. "436-EHV-0-0006-0202", // EastCh 09 / Patch 07
  141. "436-EHV-0-0006-0302", // EastCh 10 / Patch 08
  142. "436-EHV-0-0006-0402", // EastCh 11 / Patch 09
  143. "436-EHV-0-0006-0502", // EastCh 12 / Patch 10
  144. "436-EHV-0-0006-0602", // EastCh 13 / Patch 11
  145. "436-EHV-0-0006-0702", // EastCh 14 / Patch 12
  146. "436-EHV-0-0006-0802", // EastCh 15 / Patch 13
  147. "436-EHV-0-0008-0102", // EastCh 16 / Patch 14
  148. "436-EHV-0-0008-0202", // EastCh 16 / Patch 15
  149. "436-EHV-0-0008-0302", // EastCh 17 / Patch 16
  150. "436-EHV-0-0008-0402", // EastCh 18 / Patch 17
  151. "436-EHV-0-0008-0502", // EastCh 19 / Patch 18
  152. "436-EHV-0-0008-0602", // EastCh 20 / Patch 19
  153. "436-EHV-0-0008-0702", // EastCh 21 / Patch 20
  154. "436-EHV-0-0008-0802", // EastCh 22 / Patch 21
  155. "436-EHV-0-0002-0102", // IE dipole east
  156. "436-EHV-0-1003-0002", // IE common
  157. ) ,
  158. );
  159. function flatten(array $array) {
  160. $return = array();
  161. array_walk_recursive($array, function ($a) use (&$return) {
  162. $return[] = $a;
  163. });
  164. return $return;
  165. }
  166. // set time zone to local German time
  167. date_default_timezone_set("Europe/Berlin");
  168. // build HTTP context
  169. $context = stream_context_create([
  170. 'http' => [
  171. 'header' => "Authorization: Basic " . base64_encode("$username:$password") . "\r\n" .
  172. "Connection: close" . "\r\n",
  173. ],
  174. ]);
  175. $KDB_API_URL = "https://kdb-test.kaas.kit.edu/kdb-api.fcgi/json";
  176. //$KDB_API_URL = "http://localhost:9999/json";
  177. $counter = 0;
  178. $calltime = time();
  179. // do {
  180. $errorcount = 0;
  181. $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>";
  182. $starttime = time();
  183. //echo "Polling database." . PHP_EOL;
  184. // new kdb.kaas.kit.edu added and optimized readout parameters
  185. $fileip = file_get_contents($KDB_API_URL . "?relations&kn=" . join(",", flatten($relationKatrinNumbers)) . "&only_recent=1", false, $context);
  186. //show_status($id+1,sizeof($adeiChannel));
  187. if (substr($fileip, 0, 5) == "ERROR") {
  188. $errorcount++;
  189. $errormessage = $errormessage . "Error querying database<br>";
  190. }
  191. $json = json_decode($fileip, true);
  192. //echo "Database result (JSON):" . PHP_EOL;
  193. //var_dump($json);
  194. $patchpanelMap = array();
  195. if ($json && $json["result"]) {
  196. foreach ($json["result"]["Relation"] as $kn => $data) {
  197. $target = $data["AssociateNumber"];
  198. $patchpanelMap[$kn] = $target;
  199. //echo "\t" . $kn . " => " . $target . PHP_EOL;
  200. }
  201. }
  202. // increse loop counter
  203. $counter++;
  204. $stoptime = time();
  205. // $wait_till = $starttime + 11;
  206. if ($stoptime - $starttime > 10) {
  207. $errormessage .= "ERROR: delayed readout " . ($stoptime - $starttime) . " s ";
  208. $errorcount++;
  209. }
  210. //echo "Writing patchpanel database data to file." . PHP_EOL;
  211. $strFileContent = "";
  212. $fname = "patchpanel/data/activeIEconfig.txt";
  213. $offset = 2;
  214. assert( sizeof($relationKatrinNumbers) == 2 );
  215. assert( sizeof($relationTargetNumbers) == 2 );
  216. for ($eastwest = 0; $eastwest < 2; $eastwest++) {
  217. // loop over each half of the array
  218. assert( sizeof($relationKatrinNumbers[$eastwest]) == 22 );
  219. assert( sizeof($relationTargetNumbers[$eastwest]) >= 22 );
  220. foreach ($relationKatrinNumbers[$eastwest] as $kn) {
  221. $index = 'IE'; // default to IE common
  222. $target = "(default)";
  223. if (array_key_exists($kn, $patchpanelMap)) {
  224. $target = $patchpanelMap[$kn];
  225. if ($target == end($relationTargetNumbers[$eastwest])) { // last entry = IE common
  226. $index = 'IE';
  227. }
  228. else {
  229. $index = array_search($target, $relationTargetNumbers[$eastwest]) + $offset;
  230. }
  231. }
  232. echo "\t" . $kn . " => " . $target . " [" . $index . "]" . PHP_EOL;
  233. $strFileContent .= $index . "\n";
  234. }
  235. }
  236. // write ADEI data to file
  237. if (file_exists($fname)) {
  238. rename($fname, $fname . ".bak");
  239. }
  240. $file_w = fopen($fname, "w+");
  241. //sleep(5);
  242. if ($file_w) {
  243. fwrite($file_w, $strFileContent);
  244. fclose($file_w);
  245. }
  246. // write log file
  247. $log_message = "";
  248. $log_message = date(DATE_RFC850) . ": database polled: loop " . $counter;
  249. $log_message .= ", poll time " . ($stoptime - $starttime) . " s";
  250. if ($stoptime - $starttime > 10) {
  251. $log_message .= ", delayed readout";
  252. }
  253. $log_message .= PHP_EOL;
  254. $file_log = fopen("logs/poll_database.log", "a");
  255. fwrite($file_log, $log_message);
  256. fclose($file_log);
  257. if ($errorcount > 0) {
  258. echo $errormessage;
  259. }
  260. // //echo "Loop ".$counter." done.".PHP_EOL;
  261. // if ($wait_till <= $stoptime) {
  262. // sleep(3);
  263. // }
  264. // else {
  265. // time_sleep_until($wait_till);
  266. // }
  267. // } while ($counter < 10 && time() < $calltime + 51);
  268. //echo "All loops done.".PHP_EOL;
  269. ?>