2010-09-11 16:11:35 +02:00
|
|
|
// realwx_ctrl.cxx -- Process real weather data
|
|
|
|
//
|
|
|
|
// Written by David Megginson, started February 2002.
|
|
|
|
// Rewritten by Torsten Dreyer, August 2010
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
#include "metarproperties.hxx"
|
|
|
|
#include "metarairportfilter.hxx"
|
|
|
|
#include "fgmetar.hxx"
|
|
|
|
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
|
|
|
|
#include <simgear/structure/exception.hxx>
|
|
|
|
#include <simgear/misc/strutils.hxx>
|
2011-02-06 15:44:09 +01:00
|
|
|
#include <simgear/props/tiedpropertylist.hxx>
|
2010-09-11 16:11:35 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#if defined(ENABLE_THREADS)
|
|
|
|
#include <OpenThreads/Thread>
|
|
|
|
#include <simgear/threads/SGQueue.hxx>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace Environment {
|
|
|
|
|
2011-01-07 13:11:06 +01:00
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
class LiveMetarProperties : public MetarProperties {
|
|
|
|
public:
|
|
|
|
LiveMetarProperties( SGPropertyNode_ptr rootNode );
|
|
|
|
virtual ~LiveMetarProperties();
|
|
|
|
virtual void update( double dt );
|
|
|
|
|
|
|
|
virtual double getTimeToLive() const { return _timeToLive; }
|
|
|
|
virtual void setTimeToLive( double value ) { _timeToLive = value; }
|
|
|
|
private:
|
|
|
|
double _timeToLive;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef SGSharedPtr<LiveMetarProperties> LiveMetarProperties_ptr;
|
|
|
|
|
|
|
|
LiveMetarProperties::LiveMetarProperties( SGPropertyNode_ptr rootNode ) :
|
|
|
|
MetarProperties( rootNode ),
|
|
|
|
_timeToLive(0.0)
|
|
|
|
{
|
|
|
|
_tiedProperties.Tie("time-to-live", &_timeToLive );
|
|
|
|
}
|
|
|
|
|
|
|
|
LiveMetarProperties::~LiveMetarProperties()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveMetarProperties::update( double dt )
|
|
|
|
{
|
|
|
|
_timeToLive -= dt;
|
|
|
|
if( _timeToLive < 0.0 ) _timeToLive = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
class BasicRealWxController : public RealWxController
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BasicRealWxController( SGPropertyNode_ptr rootNode );
|
|
|
|
virtual ~BasicRealWxController ();
|
|
|
|
|
|
|
|
virtual void init ();
|
|
|
|
virtual void reinit ();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void bind();
|
|
|
|
void unbind();
|
2010-10-11 21:21:53 +02:00
|
|
|
void update( double dt );
|
|
|
|
|
|
|
|
virtual void update( bool first, double dt ) = 0;
|
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 _longitude_n;
|
|
|
|
SGPropertyNode_ptr _latitude_n;
|
|
|
|
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;
|
2010-10-11 21:21:53 +02:00
|
|
|
bool __enabled;
|
2011-02-06 15:44:09 +01:00
|
|
|
simgear::TiedPropertyList _tiedProperties;
|
2011-01-07 13:11:06 +01:00
|
|
|
; typedef std::vector<LiveMetarProperties_ptr> MetarPropertiesList;
|
|
|
|
MetarPropertiesList _metarProperties;
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode ) :
|
|
|
|
_rootNode(rootNode),
|
|
|
|
_longitude_n( fgGetNode( "/position/longitude-deg", true )),
|
|
|
|
_latitude_n( fgGetNode( "/position/latitude-deg", true )),
|
|
|
|
_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),
|
2011-01-07 13:11:06 +01:00
|
|
|
__enabled(false)
|
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(
|
|
|
|
fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ) ) );
|
|
|
|
|
|
|
|
PropertyList metars = rootNode->getChildren("metar");
|
|
|
|
for( PropertyList::size_type i = 1; i < metars.size(); i++ ) {
|
|
|
|
SG_LOG( SG_ALL, SG_INFO, "Adding metar properties at " << metars[i]->getStringValue() );
|
|
|
|
_metarProperties.push_back( new LiveMetarProperties(
|
|
|
|
fgGetNode( metars[i]->getStringValue(), true )));
|
|
|
|
}
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BasicRealWxController::~BasicRealWxController()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicRealWxController::init()
|
|
|
|
{
|
2010-10-11 21:21:53 +02:00
|
|
|
__enabled = false;
|
2010-09-11 16:11:35 +02:00
|
|
|
update(0); // fetch data ASAP
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicRealWxController::reinit()
|
|
|
|
{
|
2010-10-11 21:21:53 +02:00
|
|
|
__enabled = false;
|
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 )
|
|
|
|
{
|
|
|
|
if( _enabled ) {
|
2011-01-07 13:11:06 +01:00
|
|
|
bool firstIteration = !__enabled; // first iteration after being enabled?
|
|
|
|
|
|
|
|
// clock tick for every METAR in stock
|
|
|
|
for( MetarPropertiesList::iterator it = _metarProperties.begin();
|
|
|
|
it != _metarProperties.end(); it++ ) {
|
|
|
|
// first round? All received METARs are outdated
|
|
|
|
if( firstIteration ) (*it)->setTimeToLive( 0.0 );
|
|
|
|
(*it)->update(dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
update( firstIteration, dt );
|
2010-10-11 21:21:53 +02:00
|
|
|
__enabled = true;
|
|
|
|
} else {
|
|
|
|
__enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
class NoaaMetarRealWxController : public BasicRealWxController {
|
|
|
|
public:
|
|
|
|
NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
|
|
|
|
virtual ~NoaaMetarRealWxController();
|
2010-10-11 21:21:53 +02:00
|
|
|
virtual void update (bool first, double delta_time_sec);
|
2011-02-13 16:50:23 +01:00
|
|
|
virtual void shutdown ();
|
2010-09-11 16:11:35 +02:00
|
|
|
|
|
|
|
class MetarLoadRequest {
|
|
|
|
public:
|
2011-01-23 21:51:07 +01:00
|
|
|
MetarLoadRequest( const string & stationId ) :
|
|
|
|
_stationId(stationId),
|
|
|
|
_proxyHost(fgGetNode("/sim/presets/proxy/host", true)->getStringValue()),
|
|
|
|
_proxyPort(fgGetNode("/sim/presets/proxy/port", true)->getStringValue()),
|
|
|
|
_proxyAuth(fgGetNode("/sim/presets/proxy/authentication", true)->getStringValue())
|
|
|
|
{}
|
|
|
|
MetarLoadRequest( const MetarLoadRequest & other ) :
|
|
|
|
_stationId(other._stationId),
|
|
|
|
_proxyHost(other._proxyAuth),
|
|
|
|
_proxyPort(other._proxyPort),
|
|
|
|
_proxyAuth(other._proxyAuth)
|
|
|
|
{}
|
2010-09-11 16:11:35 +02:00
|
|
|
string _stationId;
|
|
|
|
string _proxyHost;
|
|
|
|
string _proxyPort;
|
|
|
|
string _proxyAuth;
|
|
|
|
private:
|
|
|
|
};
|
2011-01-07 13:11:06 +01:00
|
|
|
|
|
|
|
class MetarLoadResponse {
|
|
|
|
public:
|
|
|
|
MetarLoadResponse( const string & stationId, const string metar ) {
|
|
|
|
_stationId = stationId;
|
|
|
|
_metar = metar;
|
|
|
|
}
|
|
|
|
MetarLoadResponse( const MetarLoadResponse & other ) {
|
|
|
|
_stationId = other._stationId;
|
|
|
|
_metar = other._metar;
|
|
|
|
}
|
|
|
|
string _stationId;
|
|
|
|
string _metar;
|
|
|
|
};
|
2010-09-11 16:11:35 +02:00
|
|
|
private:
|
|
|
|
double _positionTimeToLive;
|
2011-01-07 13:11:06 +01:00
|
|
|
double _requestTimer;
|
2010-09-11 16:11:35 +02:00
|
|
|
|
|
|
|
#if defined(ENABLE_THREADS)
|
|
|
|
class MetarLoadThread : public OpenThreads::Thread {
|
|
|
|
public:
|
2010-10-11 21:21:53 +02:00
|
|
|
MetarLoadThread( long maxAge );
|
2011-02-13 16:50:23 +01:00
|
|
|
virtual ~MetarLoadThread( ) { stop(); }
|
2010-10-11 21:21:53 +02:00
|
|
|
void requestMetar( const MetarLoadRequest & metarRequest, bool background = true );
|
|
|
|
bool hasMetar() { return _responseQueue.size() > 0; }
|
2011-01-07 13:11:06 +01:00
|
|
|
MetarLoadResponse getMetar() { return _responseQueue.pop(); }
|
2010-10-11 21:21:53 +02:00
|
|
|
virtual void run();
|
2011-02-13 16:50:23 +01:00
|
|
|
void stop();
|
2010-09-11 16:11:35 +02:00
|
|
|
private:
|
2010-10-11 21:21:53 +02:00
|
|
|
void fetch( const MetarLoadRequest & );
|
2010-09-27 18:59:26 +02:00
|
|
|
long _maxAge;
|
2011-01-07 13:11:06 +01:00
|
|
|
long _minRequestInterval;
|
2011-02-13 16:50:23 +01:00
|
|
|
volatile bool _stop;
|
2010-09-11 16:11:35 +02:00
|
|
|
SGBlockingQueue <MetarLoadRequest> _requestQueue;
|
2011-01-07 13:11:06 +01:00
|
|
|
SGBlockingQueue <MetarLoadResponse> _responseQueue;
|
2010-09-11 16:11:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
MetarLoadThread * _metarLoadThread;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ) :
|
|
|
|
BasicRealWxController(rootNode),
|
|
|
|
_positionTimeToLive(0.0),
|
2011-01-07 13:11:06 +01:00
|
|
|
_requestTimer(0.0)
|
2010-09-11 16:11:35 +02:00
|
|
|
{
|
|
|
|
#if defined(ENABLE_THREADS)
|
2010-09-27 18:59:26 +02:00
|
|
|
_metarLoadThread = new MetarLoadThread(getMetarMaxAgeMin());
|
2010-09-11 16:11:35 +02:00
|
|
|
_metarLoadThread->start();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-02-13 16:50:23 +01:00
|
|
|
void NoaaMetarRealWxController::shutdown()
|
2010-09-11 16:11:35 +02:00
|
|
|
{
|
|
|
|
#if defined(ENABLE_THREADS)
|
|
|
|
if( _metarLoadThread ) {
|
|
|
|
delete _metarLoadThread;
|
2011-02-13 16:50:23 +01:00
|
|
|
_metarLoadThread = NULL;
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
#endif // ENABLE_THREADS
|
|
|
|
}
|
|
|
|
|
2011-02-13 16:50:23 +01:00
|
|
|
NoaaMetarRealWxController::~NoaaMetarRealWxController()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
void NoaaMetarRealWxController::update( bool first, double dt )
|
2010-09-11 16:11:35 +02:00
|
|
|
{
|
|
|
|
_positionTimeToLive -= dt;
|
2011-01-07 13:11:06 +01:00
|
|
|
_requestTimer -= dt;
|
2010-09-11 16:11:35 +02:00
|
|
|
|
2011-01-07 13:11:06 +01:00
|
|
|
if( _positionTimeToLive <= 0.0 ) {
|
|
|
|
// check nearest airport
|
|
|
|
SG_LOG(SG_ALL, SG_INFO, "NoaaMetarRealWxController::update(): (re) checking nearby airport with METAR" );
|
2010-09-11 16:11:35 +02:00
|
|
|
_positionTimeToLive = 60.0;
|
|
|
|
|
|
|
|
SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
|
|
|
|
|
|
|
|
FGAirport * nearestAirport = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
|
|
|
|
if( nearestAirport == NULL ) {
|
|
|
|
SG_LOG(SG_ALL,SG_WARN,"RealWxController::update can't find airport with METAR within 10000NM" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-07 13:11:06 +01:00
|
|
|
SG_LOG(SG_ALL, SG_INFO,
|
|
|
|
"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_ALL, SG_INFO,
|
|
|
|
"NoaaMetarRealWxController::update(): nearest airport with METAR has changed. Old: '" <<
|
|
|
|
_metarProperties[0]->getStationId() <<
|
|
|
|
"', new: '" << nearestAirport->ident() << "'" );
|
|
|
|
_metarProperties[0]->setStationId( nearestAirport->ident() );
|
|
|
|
_metarProperties[0]->setTimeToLive( 0.0 );
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-07 13:11:06 +01:00
|
|
|
|
|
|
|
if( _requestTimer <= 0.0 ) {
|
|
|
|
_requestTimer = 10.0;
|
|
|
|
|
|
|
|
for( MetarPropertiesList::iterator it = _metarProperties.begin();
|
|
|
|
it != _metarProperties.end(); it++ ) {
|
|
|
|
|
|
|
|
if( (*it)->getTimeToLive() > 0.0 ) continue;
|
|
|
|
const std::string & stationId = (*it)->getStationId();
|
|
|
|
if( stationId.empty() ) continue;
|
|
|
|
|
|
|
|
SG_LOG(SG_ALL, SG_INFO,
|
|
|
|
"NoaaMetarRealWxController::update(): spawning load request for station-id '" << stationId << "'" );
|
|
|
|
|
|
|
|
MetarLoadRequest request( stationId );
|
2011-02-13 16:50:23 +01:00
|
|
|
// load the metar for the nearest airport in the foreground if the fdm is uninitialized
|
2011-01-07 13:11:06 +01:00
|
|
|
// to make sure a metar is received
|
|
|
|
// before the automatic runway selection code runs. All subsequent calls
|
|
|
|
// run in the background
|
|
|
|
bool background = fgGetBool("/sim/fdm-initialized", false ) || it != _metarProperties.begin();
|
|
|
|
_metarLoadThread->requestMetar( request, background );
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 13:11:06 +01:00
|
|
|
// pick all the received responses from the result queue and update the associated
|
|
|
|
// property tree
|
|
|
|
while( _metarLoadThread->hasMetar() ) {
|
|
|
|
MetarLoadResponse metar = _metarLoadThread->getMetar();
|
|
|
|
SG_LOG( SG_ALL, SG_INFO, "NoaaMetarRwalWxController::update() received METAR for " << metar._stationId << ": " << metar._metar );
|
|
|
|
for( MetarPropertiesList::iterator it = _metarProperties.begin();
|
|
|
|
it != _metarProperties.end(); it++ ) {
|
|
|
|
if( (*it)->getStationId() != metar._stationId )
|
|
|
|
continue;
|
|
|
|
(*it)->setTimeToLive(900);
|
|
|
|
(*it)->setMetar( metar._metar );
|
|
|
|
}
|
2010-10-11 21:21:53 +02:00
|
|
|
}
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#if defined(ENABLE_THREADS)
|
2010-09-27 18:59:26 +02:00
|
|
|
NoaaMetarRealWxController::MetarLoadThread::MetarLoadThread( long maxAge ) :
|
2011-01-07 13:11:06 +01:00
|
|
|
_maxAge(maxAge),
|
2011-02-13 16:50:23 +01:00
|
|
|
_minRequestInterval(2000),
|
|
|
|
_stop(false)
|
2010-09-11 16:11:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest, bool background )
|
2010-09-11 16:11:35 +02:00
|
|
|
{
|
2010-10-11 21:21:53 +02:00
|
|
|
if( background ) {
|
|
|
|
if( _requestQueue.size() > 10 ) {
|
|
|
|
SG_LOG(SG_ALL,SG_ALERT,
|
|
|
|
"NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping "
|
|
|
|
<< metarRequest._stationId );
|
|
|
|
return;
|
|
|
|
}
|
2010-09-11 16:11:35 +02:00
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
_requestQueue.push( metarRequest );
|
|
|
|
} else {
|
|
|
|
fetch( metarRequest );
|
|
|
|
}
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
2011-02-13 16:50:23 +01:00
|
|
|
void NoaaMetarRealWxController::MetarLoadThread::stop()
|
|
|
|
{
|
|
|
|
// set stop flag and wake up the thread with an empty request
|
|
|
|
_stop = true;
|
|
|
|
MetarLoadRequest request("");
|
|
|
|
requestMetar(request);
|
|
|
|
join();
|
|
|
|
}
|
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
void NoaaMetarRealWxController::MetarLoadThread::run()
|
|
|
|
{
|
2011-01-07 13:11:06 +01:00
|
|
|
SGTimeStamp lastRun = SGTimeStamp::fromSec(0);
|
2010-09-11 16:11:35 +02:00
|
|
|
for( ;; ) {
|
2011-01-07 13:11:06 +01:00
|
|
|
SGTimeStamp dt = SGTimeStamp::now() - lastRun;
|
|
|
|
|
2011-02-13 16:50:23 +01:00
|
|
|
long delayMs = _minRequestInterval - dt.getSeconds() * 1000;
|
|
|
|
while (( delayMs > 0 ) && !_stop)
|
|
|
|
{
|
|
|
|
// sleep no more than 3 seconds at a time, otherwise shutdown response is too slow
|
|
|
|
long sleepMs = (delayMs>3000) ? 3000 : delayMs;
|
|
|
|
microSleep( sleepMs * 1000 );
|
|
|
|
delayMs -= sleepMs;
|
|
|
|
}
|
2011-01-07 13:11:06 +01:00
|
|
|
|
2011-02-13 16:50:23 +01:00
|
|
|
if (_stop)
|
|
|
|
break;
|
|
|
|
|
|
|
|
lastRun = SGTimeStamp::now();
|
|
|
|
|
2010-09-11 16:11:35 +02:00
|
|
|
const MetarLoadRequest request = _requestQueue.pop();
|
|
|
|
|
2011-02-13 16:50:23 +01:00
|
|
|
if (( request._stationId.size() == 0 ) || _stop)
|
2010-09-11 16:11:35 +02:00
|
|
|
break;
|
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
fetch( request );
|
|
|
|
}
|
|
|
|
}
|
2010-09-11 16:11:35 +02:00
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
void NoaaMetarRealWxController::MetarLoadThread::fetch( const MetarLoadRequest & request )
|
|
|
|
{
|
|
|
|
SGSharedPtr<FGMetar> result = NULL;
|
2010-09-11 16:11:35 +02:00
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
try {
|
|
|
|
result = new FGMetar( request._stationId, request._proxyHost, request._proxyPort, request._proxyAuth );
|
2011-01-07 13:11:06 +01:00
|
|
|
_minRequestInterval = 2000;
|
2010-10-11 21:21:53 +02:00
|
|
|
} catch (const sg_io_exception& e) {
|
|
|
|
SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): can't get METAR for "
|
|
|
|
<< request._stationId << ":" << e.getFormattedMessage().c_str() );
|
2011-01-07 13:11:06 +01:00
|
|
|
_minRequestInterval += _minRequestInterval/2;
|
|
|
|
if( _minRequestInterval > 30000 )
|
|
|
|
_minRequestInterval = 30000;
|
2010-10-11 21:21:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-09-27 18:59:26 +02:00
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
string reply = result->getData();
|
|
|
|
std::replace(reply.begin(), reply.end(), '\n', ' ');
|
|
|
|
string metar = simgear::strutils::strip( reply );
|
2010-09-27 18:59:26 +02:00
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
if( metar.empty() ) {
|
|
|
|
SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): dropping empty METAR for "
|
|
|
|
<< request._stationId );
|
|
|
|
return;
|
|
|
|
}
|
2010-09-27 18:59:26 +02:00
|
|
|
|
2010-10-11 21:21:53 +02:00
|
|
|
if( _maxAge && result->getAge_min() > _maxAge ) {
|
|
|
|
SG_LOG( SG_GENERAL, SG_ALERT, "NoaaMetarRealWxController::fetchMetar(): dropping outdated METAR "
|
|
|
|
<< metar );
|
|
|
|
return;
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
2010-10-11 21:21:53 +02:00
|
|
|
|
2011-01-07 13:11:06 +01:00
|
|
|
MetarLoadResponse response( request._stationId, metar );
|
|
|
|
_responseQueue.push( response );
|
2010-09-11 16:11:35 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
RealWxController * RealWxController::createInstance( SGPropertyNode_ptr rootNode )
|
|
|
|
{
|
|
|
|
// string dataSource = rootNode->getStringValue("data-source", "noaa" );
|
|
|
|
// if( dataSource == "nwx" ) {
|
|
|
|
// return new NwxMetarRealWxController( rootNode );
|
|
|
|
// } else {
|
|
|
|
return new NoaaMetarRealWxController( rootNode );
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
} // namespace Environment
|