⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wait_for_status.php

📁 Linux嵌入式设计配套光盘,学习嵌入式设计可参考
💻 PHP
字号:
<?php/********************************************************************  * Copyright (c) 2006, Graham P Phillips * All rights reserved. * * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met: * *   * Redistributions of source code must retain the above copyright  *     notice, this list of conditions and the following disclaimer. *   * Redistributions in binary form must reproduce the above  *     copyright notice, this list of conditions and the following  *     disclaimer in the documentation and/or other materials provided  *     with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. ********************************************************************/include_once "php_params.php";define('LOCALHOST', '127.0.0.1');define('LADD_PORT', 8888);define('LOGMUXD_PORT', 8887);define('STATUS_CHANGE_PORT', 4444);define('MAX_WAIT_FOR_UPDATE', 10); // in seconds// This program gets the 'ID' of what is currently being// displayed on the browser window from the POST data.  If// what is currently being displayed corresponds to the most// recent events (logs), then it opens a connection to the// logger and wait for another event.  If what is displayed// is not the most recent event, then it returns immediately// with the latest Laddie status and logs. You may change // MAX_WAIT_FOR_UPDATE above to change the periodic automatic update rate.    $params = read_params();  $disp_id = array_key_exists('disp_id', $params)? $params['disp_id'] : -1;  $curr_id = get_log_count(LOCALHOST,LOGMUXD_PORT);   /* If curr_id is negative, it means that we were unable to get the current   * log count from logmuxd.  This also means that our AJAX mechanism will    * not work properly. In this case, to avoid the client and server from    * exchanging AJAX messages very quickly, we'll handle this case the same    * way as when curr_id is equal to disp_id.    */  if ($disp_id === $curr_id || $curr_id < 0) {    $fp = fsockopen(LOCALHOST, STATUS_CHANGE_PORT, $errno, $errstr, 30);    if ($fp) {      /* block for at most MAX_WAIT_FOR_UPDATE seconds */      stream_set_timeout($fp, MAX_WAIT_FOR_UPDATE);      $ignore_response = fgets($fp, 128);      fclose($fp);    } else {      sleep(MAX_WAIT_FOR_UPDATE);    }    $curr_id = get_log_count(LOCALHOST,LOGMUXD_PORT);  }  header("Content-Type: text/xml");  print("<?xml version=\"1.0\" ?>\n");  print("<laddie_status>\n");  // Suppress Postgres error messages  error_reporting(error_reporting() & 0xFFFD);  // connect to the database   $connection = pg_connect(LOCALHOST, LADD_PORT, "");  if ($connection == "") {     print("</laddie_status>\n");    exit();  }  // Get and format status for all zones  $command = "SELECT id, name, enabled, alarm FROM Zone";  $result = pg_exec($connection, $command);  if ($result == "") {     print("</laddie_status>\n");    exit();  }  for($row = 0; $row < pg_NumRows($result); $row++)  {    $id    = pg_result($result, $row, 0);    $name  = pg_result($result, $row, 1);    $enabled = pg_result($result, $row, 2);    $alarm   = pg_result($result, $row, 3);    printf("<zone id=\"%s\" name=\"%s\" enabled=\"%s\">", $id, $name, $enabled);    printf("%s</zone>\n", $alarm);  }  // free the result for the status, close conn to Ladd  pg_freeresult($result);  pg_close($connection);  // Give current log count  printf("<logcount>%d</logcount>\n", $curr_id);  // connect to the RTA interface on the logger  $connection = pg_connect(LOCALHOST, LOGMUXD_PORT, "");  if ($connection == "") {     print("</laddie_status>\n");    exit();  }  // Get and display the most recent log messages  // The number 5 is arbitrary, based on how many log  // messages the web UI wishes to display.   $command = "SELECT log FROM TblDest LIMIT 5";  $result = pg_exec($connection, $command);  if ($result == "") {     pg_close($connection);    print("</laddie_status>\n");    exit();  }  // print each log message as a <log>  $nrow = pg_NumRows($result);  for($row = 0; $row < $nrow; $row++)  {    $log = pg_result($result, $row, 0);    print("<log>$log</log>\n");  }  // free the result for the log display  pg_freeresult($result);  pg_close($connection);  print("</laddie_status>\n");?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -