Get weather Information

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WeatherHacks.cpp Source File

WeatherHacks.cpp

00001 #include <mbed.h>
00002 #include "WeatherHacks.h"
00003 
00004 #include "EthernetInterface.h"
00005 #include "HTTPClient.h"
00006 #include "XMLaide.h"
00007 
00008 #define RSS_URL      "http://weather.livedoor.com/forecast/rss/area/130010.xml"
00009 
00010 #if 0
00011 //Enable debug
00012 #define DBG(x, ...) std::printf("[WeatherHacks : DBG]"x"\r\n", ##__VA_ARGS__);
00013 #define WARN(x, ...) std::printf("[WeatherHacks : WARN]"x"\r\n", ##__VA_ARGS__);
00014 #else
00015 //Disable debug
00016 #define DBG(x, ...)
00017 #define WARN(x, ...)
00018 #endif
00019 
00020 #define ERR(x, ...) std::printf("[WeatherHacks : ERR]"x"\r\n", ##__VA_ARGS__);
00021 
00022 #if 0
00023 #define TERMINAL(x, ...) std::printf("[WeatherHacks :]"x"\r\n", ##__VA_ARGS__);
00024 #else
00025 #define TERMINAL(x, ...) 
00026 #endif
00027 
00028 extern HTTPClient httpClient;
00029 
00030 void WH_init(void) 
00031 {
00032     
00033 }
00034 
00035 #define LINK_SIZE 200
00036 static void getLink(const char *buff, char *link)
00037 {
00038     const char *p = buff ;
00039     p = XML_getTag(p, "item") ; p = XML_getTag(p, "item") ; p = XML_getTag(p, "link") ;
00040     p = XML_getElement(p, link, LINK_SIZE) ;
00041 }
00042 
00043 static const char *getTodayP(const char *p) {
00044     return XML_getTag(p, "table") ;
00045 }
00046 
00047 static const char *getTomorrowP(const char *p) {
00048     p    = XML_getTag(p, "table") ; p = XML_getTag(p, "table") ;
00049     return XML_getTag(p, "table") ;
00050 }
00051 
00052 #define TEMP_SIZE 5
00053 static const char *getHiTemp(const char *p, char *hi)
00054 {
00055     p = XML_getTag(p, "table") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ;
00056     p = XML_getTag(p, "span") ;
00057     p = XML_getElement(p, hi, 2) ;
00058     return p ;
00059 }
00060 
00061 static const char *getLoTemp(const char *p, char *lo)
00062 {
00063     p = XML_getTag(p, "tr") ; p = XML_getTag(p, "tr") ;
00064     p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ;
00065     p = XML_getTag(p, "span") ;
00066     p = XML_getElement(p, lo, 2) ;
00067     return p ;
00068 }
00069 
00070 static const char *getPrec(const char *p, char *prec)
00071 {
00072     p = XML_getTag(p, "th") ; p = XML_getTag(p, "th") ;
00073     p = XML_getTag(p, "td") ;    p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ;
00074     p = XML_getElement(p, prec, 2) ;
00075     if(strcmp(prec, "0%") == 0)strcpy(prec, " 0") ;
00076     return p ;
00077 }
00078 
00079 static void getInfo(const char *buff, char *hi, char *lo, char *prec)
00080 {
00081     #define JST (9*60*60)
00082     struct tm t;
00083     time_t ctTime;
00084     ctTime = time(NULL) + JST ;
00085     t  = *localtime(&ctTime);
00086     TERMINAL("Time: % 2d:%02d => ", t.tm_hour, t.tm_min) ;
00087     const char *p ;
00088     
00089     if(t.tm_hour >= 12) {
00090         p = getTomorrowP(buff) ;
00091     } else {
00092         p = getTodayP(buff) ;
00093     }
00094     p = getHiTemp(p, hi) ;
00095     p = getLoTemp(p, lo) ;
00096     p = getPrec(p, prec) ;
00097     TERMINAL("Hi=%s, Lo=%s, Prec=%s\n", hi, lo, prec) ;
00098 }
00099 
00100 #define FREE(p) if(p)free(p)
00101 #define BUFF_SIZE 1024*12
00102 
00103 void WH_getInfo(char *buff, int size, char *hiTemp, char *loTemp, char *prec)
00104 {
00105     int ret ;
00106     char link[LINK_SIZE] ;
00107 
00108     ret = httpClient.get(RSS_URL, buff, size);
00109     if (!ret) {
00110         DBG("Result: %s\n", buff);
00111     } else {
00112         ERR("Error %s\n- ret = %d - HTTP return code = %d\n", RSS_URL, ret, httpClient.getHTTPResponseCode());
00113     }
00114     
00115     getLink(buff, link) ; DBG("link=%s\n", link) ;
00116     ret = httpClient.get(link, buff, size) ;
00117         if (!ret) {
00118         DBG("Result: %s\n", buff) ;
00119     } else {
00120         ERR("Error %s\n- ret = %d - HTTP return code = %d\n", link, ret, httpClient.getHTTPResponseCode());
00121     }
00122     getInfo(buff, hiTemp, loTemp, prec);
00123 
00124 }