Changeset 98


Ignore:
Timestamp:
18/01/07 06:09:59 (5 years ago)
Author:
nigel
Message:

Fixed graphing routine
Added rain graph
Cleaned up unused methods

File:
1 edited

Legend:

Unmodified
Added
Removed
  • openweather/openweather.module

    r97 r98  
    209209 
    210210/** 
     211 * Gets (and caches) the latest readings 
     212 */ 
     213function openweather_get_latest($nid){ 
     214  static $cache = array(); 
     215  if (array_key_exists($nid,$cache)) 
     216    return $cache[$nid]; 
     217  else { 
     218    $data = db_query("SELECT * FROM {openweather_readings} WHERE nid = $nid ORDER BY `timestamp` DESC LIMIT 1"); 
     219    $data = db_fetch_object($data); 
     220    $cache[$nid] = $data; 
     221    return $data; 
     222  } 
     223} 
     224 
     225 
     226/** 
    211227  * Summary table of weather station feed 
    212228  */ 
    213229function openweather_summary($nid){ 
    214   $data = db_query("SELECT * FROM {openweather_readings} WHERE nid = $nid ORDER BY `timestamp` DESC LIMIT 1"); 
    215   $data = db_fetch_object($data); 
     230  $data = openweather_get_latest($nid); 
    216231 
    217232  $output .= "<table>"; 
    218   $output .= "<tr><th>Temperature</th><td>$data->temp_out dec C</td><th>Humidity</th><td>$data->rel_hum_out %</td></tr>"; 
    219   $output .= "<tr><th>Wind speed</th><td>$data->windspeed kMh</td><th>Wind direction</th><td>".openweather_to_direction($data->wind_angle)."($data->wind_angle)</td></tr>"; 
     233  $output .= "<tr><th>Temperature</th><td>$data->temp_out &#176;C</td><th>Humidity</th><td>$data->rel_hum_out %</td></tr>"; 
     234  $output .= "<tr><th>Wind speed</th><td>$data->windspeed kmh</td><th>Wind direction</th><td>".openweather_to_direction($data->wind_angle)."($data->wind_angle &#176;)</td></tr>"; 
    220235  $output .= "<tr><th>Relative pressure</th><td>$data->rel_pressure</td><th>Timestamp</th><td>$data->timestamp</td></tr>"; 
    221236  $output .= "</table>"; 
     
    257272  $nid = arg(1); 
    258273  $node = node_load($nid); 
    259   $results = openweather_get_values($node); 
    260274  drupal_set_header('Content-Type: text/csv; charset=utf-8'); 
    261275  drupal_set_header('Content-Disposition: attachment; filename="openweather.csv";'); 
    262   print "Time, Value\n"; 
    263   foreach($results as $row){ 
    264     print '"'.$row->timestamp.'",'.$row->value."\n"; 
     276  print "Time, temp_out, rel_hum_out, windspeed, wind_angle, wind_chill, rain_1h, rel_pressure, dewpoint\n"; 
     277   
     278  $results = db_query("SELECT * FROM {openweather_readings} WHERE nid = %d ORDER BY timestamp DESC", $node->nid); 
     279  while ($row = db_fetch_object($results)){ 
     280    print "$row->timestamp, $row->temp_out, $row->rel_hum_out, $row->windspeed, $row->wind_angle, $row->wind_chill, $row->rain_1h, $row->rel_pressure, $row->dewpoint\n"; 
    265281  } 
    266282} 
     
    288304} 
    289305 
    290 /** 
    291   * Average usage over $days, from midday before the last reading 
    292   * @param values - object with date and value 
    293   * @param days - the number of days to average over 
    294   * @param count - the number of entries to calculate 
    295   */ 
    296 function openweather_extrapolate($values, $days, $count){ 
    297   $return = array(); 
    298  
    299   $last = reset($values); 
    300   $row = $last; 
    301   $target = strtotime($row->timestamp); // Set the first target 
    302   $value = $row->value; // We know what the first reading is! 
    303  
    304   for ($i = 0 ; $i < $count ; $i++){ 
    305     // Increment target, and store old value 
    306     $oldValue = $value; 
    307     $oldTarget = $target; 
    308     $target -= $days*60*60*24; 
    309  
    310     while ( !($row === false) && strtotime($row->timestamp) > $target){ 
    311       $last = $row; 
    312       $row = next($values); 
    313     } 
    314     if ($row === false) { 
    315       break; 
    316     } else { 
    317       $timeBetween = (strtotime( $last->timestamp ) - strtotime( $row->timestamp )); 
    318       $timeToTarget = $target - strtotime( $row->timestamp ); 
    319       $value = $row->value + (($last->value - $row->value)/$timeBetween*$timeToTarget); 
    320       $entry = new StdClass(); 
    321       $entry->from = date("d/m/Y H:i", $target); 
    322       $entry->to = date("d/m/Y H:i", $oldTarget); 
    323       $entry->value = $oldValue - $value; 
    324       $return[$i] = $entry; 
    325     } 
    326   } 
    327  
    328   return $return; 
    329 } 
    330  
    331  
    332 /** 
    333   * Draw a CSS/HTML graph 
    334   */ 
    335 /* 
    336 function openweather_graph($entries){ 
    337   if (count($entries) == 0) 
    338     return "No entries"; 
    339   $height = 200; 
    340   $width = 500; 
    341   $output = '<div style="background: white; border: 1px solid; position: relative; width: '.$width.'px; height:'.$height.'px">'; 
    342  
    343   $w = floor($width/count($entries)); 
    344   $offset = ($width-($w*count($entries)))/2; 
    345   $i = 0; 
    346    
    347   $max = 0; 
    348   foreach ($entries as $e){ 
    349     if ($e->value > $max) 
    350       $max = $e->value; 
    351   } 
    352  
    353   foreach ($entries as $e){ 
    354     $output .= "<div title='".$e->from." to ".$e->to." value: ".sprintf("%1.2f",$e->value)."' style='width:${w}px; height:".floor($e->value/$max*$height)."px; bottom: 0px; position: absolute; right:".($i*$w+$offset)."px; background: grey; border: solid 1px;'>".round($e->value)."</div>\n"; 
    355     $i++; 
    356   } 
    357   $output .= '</div>'; 
    358   return $output; 
    359 } 
    360 */ 
    361306function openweather_xmlrpc(){ 
    362307  return array( 
     
    396341  } 
    397342} 
    398  
    399343 
    400344/** 
     
    439383    break; 
    440384 
     385  case 'rain_1h': 
     386    $title = t("Rainfall in previous hour"); 
     387    $autoScale = false; 
     388    $units = 'mm'; 
     389    $decimals = 1; 
     390    break; 
     391 
    441392  default: 
    442393    return; 
    443394  } 
    444395 
    445   $r = db_query("SELECT time(`timestamp`) AS `timevalue`, unix_timestamp(`timestamp`) as `utimestamp`, $type FROM {openweather_readings} WHERE nid = $nid ORDER BY `timestamp` DESC LIMIT 144" ); 
     396  $data = openweather_get_latest($nid); 
     397 
     398  $r = db_query("SELECT time(`timestamp`) AS `timevalue`, unix_timestamp(`timestamp`) as `utimestamp`, $type FROM {openweather_readings} WHERE nid = $nid AND unix_timestamp(timestamp) > ".(strtotime($data->timestamp)-24*60*60)." ORDER BY `timestamp` DESC LIMIT 500" ); 
    446399 
    447400  drupal_set_header('Content-Type: image/png'); 
     
    459412  $g->y_data = array( $type=>array() ); 
    460413  $minY = null; 
     414  $count = 0; 
    461415  while ($e = db_fetch_object($r)) { 
     416    $count++; 
    462417    $g->x_data = array_merge($g->x_data, array($e->timevalue=>$e->utimestamp)); 
    463418    $g->y_data[$type][] = $e->$type; 
    464     //echo $e->$type.",".$e->timevalue.",".$e->utimestamp."<br>"; 
    465419    if ($minY == null || $e->$type < $minY) $minY = $e->$type; 
    466420  } 
     
    472426 
    473427  if ($autoScale) $g->parameter['y_min_left'] = $minY; 
     428 
     429  $g->parameter['x_axis_text'] = $count/12; 
    474430   
    475431  $g->draw(); 
Note: See TracChangeset for help on using the changeset viewer.