get_monitor_table.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. include("./connect_sql.php");
  3. $json = file_get_contents('../../r03/display_info.json');
  4. $display_info = json_decode($json, true);
  5. $type = "";
  6. if (isset($_GET["monitor"])) {
  7. $monitor = $_GET["monitor"];
  8. $type = explode('-', $monitor)[0];
  9. /*if ($type == "SIS" || $type == "SID") {
  10. $monitor .= "A";
  11. }*/
  12. }
  13. $table = [];
  14. $unit = "";
  15. $haveDepth = false;
  16. $haveDirection = false;
  17. $key = array_search($type, array_column($display_info, 'type'));
  18. if ($key !== false) {
  19. $display = $display_info[$key]["display"];
  20. $unit = $display_info[$key]["unit"];
  21. $haveDepth = $display_info[$key]["haveDepth"];
  22. $haveDirection = $display_info[$key]["haveDirection"];
  23. if ($haveDepth) {
  24. //$display .= '],[Depth';
  25. $sql = " SELECT [Date],count(DISTINCT [Depth])
  26. FROM [BIMMonitor].[dbo].[{$type}_Data] WHERE [EquipmentID] = '{$monitor}' GROUP BY [Date]";
  27. $stmt = sqlsrv_query($conn, $sql);
  28. $depth = "";
  29. while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
  30. $depthCount = $row[1];
  31. }
  32. for ($i = 1; $i < $depthCount; $i++) {
  33. $depth = $depth . ",[" . $i * 0.5 . "]";
  34. }
  35. $sql = " SELECT *
  36. FROM (
  37. SELECT [Date],[TotalDisplacement],[Depth]
  38. FROM [BIMMonitor].[dbo].[{$type}_Data] WHERE [EquipmentID] = '{$monitor}'
  39. ) o
  40. PIVOT (
  41. MAX([TotalDisplacement])
  42. FOR [Depth] IN ([TOP] ${depth})
  43. ) n;";
  44. } else if ($haveDirection) {
  45. $sql = " SELECT *
  46. FROM (
  47. SELECT [Date],[Direction],[TotalTilt]
  48. FROM [BIMMonitor].[dbo].[{$type}_Data] WHERE [EquipmentID] like '%{$monitor}%'
  49. ) o
  50. PIVOT (
  51. MAX([TotalTilt])
  52. FOR [Direction] IN ([1-3], [2-4])
  53. ) n;";
  54. } else {
  55. $sql = "SELECT [Date],[{$display}] FROM [{$type}_Data] WHERE [EquipmentID] = '{$monitor}';";
  56. }
  57. $stmt = sqlsrv_query($conn, $sql);
  58. while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
  59. array_push($table, $row);
  60. }
  61. if ($stmt === false) {
  62. if (($errors = sqlsrv_errors()) != null) {
  63. foreach ($errors as $error) {
  64. echo "SQLSTATE: " . $error['SQLSTATE'] . "<br />";
  65. echo "code: " . $error['code'] . "<br />";
  66. echo "message: " . $error['message'] . "<br />";
  67. echo ($sql . "<br />");
  68. var_dump($row);
  69. }
  70. }
  71. }
  72. }
  73. $ajax["table"] = $table;
  74. $ajax["unit"] = $unit;
  75. $ajax["haveDepth"] = $haveDepth;
  76. $ajax["haveDirection"] = $haveDirection;
  77. echo (json_encode($ajax));