Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>ALM</title>
- </head>
- <body>
- <?php
- date_default_timezone_set("UTC");
- $tstamp = time();
- $InsertTime = date("Y-m-d H:i:s",$tstamp);
- if ($_SERVER['REQUEST_METHOD'] == 'POST')
- {
- $data = json_decode(file_get_contents("php://input"));
- if(!empty($data->TRID))
- {
- $servername = "";
- $username = "";
- $password = "";
- $dbname = "".$data->TRID;
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- if(!empty($data->MESSAGE_ID))
- {
- echo "(ID:".$data->MESSAGE_ID.")";
- }else
- {
- echo "(ID:0)";
- }
- if($data->POST_TYPE == "TX_ALM")
- {
- $sql1 = "SELECT * FROM TBL_CM_DEFINITIONS ORDER BY OUTPUT_ID ASC";
- $result1 = $conn->query($sql1);
- $index1 = 0;
- $OutputTitle[$index1] = "---";
- $index1++;
- while($row1 = $result1->fetch_assoc())
- {
- $OutputTitle[$index1] = $row1["OUTPUT_TITLE"];
- $index1++;
- }
- $recordcount=0;
- $COMMS_MODIFIED = 0;
- $LCA_MODIFIED = 0;
- $HCA_MODIFIED = 0;
- $LRA_MODIFIED = 0;
- $HRA_MODIFIED = 0;
- $MAINS_MODIFIED = 0;
- $DOOR_MODIFED = 0;
- $SURGE_MODIFIED = 0;
- $arrlength1=count($data->OUTPUT_ID);
- for($index1 = 0; $index1 < $arrlength1; $index1++)
- {
- $sql1 = "SELECT * FROM TBL_ALM_HISTORY WHERE OUTPUT_ID = ".$data->OUTPUT_ID[$index1]."";
- $result1 = $conn->query($sql1);
- $rowcount = 0;
- while($row1 = $result1->fetch_assoc())
- {
- // check for a change to the alarm history
- if($data->OUTPUT_ID[$index1] == 0)
- {
- // compare digital input status
- if((($data->ALARM_STATUS[$index1]&0x1000)>>12) != $row1["MAINS"])
- {
- $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
- $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
- $ALM_Description[$rowcount] = "Mains Fail Alarm";
- if($row1["MAINS"])
- $ALM_Status[$rowcount] = "Inactive";
- else
- $ALM_Status[$rowcount] = "Active";
- $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
- $MAINS_MODIFIED = 1;
- $rowcount++;
- }
- if((($data->ALARM_STATUS[$index1]&0x2000)>>13) != $row1["DOOR"])
- {
- $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
- $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
- $ALM_Description[$rowcount] = "Door Open Alarm";
- if($row1["DOOR"])
- $ALM_Status[$rowcount] = "Inactive";
- else
- $ALM_Status[$rowcount] = "Active";
- $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
- $DOOR_MODIFIED = 1;
- $rowcount++;
- }
- if((($data->ALARM_STATUS[$index1]&0x4000)>>14) != $row1["SURGE"])
- {
- $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
- $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
- $ALM_Description[$rowcount] = "Surge Suppressor Alarm";
- if($row1["SURGE"])
- $ALM_Status[$rowcount] = "Inactive";
- else
- $ALM_Status[$rowcount] = "Active";
- $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
- $SURGE_MODIFIED = 1;
- $rowcount++;
- }
- }else // end outputID = 0
- {
- // OutputID does not equal zero
- if($data->COMMS_STATUS[$index1] != $row1["COMMS_STATUS"])
- {
- $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
- $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
- $ALM_Description[$rowcount] = "Control Module Not Responding";
- if($row1["COMMS_STATUS"])
- $ALM_Status[$rowcount] = "Active";
- else
- $ALM_Status[$rowcount] = "Inactive";
- $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
- $COMMS_MODIFIED = 1;
- $rowcount++;
- }
- if((($data->ALARM_STATUS[$index1]&0x0100)>>8) != $row1["LCA"])
- {
- $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
- $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
- $ALM_Description[$rowcount] = "Low Current Alarm";
- if($row1["LCA"])
- $ALM_Status[$rowcount] = "Inactive";
- else
- $ALM_Status[$rowcount] = "Active";
- $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
- $LCA_MODIFIED = 1;
- $rowcount++;
- }
- if((($data->ALARM_STATUS[$index1]&0x0200)>>9) != $row1["HCA"])
- {
- $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
- $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
- $ALM_Description[$rowcount] = "High Current Alarm";
- if($row1["HCA"])
- $ALM_Status[$rowcount] = "Inactive";
- else
- $ALM_Status[$rowcount] = "Active";
- $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
- $HCA_MODIFIED = 1;
- $rowcount++;
- }
- if((($data->ALARM_STATUS[$index1]&0x0400)>>10) != $row1["LRA"])
- {
- $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
- $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
- $ALM_Description[$rowcount] = "Low Reference Alarm";
- if($row1["LRA"])
- $ALM_Status[$rowcount] = "Inactive";
- else
- $ALM_Status[$rowcount] = "Active";
- $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
- $LRA_MODIFIED = 1;
- $rowcount++;
- }
- if((($data->ALARM_STATUS[$index1]&0x0800)>>11) != $row1["HRA"])
- {
- $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
- $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
- $ALM_Description[$rowcount] = "High Reference Alarm";
- if($row1["HRA"])
- $ALM_Status[$rowcount] = "Inactive";
- else
- $ALM_Status[$rowcount] = "Active";
- $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
- $HRA_MODIFIED = 1;
- $rowcount++;
- }
- } // end outputID not zero
- } // end table alarm history query
- // update alarm history table
- // prepare and bind
- $stmt = $conn->prepare("UPDATE TBL_ALM_HISTORY SET COMMS_STATUS=?, LCA=?, HCA=?, LRA=?, HRA=?, MAINS=?, DOOR=?, SURGE=? WHERE OUTPUT_ID=?");
- $stmt->bind_param("ddddddddd", $ALM_CS, $ALM_LCA, $ALM_HCA, $ALM_LRA, $ALM_HRA, $ALM_MAINS, $ALM_DOOR, $ALM_SURGE, $ALM_OID);
- $ALM_CS = $data->COMMS_STATUS[$index1];
- $ALM_LCA = (($data->ALARM_STATUS[$index1]&0x0100)>>8);
- $ALM_HCA = (($data->ALARM_STATUS[$index1]&0x0200)>>9);
- $ALM_LRA = (($data->ALARM_STATUS[$index1]&0x0400)>>10);
- $ALM_HRA = (($data->ALARM_STATUS[$index1]&0x0800)>>11);
- $ALM_MAINS = (($data->ALARM_STATUS[$index1]&0x1000)>>12);
- $ALM_DOOR = (($data->ALARM_STATUS[$index1]&0x2000)>>13);
- $ALM_SURGE = (($data->ALARM_STATUS[$index1]&0x4000)>>14);
- $ALM_OID = $data->OUTPUT_ID[$index1];
- $stmt->execute();
- for($index2=0;$index2<$rowcount;$index2++)
- {
- $stmt = $conn->prepare("INSERT INTO TBL_ALM_DATA (TIME, OUTPUT_TITLE, ALARM_DESCRIPTION, ALARM_STATUS, ALARM_INDICATOR) VALUES (?,?,?,?,?)");
- $stmt->bind_param("ssssd", $ALM_TS, $ALM_OT, $ALM_D, $ALM_ST, $ALM_IND);
- $ALM_TS = $ALM_Timestamp[$index2];
- $ALM_OT = $ALM_OutputTitle[$index2];
- $ALM_D = $ALM_Description[$index2];
- $ALM_ST = $ALM_Status[$index2];
- $ALM_IND = $ALM_Indicator[$index2];
- $stmt->execute();
- }
- //create email entries
- for($index2=0;$index2<$rowcount;$index2++)
- {
- $EMAIL_TIMESTAMP[$recordcount] = $ALM_Timestamp[$index2];
- $EMAIL_OUTPUT_TITLE[$recordcount] = $ALM_OutputTitle[$index2];
- $EMAIL_DESCRIPTION[$recordcount] = $ALM_Description[$index2];
- $EMAIL_STATUS[$recordcount] = $ALM_Status[$index2];
- $EMAIL_INDICATOR[$recordcount] = $ALM_Indicator[$index2];
- $recordcount++;
- }
- } // end alarm record query - move to next instance of alarm
- // start generate email
- // determine site name and TRU name
- if($recordcount>0) // if registry entries exist
- {
- $sql3 = "SELECT TRU_Name, Site_ID FROM corros10_global.tru WHERE TRU_ID='ce".$data->TRID."'";
- $result3 = $conn->query($sql3);
- while($row3 = $result3->fetch_assoc())
- {
- $TRU_Name = $row3["TRU_Name"];
- $Site_ID = $row3["Site_ID"];
- }
- $sql3 = "SELECT Site_Name FROM corros10_global.sites WHERE Site_ID=".$Site_ID."";
- $result3 = $conn->query($sql3);
- while($row3 = $result3->fetch_assoc())
- {
- $Site_Name = $row3["Site_Name"];
- }
- // Create a list of users to send emails to
- $sql2 = "SELECT * FROM TBL_ALM_USERS WHERE 1";
- $result2 = $conn->query($sql2);
- // send applicable emails
- if ($result2->num_rows > 0)
- {
- while($row2 = $result2->fetch_assoc()) // start processing each user
- {
- // determine if email should be sent to the selected user
- $sendemail = 0;
- if($MAINS_MODIFIED && $row2["MAINS"])
- $sendemail = 1;
- if($DOOR_MODIFIED && $row2["DOOR"])
- $sendemail = 1;
- if($SURGE_MODIFIED && $row2["SURGE"])
- $sendemail = 1;
- if($LCA_MODIFIED && $row2["LCA"])
- $sendemail = 1;
- if($HCA_MODIFIED && $row2["HCA"])
- $sendemail = 1;
- if($LRA_MODIFIED && $row2["LRA"])
- $sendemail = 1;
- if($HRA_MODIFIED && $row2["HRA"])
- $sendemail = 1;
- if($COMMS_MODIFIED && $row2["COMMS_STATUS"])
- $sendemail = 1;
- if($sendemail)
- {
- $to = $row2["EMAIL"];
- $subject = "Corrosion Electronics RMCS Alert";
- $headers = "MIME-Version: 1.0" . "\r\n";
- $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
- $headers .= "From: rmcs@corrosionelectronics.com.au" . "\r\n";
- $message = "
- <html>
- <head>
- <title>CE RMCS</title>
- <style media='screen' type='text/css'>
- body {
- background-color: #f9f9f9;
- }
- p, a, h1,h2,h3 {
- font-family: Arial, Helvetica, sans-serif;
- }
- th {
- font-family: Arial, Helvetica, sans-serif;
- font-size: 0.8em;
- background: #666;
- color: #FFF;
- padding: 10px 25px 10px 25px;
- border-collapse: separate;
- border: 1px solid #000;
- }
- td {
- font-family: Arial, Helvetica, sans-serif;
- font-size: 0.8em;
- border: 1px solid #DDD;
- text-align:center;
- vertical-align:central;
- padding: 5px 25px 5px 25px;
- }
- </style>
- </head>
- <body>
- <h3>Corrosion Electronics Remote Monitoring and Control System - Automated Notification</h3>
- <table>
- <tr>
- <th>Site Name</td>
- <th>TRU Name</th>
- </tr>
- <td>".$Site_Name."</td>
- <td>".$TRU_Name."</td>
- </tr>
- </table>";
- $message .= "
- </br>
- <p>The following alarm registry entries have recently been added.</p>
- </br>
- <table>
- <tr>
- <th>Time</th>
- <th>Control Module</th>
- <th>Alarm Description</th>
- <th>Alarm Status</th>
- </tr>
- ";
- for($index1=0;$index1<$recordcount;$index1++) // start inserting table row
- {
- $addentry-0;
- if($EMAIL_DESCRIPTION[$index1]=="Low Current Alarm" && $row2["LCA"])
- $addentry=1;
- if($EMAIL_DESCRIPTION[$index1]=="High Current Alarm" && $row2["HCA"])
- $addentry=1;
- if($EMAIL_DESCRIPTION[$index1]=="Low Reference Alarm" && $row2["LRA"])
- $addentry=1;
- if($EMAIL_DESCRIPTION[$index1]=="High Reference Alarm" && $row2["HRA"])
- $addentry=1;
- if($EMAIL_DESCRIPTION[$index1]=="Control Module Not Responding" && $row2["COMMS_STATUS"])
- $addentry=1;
- if($EMAIL_DESCRIPTION[$index1]=="Mains Fail Alarm" && $row2["MAINS"])
- $addentry=1;
- if($EMAIL_DESCRIPTION[$index1]=="Door Open Alarm" && $row2["DOOR"])
- $addentry=1;
- if($EMAIL_DESCRIPTION[$index1]=="Surge Suppressor Alarm" && $row2["SURGE"])
- $addentry=1;
- if($addentry)
- {
- $message .= "
- <tr>
- <td>".$EMAIL_TIMESTAMP[$index1]."</td>
- <td>".$EMAIL_OUTPUT_TITLE[$index1]."</td>
- <td>".$EMAIL_DESCRIPTION[$index1]."</td>
- <td>".$EMAIL_STATUS[$index1]."</td>
- </tr>";
- }
- } // end inserting table row
- $message .= "
- </table>";
- $message .= "
- </br>
- <p>Please log onto the RMCS website for more information..</p>
- </br>
- <a href='https://rmcs.corrosionelectronics.com.au' >rmcs.corrosionelectronics.com.au</a>
- </body>
- </html>
- ";
- mail($to,$subject,$message,$headers);
- } // end if sendemail
- } // goto processing next each user
- } // end sending emails
- } // end if registry entries exist
- // end generate email
- } // end post type = TX_ALM
- } // end data empty if statement
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement