Все-таки переподключение к брокеру лучше выполнять вручную...
[weathermon.git] / web / image_minmax.php
index a0a6326addeda0a0f9aa647f25c1e34850528afd..edc1708e600cea7c60627655209218c38c150ce2 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'];
@@ -39,8 +48,7 @@ if ($type and $param) {
   }                                                                
 
   $q = $db -> prepare(
-    'select st.st_fill_color_top,st.st_fill_color_bottom,st.st_description,u.id,u.unit_group from st_parameters st,units u where st.id='.$param.' and st.st_unit=u.id
-'
+    'select st.st_fill_color_top,st.st_fill_color_bottom,st.st_description,u.id,u.unit_group from st_parameters st,units u where st.id='.$param.' and st.st_unit=u.id'
   );
   $q -> execute();
 
@@ -75,63 +83,105 @@ if ($type and $param) {
     $next_year = $year;
     $next_month = $month+1;
 
-    if ($month==13) {
+    if ($month==12) {
     
       $next_year++;
-      $month=1;
+      $next_month=1;
+
+    }
+
+    $curr = date("Ym");
+    $ym=sprintf('%04d%02d',$next_year,$next_month);
+    
+    if ($curr>$ym) {
+    
+      $cachefilename='meteo.month.'.$sensor.'.'.$param.'.'.$to_unit.'.'.$year.'-'.$month.'.'.$img_format;
     
     }
 
+    $datestr=sprintf("%04d%02d01",$year,$month);
+    $nextdatestr=sprintf("%04d%02d01",$next_year,$next_month);
+
     $q = $db -> prepare(
-      '
+     '
         select 
           x,
-          unitconv(min(value),'.$from_unit.','.$to_unit.') min_value,
-          unitconv(max(value),'.$from_unit.','.$to_unit.') max_value
+          unitconv(min(min),'.$from_unit.','.$to_unit.') min_value,
+          unitconv(max(max),'.$from_unit.','.$to_unit.') max_value
         from  (
           select 
-            unix_timestamp(cast(timestamp as date)) x,
-            value
+            unix_timestamp(day) x,
+            min,
+            max
           from 
-            sensor_values 
+            sensors_ranges 
           where 
-            timestamp>=str_to_date(\''.$year.$month.'\',\'%Y%m\')
-            and timestamp<str_to_date(\''.$next_year.$next_month.'\',\'%Y%m\')
-            and sensor_id='.$sensor.'
-            and parameter_id='.$param.'
+            day>=STR_TO_DATE(\''.$datestr.'\',\'%Y%m%d\')
+            and day<STR_TO_DATE(\''.$nextdatestr.'\',\'%Y%m%d\')
+            and sensor='.$sensor.'
+            and parameter='.$param.'
           ) t group by x
         order by x'
     );
-    
+
   } elseif ($type == "year") {
 
     $next_year = $year+1;
 
+    $curr = date("Y");
+        
+    if ($curr>$next_year) {
+                
+        $cachefilename='meteo.year.'.$sensor.'.'.$param.'.'.$year.'.'.$img_format;
+                          
+    }
+
+    $datestr=sprintf("%04d0101",$year);
+    $nextdatestr=sprintf("%04d0101",$next_year);
+
     $q = $db -> prepare(
       '
         select 
           x,
-          unitconv(min(value),'.$from_unit.','.$to_unit.') min_value,
-          unitconv(max(value),'.$from_unit.','.$to_unit.') max_value
+          unitconv(min(min),'.$from_unit.','.$to_unit.') min_value,
+          unitconv(max(max),'.$from_unit.','.$to_unit.') max_value
         from  (
           select 
             unix_timestamp(
-              DATE_SUB(cast(timestamp as date), INTERVAL DAYOFWEEK(cast(timestamp as date))-1 DAY)
+              DATE_SUB(day, INTERVAL DAYOFWEEK(day)-1 DAY)
               ) x,
-            value
+            min,
+            max
           from 
-            sensor_values 
+            sensors_ranges
           where 
-            timestamp>=str_to_date(\''.$year.'\',\'%Y\')
-            and timestamp<str_to_date(\''.$next_year.'\',\'%Y\')
-            and sensor_id='.$sensor.'
-            and parameter_id='.$param.'
+            day>=STR_TO_DATE(\''.$datestr.'\',\'%Y%m%d\')
+            and day<STR_TO_DATE(\''.$nextdatestr.'\',\'%Y%m%d\')
+            and sensor='.$sensor.'
+            and parameter='.$param.'
           ) t group by x
         order by x'
     );
   
   }
 
+  $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)) {
@@ -142,9 +192,7 @@ if ($type and $param) {
     
   }                                                                
 
-
   // Create the graph
-  $g = new Graph(640,480);
   $g->graph_theme = null;
 
   //$g->img->SetAntiAliasing();
@@ -154,6 +202,7 @@ if ($type and $param) {
   $g->SetScale('datlin');
   $g->xaxis->SetLabelAngle(90);
   $g->xaxis->SetPos("min");
+  $g->xaxis->scale->SetTimeAlign( MINADJ_1 );
 
   // We use a scatterplot to illustrate the original
   // contro points.
@@ -161,14 +210,14 @@ if ($type and $param) {
   $bplot = new LinePlot($maxdata,$xdata);
   $g->Add($bplot);
   $bplot->SetColor($fill_color_top);
-  $bplot->SetFillGradient($fill_color_top,$fill_color_bottom,100,TRUE);
+  $bplot->SetFillGradient($fill_color_top.'@0.2',$fill_color_bottom.'@0.9',100,TRUE);
   $bplot->SetFillFromYMin(TRUE);       
   $bplot->SetWeight(4);
 
   $aplot = new LinePlot($mindata,$xdata);
   $g->Add($aplot);
   $aplot->SetColor($fill_color_bottom);
-  $aplot->SetFillGradient($fill_color_bottom,'white',100,TRUE);
+  $aplot->SetFillGradient($fill_color_bottom.'@0.2','white@0.9',100,TRUE);
   $aplot->SetFillFromYMin(TRUE);       
   $aplot->SetWeight(4);
 
@@ -179,6 +228,11 @@ if ($type and $param) {
   $g->subtitle->SetColor('darkred');
   $g->SetMarginColor('silver');
 
+  $g->xgrid->Show();
+  $g->xgrid->SetLineStyle('dotted');
+  $g->ygrid->Show();
+  $g->ygrid->SetLineStyle('dotted');
+        
   // Add the plots to the graph and stroke
   $g->Stroke();