Changeset 98
- Timestamp:
- 18/01/07 06:09:59 (5 years ago)
- File:
-
- 1 edited
-
openweather/openweather.module (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
openweather/openweather.module
r97 r98 209 209 210 210 /** 211 * Gets (and caches) the latest readings 212 */ 213 function 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 /** 211 227 * Summary table of weather station feed 212 228 */ 213 229 function 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); 216 231 217 232 $output .= "<table>"; 218 $output .= "<tr><th>Temperature</th><td>$data->temp_out decC</td><th>Humidity</th><td>$data->rel_hum_out %</td></tr>";219 $output .= "<tr><th>Wind speed</th><td>$data->windspeed k Mh</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 °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 °)</td></tr>"; 220 235 $output .= "<tr><th>Relative pressure</th><td>$data->rel_pressure</td><th>Timestamp</th><td>$data->timestamp</td></tr>"; 221 236 $output .= "</table>"; … … 257 272 $nid = arg(1); 258 273 $node = node_load($nid); 259 $results = openweather_get_values($node);260 274 drupal_set_header('Content-Type: text/csv; charset=utf-8'); 261 275 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"; 265 281 } 266 282 } … … 288 304 } 289 305 290 /**291 * Average usage over $days, from midday before the last reading292 * @param values - object with date and value293 * @param days - the number of days to average over294 * @param count - the number of entries to calculate295 */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 target302 $value = $row->value; // We know what the first reading is!303 304 for ($i = 0 ; $i < $count ; $i++){305 // Increment target, and store old value306 $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 graph334 */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 */361 306 function openweather_xmlrpc(){ 362 307 return array( … … 396 341 } 397 342 } 398 399 343 400 344 /** … … 439 383 break; 440 384 385 case 'rain_1h': 386 $title = t("Rainfall in previous hour"); 387 $autoScale = false; 388 $units = 'mm'; 389 $decimals = 1; 390 break; 391 441 392 default: 442 393 return; 443 394 } 444 395 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" ); 446 399 447 400 drupal_set_header('Content-Type: image/png'); … … 459 412 $g->y_data = array( $type=>array() ); 460 413 $minY = null; 414 $count = 0; 461 415 while ($e = db_fetch_object($r)) { 416 $count++; 462 417 $g->x_data = array_merge($g->x_data, array($e->timevalue=>$e->utimestamp)); 463 418 $g->y_data[$type][] = $e->$type; 464 //echo $e->$type.",".$e->timevalue.",".$e->utimestamp."<br>";465 419 if ($minY == null || $e->$type < $minY) $minY = $e->$type; 466 420 } … … 472 426 473 427 if ($autoScale) $g->parameter['y_min_left'] = $minY; 428 429 $g->parameter['x_axis_text'] = $count/12; 474 430 475 431 $g->draw();
Note: See TracChangeset
for help on using the changeset viewer.
