David Smart / NWSWeather

NWSWeather.h

Committer:
WiredHome
Date:
2014-03-16
Revision:
2:eae60b64066e
Parent:
1:e077d6502c94
Child:
3:41504de1f387

File content as of revision 2:eae60b64066e:

/// @file NWSWeather.h
/// This file contains the interface for gathering forecast from NWS.
///
/// @attention This program is copyright (c) 2014 by Smartware Computing, all 
/// rights reserved.
/// 
/// This software, and/or material is the property of Smartware Computing. All
/// use, disclosure, and/or reproduction not specifically authorized by Smartware
/// Computing is prohibited.
///
/// This software may be freely used for non-commercial purposes, and any 
/// derivative work shall carry the original copyright. The author of
/// a derivative work shall make clear that it is a derivative work.
///
/// @author David Smart, Smartware Computing
///
#ifndef NWSWEATHER_H
#define NWSWEATHER_H
#include "mbed.h"
#include "HTTPClient.h"

/// NOAA's National Weather Service handler class.
///
/// 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".
///
/// @attention Proper use of this should follow the NWS guideline,
///     which is to not request this data from the server more often than
///     necessary, and ideally 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.
///
/// Only the essential parameters are gathered, which is generally those
/// from which you can derive others. It turns out this is also most of the
/// parameters.
///
/// \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)
/// \li                location
/// \li                 weather
/// \li                  temp_f
/// \li                  temp_c
/// \li       relative_humidity
/// \li            wind_degrees
/// \li                wind_mph
/// \li             pressure_mb
/// \li             pressure_in
/// \li              dewpoint_f
/// \li              dewpoint_c
/// \li             windchill_f
/// \li             windchill_c
/// \li           visibility_mi
/// \li           icon_url_base - http://forecast.weather.gov/images/wtf/small/
/// \li           icon_url_name - ovc.png (changes based on forecast)
/// \li                latitude
/// \li               longitude
///
class NWSWeather
{
public:
    /// Return code interpretation.
    typedef enum
    {
        noerror,        ///< no error, or parameter has been updated.
        nomemory,       ///< insufficient memory to complete operation.
        noresponse,     ///< no response from server.
        badparameter,   ///< bad parameter to function (typically out of range value).
        noupdate,       ///< no update available.
        noparamfound    ///< no parameter found.
    } NWSReturnCode_T;
    
    /// Identifies the type of a parameter.
    typedef enum
    {
        isFloat,        ///< this parameter is a floating point value.
        isInt,          ///< this parameter is an integer.
        isString        ///< this parameter is a string value.
    } TypeOf_T;
    
    /// The union of available types.
    typedef union
    {
        float  fValue;  ///< the floating point value
        int    iValue;  ///< the integer value
        char * sValue;  ///< the string value
    } Value_T;
    
    /// The internal maintenance structure.
    typedef struct 
    {
        char * name;
        TypeOf_T typeis;
        bool updated;
        Value_T value;
    } XMLItem_T;


    /// Constructor.
    ///
    /// Create the object to acquire the weather information from NOAA.
    /// The full form of a valid url to acquire the data from is:
    /// "http://www.weather.gov/data/current_obs/SITE.xml", where SITE
    /// is replaced by the proper site location code.
    ///
    /// location codes are available at http://weather.noaa.gov/data/nsd_bbsss.txt,
    /// and you can use the http://forecast.weather.gov/zipcity.php search tool
    /// 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
    /// compatible.
    ///
    /// @code
    /// NWSWeather wx;
    /// wx.get("KALO"};
    /// wx.PrintAllWeatherRecords();
    /// @endcode
    ///
    /// @code
    /// NWSWeather wx("http://some.alternate.site/path/");
    /// wx.get("KALO"};
    /// wx.PrintAllWeatherRecords();
    /// @endcode
    /// 
    /// @param baseurl is an optional parameter to set the base url. If this
    ///     is not set "http://www.weather.gov/data/current_obs/" is used.
    ///     If it is set, that alternate base url is used.
    ///
    NWSWeather(const char * baseurl = "http://www.weather.gov/data/current_obs/");
    
    /// Destructor.
    ~NWSWeather();
    
    /// set an alternate base url after construction of the NWSWeather object.
    ///
    /// @param alternateurl is the new url to replace the baseurl.
    /// @param returns success/failure code. @see NWSReturnCode_T.
    ///
    NWSReturnCode_T setAlternateURL(const char * alternateurl);
    
    /// get the 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.
    ///
    /// @param site is the site/location code of the site of interest.
    /// @param 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.
    /// @param returns success/failure code. @see NWSReturnCode_T.
    ///
    NWSReturnCode_T get(const char * site, int responseSize = 2500);
    
    /// get the count of the number of weather parameters.
    ///
    /// @returns the count of the number of parameters for which 
    ///     data is expected.
    uint16_t count(void);

    /// determines if a specific parameter was updated from the last
    /// acquisition.
    ///
    /// @param i is the item of interest.
    /// @returns noerror if the parameter is up to date. @see NWSReturnCode_T.
    ///
    NWSReturnCode_T isUpdated(uint16_t i);
    
    /// determines if a specific parameter was updated from the last
    /// acquisition.
    ///
    /// @param name is the item of interest.
    /// @returns noerror if the parameter is up to date. @see NWSReturnCode_T.
    ///
    NWSReturnCode_T isUpdated(const char * name);
    
    /// get one of the weather parameters.
    ///
    /// This fetches one of the available weather parameters by setting
    /// user supplied pointers to reference the parameter.
    ///
    /// @code
    /// // Iterate over each of the parameters
    /// for (i=0; i<wx.count(); i++) {
    ///     char * name; TypeOf_T type; char * value;
    ///     if (wx.getParam(i, name, &type, value)) {
    ///         // print the values
    ///     }
    /// }
    /// @endcode
    ///
    /// @code
    /// // Get the names of the available parameters.
    /// for (i=0; i<wx.count(); i++) {
    ///     char *name;
    ///     if (wx.getParam(i, name) == noerror) {
    ///         // print the names of the parameters
    ///     }
    /// }
    /// @endcode
    ///
    /// @param i is index of the parameter of interest.
    /// @param name is a pointer that is set to point to the name of parameter i.
    /// @param value is an optional pointer that is then set to point to the value of parameter i.
    /// @param typeis is an optional pointer that is set to indicate the type of the value.
    /// @param returns success/failure code. @see NWSReturnCode_T.
    ///
    NWSReturnCode_T getParam(uint16_t i, char *name, Value_T *value = NULL, TypeOf_T *typeis = NULL);
    
    /// get one of the weather parameters.
    ///
    /// This searchs the parameter set for the named parameter, and if found
    /// it will set the user supplied pointers to the value and type information
    /// for that parameter.
    ///
    /// @code
    /// float value; TypeOf_T type;
    /// if (wx.getParam("temp_f", &value, &type) == noerror) {
    ///     // print the values
    /// }
    /// @endcode
    ///
    /// @param name is a const pointer to a string naming the parameter of interest.
    /// @param value is a pointer that is set to point to the value of parameter i.
    /// @param typeis is a pointer that is set to indicate the type of the value.
    /// @param returns success/failure code. @see NWSReturnCode_T.
    ///
    NWSReturnCode_T getParam(const char *name, Value_T *value, TypeOf_T *typeis = NULL);
    
    /// Print to stdout the specified weather record.
    ///
    /// Prints the specified record as a "Parmeter = value\r\n" string, formatted
    /// so that subsequent prints line up at the '=' sign.
    ///
    /// @param i specifies the record.
    ///
    void PrintWeatherRecord(uint16_t i);

    /// Printa all the records to stdout.
    ///
    /// calls PrintWeatherRecord for each available parameter.
    ///
    void PrintAllWeatherRecords(void);
    
private:
    HTTPClient http;
    char * m_baseurl;
    NWSReturnCode_T ParseWeatherRecord(char * p);
    void ParseWeatherXML(char * message);
    void ClearWeatherRecords(void);
};
#endif // NWSWEATHER_H