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

📄 setup_alarm.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";include_once "layout.php";define('LOCALHOST', '127.0.0.1');  /* 127.0.0.1 is better than localhost */define('LADD_PORT', 8888);define('DB_USER', '');define('LATCH_TYPE', "Latch Type");define('CONTACT_TYPE', "Contact Type");define('NORMALLY_CLOSED', "Normally Closed");define('NORMALLY_OPEN', "Normally Open");class Zone {  var $id;  var $name;  var $enabled;  var $alarm;  var $latching;  var $edge;     // an edge of 1 means "normally closed"  function Zone($id,$name,$enabled, $alarm, $latching, $edge) {    $this->id = $id;    $this->name = $name;    $this->enabled = $enabled;    $this->alarm = $alarm;    $this->latching = $latching;    $this->edge = $edge;  }}/*************************************************************** * readZoneStatus():  Read the status of the alarm daemon and  * return an array of Zone structures.   * * Input:   An already opened connection to the alarm daemon.  * Return:  NULL          - on error  *          array of Zone - if successful  * Output:  An array of error messages if NULL is returned.  * Effects: none  **************************************************************/function readZoneStatus($connection, &$error) {  $command = "SELECT id, name, enabled, alarm, latching, edge FROM Zone";  $result = pg_exec($connection, $command);  if (!$result) {     $error[] = "SQL command failed. Connected to \"" . LOCALHOST . "\" on port " . LADD_PORT . ".";      return NULL;   }  $status = array();  for ($row = 0; $row < pg_NumRows($result); $row++) {    $status[] = new Zone(      pg_result($result, $row, 0),  /* id */      pg_result($result, $row, 1),  /* name */      pg_result($result, $row, 2),  /* enabled */      pg_result($result, $row, 3),  /* alarm */      pg_result($result, $row, 4),  /* latching */      pg_result($result, $row, 5)); /* edge */  }  return $status;}/* Reads the required zone config from web form.   * Returns an array of Zone structures. * Return NULL if the user did not do an update. */function read_ui_zone_status() {  $status = array();  $params = read_params();  for ($id = 1; $id <= 5; $id++) {    if (!array_key_exists("Name_" . $id, $params))       return NULL;    if (!array_key_exists("Enabled_" . $id, $params))       return NULL;    if (!array_key_exists("Latching_" . $id, $params))       return NULL;    if (!array_key_exists("Edge_" . $id, $params))       return NULL;       $zone = new Zone(      $id,  /* id */      $params["Name_" . $id],   /* name */      (strcasecmp($params["Enabled_" . $id], "Enabled") == 0)? 1 : 0,   /* enabled */      -1,   /* alarm */      (strcasecmp($params["Latching_" . $id], "Latching") == 0)? 1 : 0,   /* latching */      (strcasecmp($params["Edge_" . $id], NORMALLY_CLOSED) == 0)? 1 : 0);   /* edge */    $status[$id] = $zone;  }  return $status;}/* returns -1 on error.   * The statusArray is an array of Zones.  The zone->alarm is not used.   */function set_zone_status($connection, $statusArray, &$error) {  $someError = false;   for ($id = 1; $id <= 5; $id++) {    $zone = $statusArray[$id];    $command = "UPDATE Zone SET name=\"$zone->name\", enabled=$zone->enabled, latching=$zone->latching, edge=$zone->edge WHERE id=$id";    $result = pg_exec($connection, $command);    if (!$result) {      $error[] = "SQL Command failed: $command";       $someError = true;    } else {      pg_freeresult($result);    }  }  return $someError? -1 : 0;} // make common functionfunction displayErrorMsg($error) {  $widget = "<h2>Error</h2>";  foreach ($error as $msg) {      $widget .= "$msg <br>\n";  }  display_page("Setup", "Alarm", $widget);}function generate_id_html($zone) {  return "<td> $zone->id </td>"; }function generate_name_html($zone) {  return "<td> <input type=text name=Name_$zone->id value=\"$zone->name\" /> </td>"; }function generate_enabled_html($zone) {  // Mozilla doesn't seem to support selected.  // IE does.  if ($zone->enabled)     return "<td> <select name=Enabled_$zone->id> <option selected>Enabled</option> <option>Disabled</option> </select> </td>";  else    return "<td> <select name=Enabled_$zone->id> <option selected>Disabled</option> <option>Enabled</option> </select> </td>";} function generate_latching_html($zone) {  if ($zone->latching)     return "<td> <select name=Latching_$zone->id> <option selected>Latching</option> <option>Non-latching</option> </select> </td>";  else    return "<td> <select name=Latching_$zone->id> <option selected>Non-Latching</option> <option>Latching</option> </select> </td>";} function generate_edge_html($zone) {  // An edge of 1 means "normally closed".   if ($zone->edge)     return "<td> <select name=Edge_$zone->id> <option selected>" . NORMALLY_CLOSED . "</option> <option>" . NORMALLY_OPEN . "</option> </select> </td>";  else     return "<td> <select name=Edge_$zone->id> <option selected>" . NORMALLY_OPEN. "</option> <option>" . NORMALLY_CLOSED . "</option> </select> </td>";} function displayZoneForm($statusArray) {$tableHeader = "<h2>Setup Alarm Zones</h2>        <form action=\"setup_alarm.php\" method=post >        <table>          <tr>            <th>              Zone            </th>            <th>              Name            </th>            <th>              State            </th>            <th>" .               LATCH_TYPE .	    "</th>            <th>" .               CONTACT_TYPE .            "</th>          </tr>"; $tableFooter = <<< TABLE_FOOTER        </table>        <p>          <input type="submit" name="Update" value="Update">        </p>        </form>TABLE_FOOTER;  $widget = $tableHeader;   foreach ($statusArray as $zone) {    if ($zone->enabled && $zone->alarm)       $status = 'Alarm';    else if ($zone->enabled)        $status = 'Safe';    else       $status = 'Inactive';    $idHtml = generate_id_html($zone);    $nameHtml = generate_name_html($zone);    $enabledHtml = generate_enabled_html($zone);    $latchingHtml = generate_latching_html($zone);    $edgeHtml = generate_edge_html($zone);    switch ($status) {    case 'Alarm':      $widget .= "<tr class=menu2-color> " . $idHtml . $nameHtml . $enabledHtml .  $latchingHtml . $edgeHtml . "</tr>\n";      break;    default:      $widget .= "<tr> " . $idHtml . $nameHtml . $enabledHtml .  $latchingHtml . $edgeHtml . "</tr>\n";    }  }  $widget .= $tableFooter;   display_page("Setup", "Alarm", $widget);}/* Main function */$error = array();if (!function_exists('pg_connect')) {     $error[] = "The pg_connect() function is not defined.";      $error[] = "This is probably because PHP on the web server was not configured with the --with-pgsql option.";      displayErrorMsg($error);      return; }/* Suppress Postgres error messages */error_reporting(error_reporting() & 0xFFFD);$connection = pg_connect(LOCALHOST, LADD_PORT, DB_USER);if (!$connection) {  $error[] = "Unable to connect to daemon.  Attempted to connect to \"" . LOCALHOST . "\" on port " . LADD_PORT . ".";   displayErrorMsg($error);   return;}/* Read the new zone setting from UI form */$statusArray = read_ui_zone_status();if ($statusArray)  set_zone_status($connection, $statusArray, $error); $statusArray = readZoneStatus($connection, $error);if (!$statusArray)   displayErrorMsg($error); else  displayZoneForm($statusArray); ?>

⌨️ 快捷键说明

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