1) Изменен Arduino-скетч для работы на Arduino Yun и более эффективной обработки...
[weathermon.git] / web / image.php
index 2a2d2f395415114b0e24efbce8f01c6f6c1980ff..eaa6fda004255c79172a12f139a50ff52755ef20 100644 (file)
@@ -16,6 +16,15 @@ if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_sche
 
 }  
 
+$supported = imagetypes();
+if( $supported & IMG_PNG )    $img_format="png";
+elseif( $supported & IMG_GIF ) $img_format="gif";
+elseif( $supported & IMG_JPG ) $img_format="jpeg";
+elseif( $supported & IMG_WBMP ) $img_format="wbmp";
+elseif( $supported & IMG_XPM ) $img_format="xpm";
+
+$cachefilename = NULL;
+
 $db -> exec('SET CHARACTER SET utf8');
   
 $type = $_REQUEST['type'];
@@ -77,9 +86,17 @@ if ($type and $param) {
     
   } elseif ($type == 'range') {
 
+    $curr = intval(date('YmdHis'));
+
     $from = intval($_REQUEST['fromdate']);
     $to = intval($_REQUEST['todate']);
 
+    if ($curr>$to) {
+    
+        $cachefilename='meteo.'.$sensor.'.'.$param.'.'.$from.'-'.$to.'.'.$img_format;
+    
+    }
+
     $q = $db -> prepare(
       'select unix_timestamp(timestamp) as x,unitconv(value,'.$from_unit.','.$to_unit.') as y from sensor_values where timestamp>=str_to_date("'.$from.'","%Y%m%d%H%i%s") and timestamp<=str_to_date("'.$to.'","%Y%m%d%H%i%s") and sensor_id='.$sensor.' and parameter_id='.$param.' order by timestamp'
     );
@@ -88,6 +105,22 @@ if ($type and $param) {
   
   }
 
+  $g = new Graph(640,480);
+  
+  if ($cachefilename) {
+      if ($g->cache->IsValid($cachefilename)) {
+  
+          $g->cache->StreamImgFile($g->img,$cachefilename);
+          return;
+  
+      } else {
+      
+          $timeout = 8640000;
+          $g->SetupCache($cachefilename,$timeout);
+      
+      }
+  }
+
   $q -> execute();
     
   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
@@ -132,7 +165,6 @@ if ($type and $param) {
   }
 
   // Create the graph
-  $g = new Graph(640,480);
   $g->graph_theme = null;
 
   //$g->img->SetAntiAliasing();