Get weather Information
WeatherHacks.cpp
- Committer:
- takashikojo
- Date:
- 2015-06-27
- Revision:
- 0:6d53d6d34ac3
- Child:
- 1:8655f1720450
File content as of revision 0:6d53d6d34ac3:
#include <mbed.h> #include "WeatherHacks.h" #include "EthernetInterface.h" #include "HTTPClient.h" #include "XMLaide.h" #define RSS_URL "http://weather.livedoor.com/forecast/rss/area/130010.xml" #if 0 //Enable debug #define DBG(x, ...) std::printf("[WeatherHacks : DBG]"x"\r\n", ##__VA_ARGS__); #define WARN(x, ...) std::printf("[WeatherHacks : WARN]"x"\r\n", ##__VA_ARGS__); #else //Disable debug #define DBG(x, ...) #define WARN(x, ...) #endif #define ERR(x, ...) std::printf("[WeatherHacks : ERR]"x"\r\n", ##__VA_ARGS__); static HTTPClient http; void WH_init(void) { } #define LINK_SIZE 100 static void getLink(const char *buff, char *link) { const char *p = buff ; p = XML_getTag(p, "item") ; p = XML_getTag(p, "item") ; p = XML_getTag(p, "link") ; p = XML_getElement(p, link, LINK_SIZE) ; } static const char *getTodayP(const char *p) { return XML_getTag(p, "table") ; } static const char *getTomorrowP(const char *p) { p = XML_getTag(p, "table") ; p = XML_getTag(p, "table") ; return XML_getTag(p, "table") ; } #define TEMP_SIZE 5 static const char *getHiTemp(const char *p, char *hi) { p = XML_getTag(p, "table") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "span") ; p = XML_getElement(p, hi, 2) ; return p ; } static const char *getLoTemp(const char *p, char *lo) { p = XML_getTag(p, "tr") ; p = XML_getTag(p, "tr") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "span") ; p = XML_getElement(p, lo, 2) ; return p ; } static const char *getPrec(const char *p, char *prec) { p = XML_getTag(p, "th") ; p = XML_getTag(p, "th") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; p = XML_getElement(p, prec, 2) ; if(strcmp(prec, "--") == 0)strcpy(prec, " 0") ; return p ; } static void getInfo(const char *buff, char *hi, char *lo, char *prec) { #define JST (9*60*60) struct tm t; time_t ctTime; ctTime = time(NULL) + JST ; t = *localtime(&ctTime); const char *p ; if(t.tm_hour >= 12) { p = getTomorrowP(buff) ; } else { p = getTodayP(buff) ; } p = getHiTemp(p, hi) ; p = getLoTemp(p, lo) ; p = getPrec(p, prec) ; } #define FREE(p) if(p)free(p) #define BUFF_SIZE 1024*12 void WH_getInfo(void) { int ret ; char *recv = (char *)malloc(BUFF_SIZE) ; char *link = (char *)malloc(LINK_SIZE) ; char hiTemp[TEMP_SIZE] ; char loTemp[TEMP_SIZE] ; char prec[TEMP_SIZE] ; if((recv == NULL) || (link == NULL)){ FREE(recv) ; FREE(link) ; ERR("No Memory, WH_getInfo") ; return ; } ret = http.get(RSS_URL, recv, BUFF_SIZE); if (!ret) { //DBG("Result: %s\n", recv); } else { ERR("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); } getLink(recv, link) ; DBG("link=%s", link) ; ret = http.get(link, recv, BUFF_SIZE); if (!ret) { DBG("Result: %s\n", recv); } else { ERR("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); } getInfo(recv, hiTemp, loTemp, prec) ; printf("Hi=%s, Lo=%s, Prec=%s\n", hiTemp, loTemp, prec) ; FREE(recv) ; FREE(link) ; }