Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: NWSWeather.cpp
- Revision:
- 2:eae60b64066e
- Parent:
- 1:e077d6502c94
- Child:
- 4:94dfdc405640
--- a/NWSWeather.cpp Sun Mar 16 16:03:57 2014 +0000 +++ b/NWSWeather.cpp Sun Mar 16 20:25:34 2014 +0000 @@ -1,5 +1,34 @@ +// +// 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 +// +#include "NWSWeather.h" -#include "NWSWeather.h" +//#define DEBUG "NWS " +// ... +// INFO("Stuff to show %d", var); // new-line is automatically appended +// +#if (defined(DEBUG) && !defined(TARGET_LPC11U24)) +#define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#else +#define INFO(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#endif static NWSWeather::XMLItem_T WeatherData[] = { @@ -61,35 +90,79 @@ NWSWeather::NWSReturnCode_T NWSWeather::get(const char * site, int responseSize) { - Timer timer; + NWSReturnCode_T retCode = nomemory; + char *url = NULL; char *message = (char *)malloc(responseSize); - if (message) { - char * url = (char *)malloc(strlen(m_baseurl) + strlen(site) + 5); + INFO("get(%s)", site); + if (!message) + ERR("no memory"); + while (message) { + url = (char *)malloc(strlen(m_baseurl) + strlen(site) + 5); if (url) { strcpy(url, m_baseurl); strcat(url, site); strcat(url, ".xml"); + INFO(" url: %s", url); http.setMaxRedirections(3); - //printf("get(%s)\r\n", url); - timer.start(); int ret = http.get(url, message, responseSize); - int elapsed = timer.read_ms(); if (!ret) { + INFO("ret is %d", ret); if (http.getHTTPResponseCode() >= 300 && http.getHTTPResponseCode() < 400) { - return noerror; // redirection that was not satisfied. + retCode = noerror; // redirection that was not satisfied. + break; } else { ParseWeatherXML(message); - return noerror; + retCode = noerror; + break; } } else { - return noresponse; + WARN("get returned %d, no response?", ret); + retCode = noresponse; + break; } } else { - return nomemory; + ERR("no memory"); + retCode = nomemory; + break; + } + } // while(...) but configured with break for only 1 pass. + INFO(" ret is %d", retCode); + if (url) + free(url); + if (message) + free(message); + INFO(" mem freed."); + return retCode; +} + + +NWSWeather::NWSReturnCode_T NWSWeather::isUpdated(uint16_t i) +{ + if (i < WeatherItemCount) { + if (WeatherData[i].updated) + return noerror; + else + return noupdate; + } else { + return badparameter; + } +} + + +NWSWeather::NWSReturnCode_T NWSWeather::isUpdated(const char * name) +{ + if (name) { + for (int i=0; i < WeatherItemCount; i++) { + if (strcmp(name, WeatherData[i].name) == 0) { + if (WeatherData[i].updated) + return noerror; + else + return noupdate; + } } } - return nomemory; + return badparameter; } @@ -107,27 +180,17 @@ } -NWSWeather::NWSReturnCode_T NWSWeather::isUpdated(uint16_t i) -{ - if (i < WeatherItemCount && WeatherData[i].updated) - return noerror; - else - return noupdate; -} - - NWSWeather::NWSReturnCode_T NWSWeather::getParam(const char *name, Value_T *value, TypeOf_T *typeis) { - if (value) { - for (int i=0; i < WeatherItemCount; i++) { - //printf("Compare(%s,%s)\r\n", name, WeatherData[i].name); - if (strcmp(name, WeatherData[i].name) == 0) { - *value = WeatherData[i].value; - //printf(" assignment.\r\n"); - if (typeis) - *typeis = WeatherData[i].typeis; - return noerror; - } + INFO("getParam(%s)", name); + for (int i=0; i < WeatherItemCount; i++) { + //printf("Compare(%s,%s)\r\n", name, WeatherData[i].name); + if (strcmp(name, WeatherData[i].name) == 0) { + *value = WeatherData[i].value; + INFO("assigned value."); + if (typeis) + *typeis = WeatherData[i].typeis; + return noerror; } } return badparameter;