Advertisement
Guest User

Untitled

a guest
Feb 17th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. [
  2. [
  3. 1455628560,
  4. 2340
  5. ],
  6. [
  7. 1455628620,
  8. 2340
  9. ],
  10. ]
  11.  
  12. <?php
  13.  
  14. $sensorid='###';
  15. $token='###';
  16.  
  17. $interval='###'; #{hour, day, month, year, night}
  18. $unit='###'; #{watt, kwhperyear, eurperyear, audperyear, lpermin, lperday, m3peryear}
  19. $resolution='###'; # {minute, 15min, hour, day, week, month, year, decade, night}
  20.  
  21.  
  22. $db_user = "###";
  23. $db_pass = "###";
  24. $db_host = "###";
  25. $db_name = "###";
  26. $table = "###";
  27.  
  28.  
  29.  
  30.  
  31. class Flukso {
  32. private $sensorid, $token, $interval, $unit, $resolution;
  33. private $url='https://api.flukso.net/sensor/';
  34.  
  35. public function __construct($u_sensorid, $u_token, $u_interval, $u_unit, $u_resolution) {
  36. $this->sensorid = $u_sensorid;
  37. $this->token = $u_token;
  38. if (empty($u_interval)) {
  39. $this->interval = 'hour';
  40. } else {
  41. $this->interval = $u_interval;
  42. }
  43. if (empty($u_unit)) {
  44. $this->unit = 'watt';
  45. } else {
  46. $this->unit = $u_unit;
  47. }
  48. $this->resolution = $u_resolution;
  49. $this->getdata();
  50. }
  51.  
  52. private function getdata() {
  53. <a href="http://www.flukso.net/content/jsonrest-api-released<br />
  54. " title="http://www.flukso.net/content/jsonrest-api-released<br />
  55. ">http://www.flukso.net/content/jsonrest-api-released<br /></a>
  56. $header=array();
  57. $header[]="Accept: application/json";
  58. $header[]="X-Version: 1.0";
  59. $header[]='X-Token: '.$this->token;
  60.  
  61. $request=$this->url.$this->sensorid.'?interval='.$this->interval.'&unit='.$this->unit.'&resolution='.$this->resolution;
  62.  
  63. $ch=curl_init();
  64. curl_setopt($ch,CURLOPT_URL,$request);
  65. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  67. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  68. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  69. $this->data=curl_exec($ch);
  70. curl_close($ch);
  71.  
  72. }
  73. public function __toString() {
  74. return $this->data;
  75. }
  76.  
  77. }
  78.  
  79.  
  80.  
  81. $fluksodata = new Flukso($sensorid, $token, $interval, $unit, $resolution);
  82.  
  83. $fluksodata=json_decode($fluksodata);
  84.  
  85.  
  86.  
  87. $link = mysql_connect($db_host, $db_user, $db_pass)
  88. or die ("Error ".date('Y-m-d H:i:s')."Failed to connect to MySQL.n<b>A fatal MySQL error occured</b>.nQuery: " . $query . "nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
  89. mysql_select_db ($db_name)
  90. or die ("Error ".date('Y-m-d H:i:s')."Failed to connect to the database ".$db_name.".n<b>A fatal MySQL error occured</b>.nQuery: " . $query . "nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
  91.  
  92.  
  93. foreach($fluksodata as $value){
  94.  
  95.  
  96. if ( "$value[1]" != "nan" )
  97. {
  98.  
  99. $corrected_ts = $value[0] - 18000;
  100. $formatdatetime = date('Y-m-d H:i:s', $corrected_ts);
  101. $year = date('Y', $corrected_ts);
  102.  
  103. $number_days_this_year = date("z", mktime(0,0,0,12,31,$year)) + 1;
  104. $corrected_value = $value[1] / $number_days_this_year;
  105.  
  106. $query = "insert ignore into $table (ts, value) values ('$formatdatetime', $corrected_value)";
  107. $result = mysql_query ($query)
  108. or die ("Error ".date('Y-m-d H:i:s')."Failed to insert ".$table." record.nA fatal MySQL error occured.nQuery: " . $query . "nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
  109.  
  110. }
  111. }
  112.  
  113.  
  114.  
  115. mysql_close($link);
  116.  
  117. ?>
  118.  
  119. <?php
  120. $jsonStr = "[
  121. [
  122. 1455628560,
  123. 2340
  124. ],
  125. [
  126. 1455628620,
  127. 2340
  128. ]
  129. ]";
  130.  
  131.  
  132. $json = json_decode($jsonStr);
  133. echo "<pre>";
  134. var_dump($json);
  135. echo $json[0][0];
  136. echo "n";
  137. echo $json[1][0];
  138. echo "</pre>";
  139.  
  140. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement