Get weather Information
WeatherHacks.cpp@1:8655f1720450, 2015-06-28 (annotated)
- Committer:
- takashikojo
- Date:
- Sun Jun 28 00:17:02 2015 +0000
- Revision:
- 1:8655f1720450
- Parent:
- 0:6d53d6d34ac3
- Child:
- 2:76b812680942
Added Weather Hacks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:6d53d6d34ac3 | 1 | #include <mbed.h> |
takashikojo | 0:6d53d6d34ac3 | 2 | #include "WeatherHacks.h" |
takashikojo | 0:6d53d6d34ac3 | 3 | |
takashikojo | 0:6d53d6d34ac3 | 4 | #include "EthernetInterface.h" |
takashikojo | 0:6d53d6d34ac3 | 5 | #include "HTTPClient.h" |
takashikojo | 0:6d53d6d34ac3 | 6 | #include "XMLaide.h" |
takashikojo | 0:6d53d6d34ac3 | 7 | |
takashikojo | 0:6d53d6d34ac3 | 8 | #define RSS_URL "http://weather.livedoor.com/forecast/rss/area/130010.xml" |
takashikojo | 0:6d53d6d34ac3 | 9 | |
takashikojo | 0:6d53d6d34ac3 | 10 | #if 0 |
takashikojo | 0:6d53d6d34ac3 | 11 | //Enable debug |
takashikojo | 0:6d53d6d34ac3 | 12 | #define DBG(x, ...) std::printf("[WeatherHacks : DBG]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 0:6d53d6d34ac3 | 13 | #define WARN(x, ...) std::printf("[WeatherHacks : WARN]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 0:6d53d6d34ac3 | 14 | #else |
takashikojo | 0:6d53d6d34ac3 | 15 | //Disable debug |
takashikojo | 0:6d53d6d34ac3 | 16 | #define DBG(x, ...) |
takashikojo | 0:6d53d6d34ac3 | 17 | #define WARN(x, ...) |
takashikojo | 0:6d53d6d34ac3 | 18 | #endif |
takashikojo | 0:6d53d6d34ac3 | 19 | |
takashikojo | 0:6d53d6d34ac3 | 20 | #define ERR(x, ...) std::printf("[WeatherHacks : ERR]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 0:6d53d6d34ac3 | 21 | |
takashikojo | 1:8655f1720450 | 22 | extern HTTPClient httpClient; |
takashikojo | 0:6d53d6d34ac3 | 23 | |
takashikojo | 0:6d53d6d34ac3 | 24 | void WH_init(void) |
takashikojo | 0:6d53d6d34ac3 | 25 | { |
takashikojo | 0:6d53d6d34ac3 | 26 | |
takashikojo | 0:6d53d6d34ac3 | 27 | } |
takashikojo | 0:6d53d6d34ac3 | 28 | |
takashikojo | 0:6d53d6d34ac3 | 29 | #define LINK_SIZE 100 |
takashikojo | 0:6d53d6d34ac3 | 30 | static void getLink(const char *buff, char *link) |
takashikojo | 0:6d53d6d34ac3 | 31 | { |
takashikojo | 0:6d53d6d34ac3 | 32 | const char *p = buff ; |
takashikojo | 0:6d53d6d34ac3 | 33 | p = XML_getTag(p, "item") ; p = XML_getTag(p, "item") ; p = XML_getTag(p, "link") ; |
takashikojo | 0:6d53d6d34ac3 | 34 | p = XML_getElement(p, link, LINK_SIZE) ; |
takashikojo | 0:6d53d6d34ac3 | 35 | } |
takashikojo | 0:6d53d6d34ac3 | 36 | |
takashikojo | 0:6d53d6d34ac3 | 37 | static const char *getTodayP(const char *p) { |
takashikojo | 0:6d53d6d34ac3 | 38 | return XML_getTag(p, "table") ; |
takashikojo | 0:6d53d6d34ac3 | 39 | } |
takashikojo | 0:6d53d6d34ac3 | 40 | |
takashikojo | 0:6d53d6d34ac3 | 41 | static const char *getTomorrowP(const char *p) { |
takashikojo | 0:6d53d6d34ac3 | 42 | p = XML_getTag(p, "table") ; p = XML_getTag(p, "table") ; |
takashikojo | 0:6d53d6d34ac3 | 43 | return XML_getTag(p, "table") ; |
takashikojo | 0:6d53d6d34ac3 | 44 | } |
takashikojo | 0:6d53d6d34ac3 | 45 | |
takashikojo | 0:6d53d6d34ac3 | 46 | #define TEMP_SIZE 5 |
takashikojo | 0:6d53d6d34ac3 | 47 | static const char *getHiTemp(const char *p, char *hi) |
takashikojo | 0:6d53d6d34ac3 | 48 | { |
takashikojo | 0:6d53d6d34ac3 | 49 | p = XML_getTag(p, "table") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; |
takashikojo | 0:6d53d6d34ac3 | 50 | p = XML_getTag(p, "span") ; |
takashikojo | 0:6d53d6d34ac3 | 51 | p = XML_getElement(p, hi, 2) ; |
takashikojo | 0:6d53d6d34ac3 | 52 | return p ; |
takashikojo | 0:6d53d6d34ac3 | 53 | } |
takashikojo | 0:6d53d6d34ac3 | 54 | |
takashikojo | 0:6d53d6d34ac3 | 55 | static const char *getLoTemp(const char *p, char *lo) |
takashikojo | 0:6d53d6d34ac3 | 56 | { |
takashikojo | 0:6d53d6d34ac3 | 57 | p = XML_getTag(p, "tr") ; p = XML_getTag(p, "tr") ; |
takashikojo | 0:6d53d6d34ac3 | 58 | p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; |
takashikojo | 0:6d53d6d34ac3 | 59 | p = XML_getTag(p, "span") ; |
takashikojo | 0:6d53d6d34ac3 | 60 | p = XML_getElement(p, lo, 2) ; |
takashikojo | 0:6d53d6d34ac3 | 61 | return p ; |
takashikojo | 0:6d53d6d34ac3 | 62 | } |
takashikojo | 0:6d53d6d34ac3 | 63 | |
takashikojo | 0:6d53d6d34ac3 | 64 | static const char *getPrec(const char *p, char *prec) |
takashikojo | 0:6d53d6d34ac3 | 65 | { |
takashikojo | 0:6d53d6d34ac3 | 66 | p = XML_getTag(p, "th") ; p = XML_getTag(p, "th") ; |
takashikojo | 0:6d53d6d34ac3 | 67 | p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; |
takashikojo | 0:6d53d6d34ac3 | 68 | p = XML_getElement(p, prec, 2) ; |
takashikojo | 0:6d53d6d34ac3 | 69 | if(strcmp(prec, "--") == 0)strcpy(prec, " 0") ; |
takashikojo | 1:8655f1720450 | 70 | if(strcmp(prec, "0%") == 0)strcpy(prec, " 0") ; |
takashikojo | 0:6d53d6d34ac3 | 71 | return p ; |
takashikojo | 0:6d53d6d34ac3 | 72 | } |
takashikojo | 0:6d53d6d34ac3 | 73 | |
takashikojo | 0:6d53d6d34ac3 | 74 | static void getInfo(const char *buff, char *hi, char *lo, char *prec) |
takashikojo | 0:6d53d6d34ac3 | 75 | { |
takashikojo | 0:6d53d6d34ac3 | 76 | #define JST (9*60*60) |
takashikojo | 0:6d53d6d34ac3 | 77 | struct tm t; |
takashikojo | 0:6d53d6d34ac3 | 78 | time_t ctTime; |
takashikojo | 0:6d53d6d34ac3 | 79 | ctTime = time(NULL) + JST ; |
takashikojo | 0:6d53d6d34ac3 | 80 | t = *localtime(&ctTime); |
takashikojo | 0:6d53d6d34ac3 | 81 | const char *p ; |
takashikojo | 0:6d53d6d34ac3 | 82 | |
takashikojo | 0:6d53d6d34ac3 | 83 | if(t.tm_hour >= 12) { |
takashikojo | 0:6d53d6d34ac3 | 84 | p = getTomorrowP(buff) ; |
takashikojo | 0:6d53d6d34ac3 | 85 | } else { |
takashikojo | 0:6d53d6d34ac3 | 86 | p = getTodayP(buff) ; |
takashikojo | 0:6d53d6d34ac3 | 87 | } |
takashikojo | 0:6d53d6d34ac3 | 88 | p = getHiTemp(p, hi) ; |
takashikojo | 0:6d53d6d34ac3 | 89 | p = getLoTemp(p, lo) ; |
takashikojo | 0:6d53d6d34ac3 | 90 | p = getPrec(p, prec) ; |
takashikojo | 0:6d53d6d34ac3 | 91 | } |
takashikojo | 0:6d53d6d34ac3 | 92 | |
takashikojo | 0:6d53d6d34ac3 | 93 | #define FREE(p) if(p)free(p) |
takashikojo | 0:6d53d6d34ac3 | 94 | #define BUFF_SIZE 1024*12 |
takashikojo | 0:6d53d6d34ac3 | 95 | |
takashikojo | 1:8655f1720450 | 96 | void WH_getInfo(char *buff, int size) |
takashikojo | 0:6d53d6d34ac3 | 97 | { |
takashikojo | 0:6d53d6d34ac3 | 98 | int ret ; |
takashikojo | 1:8655f1720450 | 99 | #define LINK_SIZE 200 |
takashikojo | 1:8655f1720450 | 100 | char link[LINK_SIZE] ; |
takashikojo | 0:6d53d6d34ac3 | 101 | char hiTemp[TEMP_SIZE] ; |
takashikojo | 0:6d53d6d34ac3 | 102 | char loTemp[TEMP_SIZE] ; |
takashikojo | 0:6d53d6d34ac3 | 103 | char prec[TEMP_SIZE] ; |
takashikojo | 0:6d53d6d34ac3 | 104 | |
takashikojo | 1:8655f1720450 | 105 | ret = httpClient.get(RSS_URL, buff, size); |
takashikojo | 0:6d53d6d34ac3 | 106 | if (!ret) { |
takashikojo | 0:6d53d6d34ac3 | 107 | //DBG("Result: %s\n", recv); |
takashikojo | 0:6d53d6d34ac3 | 108 | } else { |
takashikojo | 1:8655f1720450 | 109 | ERR("Error - ret = %d - HTTP return code = %d\n", ret, httpClient.getHTTPResponseCode()); |
takashikojo | 0:6d53d6d34ac3 | 110 | } |
takashikojo | 1:8655f1720450 | 111 | |
takashikojo | 1:8655f1720450 | 112 | getLink(buff, link) ; DBG("link=%s\n", link) ; |
takashikojo | 1:8655f1720450 | 113 | ret = httpClient.get(link, buff, size) ; |
takashikojo | 1:8655f1720450 | 114 | getInfo(buff, hiTemp, loTemp, prec); |
takashikojo | 0:6d53d6d34ac3 | 115 | printf("Hi=%s, Lo=%s, Prec=%s\n", hiTemp, loTemp, prec) ; |
takashikojo | 0:6d53d6d34ac3 | 116 | } |