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.
Dependencies: AlarmClock DigitalClock EthernetInterface FourDigitLED HTTPClient NTPClient SDFileSystem TrainStat WeatherInfo XML_aide mbed-rtos mbed picojson wolfSSL
net_main.txt
- Committer:
- takashikojo
- Date:
- 2015-06-26
- Revision:
- 2:bcc1a40ffa04
File content as of revision 2:bcc1a40ffa04:
#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"
extern AlarmClock alarmclock ;
static HTTPClient http;
static char recvBuff[1024*64];
static picojson::value trainStat ;
void metro_query(const char *type, const char *query, char *recv, 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) ;
//puts(queryBuff) ;
ret = http.get(queryBuff, recvBuff, size);
if (!ret) {
// printf("Result: %s\n", recv);
} else {
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
}
}
static std::map<std::string, string> lineTbl ;
static void init_station(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 = "" ;
static void set_station(string name) {
map<string, string>::iterator it = lineTbl.begin(); ;
for(it = lineTbl.begin(); it != lineTbl.end(); ++it) {
if(it->second == name){
watchingLine = it->first ;
}
}
}
static void print_stat(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) ;
}
static bool get_stat(const char *buff) {
std::string err;
picojson::parse(trainStat, (const char *)buff, (const char *)recvBuff+strlen(recvBuff), &err);
if (!err.empty()) {
printf("Metro Site Result ERROR: %s", err.c_str());
return false ;
}
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 == "")
print_stat(
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){
print_stat(
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 ;
}
void net_main(void const *av)
{
bool sw ;
init_line() ;
set_line("有楽町線") ;
while(1) {
sw = !sw ;
metro_query("datapoints", "odpt:TrainInformation", recvBuff, sizeof(recvBuff)) ;
if(!get_stat((const char *)recvBuff))
{ printf("遅れあり\n") ; alarmclock.setBlink(true) ; }
else
{ printf("遅れなし\n") ; alarmclock.setBlink(false) ; }
wait(60.0) ;
}
}