X-Git-Url: https://git.rvb.name/weathermon.git/blobdiff_plain/3a48d89e76fd2ab87dabf08632474c529cf77ad4..49789b14a3818948a0f10e40108545965b8e1a81:/web/archive.php?ds=inline
diff --git a/web/archive.php b/web/archive.php
index 76708f7..031bce7 100644
--- a/web/archive.php
+++ b/web/archive.php
@@ -2,9 +2,29 @@
 
 
     
-       
+       
   WeatherMon (аÑÑ
ив)                       
    
+  
 
          
  prepare(
+    'select u.id,u.unit_group from st_parameters st,units u where st.id='.$param.' and st.st_unit=u.id'
+  );
+  $qug -> execute();
+                                    
+  while ($row = $qug -> fetch(PDO::FETCH_ASSOC)) {
+    $from_unit = $row['id'];
+    $unit_group = $row['unit_group'];
+  }
+                                                                       
+  if (!empty($_COOKIE['unit_'.$unit_group])) {
+    $to_unit=intval($_COOKIE['unit_'.$unit_group]);
+  } else {
+    $to_unit=$from_unit;
+  }
+                                                                                                                
+  $qu = $db -> prepare(
+    'select u.name_short from units u where u.id='.$to_unit
+  );
+  $qu -> execute();
+                                                                                                                                                                 
+  while ($row = $qu -> fetch(PDO::FETCH_ASSOC)) {
+    $param_unit = $row['name_short'];
+  }
+ 
+  return Array(
+    'from' => $from_unit,
+    'to' => $to_unit,
+    'name' => $param_unit
+  );                                                                                                                                                                                    
+
+}
+
+function convert_unit($db,$value,$from,$to) {
+
+  if ($from==$to) {
+
+    $val = $value;
+
+  } else {
+
+    $qv = $db -> prepare(
+      'select round(unitconv('.$value.','.$from.','.$to.'),2) res'
+    );
+    $qv -> execute();
+                            
+    while ($row = $qv -> fetch(PDO::FETCH_ASSOC)) {
+      $val = $row['res'];
+    }
+
+  }
+                                                        
+  return $val;
+
+}
+
 $year = $_REQUEST['year']; 
 $month = $_REQUEST['month'];
 $day = $_REQUEST['day'];
@@ -26,9 +104,9 @@ if (! $year) {
         
   $q = $db -> prepare(
     'select 
-       date_format(timestamp,\'%Y\') as year
+       date_format(day,\'%Y\') as year
      from 
-       sensor_values 
+       calendar
      group by year  
      order by year');
   $q -> execute();
@@ -37,7 +115,7 @@ if (! $year) {
  fetch(PDO::FETCH_ASSOC)) {
 ?> 
-  ÐаннÑе за  год  
+  ÐаннÑе за  год  
  prepare(
           'select 
-             date_format(timestamp,\'%m\') as month,
-             date_format(timestamp,\'%d\') as day
+             date_format(day,\'%m\') as month,
+             date_format(day,\'%d\') as day
            from 
-             sensor_values
+             calendar
            where 
-             timestamp>=str_to_date(\''.$year.'-01-01\',\'%Y-%m-%d\') and 
-             timestamp=str_to_date(\''.$year.'-01-01\',\'%Y-%m-%d\') and 
+             day'.$month_name.' ';
+          echo '';
         } else {
           echo ''.$month_name.' ';
         }
@@ -96,18 +174,18 @@ if (! $year) {
     $q = $db -> prepare (
       'select 
          s.id sensor,p.id param,s.s_description sensor_name,p.st_description param_name,
-         count(distinct date_format(v.timestamp,\'%d\')) cnt,
-         round(min(v.value),1) min_value,
-         round(max(v.value),1) max_value,
+         count(distinct day) cnt,
+         round(min(v.min),1) min_value,
+         round(max(v.max),1) max_value,
          u.name_short unit
        from
-         sensor_values v,sensors s,st_parameters p,units u
+         sensors_ranges v,sensors s,st_parameters p,units u
        where 
-         v.sensor_id=s.id and
-         v.parameter_id=p.id and
+         v.sensor=s.id and
+         v.parameter=p.id and
          p.st_unit=u.id and
-         v.timestamp>=str_to_date(\''.$year.'-01-01\',\'%Y-%m-%d\') and 
-         v.timestamp=str_to_date(\''.$year.'-01-01\',\'%Y-%m-%d\') and 
+         v.day fetchAll(PDO::FETCH_ASSOC);
       
     foreach ($sensors as $sensor) {
-      
+
+      $units = get_unit($db,$sensor['param']);
+      $from_unit = $units['from'];
+      $to_unit = $units['to'];
+      $param_unit = $units['name'];
+
+      $min_val = convert_unit($db,$sensor['min_value'],$from_unit,$to_unit);
+      $max_val = convert_unit($db,$sensor['max_value'],$from_unit,$to_unit);
+     
       echo ''.$sensor['param_name'].'('.$sensor['sensor_name'].') ';
       echo '';
       echo '';
-      echo 'ÐинималÑное знаÑение за год '.$sensor['min_value'].' '.$sensor['unit'].'  ';
-      echo 'ÐакÑималÑное знаÑение за год '.$sensor['max_value'].' '.$sensor['unit'].'  ';
+      echo 'ÐинималÑное знаÑение за год '.$min_val.' '.$param_unit.'  ';
+      echo 'ÐакÑималÑное знаÑение за год '.$max_val.' '.$param_unit.'  ';
       echo '  ';
       echo '
';
 
@@ -154,12 +240,12 @@ if (! $year) {
 
       $q = $db -> prepare( 
           'select  
-             date_format(timestamp,\'%d\') as day
+             date_format(day,\'%d\') as day
            from 
-             sensor_values
+             sensors_ranges
            where 
-             timestamp>=str_to_date(\''.$year.'-'.$month.'-01\',\'%Y-%m-%d\') and 
-             timestamp=str_to_date(\''.$year.'-'.$month.'-01\',\'%Y-%m-%d\') and 
+             day prepare (
           'select 
              s.id sensor,p.id param,s.s_description sensor_name,p.st_description param_name,
-             count(distinct date_format(v.timestamp,\'%d\')) cnt,
-             round(min(v.value),1) min_value,
-             round(max(v.value),1) max_value,
+             count(distinct day) cnt,
+             round(min(v.min),1) min_value,
+             round(max(v.max),1) max_value,
              u.name_short unit
            from
-             sensor_values v,sensors s,st_parameters p,units u
+             sensors_ranges v,sensors s,st_parameters p,units u
            where 
-             v.sensor_id=s.id and
-             v.parameter_id=p.id and
+             v.sensor=s.id and
+             v.parameter=p.id and
              p.st_unit=u.id and
-             v.timestamp>=str_to_date(\''.$year.'-'.$month.'-01\',\'%Y-%m-%d\') and 
-             v.timestamp=str_to_date(\''.$year.'-'.$month.'-01\',\'%Y-%m-%d\') and 
+             v.day fetchAll(PDO::FETCH_ASSOC);
       
       foreach ($sensors as $sensor) {
+
+        $units = get_unit($db,$sensor['param']);
+        $from_unit = $units['from'];
+        $to_unit = $units['to'];
+        $param_unit = $units['name'];
+
+        $min_val = convert_unit($db,$sensor['min_value'],$from_unit,$to_unit);
+        $max_val = convert_unit($db,$sensor['max_value'],$from_unit,$to_unit);
       
         echo ''.$sensor['param_name'].'('.$sensor['sensor_name'].') ';
         echo '';
         echo '';
-        echo 'ÐинималÑное знаÑение за меÑÑÑ '.$sensor['min_value'].' '.$sensor['unit'].'  ';
-        echo 'ÐакÑималÑное знаÑение за меÑÑÑ '.$sensor['max_value'].' '.$sensor['unit'].'  ';
+        echo 'ÐинималÑное знаÑение за меÑÑÑ '.$min_val.' '.$param_unit.'  ';
+        echo 'ÐакÑималÑное знаÑение за меÑÑÑ '.$max_val.' '.$param_unit.'  ';
         echo '  ';
         echo '
';
 
@@ -227,14 +321,13 @@ if (! $year) {
 
       $q = $db -> prepare(
   	    'select 
-    		distinct v.sensor_id,s.s_description,p.id as param_id,p.st_description 
+    		distinct v.sensor as sensor_id,s.s_description,p.id as param_id,p.st_description 
   	     from 
-    		sensor_values v,st_parameters p,sensors s 
+    		sensors_ranges v,st_parameters p,sensors s 
   	     where 
-                v.timestamp>=str_to_date(\''.$year.'-'.$month.'-'.$day.'\',\'%Y-%m-%d\') and 
-                v.timestamp execute();