2010-09-11 16:11:35 +02:00
|
|
|
// realwx_ctrl.cxx -- Process real weather data
|
|
|
|
//
|
|
|
|
// Written by David Megginson, started February 2002.
|
2011-08-26 09:01:31 +02:00
|
|
|
// Rewritten by Torsten Dreyer, August 2010, August 2011
|
2010-09-11 16:11:35 +02:00
|
|
|
//
|
|
|
|
// Copyright (C) 2002 David Megginson - david@megginson.com
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "realwx_ctrl.hxx"
|
|
|
|
|
2012-04-18 10:25:27 +01:00
|
|
|
#include <algorithm>
|
2011-08-08 18:16:49 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-09-23 16:55:29 +01:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2011-08-08 18:16:49 +01:00
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
#include <simgear/structure/exception.hxx>
|
|
|
|
#include <simgear/misc/strutils.hxx>
|
2011-02-06 15:44:09 +01:00
|
|
|
#include <simgear/props/tiedpropertylist.hxx>
|
2011-08-08 18:16:49 +01:00
|
|
|
#include <simgear/io/HTTPRequest.hxx>
|
|
|
|
#include <simgear/timing/sg_time.hxx>
|
2011-11-01 22:40:31 +00:00
|
|
|
#include <simgear/structure/event_mgr.hxx>
|
2012-09-23 16:55:29 +01:00
|
|
|
#include <simgear/structure/commands.hxx>
|
2011-08-08 18:16:49 +01:00
|
|
|
|
2012-04-18 10:25:27 +01:00
|
|
|
#include "metarproperties.hxx"
|
|
|
|
#include "metarairportfilter.hxx"
|
|
|
|
#include "fgmetar.hxx"
|
|
|
|
#include <Network/HTTPClient.hxx>
|
|
|
|
#include <Main/fg_props.hxx>
|
2010-09-11 16:11:35 +02:00
|
|
|
|
|
|
|
namespace Environment {
|
2011-08-26 09:01:31 +02:00
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
|
2011-01-07 13:11:06 +01:00
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
class MetarDataHandler {
|
2011-01-07 13:11:06 +01:00
|
|
|
public:
|
2011-08-26 09:01:31 +02:00
|
|
|
virtual void handleMetarData( const std::string & data ) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MetarRequester {
|
|
|
|
public:
|
|
|
|
virtual void requestMetar( MetarDataHandler * metarDataHandler, const std::string & id ) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
class LiveMetarProperties : public MetarProperties, MetarDataHandler {
|
|
|
|
public:
|
|
|
|
LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester );
|
2011-01-07 13:11:06 +01:00
|
|
|
virtual ~LiveMetarProperties();
|
|
|
|
virtual void update( double dt );
|
|
|
|
|
|
|
|
virtual double getTimeToLive() const { return _timeToLive; }
|
2012-12-16 15:05:21 +00:00
|
|
|
virtual void resetTimeToLive()
|
|
|
|
{ _timeToLive = 0.00; _pollingTimer = 0.0; }
|
2011-08-26 09:01:31 +02:00
|
|
|
|
|
|
|
// implementation of MetarDataHandler
|
|
|
|
virtual void handleMetarData( const std::string & data );
|
|
|
|
|
|
|
|
static const unsigned MAX_POLLING_INTERVAL_SECONDS = 10;
|
|
|
|
static const unsigned DEFAULT_TIME_TO_LIVE_SECONDS = 900;
|
|
|
|
|
2011-01-07 13:11:06 +01:00
|
|
|
private:
|
|
|
|
double _timeToLive;
|
2011-08-26 09:01:31 +02:00
|
|
|
double _pollingTimer;
|
|
|
|
MetarRequester * _metarRequester;
|
2011-01-07 13:11:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef SGSharedPtr<LiveMetarProperties> LiveMetarProperties_ptr;
|
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
LiveMetarProperties::LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester ) :
|
2011-01-07 13:11:06 +01:00
|
|
|
MetarProperties( rootNode ),
|
2011-08-26 09:01:31 +02:00
|
|
|
_timeToLive(0.0),
|
|
|
|
_pollingTimer(0.0),
|
|
|
|
_metarRequester(metarRequester)
|
2011-01-07 13:11:06 +01:00
|
|
|
{
|
|
|
|
_tiedProperties.Tie("time-to-live", &_timeToLive );
|
|
|
|
}
|
|
|
|
|
|
|
|
LiveMetarProperties::~LiveMetarProperties()
|
|
|
|
{
|
2011-03-07 19:37:04 +01:00
|
|
|
_tiedProperties.Untie();
|
2011-01-07 13:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LiveMetarProperties::update( double dt )
|
|
|
|
{
|
|
|
|
_timeToLive -= dt;
|
2011-08-26 09:01:31 +02:00
|
|
|
_pollingTimer -= dt;
|
2012-12-09 19:41:31 +00:00
|
|
|
if( _timeToLive <= 0.0 ) {
|
2011-08-26 09:01:31 +02:00
|
|
|
_timeToLive = 0.0;
|
|
|
|
std::string stationId = getStationId();
|
|
|
|
if( stationId.empty() ) return;
|
|
|
|
if( _pollingTimer > 0.0 ) return;
|
|
|
|
_metarRequester->requestMetar( this, stationId );
|
|
|
|
_pollingTimer = MAX_POLLING_INTERVAL_SECONDS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveMetarProperties::handleMetarData( const std::string & data )
|
|
|
|
{
|
|
|
|
SG_LOG( SG_ENVIRONMENT, SG_INFO, "LiveMetarProperties::handleMetarData() received METAR for " << getStationId() << ": " << data );
|
|
|
|
_timeToLive = DEFAULT_TIME_TO_LIVE_SECONDS;
|
|
|
|
setMetar( data );
|
2011-01-07 13:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
class BasicRealWxController : public RealWxController
|
|
|
|
{
|
|
|
|
public:
|
2011-08-26 09:01:31 +02:00
|
|
|
BasicRealWxController( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester );
|
2010-09-11 16:11:35 +02:00
|
|
|
virtual ~BasicRealWxController ();
|
|
|
|
|
|
|
|
virtual void init ();
|
|
|
|
virtual void reinit ();
|
2011-11-01 22:40:31 +00:00
|
|
|
virtual void shutdown ();
|
|
|
|
|
2012-09-23 16:55:29 +01:00
|
|
|
/**
|
|
|
|
* Create a metar-property binding at the specified property path,
|
|
|
|
* and initiate a request for the specified station-ID (which may be
|
|
|
|
* empty). If the property path is already mapped, the station ID
|
|
|
|
* will be updated.
|
|
|
|
*/
|
|
|
|
void addMetarAtPath(const string& propPath, const string& icao);
|
|
|
|
|
|
|
|
void removeMetarAtPath(const string& propPath);
|
2010-09-11 16:11:35 +02:00
|
|
|
protected:
|
|
|
|
void bind();
|
|
|
|
void unbind();
|
2010-10-11 21:21:53 +02:00
|
|
|
void update( double dt );
|
|
|
|
|
2011-11-01 22:40:31 +00:00
|
|
|
void checkNearbyMetar();
|
2010-09-11 16:11:35 +02:00
|
|
|
|
2010-09-27 18:59:26 +02:00
|
|
|
long getMetarMaxAgeMin() const { return _max_age_n == NULL ? 0 : _max_age_n->getLongValue(); }
|
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
SGPropertyNode_ptr _rootNode;
|
|
|
|
SGPropertyNode_ptr _ground_elevation_n;
|
2010-09-27 18:59:26 +02:00
|
|
|
SGPropertyNode_ptr _max_age_n;
|
2010-09-11 16:11:35 +02:00
|
|
|
|
|
|
|
bool _enabled;
|
2012-09-23 16:55:29 +01:00
|
|
|
bool _wasEnabled;
|
2011-02-06 15:44:09 +01:00
|
|
|
simgear::TiedPropertyList _tiedProperties;
|
2011-03-07 19:37:04 +01:00
|
|
|
typedef std::vector<LiveMetarProperties_ptr> MetarPropertiesList;
|
2011-01-07 13:11:06 +01:00
|
|
|
MetarPropertiesList _metarProperties;
|
2012-09-23 16:55:29 +01:00
|
|
|
MetarRequester* _requester;
|
2011-11-01 22:40:31 +00:00
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
};
|
|
|
|
|
2012-09-23 16:55:29 +01:00
|
|
|
static bool commandRequestMetar(const SGPropertyNode* arg)
|
|
|
|
{
|
|
|
|
SGSubsystemGroup* envMgr = (SGSubsystemGroup*) globals->get_subsystem("environment");
|
|
|
|
if (!envMgr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicRealWxController* self = (BasicRealWxController*) envMgr->get_subsystem("realwx");
|
|
|
|
if (!self) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string icao(arg->getStringValue("station"));
|
|
|
|
boost::to_upper(icao);
|
|
|
|
string path = arg->getStringValue("path");
|
|
|
|
self->addMetarAtPath(path, icao);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool commandClearMetar(const SGPropertyNode* arg)
|
|
|
|
{
|
|
|
|
SGSubsystemGroup* envMgr = (SGSubsystemGroup*) globals->get_subsystem("environment");
|
|
|
|
if (!envMgr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicRealWxController* self = (BasicRealWxController*) envMgr->get_subsystem("realwx");
|
|
|
|
if (!self) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string path = arg->getStringValue("path");
|
|
|
|
self->removeMetarAtPath(path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
/*
|
|
|
|
Properties
|
|
|
|
~/enabled: bool Enables/Disables the realwx controller
|
|
|
|
~/metar[1..n]: string Target property path for metar data
|
|
|
|
*/
|
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester ) :
|
2010-09-11 16:11:35 +02:00
|
|
|
_rootNode(rootNode),
|
|
|
|
_ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )),
|
2010-09-27 18:59:26 +02:00
|
|
|
_max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ),
|
2010-09-11 16:11:35 +02:00
|
|
|
_enabled(true),
|
2012-09-23 16:55:29 +01:00
|
|
|
_wasEnabled(false),
|
|
|
|
_requester(metarRequester)
|
2010-09-11 16:11:35 +02:00
|
|
|
{
|
2011-01-07 13:11:06 +01:00
|
|
|
// at least instantiate MetarProperties for /environment/metar
|
|
|
|
_metarProperties.push_back( new LiveMetarProperties(
|
2011-08-26 09:01:31 +02:00
|
|
|
fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ), metarRequester ));
|
2011-01-07 13:11:06 +01:00
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
BOOST_FOREACH( SGPropertyNode_ptr n, rootNode->getChildren("metar") ) {
|
2012-09-23 16:55:29 +01:00
|
|
|
SGPropertyNode_ptr metarNode = fgGetNode( n->getStringValue(), true );
|
|
|
|
addMetarAtPath(metarNode->getPath(), "");
|
2011-01-07 13:11:06 +01:00
|
|
|
}
|
2012-09-23 16:55:29 +01:00
|
|
|
|
|
|
|
SGCommandMgr::instance()->addCommand("request-metar", commandRequestMetar);
|
|
|
|
SGCommandMgr::instance()->addCommand("clear-metar", commandClearMetar);
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BasicRealWxController::~BasicRealWxController()
|
|
|
|
{
|
2012-09-23 16:55:29 +01:00
|
|
|
//SGCommandMgr::instance()->removeCommand("request-metar");
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BasicRealWxController::init()
|
|
|
|
{
|
2012-09-23 16:55:29 +01:00
|
|
|
_wasEnabled = false;
|
2012-12-09 19:41:31 +00:00
|
|
|
|
|
|
|
checkNearbyMetar();
|
2010-09-11 16:11:35 +02:00
|
|
|
update(0); // fetch data ASAP
|
2011-11-01 22:40:31 +00:00
|
|
|
|
|
|
|
globals->get_event_mgr()->addTask("checkNearbyMetar", this,
|
|
|
|
&BasicRealWxController::checkNearbyMetar, 60 );
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BasicRealWxController::reinit()
|
|
|
|
{
|
2012-09-23 16:55:29 +01:00
|
|
|
_wasEnabled = false;
|
2012-12-16 15:05:21 +00:00
|
|
|
checkNearbyMetar();
|
|
|
|
update(0); // fetch data ASAP
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
2011-11-01 22:40:31 +00:00
|
|
|
|
|
|
|
void BasicRealWxController::shutdown()
|
|
|
|
{
|
|
|
|
globals->get_event_mgr()->removeTask("checkNearbyMetar");
|
|
|
|
}
|
2010-09-11 16:11:35 +02:00
|
|
|
|
|
|
|
void BasicRealWxController::bind()
|
|
|
|
{
|
|
|
|
_tiedProperties.setRoot( _rootNode );
|
|
|
|
_tiedProperties.Tie( "enabled", &_enabled );
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicRealWxController::unbind()
|
|
|
|
{
|
|
|
|
_tiedProperties.Untie();
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
void BasicRealWxController::update( double dt )
|
2012-12-16 15:05:21 +00:00
|
|
|
{
|
2010-10-11 21:21:53 +02:00
|
|
|
if( _enabled ) {
|
2012-09-23 16:55:29 +01:00
|
|
|
bool firstIteration = !_wasEnabled;
|
2011-01-07 13:11:06 +01:00
|
|
|
// clock tick for every METAR in stock
|
2011-08-26 09:01:31 +02:00
|
|
|
BOOST_FOREACH(LiveMetarProperties* p, _metarProperties) {
|
2011-01-07 13:11:06 +01:00
|
|
|
// first round? All received METARs are outdated
|
2012-12-16 15:05:21 +00:00
|
|
|
if( firstIteration ) p->resetTimeToLive();
|
2011-08-26 09:01:31 +02:00
|
|
|
p->update(dt);
|
2011-01-07 13:11:06 +01:00
|
|
|
}
|
|
|
|
|
2012-09-23 16:55:29 +01:00
|
|
|
_wasEnabled = true;
|
2010-10-11 21:21:53 +02:00
|
|
|
} else {
|
2012-09-23 16:55:29 +01:00
|
|
|
_wasEnabled = false;
|
2010-10-11 21:21:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-23 16:55:29 +01:00
|
|
|
void BasicRealWxController::addMetarAtPath(const string& propPath, const string& icao)
|
|
|
|
{
|
|
|
|
// check for duplicate entries
|
|
|
|
BOOST_FOREACH( LiveMetarProperties_ptr p, _metarProperties ) {
|
|
|
|
if( p->get_root_node()->getPath() == propPath ) {
|
|
|
|
// already exists
|
|
|
|
if (p->getStationId() != icao) {
|
|
|
|
p->setStationId(icao);
|
2012-12-16 15:05:21 +00:00
|
|
|
p->resetTimeToLive();
|
2012-09-23 16:55:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} // of exitsing metar properties iteration
|
|
|
|
|
|
|
|
SGPropertyNode_ptr metarNode = fgGetNode(propPath, true);
|
|
|
|
SG_LOG( SG_ENVIRONMENT, SG_INFO, "Adding metar properties at " << propPath );
|
|
|
|
LiveMetarProperties_ptr p(new LiveMetarProperties( metarNode, _requester ));
|
|
|
|
_metarProperties.push_back(p);
|
|
|
|
p->setStationId(icao);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicRealWxController::removeMetarAtPath(const string &propPath)
|
|
|
|
{
|
|
|
|
MetarPropertiesList::iterator it = _metarProperties.begin();
|
|
|
|
for (; it != _metarProperties.end(); ++it) {
|
|
|
|
LiveMetarProperties_ptr p(*it);
|
|
|
|
if( p->get_root_node()->getPath() == propPath ) {
|
|
|
|
_metarProperties.erase(it);
|
|
|
|
// final ref will drop, and delete the MetarProperties, when we return
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SG_LOG(SG_ENVIRONMENT, SG_WARN, "no metar properties at " << propPath);
|
|
|
|
}
|
|
|
|
|
2011-11-01 22:40:31 +00:00
|
|
|
void BasicRealWxController::checkNearbyMetar()
|
2011-08-26 09:01:31 +02:00
|
|
|
{
|
2011-11-01 22:40:31 +00:00
|
|
|
try {
|
|
|
|
const SGGeod & pos = globals->get_aircraft_position();
|
|
|
|
|
|
|
|
// check nearest airport
|
|
|
|
SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "NoaaMetarRealWxController::update(): (re) checking nearby airport with METAR" );
|
|
|
|
|
|
|
|
FGAirport * nearestAirport = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
|
|
|
|
if( nearestAirport == NULL ) {
|
|
|
|
SG_LOG(SG_ENVIRONMENT,SG_WARN,"RealWxController::update can't find airport with METAR within 10000NM" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SG_LOG(SG_ENVIRONMENT, SG_DEBUG,
|
|
|
|
"NoaaMetarRealWxController::update(): nearest airport with METAR is: " << nearestAirport->ident() );
|
|
|
|
|
|
|
|
// if it has changed, invalidate the associated METAR
|
|
|
|
if( _metarProperties[0]->getStationId() != nearestAirport->ident() ) {
|
|
|
|
SG_LOG(SG_ENVIRONMENT, SG_INFO,
|
|
|
|
"NoaaMetarRealWxController::update(): nearest airport with METAR has changed. Old: '" <<
|
|
|
|
_metarProperties[0]->getStationId() <<
|
|
|
|
"', new: '" << nearestAirport->ident() << "'" );
|
|
|
|
_metarProperties[0]->setStationId( nearestAirport->ident() );
|
2012-12-16 15:05:21 +00:00
|
|
|
_metarProperties[0]->resetTimeToLive();
|
2011-11-01 22:40:31 +00:00
|
|
|
}
|
2011-08-08 18:16:49 +01:00
|
|
|
}
|
2011-11-01 22:40:31 +00:00
|
|
|
catch( sg_exception & ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
}
|
2010-09-11 16:11:35 +02:00
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
/* -------------------------------------------------------------------------------- */
|
2011-08-08 18:16:49 +01:00
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
class NoaaMetarRealWxController : public BasicRealWxController, MetarRequester {
|
|
|
|
public:
|
|
|
|
NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
|
2011-08-08 18:16:49 +01:00
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
// implementation of MetarRequester
|
|
|
|
virtual void requestMetar( MetarDataHandler * metarDataHandler, const std::string & id );
|
|
|
|
|
|
|
|
private:
|
2012-04-18 10:25:27 +01:00
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
};
|
2010-09-11 16:11:35 +02:00
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ) :
|
|
|
|
BasicRealWxController(rootNode, this )
|
2010-09-11 16:11:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-26 09:01:31 +02:00
|
|
|
void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandler, const std::string & id )
|
2010-09-11 16:11:35 +02:00
|
|
|
{
|
2011-08-26 09:01:31 +02:00
|
|
|
class NoaaMetarGetRequest : public simgear::HTTP::Request
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NoaaMetarGetRequest(MetarDataHandler* metarDataHandler, const string& stationId ) :
|
|
|
|
Request("http://weather.noaa.gov/pub/data/observations/metar/stations/" + stationId + ".TXT"),
|
|
|
|
_fromProxy(false),
|
|
|
|
_metarDataHandler(metarDataHandler)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual string_list requestHeaders() const
|
|
|
|
{
|
|
|
|
string_list reply;
|
|
|
|
reply.push_back("X-TIME");
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string header(const std::string& name) const
|
|
|
|
{
|
|
|
|
string reply;
|
|
|
|
|
|
|
|
if( name == "X-TIME" ) {
|
2011-10-17 17:41:59 +01:00
|
|
|
std::ostringstream buf;
|
2011-08-26 09:01:31 +02:00
|
|
|
buf << globals->get_time_params()->get_cur_time();
|
|
|
|
reply = buf.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void responseHeader(const string& key, const string& value)
|
|
|
|
{
|
|
|
|
if (key == "x-metarproxy") {
|
|
|
|
_fromProxy = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void gotBodyData(const char* s, int n)
|
|
|
|
{
|
|
|
|
_metar += string(s, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void responseComplete()
|
|
|
|
{
|
|
|
|
if (responseCode() == 200) {
|
2011-12-31 17:31:13 +01:00
|
|
|
_metarDataHandler->handleMetarData( simgear::strutils::simplify(_metar) );
|
2011-08-26 09:01:31 +02:00
|
|
|
} else {
|
|
|
|
SG_LOG(SG_ENVIRONMENT, SG_WARN, "metar download failed:" << url() << ": reason:" << responseReason());
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 19:41:31 +00:00
|
|
|
|
|
|
|
virtual void failed()
|
|
|
|
{
|
|
|
|
SG_LOG(SG_ENVIRONMENT, SG_INFO, "metar download failure");
|
|
|
|
}
|
2011-08-26 09:01:31 +02:00
|
|
|
|
2012-03-06 22:28:18 +01:00
|
|
|
// bool fromMetarProxy() const
|
|
|
|
// { return _fromProxy; }
|
2011-08-26 09:01:31 +02:00
|
|
|
private:
|
|
|
|
string _metar;
|
|
|
|
bool _fromProxy;
|
|
|
|
MetarDataHandler * _metarDataHandler;
|
|
|
|
};
|
|
|
|
|
2012-09-23 16:55:29 +01:00
|
|
|
string upperId = boost::to_upper_copy(id);
|
2011-08-26 09:01:31 +02:00
|
|
|
|
2012-09-23 16:55:29 +01:00
|
|
|
SG_LOG(SG_ENVIRONMENT, SG_INFO,
|
|
|
|
"NoaaMetarRealWxController::update(): spawning load request for station-id '" << upperId << "'" );
|
2012-11-19 11:30:12 +00:00
|
|
|
FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
|
|
|
|
if (http) {
|
|
|
|
http->makeRequest(new NoaaMetarGetRequest(metarDataHandler, upperId));
|
|
|
|
}
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
RealWxController * RealWxController::createInstance( SGPropertyNode_ptr rootNode )
|
|
|
|
{
|
2012-09-23 16:55:29 +01:00
|
|
|
return new NoaaMetarRealWxController( rootNode );
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
} // namespace Environment
|