Train status
tokyoMetro.cpp@6:d4d781f31f70, 2015-11-22 (annotated)
- Committer:
- takashikojo
- Date:
- Sun Nov 22 08:47:56 2015 +0000
- Revision:
- 6:d4d781f31f70
- Parent:
- 5:6105553963bb
Serial Terminal message macro
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:a59f55690685 | 1 | #include "mbed.h" |
takashikojo | 0:a59f55690685 | 2 | #include "EthernetInterface.h" |
takashikojo | 0:a59f55690685 | 3 | #include "HTTPClient.h" |
takashikojo | 0:a59f55690685 | 4 | #include "vector" |
takashikojo | 0:a59f55690685 | 5 | #include "picojson.h" |
takashikojo | 4:acfd6fbf9f9e | 6 | #include <SDFileSystem.h> |
takashikojo | 0:a59f55690685 | 7 | |
takashikojo | 0:a59f55690685 | 8 | #include "AlarmClock.h" |
takashikojo | 4:acfd6fbf9f9e | 9 | #include "matchLine.h" |
takashikojo | 0:a59f55690685 | 10 | |
takashikojo | 5:6105553963bb | 11 | #define ACCESS_TOKEN "/sd/TMetroToken.txt" |
takashikojo | 4:acfd6fbf9f9e | 12 | |
takashikojo | 0:a59f55690685 | 13 | #define API_URL "https://api.tokyometroapp.jp:443/api/v2" |
takashikojo | 0:a59f55690685 | 14 | |
takashikojo | 0:a59f55690685 | 15 | #if 0 |
takashikojo | 0:a59f55690685 | 16 | //Enable debug |
takashikojo | 1:26a0a9220f01 | 17 | #define DBG(x, ...) std::printf("[tokyoMetro : DBG]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 1:26a0a9220f01 | 18 | #define WARN(x, ...) std::printf("[tokyoMetro : WARN]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 0:a59f55690685 | 19 | #else |
takashikojo | 0:a59f55690685 | 20 | //Disable debug |
takashikojo | 0:a59f55690685 | 21 | #define DBG(x, ...) |
takashikojo | 0:a59f55690685 | 22 | #define WARN(x, ...) |
takashikojo | 0:a59f55690685 | 23 | #endif |
takashikojo | 0:a59f55690685 | 24 | |
takashikojo | 1:26a0a9220f01 | 25 | #define ERR(x, ...) std::printf("[tokyoMetro : ERR]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 0:a59f55690685 | 26 | |
takashikojo | 0:a59f55690685 | 27 | extern AlarmClock alarmclock ; |
takashikojo | 2:58e7fabcba89 | 28 | extern HTTPClient httpClient; |
takashikojo | 4:acfd6fbf9f9e | 29 | extern matchLine watchList ; |
takashikojo | 4:acfd6fbf9f9e | 30 | extern SDFileSystem sdCard ; |
takashikojo | 0:a59f55690685 | 31 | |
takashikojo | 0:a59f55690685 | 32 | static picojson::value trainStat ; |
takashikojo | 0:a59f55690685 | 33 | |
takashikojo | 4:acfd6fbf9f9e | 34 | static std::map<std::string, string> lineTbl ; |
takashikojo | 5:6105553963bb | 35 | void TMetro_initLine(void) |
takashikojo | 5:6105553963bb | 36 | { |
takashikojo | 4:acfd6fbf9f9e | 37 | lineTbl["odpt.Railway:TokyoMetro.Tozai"] = "東京メトロ東西線 " ; |
takashikojo | 5:6105553963bb | 38 | lineTbl["odpt.Railway:TokyoMetro.Marunouchi"] = "東京メトロ丸の内線" ; |
takashikojo | 5:6105553963bb | 39 | lineTbl["odpt.Railway:TokyoMetro.Namboku"] = "東京メトロ南北線 " ; |
takashikojo | 5:6105553963bb | 40 | lineTbl["odpt.Railway:TokyoMetro.Hibiya"] = "東京メトロ日比谷線" ; |
takashikojo | 5:6105553963bb | 41 | lineTbl["odpt.Railway:TokyoMetro.Fukutoshin"] = "東京メトロ副都心線" ; |
takashikojo | 5:6105553963bb | 42 | lineTbl["odpt.Railway:TokyoMetro.Hanzomon"] = "東京メトロ半蔵門線" ; |
takashikojo | 5:6105553963bb | 43 | lineTbl["odpt.Railway:TokyoMetro.Ginza"] = "東京メトロ銀座線 " ; |
takashikojo | 5:6105553963bb | 44 | lineTbl["odpt.Railway:TokyoMetro.Yurakucho"] = "東京メトロ有楽町線" ; |
takashikojo | 4:acfd6fbf9f9e | 45 | lineTbl["odpt.Railway:TokyoMetro.Chiyoda"] = "東京メトロ千代田線" ; |
takashikojo | 4:acfd6fbf9f9e | 46 | } |
takashikojo | 4:acfd6fbf9f9e | 47 | |
takashikojo | 4:acfd6fbf9f9e | 48 | #define TOKEN_SIZE 100 |
takashikojo | 4:acfd6fbf9f9e | 49 | static char accessToken[TOKEN_SIZE] = { 0x0 } ; |
takashikojo | 4:acfd6fbf9f9e | 50 | |
takashikojo | 4:acfd6fbf9f9e | 51 | static void removeCRLF(char *str) |
takashikojo | 4:acfd6fbf9f9e | 52 | { |
takashikojo | 5:6105553963bb | 53 | for(int i = strlen(str)-1; i>0 ; i--) { |
takashikojo | 4:acfd6fbf9f9e | 54 | if((str[strlen(str)-1] == '\n') || (str[strlen(str)-1] == '\r')) |
takashikojo | 4:acfd6fbf9f9e | 55 | str[strlen(str)-1] = '\0' ; |
takashikojo | 4:acfd6fbf9f9e | 56 | else break ; |
takashikojo | 4:acfd6fbf9f9e | 57 | } |
takashikojo | 4:acfd6fbf9f9e | 58 | } |
takashikojo | 4:acfd6fbf9f9e | 59 | |
takashikojo | 4:acfd6fbf9f9e | 60 | static const char *getToken() |
takashikojo | 4:acfd6fbf9f9e | 61 | { |
takashikojo | 4:acfd6fbf9f9e | 62 | FILE *fp ; |
takashikojo | 4:acfd6fbf9f9e | 63 | if(accessToken[0] == 0x0) { |
takashikojo | 4:acfd6fbf9f9e | 64 | fp = fopen(ACCESS_TOKEN, "r"); |
takashikojo | 4:acfd6fbf9f9e | 65 | if (fp == NULL) { |
takashikojo | 4:acfd6fbf9f9e | 66 | ERR("Cannot open \"%s\"\n", ACCESS_TOKEN) ; |
takashikojo | 4:acfd6fbf9f9e | 67 | return false ; |
takashikojo | 4:acfd6fbf9f9e | 68 | } |
takashikojo | 4:acfd6fbf9f9e | 69 | fgets(accessToken, sizeof(accessToken), fp) ; |
takashikojo | 4:acfd6fbf9f9e | 70 | removeCRLF(accessToken) ; |
takashikojo | 5:6105553963bb | 71 | fclose(fp) ; |
takashikojo | 5:6105553963bb | 72 | TMetro_initLine() ; |
takashikojo | 4:acfd6fbf9f9e | 73 | } |
takashikojo | 4:acfd6fbf9f9e | 74 | return accessToken ; |
takashikojo | 4:acfd6fbf9f9e | 75 | } |
takashikojo | 4:acfd6fbf9f9e | 76 | |
takashikojo | 4:acfd6fbf9f9e | 77 | bool TMetro_query(const char *type, const char *query, char *recv, unsigned int size) |
takashikojo | 4:acfd6fbf9f9e | 78 | { |
takashikojo | 0:a59f55690685 | 79 | int ret ; |
takashikojo | 4:acfd6fbf9f9e | 80 | #define BUFF_SIZE 256 |
takashikojo | 0:a59f55690685 | 81 | char queryBuff[BUFF_SIZE] ; |
takashikojo | 4:acfd6fbf9f9e | 82 | sprintf(queryBuff, "%s/%s?rdf:type=%s&acl:consumerKey=%s", API_URL, type, query, getToken()) ; |
takashikojo | 2:58e7fabcba89 | 83 | DBG("%s",queryBuff) ; |
takashikojo | 2:58e7fabcba89 | 84 | ret = httpClient.get(queryBuff, recv, size); |
takashikojo | 0:a59f55690685 | 85 | if (!ret) { |
takashikojo | 0:a59f55690685 | 86 | DBG("Result: %s\n", recv); |
takashikojo | 2:58e7fabcba89 | 87 | return true ; |
takashikojo | 0:a59f55690685 | 88 | } else { |
takashikojo | 2:58e7fabcba89 | 89 | ERR("Error - ret = %d - HTTP return code = %d\n", ret, httpClient.getHTTPResponseCode()); |
takashikojo | 2:58e7fabcba89 | 90 | return false ; |
takashikojo | 0:a59f55690685 | 91 | } |
takashikojo | 0:a59f55690685 | 92 | } |
takashikojo | 0:a59f55690685 | 93 | |
takashikojo | 4:acfd6fbf9f9e | 94 | static void printStat(const char *line, const char *stat) |
takashikojo | 4:acfd6fbf9f9e | 95 | { |
takashikojo | 4:acfd6fbf9f9e | 96 | #define JST (9*60*60) |
takashikojo | 0:a59f55690685 | 97 | struct tm t; |
takashikojo | 0:a59f55690685 | 98 | time_t ctTime; |
takashikojo | 0:a59f55690685 | 99 | ctTime = time(NULL) + JST ; |
takashikojo | 0:a59f55690685 | 100 | t = *localtime(&ctTime); |
takashikojo | 4:acfd6fbf9f9e | 101 | |
takashikojo | 4:acfd6fbf9f9e | 102 | printf("%d月%d日%d時%d分:%s:%s\n", |
takashikojo | 4:acfd6fbf9f9e | 103 | t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, line, stat) ; |
takashikojo | 0:a59f55690685 | 104 | } |
takashikojo | 0:a59f55690685 | 105 | |
takashikojo | 4:acfd6fbf9f9e | 106 | bool TMetro_getStat(const char *buff) |
takashikojo | 4:acfd6fbf9f9e | 107 | { |
takashikojo | 0:a59f55690685 | 108 | |
takashikojo | 0:a59f55690685 | 109 | std::string err; |
takashikojo | 0:a59f55690685 | 110 | picojson::parse(trainStat, (const char *)buff, (const char *)buff+strlen(buff), &err); |
takashikojo | 0:a59f55690685 | 111 | if (!err.empty()) { |
takashikojo | 0:a59f55690685 | 112 | ERR("Metro Site Result ERROR: %s", err.c_str()); |
takashikojo | 1:26a0a9220f01 | 113 | return false ; |
takashikojo | 0:a59f55690685 | 114 | } |
takashikojo | 0:a59f55690685 | 115 | picojson::array array = trainStat.get<picojson::array>(); |
takashikojo | 4:acfd6fbf9f9e | 116 | for (picojson::array::iterator it = array.begin(); it != array.end(); it++) { |
takashikojo | 0:a59f55690685 | 117 | picojson::object& obj = it->get<picojson::object>(); |
takashikojo | 6:d4d781f31f70 | 118 | DBG("Line=%s, %s, %s\n", obj["odpt:railway"].get<std::string>().c_str(), |
takashikojo | 6:d4d781f31f70 | 119 | lineTbl[obj["odpt:railway"].get<std::string>()].c_str(), |
takashikojo | 6:d4d781f31f70 | 120 | obj["odpt:trainInformationText"].get<std::string>()) ; |
takashikojo | 4:acfd6fbf9f9e | 121 | if(watchList.find(lineTbl[obj["odpt:railway"].get<std::string>()])) { |
takashikojo | 0:a59f55690685 | 122 | printStat( |
takashikojo | 0:a59f55690685 | 123 | lineTbl[obj["odpt:railway"].get<std::string>()].c_str(), |
takashikojo | 0:a59f55690685 | 124 | obj["odpt:trainInformationText"].get<std::string>().c_str()); |
takashikojo | 0:a59f55690685 | 125 | if(obj["odpt:trainInformationText"].get<std::string>().find("平常どおり") == std::string::npos) |
takashikojo | 4:acfd6fbf9f9e | 126 | return true ; |
takashikojo | 0:a59f55690685 | 127 | } |
takashikojo | 0:a59f55690685 | 128 | } |
takashikojo | 4:acfd6fbf9f9e | 129 | return false ; |
takashikojo | 0:a59f55690685 | 130 | } |