]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/metar_main.cxx
Adapt logging level for some messages.
[flightgear.git] / src / Main / metar_main.cxx
index 03805806b6a903b57067c3ded0a98d5b9a9885b5..7b904d3a245f492f97246ca1a2613c766926557e 100644 (file)
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
 #include <iomanip>
 #include <sstream>
+#include <iostream>
 #include <string.h>
+#include <time.h>
+#include <cstdlib>
 
 #include <simgear/debug/logstream.hxx>
-#include <simgear/structure/exception.hxx>
 #include <simgear/environment/metar.hxx>
+#include <simgear/structure/exception.hxx>
+
+#include <simgear/io/HTTPClient.hxx>
+#include <simgear/io/HTTPRequest.hxx>
+#include <simgear/timing/timestamp.hxx>
 
 using namespace std;
+using namespace simgear;
+
+class MetarRequest : public HTTP::Request
+{
+public:
+    bool complete;
+    bool failed;
+    string metarData;
+    bool fromProxy;
+    
+    MetarRequest(const std::string& stationId) : 
+        HTTP::Request("http://weather.noaa.gov/pub/data/observations/metar/stations/" + stationId + ".TXT"),
+        complete(false),
+        failed(false)
+    {
+        fromProxy = false;
+    }
+    
+protected:
+    
+    virtual void responseHeader(const string& key, const string& value)
+    {
+        if (key == "x-metarproxy") {
+            fromProxy = true;
+        }
+    }
+
+    virtual void gotBodyData(const char* s, int n)
+    {
+        metarData += string(s, n);
+    }
+
+    virtual void responseComplete()
+    {
+        if (responseCode() == 200) {
+            complete = true;
+        } else {
+            SG_LOG(SG_ENVIRONMENT, SG_WARN, "metar download failed:" << url() << ": reason:" << responseReason());
+            failed = true;
+        }
+    }
+};
 
 // text color
-#if defined(__linux__) || defined( __sun__ ) ||defined(__CYGWIN__) || defined( __FreeBSD__ ) || defined ( sgi )
+#if defined(__linux__) || defined(__sun) || defined(__CYGWIN__) \
+    || defined( __FreeBSD__ ) || defined ( sgi )
 #      define R "\033[31;1m"           // red
 #      define G "\033[32;1m"           // green
 #      define Y "\033[33;1m"           // yellow
@@ -74,11 +124,11 @@ const char *azimuthName(double d)
 double rnd(double r, int g = 0)
 {
        double f = pow(10.0, g);
-       return f * rint(r / f);
+       return f * floor(r / f + 0.5);
 }
 
 
-ostream& operator<<(ostream& s, SGMetarVisibility& v)
+ostream& operator<<(ostream& s, const SGMetarVisibility& v)
 {
        ostringstream buf;
        int m = v.getModifier();
@@ -133,9 +183,7 @@ void printReport(SGMetar *m)
        // date/time
        int year = m->getYear();
        int month = m->getMonth();
-       cout << "Report time:\t\t";
-       if (year != -1 && month != -1)
-               cout << year << '/' << month << '/' << m->getDay();
+       cout << "Report time:\t\t" << year << '/' << month << '/' << m->getDay();
        cout << ' ' << m->getHour() << ':';
        cout << setw(2) << setfill('0') << m->getMinute() << " UTC" << endl;
 
@@ -155,7 +203,7 @@ void printReport(SGMetar *m)
 
 
        // directed visibility
-       SGMetarVisibility *dirvis = m->getDirVisibility();
+       const SGMetarVisibility *dirvis = m->getDirVisibility();
        for (i = 0; i < 8; i++, dirvis++)
                if (dirvis->getVisibility_m() != NaN)
                        cout << "\t\t\t" << *dirvis << endl;
@@ -270,7 +318,7 @@ void printReport(SGMetar *m)
                if ((s = rwy.getExtentString()) && strlen(s))
                        surface.push_back(s);
                if ((d = rwy.getDepth()) != NaN) {
-                       sprintf(buf, "%.0lf mm", d * 1000.0);
+                       sprintf(buf, "%.1lf mm", d * 1000.0);
                        surface.push_back(buf);
                }
                if ((s = rwy.getFrictionString()) && strlen(s))
@@ -460,7 +508,7 @@ void getproxy(string& host, string& port)
 }
 
 
-void help()
+void usage()
 {
        printf(
                "Usage: metar [-v] [-e elevation] [-r|-c] <list of ICAO airport ids or METAR strings>\n"
@@ -485,7 +533,6 @@ void help()
        );
 }
 
-
 int main(int argc, char *argv[])
 {
        bool report = true;
@@ -493,16 +540,19 @@ int main(int argc, char *argv[])
        double elevation = 0.0;
 
        if (argc <= 1) {
-               help();
+               usage();
                return 0;
        }
 
        string proxy_host, proxy_port;
        getproxy(proxy_host, proxy_port);
 
+    HTTP::Client http;
+    http.setProxy(proxy_host, atoi(proxy_port.c_str()));
+    
        for (int i = 1; i < argc; i++) {
                if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
-                       help();
+                       usage();
                else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose"))
                        verbose = true;
                else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--report"))
@@ -518,13 +568,31 @@ int main(int argc, char *argv[])
                } else {
                        static bool shown = false;
                        if (verbose && !shown) {
-                               cout << "Proxy host: '" << proxy_host << "'" << endl;
-                               cout << "Proxy port: '" << proxy_port << "'" << endl << endl;
+                               cerr << "Proxy host: '" << proxy_host << "'" << endl;
+                               cerr << "Proxy port: '" << proxy_port << "'" << endl << endl;
                                shown = true;
                        }
 
                        try {
-                               SGMetar *m = new SGMetar(argv[i], proxy_host, proxy_port);
+                MetarRequest* mr = new MetarRequest(argv[i]);
+                HTTP::Request_ptr own(mr);
+                http.makeRequest(mr);
+                
+            // spin until the request completes, fails or times out
+                SGTimeStamp start(SGTimeStamp::now());
+                while (start.elapsedMSec() <  8000) {
+                    http.update();
+                    if (mr->complete || mr->failed) {
+                        break;
+                    }
+                    SGTimeStamp::sleepForMSec(1);
+                }
+                
+                if (!mr->complete) {
+                    throw sg_io_exception("metar download failed (or timed out)");
+                }
+                               SGMetar *m = new SGMetar(mr->metarData);
+                               
                                //SGMetar *m = new SGMetar("2004/01/11 01:20\nLOWG 110120Z AUTO VRB01KT 0050 1600N R35/0600 FG M06/M06 Q1019 88//////\n");
 
                                if (verbose) {
@@ -533,7 +601,6 @@ int main(int argc, char *argv[])
                                        const char *unused = m->getUnusedData();
                                        if (*unused)
                                                cerr << R"UNUSED: " << unused << ""N << endl;
-
                                }
 
                                if (report)