function weather_refresh_data in Weather 7
Parses raw METAR data string and stores results in database.
Parameters
string $icao: ICAO code.
object $metar: METAR data object, may be altered.
Return value
object METAR data object or FALSE.
1 call to weather_refresh_data()
- weather_get_metar in ./
weather.module - Fetches the latest METAR data from the database or internet.
File
- ./
weather_parser.inc, line 35 - Retrieves and parses raw METAR data and stores result in database.
Code
function weather_refresh_data($icao, &$metar) {
$metar_raw_string = weather_retrieve_data($icao);
// If there's new data available, parse it.
if (!empty($metar_raw_string)) {
$metar = weather_parse_metar($metar_raw_string);
// Calculate the next scheduled update. Use 62 minutes after the
// reported timestamp, to allow the data to propagate to the server.
$next_update_on = $metar->reported_on + 62 * 60;
// However, if the current time is more than 62 minutes
// over the reported timestamp, do not download on every page request.
// Therefore, we use 3, 6, 12, and 24 hours to check for updates.
// From then on, we check once a day for updates.
if ($next_update_on < REQUEST_TIME) {
$last_update = $metar->reported_on;
$hours = 3 * 60 * 60;
while ($last_update + $hours + 120 < REQUEST_TIME) {
if ($hours < 86400) {
$hours = $hours * 2;
}
else {
$hours = $hours + 86400;
}
}
// Add 2 minutes to allow the data to propagate to the server.
$next_update_on = $last_update + $hours + 120;
}
$metar->next_update_on = $next_update_on;
}
else {
// The download has not been successful. Calculate the time of next update
// according to last tries.
if (empty($metar)) {
// There is no entry yet, so this is the first download attempt.
// Create a new entry with 'nodata' and give some minutes to retry.
$metar = new stdClass();
$metar->icao = $icao;
$metar->reported_on = REQUEST_TIME;
$metar->next_update_on = REQUEST_TIME + 10 * 60;
$metar->image = 'nodata';
}
else {
// There has been at least one download attempt. Increase the time of
// next update to not download every few minutes.
// If 24 hours are reached, we check once a day for updates.
// This way, we gracefully handle ICAO codes which do no longer exist.
$last_update = $metar->reported_on;
$hours = 3 * 60 * 60;
while ($last_update + $hours + 120 < REQUEST_TIME) {
if ($hours < 86400) {
$hours = $hours * 2;
}
else {
$hours = $hours + 86400;
}
}
// Add 2 minutes to allow the data to propagate to the server.
$metar->next_update_on = $last_update + $hours + 120;
}
}
// Wrap drupal_write_record() into try/catch block, see #1412352.
try {
// Make sure there's no stale data around
db_delete('weather_metar')
->condition('icao', $icao)
->execute();
drupal_write_record('weather_metar', $metar);
} catch (Exception $e) {
// Nothing to do here. If the writing fails and an exception
// is raised, the writing of the data will happen on the next
// update.
watchdog_exception('weather', $e);
}
}