update_database.php 12 KB

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