Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.39 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>ALM</title>
  6. </head>
  7.  
  8. <body>
  9. <?php
  10.  
  11. date_default_timezone_set("UTC");
  12. $tstamp = time();
  13. $InsertTime = date("Y-m-d H:i:s",$tstamp);
  14.  
  15. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  16. {
  17. $data = json_decode(file_get_contents("php://input"));
  18.  
  19. if(!empty($data->TRID))
  20. {
  21.  
  22. $servername = "";
  23. $username = "";
  24. $password = "";
  25. $dbname = "".$data->TRID;
  26.  
  27. // Create connection
  28. $conn = new mysqli($servername, $username, $password, $dbname);
  29.  
  30. // Check connection
  31. if ($conn->connect_error) {
  32. die("Connection failed: " . $conn->connect_error);
  33. }
  34.  
  35. if(!empty($data->MESSAGE_ID))
  36. {
  37. echo "(ID:".$data->MESSAGE_ID.")";
  38. }else
  39. {
  40. echo "(ID:0)";
  41. }
  42.  
  43. if($data->POST_TYPE == "TX_ALM")
  44. {
  45.  
  46. $sql1 = "SELECT * FROM TBL_CM_DEFINITIONS ORDER BY OUTPUT_ID ASC";
  47. $result1 = $conn->query($sql1);
  48.  
  49. $index1 = 0;
  50. $OutputTitle[$index1] = "---";
  51. $index1++;
  52.  
  53. while($row1 = $result1->fetch_assoc())
  54. {
  55. $OutputTitle[$index1] = $row1["OUTPUT_TITLE"];
  56. $index1++;
  57. }
  58.  
  59. $recordcount=0;
  60.  
  61. $COMMS_MODIFIED = 0;
  62. $LCA_MODIFIED = 0;
  63. $HCA_MODIFIED = 0;
  64. $LRA_MODIFIED = 0;
  65. $HRA_MODIFIED = 0;
  66. $MAINS_MODIFIED = 0;
  67. $DOOR_MODIFED = 0;
  68. $SURGE_MODIFIED = 0;
  69.  
  70. $arrlength1=count($data->OUTPUT_ID);
  71. for($index1 = 0; $index1 < $arrlength1; $index1++)
  72. {
  73. $sql1 = "SELECT * FROM TBL_ALM_HISTORY WHERE OUTPUT_ID = ".$data->OUTPUT_ID[$index1]."";
  74. $result1 = $conn->query($sql1);
  75.  
  76. $rowcount = 0;
  77.  
  78. while($row1 = $result1->fetch_assoc())
  79. {
  80.  
  81. // check for a change to the alarm history
  82.  
  83. if($data->OUTPUT_ID[$index1] == 0)
  84. {
  85. // compare digital input status
  86.  
  87. if((($data->ALARM_STATUS[$index1]&0x1000)>>12) != $row1["MAINS"])
  88. {
  89. $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
  90. $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
  91. $ALM_Description[$rowcount] = "Mains Fail Alarm";
  92. if($row1["MAINS"])
  93. $ALM_Status[$rowcount] = "Inactive";
  94. else
  95. $ALM_Status[$rowcount] = "Active";
  96. $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
  97. $MAINS_MODIFIED = 1;
  98. $rowcount++;
  99. }
  100.  
  101. if((($data->ALARM_STATUS[$index1]&0x2000)>>13) != $row1["DOOR"])
  102. {
  103. $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
  104. $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
  105. $ALM_Description[$rowcount] = "Door Open Alarm";
  106. if($row1["DOOR"])
  107. $ALM_Status[$rowcount] = "Inactive";
  108. else
  109. $ALM_Status[$rowcount] = "Active";
  110. $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
  111. $DOOR_MODIFIED = 1;
  112. $rowcount++;
  113. }
  114.  
  115. if((($data->ALARM_STATUS[$index1]&0x4000)>>14) != $row1["SURGE"])
  116. {
  117. $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
  118. $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
  119. $ALM_Description[$rowcount] = "Surge Suppressor Alarm";
  120. if($row1["SURGE"])
  121. $ALM_Status[$rowcount] = "Inactive";
  122. else
  123. $ALM_Status[$rowcount] = "Active";
  124. $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
  125. $SURGE_MODIFIED = 1;
  126. $rowcount++;
  127. }
  128. }else // end outputID = 0
  129. {
  130. // OutputID does not equal zero
  131.  
  132. if($data->COMMS_STATUS[$index1] != $row1["COMMS_STATUS"])
  133. {
  134. $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
  135. $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
  136. $ALM_Description[$rowcount] = "Control Module Not Responding";
  137. if($row1["COMMS_STATUS"])
  138. $ALM_Status[$rowcount] = "Active";
  139. else
  140. $ALM_Status[$rowcount] = "Inactive";
  141. $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
  142. $COMMS_MODIFIED = 1;
  143. $rowcount++;
  144. }
  145. if((($data->ALARM_STATUS[$index1]&0x0100)>>8) != $row1["LCA"])
  146. {
  147. $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
  148. $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
  149. $ALM_Description[$rowcount] = "Low Current Alarm";
  150. if($row1["LCA"])
  151. $ALM_Status[$rowcount] = "Inactive";
  152. else
  153. $ALM_Status[$rowcount] = "Active";
  154. $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
  155. $LCA_MODIFIED = 1;
  156. $rowcount++;
  157. }
  158. if((($data->ALARM_STATUS[$index1]&0x0200)>>9) != $row1["HCA"])
  159. {
  160. $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
  161. $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
  162. $ALM_Description[$rowcount] = "High Current Alarm";
  163. if($row1["HCA"])
  164. $ALM_Status[$rowcount] = "Inactive";
  165. else
  166. $ALM_Status[$rowcount] = "Active";
  167. $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
  168. $HCA_MODIFIED = 1;
  169. $rowcount++;
  170. }
  171. if((($data->ALARM_STATUS[$index1]&0x0400)>>10) != $row1["LRA"])
  172. {
  173. $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
  174. $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
  175. $ALM_Description[$rowcount] = "Low Reference Alarm";
  176. if($row1["LRA"])
  177. $ALM_Status[$rowcount] = "Inactive";
  178. else
  179. $ALM_Status[$rowcount] = "Active";
  180. $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
  181. $LRA_MODIFIED = 1;
  182. $rowcount++;
  183. }
  184. if((($data->ALARM_STATUS[$index1]&0x0800)>>11) != $row1["HRA"])
  185. {
  186. $ALM_Timestamp[$rowcount] = $data->TIMESTAMP[$index1];
  187. $ALM_OutputTitle[$rowcount] = $OutputTitle[$data->OUTPUT_ID[$index1]];
  188. $ALM_Description[$rowcount] = "High Reference Alarm";
  189. if($row1["HRA"])
  190. $ALM_Status[$rowcount] = "Inactive";
  191. else
  192. $ALM_Status[$rowcount] = "Active";
  193. $ALM_Indicator[$rowcount] = $data->ALARM_STATUS[$index1];
  194. $HRA_MODIFIED = 1;
  195. $rowcount++;
  196. }
  197. } // end outputID not zero
  198. } // end table alarm history query
  199.  
  200. // update alarm history table
  201.  
  202. // prepare and bind
  203. $stmt = $conn->prepare("UPDATE TBL_ALM_HISTORY SET COMMS_STATUS=?, LCA=?, HCA=?, LRA=?, HRA=?, MAINS=?, DOOR=?, SURGE=? WHERE OUTPUT_ID=?");
  204. $stmt->bind_param("ddddddddd", $ALM_CS, $ALM_LCA, $ALM_HCA, $ALM_LRA, $ALM_HRA, $ALM_MAINS, $ALM_DOOR, $ALM_SURGE, $ALM_OID);
  205.  
  206. $ALM_CS = $data->COMMS_STATUS[$index1];
  207. $ALM_LCA = (($data->ALARM_STATUS[$index1]&0x0100)>>8);
  208. $ALM_HCA = (($data->ALARM_STATUS[$index1]&0x0200)>>9);
  209. $ALM_LRA = (($data->ALARM_STATUS[$index1]&0x0400)>>10);
  210. $ALM_HRA = (($data->ALARM_STATUS[$index1]&0x0800)>>11);
  211. $ALM_MAINS = (($data->ALARM_STATUS[$index1]&0x1000)>>12);
  212. $ALM_DOOR = (($data->ALARM_STATUS[$index1]&0x2000)>>13);
  213. $ALM_SURGE = (($data->ALARM_STATUS[$index1]&0x4000)>>14);
  214. $ALM_OID = $data->OUTPUT_ID[$index1];
  215.  
  216. $stmt->execute();
  217.  
  218.  
  219. for($index2=0;$index2<$rowcount;$index2++)
  220. {
  221. $stmt = $conn->prepare("INSERT INTO TBL_ALM_DATA (TIME, OUTPUT_TITLE, ALARM_DESCRIPTION, ALARM_STATUS, ALARM_INDICATOR) VALUES (?,?,?,?,?)");
  222. $stmt->bind_param("ssssd", $ALM_TS, $ALM_OT, $ALM_D, $ALM_ST, $ALM_IND);
  223.  
  224. $ALM_TS = $ALM_Timestamp[$index2];
  225. $ALM_OT = $ALM_OutputTitle[$index2];
  226. $ALM_D = $ALM_Description[$index2];
  227. $ALM_ST = $ALM_Status[$index2];
  228. $ALM_IND = $ALM_Indicator[$index2];
  229.  
  230. $stmt->execute();
  231. }
  232.  
  233. //create email entries
  234.  
  235. for($index2=0;$index2<$rowcount;$index2++)
  236. {
  237. $EMAIL_TIMESTAMP[$recordcount] = $ALM_Timestamp[$index2];
  238. $EMAIL_OUTPUT_TITLE[$recordcount] = $ALM_OutputTitle[$index2];
  239. $EMAIL_DESCRIPTION[$recordcount] = $ALM_Description[$index2];
  240. $EMAIL_STATUS[$recordcount] = $ALM_Status[$index2];
  241. $EMAIL_INDICATOR[$recordcount] = $ALM_Indicator[$index2];
  242. $recordcount++;
  243. }
  244. } // end alarm record query - move to next instance of alarm
  245.  
  246. // start generate email
  247.  
  248. // determine site name and TRU name
  249.  
  250. if($recordcount>0) // if registry entries exist
  251. {
  252.  
  253. $sql3 = "SELECT TRU_Name, Site_ID FROM corros10_global.tru WHERE TRU_ID='ce".$data->TRID."'";
  254. $result3 = $conn->query($sql3);
  255. while($row3 = $result3->fetch_assoc())
  256. {
  257. $TRU_Name = $row3["TRU_Name"];
  258. $Site_ID = $row3["Site_ID"];
  259. }
  260.  
  261. $sql3 = "SELECT Site_Name FROM corros10_global.sites WHERE Site_ID=".$Site_ID."";
  262. $result3 = $conn->query($sql3);
  263. while($row3 = $result3->fetch_assoc())
  264. {
  265. $Site_Name = $row3["Site_Name"];
  266. }
  267.  
  268. // Create a list of users to send emails to
  269.  
  270. $sql2 = "SELECT * FROM TBL_ALM_USERS WHERE 1";
  271. $result2 = $conn->query($sql2);
  272.  
  273. // send applicable emails
  274.  
  275. if ($result2->num_rows > 0)
  276. {
  277. while($row2 = $result2->fetch_assoc()) // start processing each user
  278. {
  279. // determine if email should be sent to the selected user
  280.  
  281. $sendemail = 0;
  282.  
  283. if($MAINS_MODIFIED && $row2["MAINS"])
  284. $sendemail = 1;
  285. if($DOOR_MODIFIED && $row2["DOOR"])
  286. $sendemail = 1;
  287. if($SURGE_MODIFIED && $row2["SURGE"])
  288. $sendemail = 1;
  289. if($LCA_MODIFIED && $row2["LCA"])
  290. $sendemail = 1;
  291. if($HCA_MODIFIED && $row2["HCA"])
  292. $sendemail = 1;
  293. if($LRA_MODIFIED && $row2["LRA"])
  294. $sendemail = 1;
  295. if($HRA_MODIFIED && $row2["HRA"])
  296. $sendemail = 1;
  297. if($COMMS_MODIFIED && $row2["COMMS_STATUS"])
  298. $sendemail = 1;
  299.  
  300. if($sendemail)
  301. {
  302. $to = $row2["EMAIL"];
  303. $subject = "Corrosion Electronics RMCS Alert";
  304. $headers = "MIME-Version: 1.0" . "\r\n";
  305. $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  306. $headers .= "From: rmcs@corrosionelectronics.com.au" . "\r\n";
  307. $message = "
  308. <html>
  309. <head>
  310. <title>CE RMCS</title>
  311. <style media='screen' type='text/css'>
  312. body {
  313. background-color: #f9f9f9;
  314. }
  315. p, a, h1,h2,h3 {
  316. font-family: Arial, Helvetica, sans-serif;
  317. }
  318. th {
  319. font-family: Arial, Helvetica, sans-serif;
  320. font-size: 0.8em;
  321. background: #666;
  322. color: #FFF;
  323. padding: 10px 25px 10px 25px;
  324. border-collapse: separate;
  325. border: 1px solid #000;
  326. }
  327. td {
  328. font-family: Arial, Helvetica, sans-serif;
  329. font-size: 0.8em;
  330. border: 1px solid #DDD;
  331. text-align:center;
  332. vertical-align:central;
  333. padding: 5px 25px 5px 25px;
  334. }
  335. </style>
  336. </head>
  337. <body>
  338. <h3>Corrosion Electronics Remote Monitoring and Control System - Automated Notification</h3>
  339. <table>
  340. <tr>
  341. <th>Site Name</td>
  342. <th>TRU Name</th>
  343. </tr>
  344. <td>".$Site_Name."</td>
  345. <td>".$TRU_Name."</td>
  346. </tr>
  347. </table>";
  348. $message .= "
  349. </br>
  350. <p>The following alarm registry entries have recently been added.</p>
  351. </br>
  352. <table>
  353. <tr>
  354. <th>Time</th>
  355. <th>Control Module</th>
  356. <th>Alarm Description</th>
  357. <th>Alarm Status</th>
  358. </tr>
  359. ";
  360.  
  361. for($index1=0;$index1<$recordcount;$index1++) // start inserting table row
  362. {
  363. $addentry-0;
  364.  
  365. if($EMAIL_DESCRIPTION[$index1]=="Low Current Alarm" && $row2["LCA"])
  366. $addentry=1;
  367. if($EMAIL_DESCRIPTION[$index1]=="High Current Alarm" && $row2["HCA"])
  368. $addentry=1;
  369. if($EMAIL_DESCRIPTION[$index1]=="Low Reference Alarm" && $row2["LRA"])
  370. $addentry=1;
  371. if($EMAIL_DESCRIPTION[$index1]=="High Reference Alarm" && $row2["HRA"])
  372. $addentry=1;
  373. if($EMAIL_DESCRIPTION[$index1]=="Control Module Not Responding" && $row2["COMMS_STATUS"])
  374. $addentry=1;
  375. if($EMAIL_DESCRIPTION[$index1]=="Mains Fail Alarm" && $row2["MAINS"])
  376. $addentry=1;
  377. if($EMAIL_DESCRIPTION[$index1]=="Door Open Alarm" && $row2["DOOR"])
  378. $addentry=1;
  379. if($EMAIL_DESCRIPTION[$index1]=="Surge Suppressor Alarm" && $row2["SURGE"])
  380. $addentry=1;
  381.  
  382. if($addentry)
  383. {
  384. $message .= "
  385. <tr>
  386. <td>".$EMAIL_TIMESTAMP[$index1]."</td>
  387. <td>".$EMAIL_OUTPUT_TITLE[$index1]."</td>
  388. <td>".$EMAIL_DESCRIPTION[$index1]."</td>
  389. <td>".$EMAIL_STATUS[$index1]."</td>
  390. </tr>";
  391. }
  392. } // end inserting table row
  393.  
  394. $message .= "
  395. </table>";
  396. $message .= "
  397. </br>
  398. <p>Please log onto the RMCS website for more information..</p>
  399. </br>
  400. <a href='https://rmcs.corrosionelectronics.com.au' >rmcs.corrosionelectronics.com.au</a>
  401. </body>
  402. </html>
  403. ";
  404.  
  405. mail($to,$subject,$message,$headers);
  406.  
  407. } // end if sendemail
  408. } // goto processing next each user
  409. } // end sending emails
  410. } // end if registry entries exist
  411.  
  412. // end generate email
  413.  
  414. } // end post type = TX_ALM
  415. } // end data empty if statement
  416. }
  417.  
  418. ?>
  419.  
  420. </body>
  421. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement