David Smart / NWSWeather
Revision:
17:99f09cc697fb
Parent:
9:fcd83b2bd0cd
Child:
18:699590fd8856
--- a/NWSWeather.h	Sun Nov 13 02:08:54 2016 +0000
+++ b/NWSWeather.h	Wed Jan 09 12:39:06 2019 +0000
@@ -1,7 +1,7 @@
 /// @file NWSWeather.h
 /// This file contains the interface for gathering forecast from NWS.
 ///
-/// @attention This program is copyright (c) 2014 by Smartware Computing, all 
+/// @attention This program is copyright (c) 2014 - 2019 by Smartware Computing, all 
 /// rights reserved.
 /// 
 /// This software, and/or material is the property of Smartware Computing. All
@@ -19,14 +19,27 @@
 #include "mbed.h"
 #include "HTTPClient.h"
 
-/// NOAA's National Weather Service handler class.
+// Set one of these to a 1, depending on your data source.
+#define WEATHER_GOV 0    // www.weather.gov (which went from http to https Jan 2019
+#define OPEN_WEATHER 1   // openweathermap.org (which so far remains http)
+
+
+#if WEATHER_GOV == 1
+static const char DEF_URL[] = "http://www.weather.gov/data/current_obs/";
+#elif OPEN_WEATHER == 1
+static const char DEF_URL[] = "http://api.openweathermap.org/data/2.5/weather?mode=xml&units=imperial&";
+#endif
+
+/// Weather Service
 ///
 /// This class will fetch and parse weather data from an XML feed
-/// using a URL such as "http://www.weather.gov/data/current_obs/KALO.xml".
+/// using a URL such as 
+///     + https://www.weather.gov/data/current_obs/KALO.xml
+///     + http://api.openweathermap.org/data/2.5/weather?mode=xml&units=imperial&id=<Loc_ID>&APPID=<User_ID>
 ///
-/// @attention Proper use of this should follow the NWS guideline,
+/// @attention Proper use of this should follow the guideline,
 ///     which is to not request this data from the server more often than
-///     necessary, and ideally to recognize and use the suggested_pickup
+///     necessary, and if available to recognize and use the suggested_pickup
 ///     parameter for subsequent updates of the weather information. This
 ///     class does not monitor the RTC so is unaware of the time of day. It
 ///     is therefore up to the user to manage this recommended practice.
@@ -35,6 +48,7 @@
 /// from which you can derive others. It turns out this is also most of the
 /// parameters.
 ///
+/// From weather.gov
 /// \li observation_time_rfc822 - "Sun, 16 Mar 2014 09:54:00 -0500"
 /// \li        suggested_pickup - "15 minutes after the hour"
 /// \li suggested_pickup_period - 60 (minutes)
@@ -57,6 +71,28 @@
 /// \li                latitude
 /// \li               longitude
 ///
+/// From openweathermap.org
+/// \li <current>
+/// \li <city id="4880889" name="Waterloo">
+/// \li <coord lat="42.49" lon="-92.34"/>
+/// \li <country>US</country>
+/// \li <sun set="2019-01-08T22:54:13" rise="2019-01-08T13:38:34"/>
+/// \li </city>
+/// \li <temperature unit="fahrenheit" max="36.68" min="33.26" value="34.92"/>
+/// \li <humidity unit="%" value="93"/>
+/// \li <pressure unit="hPa" value="1009"/>
+/// \li <wind>
+/// \li <speed name="Gale" value="18.34"/>
+/// \li <gusts value="10.8"/>
+/// \li <direction name="North-northeast" value="340" code="NNW"/>
+/// \li </wind>
+/// \li <clouds name="scattered clouds" value="40"/>
+/// \li <visibility value="16093"/>
+/// \li <precipitation mode="no"/>
+/// \li <weather value="scattered clouds" icon="03n" number="802"/>
+/// \li <lastupdate value="2019-01-08T12:15:00"/>
+/// \li </current>
+/// 
 class NWSWeather
 {
 public:
@@ -109,12 +145,17 @@
     /// to search based on either zip code or city, state.
     ///
     /// It is possible to construct the NWSWeather object with an alternate
-    /// base url, but this is only useful if the alternate site is xml
+    /// base url, but this is only useful if the alternate site is xml format
     /// compatible.
     ///
+    /// 
     /// @code
     /// NWSWeather wx;
+    /// #if WEATHER_GOV == 1
     /// wx.get("KALO"};
+    /// #elif OPEN_WEATHER == 1
+    /// wx.get(4880889, "user_id");
+    /// #endif
     /// wx.PrintAllWeatherRecords();
     /// @endcode
     ///
@@ -125,18 +166,28 @@
     /// @endcode
     /// 
     /// @param[in] baseurl is an optional parameter to set the base url. If this
-    ///     is not set "http://www.weather.gov/data/current_obs/" is used.
+    ///     is not set a default is used.
     ///     If it is set, that alternate base url is used.
-    ///
+    /// #ifdef WEATHER_GOV
+    ///     http://www.weather.gov/data/current_obs/
+    ///     appended are two parameters
+    ///     <loc_code>
+    ///     ".xml"
+    /// #elif OPEN_WEATHER == 1
+    ///     http://api.openweathermap.org/data/2.5/weather?mode=xml&units=imperial
+    ///     appended are two parameters
+    ///     &id=<loc_code>
+    ///     &APPID=<user_id>
+    /// #endif
     /// for future forecast
     ///     http://forecast.weather.gov/MapClick.php?lat=42.47508&lon=-92.36704926700929
     //          &unit=0&lg=english&FcstType=dwml
     ///
-    NWSWeather(const char * baseurl = "http://www.weather.gov/data/current_obs/");
+    NWSWeather(const char * baseurl = DEF_URL);
     
     /// Destructor.
     ~NWSWeather();
-    
+
     /// set an alternate base url after construction of the NWSWeather object.
     ///
     /// @param[in] alternateurl is the new url to replace the baseurl.
@@ -144,11 +195,12 @@
     ///
     NWSReturnCode_T setAlternateURL(const char * alternateurl);
     
+
+    #if WEATHER_GOV == 1
     /// get the current conditions weather data from the specified site.
     ///
-    /// This does the work to fetch the weather data from the specified site,
-    /// using the baseurl established when the NWSWeather object was constructed,
-    /// or when it was modified with the setAlternateURL API.
+    /// This does the work to fetch the weather data from weatherdata.gov,
+    /// for the site of interest.
     ///
     /// @param[in] site is the site/location code of the site of interest.
     /// @param[in] responseSize is optional but important. It defaults to 2500
@@ -157,7 +209,22 @@
     /// @returns success/failure code. @see NWSReturnCode_T.
     ///
     NWSReturnCode_T get(const char * site, int responseSize = 2500);
-    
+    #elif OPEN_WEATHER == 1    
+    /// get the current conditions weather data from the specified site.
+    ///
+    /// This does the work to fetch the weather data from openweathermap.org,
+    /// for the site of interest.
+    ///
+    /// @param[in] site is the site/location code string of the site of interest.
+    /// @param[in] userid is the code string registered with the site to access the data.
+    /// @param[in] responseSize is optional but important. It defaults to 2500
+    ///         and is intended to tell this method the size of a buffer it
+    ///         should temporarily allocate to receive and process the data.
+    /// @returns success/failure code. @see NWSReturnCode_T.
+    ///
+    NWSReturnCode_T get(const char * site, const char * userid, int responseSize = 2500);
+    #endif
+        
     /// get the count of the number of weather parameters.
     ///
     /// @returns the count of the number of parameters for which