SHARE
TWEET

Untitled

a guest Nov 13th, 2017 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // Configuration -------------------
  3. require_once('./config.php');
  4. require_once('./functions.php');
  5.  
  6. // Get URL data
  7. //$apikey = filter_input( INPUT_GET, 'apikey', FILTER_SANITIZE_URL ); // Get the URL thingy
  8. $node_id_strIN = filter_input( INPUT_GET, 'nodeid', FILTER_SANITIZE_URL );
  9. $addressIN= filter_input( INPUT_GET, 'address', FILTER_SANITIZE_URL ); // Get the URL thingy
  10. $deviceIN= filter_input( INPUT_GET, 'device', FILTER_SANITIZE_URL ); // Get the URL thingy
  11. $parameterIN= filter_input( INPUT_GET, 'parameter', FILTER_SANITIZE_URL ); // Get the URL thingy
  12. $charttypeIN= filter_input( INPUT_GET, 'charttype', FILTER_SANITIZE_URL );
  13.  
  14. //Get the limit
  15. $limitIN= filter_input( INPUT_GET, 'limit', FILTER_SANITIZE_URL );
  16.  
  17. $node_id_str = preg_replace("/[^A-Za-z0-9 ]/", '', $node_id_strIN);
  18. $address = preg_replace("/[^A-Za-z0-9 ]/", '', $addressIN );
  19. $device = preg_replace("/[^A-Za-z0-9 ]/", '', $deviceIN );
  20. $parameter = preg_replace("/[^A-Za-z0-9 ]/", '', $parameterIN );
  21. $charttype= preg_replace("/[^A-Za-z0-9 ]/", '', $charttypeIN);
  22. $limit= preg_replace("/[^A-Za-z0-9 ]/", '', $limitIN);
  23.  
  24. if ($limit<=0) $limit = 1000;
  25.  
  26. // Create connection
  27. $conn = new mysqli($mysql_host,$mysql_username,$mysql_password,$mysql_dbname);
  28.  
  29. // Check connection
  30. if ($conn->connect_error)
  31. {
  32.     die("Connection failed: " . $conn->connect_error);
  33. }
  34.  
  35.  
  36. //$sql = 'SELECT * FROM nodes WHERE ApiKey="'.$apikey.'"';
  37.  
  38. $node_id = intval($node_id_str);
  39. $sql = 'SELECT * FROM nodes WHERE ID="'.$node_id.'"';
  40. $result = $conn->query($sql);
  41.  
  42. if ($result->num_rows > 0)
  43. {
  44.     // output data
  45.     $row = mysqli_fetch_assoc($result);
  46.     $node_ident = $row["ID"];  // numeric unique identifier
  47.     //$node_id = $row["NodeID"];
  48.     $node_desc = $row["Description"];
  49.     $adr_val = intval($address);
  50.     $sql = sprintf('SELECT Timestamp, Value FROM dogodki WHERE Node="%d" AND Address="%d" AND Device="%s" AND Parameter="%s" ORDER BY Timestamp DESC LIMIT %d',$node_ident, $adr_val, $device, $parameter, $limit);
  51.                    //"VALUES (NULL ,CURRENT_TIMESTAMP , '%d', '%s', '%s', '%s', '%s');",$node_ident,$address,$device,$parameter,$value);
  52.     $result = $conn->query($sql);
  53.    
  54.     $rownum=1;
  55.  
  56.     /* Process results */
  57.     if (mysqli_num_rows($result) > 0)
  58.     {
  59.     // output data of each row
  60.       $rows = array();  
  61.       $jsonarray = array();
  62.       while($row = mysqli_fetch_assoc($result))
  63.       {      
  64.         $jsonarray[] = $row;
  65.         //$jsondate = date("Y,m,d,G,i,s", strtotime($row["Timestamp"]));
  66.         //var_dump($jsondate);
  67.         $rowdate = date_parse ( $row["Timestamp"]);
  68.         $year = $rowdate['year'];
  69.         $month = $rowdate['month']-1;
  70.         $day = $rowdate['day'];
  71.         $hour = $rowdate['hour'];
  72.         $minute = $rowdate['minute'];
  73.         $second = $rowdate['second'];
  74.         $jsnodate = sprintf("%d,%d,%d,%d,%d,%d" , $year , $month , $day , $hour , $minute , $second);
  75.         //$jsnodate = $year.",".$month.",".$day.",".$hour.",".$minute.",".$second;
  76.         //var_dump($jsnodate );
  77.         //$jsondate = date("Y,m,d,G,i,s", strtotime($strjsnodate));
  78.         $y = floatval($row["Value"]);
  79.         $rows[] = array("Date.UTC(" . $jsnodate . ")", $y);  
  80.         if ($rownum==1)
  81.         {
  82.           $rownum = 0;
  83.           $lasttimestamp =  sprintf("%d.%d.%d %d:%d:%d" , $day , $month+1 , $year , $hour , $minute , $second);
  84.           $lastreadout   =  $y;
  85.         }
  86.       }
  87.     } else {
  88.       echo "0 results";
  89.     }
  90. } else
  91. {
  92.     echo "0 IDs";
  93. }
  94.  
  95. /* Create response */
  96. $json = json_encode($jsonarray);
  97. $johanson = json_encode(array_reverse($rows), JSON_PRETTY_PRINT);
  98. $correct_json_for_chart = str_replace('"', '', $johanson);  // chart yoom ne dela, ce je DESC order!!!
  99.  
  100. $conn->close();
  101. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top