poll_adei_data.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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) return;
  6. if (empty($start_time)) $start_time = time();
  7. $now = time();
  8. $perc = (double)($done / $total);
  9. $bar = floor($perc * $size);
  10. $status_bar = "\r[";
  11. $status_bar .= str_repeat("=", $bar);
  12. if ($bar < $size) {
  13. $status_bar .= ">";
  14. $status_bar .= str_repeat(" ", $size - $bar);
  15. }
  16. else {
  17. $status_bar .= "=";
  18. }
  19. $disp = number_format($perc * 100, 0);
  20. $status_bar .= "] $disp% $done/$total";
  21. $rate = ($now - $start_time) / $done;
  22. $left = $total - $done;
  23. $eta = round($rate * $left, 2);
  24. $elapsed = $now - $start_time;
  25. $status_bar .= " remaining: " . number_format($eta) . " sec. elapsed: " . number_format($elapsed) . " sec.";
  26. echo "$status_bar ";
  27. flush();
  28. // when done, send a newline
  29. if ($done == $total) {
  30. echo "\n";
  31. }
  32. }
  33. // include channel list
  34. include "SDS_status_channels.php";
  35. $username = getenv('PHP_ADEI_USERNAME');
  36. $password = getenv('PHP_ADEI_PASSWORD');
  37. $context = stream_context_create(array(
  38. 'http' => array(
  39. 'header' => "Authorization: Basic " . base64_encode("$username:$password") ,
  40. "Connection: close\r\n"
  41. )
  42. ));
  43. // set time zone to local German time
  44. date_default_timezone_set('Europe/Berlin');
  45. $counter = 0;
  46. $calltime = time();
  47. do {
  48. $errorcount = 0;
  49. $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>';
  50. $starttime = time();
  51. //echo "Polling ADEI data.".PHP_EOL;
  52. for ($id = 0;$id < sizeof($adeiChannel);$id++) {
  53. $adeiChannels = "";
  54. for ($id2 = 0;$id2 < sizeof($adeiChannel[$id]);$id2++) {
  55. $adeiChannels = $adeiChannels . $adeiChannel[$id][$id2] . ",";
  56. }
  57. $adeiChannels = substr($adeiChannels, 0, strlen($adeiChannels) - 1);
  58. //$fileip = file_get_contents("http://katrin.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=50&format=csv", false, $context);
  59. //$fileip = file_get_contents("http://ipekatrinbackupserv1.ipe.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=60&format=csv", false, $context);
  60. // $fileip = file_get_contents("http://katrin.kit.edu/adei-katrin/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=50&format=csv", false, $context);
  61. // new adei-katrin.kaas.kit.edu added
  62. //$fileip = file_get_contents("http://adei-katrin.kaas.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=50&format=csv", false, $context);
  63. // new adei-katrin.kaas.kit.edu added and optimized readout parameters
  64. $fileip = file_get_contents("https://adei-katrin.kaas.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&rt=full&window=3600,-1&format=csv", false, $context);
  65. //show_status($id+1,sizeof($adeiChannel));
  66. if (substr($fileip, 0, 5) == "ERROR") {
  67. $errorcount++;
  68. $errormessage = $errormessage . "Error loading ADEI channel " . $id . "<br>";
  69. }
  70. for ($id2 = sizeof($adeiChannel[$id]) - 1;$id2 > - 1;$id2--) {
  71. $pos = strrpos($fileip, ",");
  72. $adeiValue[$id][$id2] = floatval(substr($fileip, $pos + 2));
  73. $fileip = substr($fileip, 0, $pos);
  74. //echo $id . " " . $id2 . " " . $adeiValue[$id][$id2] . PHP_EOL;
  75. }
  76. }
  77. if ($errorcount > 0) echo $errormessage;
  78. // increse loop counter
  79. $counter++;
  80. $stoptime = time();
  81. $wait_till = $starttime + 11;
  82. if ($stoptime - $starttime > 10) {
  83. $errormessage .= "ERROR: delayed readout " . ($stoptime - $starttime) . " s ";
  84. $errorcount++;
  85. }
  86. //echo "Writing ADEI data to file.".PHP_EOL;
  87. $strFileContent = "<" . "?php" . PHP_EOL . "$" . "adeiValue = " . var_export($adeiValue, true) . ";" . PHP_EOL . PHP_EOL;
  88. $strFileContent .= "$" . "extraction_time = '" . date(DATE_RFC850) . "';" . PHP_EOL . PHP_EOL;
  89. $strFileContent .= "$" . "extraction_timestamp = " . time() . ";" . PHP_EOL . PHP_EOL;
  90. $strFileContent .= "$" . "errorcount = " . $errorcount . ";" . PHP_EOL . PHP_EOL;
  91. $strFileContent .= "$" . "errormessage = '" . $errormessage . "';" . PHP_EOL . PHP_EOL;
  92. $strFileContent .= "$" . "valuesincluded = 1;" . PHP_EOL . PHP_EOL;
  93. $strFileContent .= "?" . ">" . PHP_EOL;
  94. // write ADEI data to file
  95. $file_w = fopen('ADEIvalues.php', 'w+');
  96. //sleep(5);
  97. fwrite($file_w, $strFileContent);
  98. fclose($file_w);
  99. // write log file
  100. $log_message = "";
  101. $log_message = date(DATE_RFC850) . ": ADEI data polled: loop " . $counter;
  102. $log_message .= ", poll time " . ($stoptime - $starttime) . " s";
  103. if ($stoptime - $starttime > 10) $log_message .= ", delayed readout";
  104. $log_message .= PHP_EOL;
  105. $file_log = fopen('logs/poll_adei_data.log', 'a');
  106. fwrite($file_log, $log_message);
  107. fclose($file_log);
  108. //echo "Loop ".$counter." done.".PHP_EOL;
  109. if ($wait_till <= $stoptime) sleep(3);
  110. else time_sleep_until($wait_till);
  111. }
  112. while (($counter < 10) && (time() < $calltime + 51));
  113. //echo "All loops done.".PHP_EOL;
  114. ?>