Train status

tokyoMetro.cpp

Committer:
takashikojo
Date:
2015-06-26
Revision:
0:a59f55690685
Child:
1:26a0a9220f01

File content as of revision 0:a59f55690685:

#include "mbed.h"
#include "EthernetInterface.h"
#include "HTTPClient.h"
#include "vector"
#include "picojson.h"

#include "AlarmClock.h"

#define ACCESS_TOKEN "6d60c19c9497e9b2f5b847d31ab70cccce76269bbaf461b5ebd90dd16e71003c"
#define API_URL      "https://api.tokyometroapp.jp:443/api/v2"

#if 0
//Enable debug
#define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
#else
//Disable debug
#define DBG(x, ...)
#define WARN(x, ...)
#endif

#define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__);


extern AlarmClock alarmclock ;
static HTTPClient http;

static picojson::value trainStat ;

void TMetro_query(const char *type, const char *query, char *recv, unsigned int size) {
    int ret ;
    #define BUFF_SIZE   256
    char queryBuff[BUFF_SIZE] ;
    sprintf(queryBuff, "%s/%s?rdf:type=%s&acl:consumerKey=%s", API_URL, type, query, ACCESS_TOKEN) ;
    DBG(queryBuff) ;
    ret = http.get(queryBuff, recv, size);
    if (!ret) {
        DBG("Result: %s\n", recv);
    } else {
        ERR("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
    }
}

static std::map<std::string, string> lineTbl ;
void TMetro_initLine(void) {
    lineTbl["odpt.Railway:TokyoMetro.Tozai"]      = "東西線 " ;
    lineTbl["odpt.Railway:TokyoMetro.Marunouchi"] = "丸の内線" ; 
    lineTbl["odpt.Railway:TokyoMetro.Namboku"]    = "南北線 " ; 
    lineTbl["odpt.Railway:TokyoMetro.Hibiya"]     = "日比谷線" ; 
    lineTbl["odpt.Railway:TokyoMetro.Fukutoshin"] = "副都心線" ; 
    lineTbl["odpt.Railway:TokyoMetro.Hanzomon"]   = "半蔵門線" ; 
    lineTbl["odpt.Railway:TokyoMetro.Ginza"]      = "銀座線 " ; 
    lineTbl["odpt.Railway:TokyoMetro.Yurakucho"]  = "有楽町線" ; 
    lineTbl["odpt.Railway:TokyoMetro.Chiyoda"]    = "千代田線" ; 
}

static string watchingLine = "" ;

void TMetro_setLine(string name) {
    map<string, string>::iterator it = lineTbl.begin();  ;
    
    DBG("TMetro_setLine\n") ;
    for(it = lineTbl.begin(); it != lineTbl.end(); ++it) {
        if(it->second == name){
            watchingLine = it->first ;
        }
    }  
}

static void printStat(const char *line, const char *stat) {
    #define JST (9*60*60)
    struct tm t;
    time_t ctTime;
    ctTime = time(NULL) + JST ;
    t  = *localtime(&ctTime);
    
    printf("%d月%d日%d時%d分:%sの運転状況:%s\n", 
        t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, line, stat) ;    
}

bool TMetro_getStat(const char *buff) {

    std::string err;
    picojson::parse(trainStat, (const char *)buff, (const char *)buff+strlen(buff), &err);
    if (!err.empty()) {
        ERR("Metro Site Result ERROR: %s", err.c_str());
        return  ;
    }
    picojson::array array = trainStat.get<picojson::array>();
    for (picojson::array::iterator it = array.begin(); it != array.end(); it++)
    {
        picojson::object& obj = it->get<picojson::object>();
        if(watchingLine == "")        
            printStat(
                lineTbl[obj["odpt:railway"].get<std::string>()].c_str(),
                obj["odpt:trainInformationText"].get<std::string>().c_str());
        else if(obj["odpt:railway"].get<std::string>()==watchingLine){
            printStat(
                lineTbl[watchingLine].c_str(),
                obj["odpt:trainInformationText"].get<std::string>().c_str());
            if(obj["odpt:trainInformationText"].get<std::string>().find("平常どおり") == std::string::npos)
                return false ;
        }
    }
    return true ;
}