Kojo / TrainStat
Committer:
takashikojo
Date:
Sun Jul 05 08:09:02 2015 +0000
Revision:
4:acfd6fbf9f9e
Parent:
2:58e7fabcba89
Child:
5:6105553963bb
access token and watch list in SD card

Who changed what in which revision?

UserRevisionLine numberNew 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 4:acfd6fbf9f9e 11 #define ACCESS_TOKEN "/sd/TMetroToken.txt" // "6d60c19c9497e9b2f5b847d31ab70cccce76269bbaf461b5ebd90dd16e71003c"
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 4:acfd6fbf9f9e 35 void TMetro_initLine(void) {
takashikojo 4:acfd6fbf9f9e 36 lineTbl["odpt.Railway:TokyoMetro.Tozai"] = "東京メトロ東西線 " ;
takashikojo 4:acfd6fbf9f9e 37 lineTbl["odpt.Railway:TokyoMetro.Marunouchi"] = "東京メトロ丸の内線" ;
takashikojo 4:acfd6fbf9f9e 38 lineTbl["odpt.Railway:TokyoMetro.Namboku"] = "東京メトロ南北線 " ;
takashikojo 4:acfd6fbf9f9e 39 lineTbl["odpt.Railway:TokyoMetro.Hibiya"] = "東京メトロ日比谷線" ;
takashikojo 4:acfd6fbf9f9e 40 lineTbl["odpt.Railway:TokyoMetro.Fukutoshin"] = "東京メトロ副都心線" ;
takashikojo 4:acfd6fbf9f9e 41 lineTbl["odpt.Railway:TokyoMetro.Hanzomon"] = "東京メトロ半蔵門線" ;
takashikojo 4:acfd6fbf9f9e 42 lineTbl["odpt.Railway:TokyoMetro.Ginza"] = "東京メトロ銀座線 " ;
takashikojo 4:acfd6fbf9f9e 43 lineTbl["odpt.Railway:TokyoMetro.Yurakucho"] = "東京メトロ有楽町線" ;
takashikojo 4:acfd6fbf9f9e 44 lineTbl["odpt.Railway:TokyoMetro.Chiyoda"] = "東京メトロ千代田線" ;
takashikojo 4:acfd6fbf9f9e 45 }
takashikojo 4:acfd6fbf9f9e 46
takashikojo 4:acfd6fbf9f9e 47 #define TOKEN_SIZE 100
takashikojo 4:acfd6fbf9f9e 48 static char accessToken[TOKEN_SIZE] = { 0x0 } ;
takashikojo 4:acfd6fbf9f9e 49
takashikojo 4:acfd6fbf9f9e 50 static void removeCRLF(char *str)
takashikojo 4:acfd6fbf9f9e 51 {
takashikojo 4:acfd6fbf9f9e 52 for(int i = strlen(str)-1; i>0 ; i--) {
takashikojo 4:acfd6fbf9f9e 53 if((str[strlen(str)-1] == '\n') || (str[strlen(str)-1] == '\r'))
takashikojo 4:acfd6fbf9f9e 54 str[strlen(str)-1] = '\0' ;
takashikojo 4:acfd6fbf9f9e 55 else break ;
takashikojo 4:acfd6fbf9f9e 56 }
takashikojo 4:acfd6fbf9f9e 57 }
takashikojo 4:acfd6fbf9f9e 58
takashikojo 4:acfd6fbf9f9e 59 static const char *getToken()
takashikojo 4:acfd6fbf9f9e 60 {
takashikojo 4:acfd6fbf9f9e 61 FILE *fp ;
takashikojo 4:acfd6fbf9f9e 62 if(accessToken[0] == 0x0) {
takashikojo 4:acfd6fbf9f9e 63 fp = fopen(ACCESS_TOKEN, "r");
takashikojo 4:acfd6fbf9f9e 64 if (fp == NULL) {
takashikojo 4:acfd6fbf9f9e 65 ERR("Cannot open \"%s\"\n", ACCESS_TOKEN) ;
takashikojo 4:acfd6fbf9f9e 66 return false ;
takashikojo 4:acfd6fbf9f9e 67 }
takashikojo 4:acfd6fbf9f9e 68 fgets(accessToken, sizeof(accessToken), fp) ;
takashikojo 4:acfd6fbf9f9e 69 removeCRLF(accessToken) ;
takashikojo 4:acfd6fbf9f9e 70 }
takashikojo 4:acfd6fbf9f9e 71 TMetro_initLine() ;
takashikojo 4:acfd6fbf9f9e 72 return accessToken ;
takashikojo 4:acfd6fbf9f9e 73 }
takashikojo 4:acfd6fbf9f9e 74
takashikojo 4:acfd6fbf9f9e 75 bool TMetro_query(const char *type, const char *query, char *recv, unsigned int size)
takashikojo 4:acfd6fbf9f9e 76 {
takashikojo 0:a59f55690685 77 int ret ;
takashikojo 4:acfd6fbf9f9e 78 #define BUFF_SIZE 256
takashikojo 0:a59f55690685 79 char queryBuff[BUFF_SIZE] ;
takashikojo 4:acfd6fbf9f9e 80 sprintf(queryBuff, "%s/%s?rdf:type=%s&acl:consumerKey=%s", API_URL, type, query, getToken()) ;
takashikojo 2:58e7fabcba89 81 DBG("%s",queryBuff) ;
takashikojo 2:58e7fabcba89 82 ret = httpClient.get(queryBuff, recv, size);
takashikojo 0:a59f55690685 83 if (!ret) {
takashikojo 0:a59f55690685 84 DBG("Result: %s\n", recv);
takashikojo 2:58e7fabcba89 85 return true ;
takashikojo 0:a59f55690685 86 } else {
takashikojo 2:58e7fabcba89 87 ERR("Error - ret = %d - HTTP return code = %d\n", ret, httpClient.getHTTPResponseCode());
takashikojo 2:58e7fabcba89 88 return false ;
takashikojo 0:a59f55690685 89 }
takashikojo 0:a59f55690685 90 }
takashikojo 0:a59f55690685 91
takashikojo 4:acfd6fbf9f9e 92 static void printStat(const char *line, const char *stat)
takashikojo 4:acfd6fbf9f9e 93 {
takashikojo 4:acfd6fbf9f9e 94 #define JST (9*60*60)
takashikojo 0:a59f55690685 95 struct tm t;
takashikojo 0:a59f55690685 96 time_t ctTime;
takashikojo 0:a59f55690685 97 ctTime = time(NULL) + JST ;
takashikojo 0:a59f55690685 98 t = *localtime(&ctTime);
takashikojo 4:acfd6fbf9f9e 99
takashikojo 4:acfd6fbf9f9e 100 printf("%d月%d日%d時%d分:%s:%s\n",
takashikojo 4:acfd6fbf9f9e 101 t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, line, stat) ;
takashikojo 0:a59f55690685 102 }
takashikojo 0:a59f55690685 103
takashikojo 4:acfd6fbf9f9e 104 bool TMetro_getStat(const char *buff)
takashikojo 4:acfd6fbf9f9e 105 {
takashikojo 0:a59f55690685 106
takashikojo 0:a59f55690685 107 std::string err;
takashikojo 0:a59f55690685 108 picojson::parse(trainStat, (const char *)buff, (const char *)buff+strlen(buff), &err);
takashikojo 0:a59f55690685 109 if (!err.empty()) {
takashikojo 0:a59f55690685 110 ERR("Metro Site Result ERROR: %s", err.c_str());
takashikojo 1:26a0a9220f01 111 return false ;
takashikojo 0:a59f55690685 112 }
takashikojo 0:a59f55690685 113 picojson::array array = trainStat.get<picojson::array>();
takashikojo 4:acfd6fbf9f9e 114 for (picojson::array::iterator it = array.begin(); it != array.end(); it++) {
takashikojo 0:a59f55690685 115 picojson::object& obj = it->get<picojson::object>();
takashikojo 4:acfd6fbf9f9e 116 DBG("Line=%s, %s\n", obj["odpt:railway"].get<std::string>().c_str(), lineTbl[obj["odpt:railway"].get<std::string>()].c_str()) ;
takashikojo 4:acfd6fbf9f9e 117 if(watchList.find(lineTbl[obj["odpt:railway"].get<std::string>()])) {
takashikojo 0:a59f55690685 118 printStat(
takashikojo 0:a59f55690685 119 lineTbl[obj["odpt:railway"].get<std::string>()].c_str(),
takashikojo 0:a59f55690685 120 obj["odpt:trainInformationText"].get<std::string>().c_str());
takashikojo 0:a59f55690685 121 if(obj["odpt:trainInformationText"].get<std::string>().find("平常どおり") == std::string::npos)
takashikojo 4:acfd6fbf9f9e 122 return true ;
takashikojo 0:a59f55690685 123 }
takashikojo 0:a59f55690685 124 }
takashikojo 4:acfd6fbf9f9e 125 return false ;
takashikojo 0:a59f55690685 126 }