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.
yahooTrain.cpp@3:5720d24f4a4a, 2015-06-28 (annotated)
- Committer:
- takashikojo
- Date:
- Sun Jun 28 12:21:33 2015 +0000
- Revision:
- 3:5720d24f4a4a
- Parent:
- 2:58e7fabcba89
- Child:
- 4:acfd6fbf9f9e
Weather, Train stat displayed on LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 1:26a0a9220f01 | 1 | #include "mbed.h" |
takashikojo | 1:26a0a9220f01 | 2 | #include <string> |
takashikojo | 1:26a0a9220f01 | 3 | #include <map> |
takashikojo | 1:26a0a9220f01 | 4 | #include "EthernetInterface.h" |
takashikojo | 1:26a0a9220f01 | 5 | #include "HTTPClient.h" |
takashikojo | 1:26a0a9220f01 | 6 | #include "XMLaide.h" |
takashikojo | 3:5720d24f4a4a | 7 | #include "WatchList.h" |
takashikojo | 1:26a0a9220f01 | 8 | |
takashikojo | 1:26a0a9220f01 | 9 | #define API_URL "http://transit.loco.yahoo.co.jp/traininfo/" |
takashikojo | 1:26a0a9220f01 | 10 | |
takashikojo | 1:26a0a9220f01 | 11 | #if 0 |
takashikojo | 1:26a0a9220f01 | 12 | //Enable debug |
takashikojo | 1:26a0a9220f01 | 13 | #define DBG(x, ...) std::printf("[yahooTrain : DBG]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 1:26a0a9220f01 | 14 | #define WARN(x, ...) std::printf("[yahooTrain : WARN]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 1:26a0a9220f01 | 15 | #else |
takashikojo | 1:26a0a9220f01 | 16 | //Disable debug |
takashikojo | 1:26a0a9220f01 | 17 | #define DBG(x, ...) |
takashikojo | 1:26a0a9220f01 | 18 | #define WARN(x, ...) |
takashikojo | 1:26a0a9220f01 | 19 | #endif |
takashikojo | 1:26a0a9220f01 | 20 | |
takashikojo | 1:26a0a9220f01 | 21 | #define ERR(x, ...) std::printf("[yahooTrain : ERR]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 1:26a0a9220f01 | 22 | |
takashikojo | 2:58e7fabcba89 | 23 | extern HTTPClient httpClient; |
takashikojo | 3:5720d24f4a4a | 24 | extern WatchList watchlist ; |
takashikojo | 1:26a0a9220f01 | 25 | |
takashikojo | 2:58e7fabcba89 | 26 | bool YahooT_query(const char *query, char *recv, unsigned int size) |
takashikojo | 1:26a0a9220f01 | 27 | { |
takashikojo | 1:26a0a9220f01 | 28 | int ret ; |
takashikojo | 1:26a0a9220f01 | 29 | #define BUFF_SIZE 256 |
takashikojo | 1:26a0a9220f01 | 30 | char queryBuff[BUFF_SIZE] ; |
takashikojo | 1:26a0a9220f01 | 31 | sprintf(queryBuff, "%s%s", API_URL, query) ; |
takashikojo | 1:26a0a9220f01 | 32 | DBG("%s", queryBuff) ; |
takashikojo | 3:5720d24f4a4a | 33 | memset(recv, '\0', size) ; |
takashikojo | 2:58e7fabcba89 | 34 | ret = httpClient.get(queryBuff, recv, size); |
takashikojo | 1:26a0a9220f01 | 35 | if (!ret) { |
takashikojo | 3:5720d24f4a4a | 36 | //DBG("Result: %s\n", recv); |
takashikojo | 2:58e7fabcba89 | 37 | return true ; |
takashikojo | 1:26a0a9220f01 | 38 | } else { |
takashikojo | 2:58e7fabcba89 | 39 | ERR("Error - ret = %d - HTTP return code = %d\n", ret, httpClient.getHTTPResponseCode()); |
takashikojo | 2:58e7fabcba89 | 40 | return false ; |
takashikojo | 1:26a0a9220f01 | 41 | } |
takashikojo | 1:26a0a9220f01 | 42 | } |
takashikojo | 1:26a0a9220f01 | 43 | |
takashikojo | 1:26a0a9220f01 | 44 | static std::map<std::string, string> lineTbl ; |
takashikojo | 1:26a0a9220f01 | 45 | void YahooT_initLine(void) |
takashikojo | 1:26a0a9220f01 | 46 | { |
takashikojo | 1:26a0a9220f01 | 47 | lineTbl["odpt.Railway:TokyoMetro.Tozai"] = "東西線 " ; |
takashikojo | 1:26a0a9220f01 | 48 | lineTbl["odpt.Railway:TokyoMetro.Marunouchi"] = "丸の内線" ; |
takashikojo | 1:26a0a9220f01 | 49 | lineTbl["odpt.Railway:TokyoMetro.Namboku"] = "南北線 " ; |
takashikojo | 1:26a0a9220f01 | 50 | lineTbl["odpt.Railway:TokyoMetro.Hibiya"] = "日比谷線" ; |
takashikojo | 1:26a0a9220f01 | 51 | lineTbl["odpt.Railway:TokyoMetro.Fukutoshin"] = "副都心線" ; |
takashikojo | 1:26a0a9220f01 | 52 | lineTbl["odpt.Railway:TokyoMetro.Hanzomon"] = "半蔵門線" ; |
takashikojo | 1:26a0a9220f01 | 53 | lineTbl["odpt.Railway:TokyoMetro.Ginza"] = "銀座線 " ; |
takashikojo | 1:26a0a9220f01 | 54 | lineTbl["odpt.Railway:TokyoMetro.Yurakucho"] = "有楽町線" ; |
takashikojo | 1:26a0a9220f01 | 55 | lineTbl["odpt.Railway:TokyoMetro.Chiyoda"] = "千代田線" ; |
takashikojo | 1:26a0a9220f01 | 56 | } |
takashikojo | 1:26a0a9220f01 | 57 | |
takashikojo | 1:26a0a9220f01 | 58 | static string watchingLine = "" ; |
takashikojo | 1:26a0a9220f01 | 59 | |
takashikojo | 1:26a0a9220f01 | 60 | void YahooT_setLine(string name) |
takashikojo | 1:26a0a9220f01 | 61 | { |
takashikojo | 1:26a0a9220f01 | 62 | map<string, string>::iterator it = lineTbl.begin(); ; |
takashikojo | 1:26a0a9220f01 | 63 | |
takashikojo | 1:26a0a9220f01 | 64 | for(it = lineTbl.begin(); it != lineTbl.end(); ++it) { |
takashikojo | 1:26a0a9220f01 | 65 | if(it->second == name) { |
takashikojo | 1:26a0a9220f01 | 66 | watchingLine = it->first ; |
takashikojo | 1:26a0a9220f01 | 67 | } |
takashikojo | 1:26a0a9220f01 | 68 | } |
takashikojo | 1:26a0a9220f01 | 69 | } |
takashikojo | 1:26a0a9220f01 | 70 | |
takashikojo | 1:26a0a9220f01 | 71 | bool YahooT_getStat(const char *buff) |
takashikojo | 1:26a0a9220f01 | 72 | { |
takashikojo | 1:26a0a9220f01 | 73 | const char *p = buff ; |
takashikojo | 1:26a0a9220f01 | 74 | const char *endTd ; |
takashikojo | 1:26a0a9220f01 | 75 | const char *endTbl ; |
takashikojo | 1:26a0a9220f01 | 76 | const char *next ; |
takashikojo | 1:26a0a9220f01 | 77 | const char *span ; |
takashikojo | 1:26a0a9220f01 | 78 | #define LINE_SIZE 40 |
takashikojo | 1:26a0a9220f01 | 79 | #define STAT_SIZE 40 |
takashikojo | 1:26a0a9220f01 | 80 | #define DESC_SIZE 200 |
takashikojo | 1:26a0a9220f01 | 81 | char line[LINE_SIZE] ; |
takashikojo | 1:26a0a9220f01 | 82 | char stat[STAT_SIZE] ; |
takashikojo | 1:26a0a9220f01 | 83 | char desc[DESC_SIZE] ; |
takashikojo | 3:5720d24f4a4a | 84 | bool delay = false ; |
takashikojo | 3:5720d24f4a4a | 85 | |
takashikojo | 1:26a0a9220f01 | 86 | while(1) { |
takashikojo | 1:26a0a9220f01 | 87 | if((p = XML_getTag(p, "table")) == NULL)break ; |
takashikojo | 1:26a0a9220f01 | 88 | endTbl = XML_getTag(p, "/table") ; |
takashikojo | 1:26a0a9220f01 | 89 | while(next = XML_getTag(p, "td")) { |
takashikojo | 3:5720d24f4a4a | 90 | if(next == NULL)break ; |
takashikojo | 1:26a0a9220f01 | 91 | if(next > endTbl)break ; |
takashikojo | 1:26a0a9220f01 | 92 | p = XML_getTag(p, "a") ; |
takashikojo | 2:58e7fabcba89 | 93 | p = XML_getElement(p, line, LINE_SIZE) ; |
takashikojo | 1:26a0a9220f01 | 94 | p = XML_getTag(p, "td"); |
takashikojo | 1:26a0a9220f01 | 95 | endTd = XML_getTag(p, "/td") ; |
takashikojo | 1:26a0a9220f01 | 96 | span = XML_getTag(p, "span"); |
takashikojo | 1:26a0a9220f01 | 97 | if((span != NULL) && (span < endTd)) { |
takashikojo | 1:26a0a9220f01 | 98 | p = XML_getTag(span, "span"); |
takashikojo | 2:58e7fabcba89 | 99 | p = XML_getElement(p, stat, STAT_SIZE) ; |
takashikojo | 1:26a0a9220f01 | 100 | } else { |
takashikojo | 2:58e7fabcba89 | 101 | p = XML_getElement(p, stat, STAT_SIZE) ; |
takashikojo | 1:26a0a9220f01 | 102 | } |
takashikojo | 1:26a0a9220f01 | 103 | p = XML_getTag(p, "td"); |
takashikojo | 2:58e7fabcba89 | 104 | p = XML_getElement(p, desc, DESC_SIZE) ; |
takashikojo | 3:5720d24f4a4a | 105 | if(watchlist.check(line) && (string)stat != "平常運転"){ |
takashikojo | 3:5720d24f4a4a | 106 | delay = true ; |
takashikojo | 3:5720d24f4a4a | 107 | printf("Delay=>%s[%s]:%s\n", line, stat, desc) ; |
takashikojo | 3:5720d24f4a4a | 108 | |
takashikojo | 3:5720d24f4a4a | 109 | } |
takashikojo | 3:5720d24f4a4a | 110 | DBG("%s[%s]:%s\n", line, stat, desc) ; |
takashikojo | 1:26a0a9220f01 | 111 | } |
takashikojo | 3:5720d24f4a4a | 112 | DBG("End of Table") ; |
takashikojo | 1:26a0a9220f01 | 113 | } |
takashikojo | 3:5720d24f4a4a | 114 | DBG("End of Buff") ; |
takashikojo | 3:5720d24f4a4a | 115 | return delay ; |
takashikojo | 1:26a0a9220f01 | 116 | } |
takashikojo | 1:26a0a9220f01 | 117 | |
takashikojo | 3:5720d24f4a4a | 118 |