Smart Clock
Dependencies: AlarmClock DigitalClock EthernetInterface FourDigitLED HTTPClient NTPClient SDFileSystem TrainStat WeatherInfo XML_aide mbed-rtos mbed picojson wolfSSL
Revision 1:1a28aaca601d, committed 2015-06-26
- Comitter:
- takashikojo
- Date:
- Fri Jun 26 08:02:30 2015 +0000
- Parent:
- 0:0689619f2486
- Child:
- 2:bcc1a40ffa04
- Commit message:
- Smart Clock, initial version
Changed in this revision
--- a/4digit-7seg.lib Thu Jun 25 23:31:17 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -4digit-7seg#7a8925d2a8e7
--- a/AlarmClock.lib Thu Jun 25 23:31:17 2015 +0000 +++ b/AlarmClock.lib Fri Jun 26 08:02:30 2015 +0000 @@ -1,1 +1,1 @@ -AlarmClock#d69dc4537754 +AlarmClock#d544fbfb330c
--- a/DigitalClock.lib Thu Jun 25 23:31:17 2015 +0000 +++ b/DigitalClock.lib Fri Jun 26 08:02:30 2015 +0000 @@ -1,1 +1,1 @@ -DigitalClock#ae969dd370ab +DigitalClock#3c9a2c4733fa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FourDigitLED.lib Fri Jun 26 08:02:30 2015 +0000 @@ -0,0 +1,1 @@ +FourDigitLED#c94aa39af4c7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Fri Jun 26 08:02:30 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/wolfSSL/code/HTTPClient/#7fd621b83b60
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TrainStat.cpp Fri Jun 26 08:02:30 2015 +0000 @@ -0,0 +1,109 @@ +#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 ; + } + 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_station() ; + set_station("有楽町線") ; + 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) ; + } +}
--- a/main.cpp Thu Jun 25 23:31:17 2015 +0000 +++ b/main.cpp Fri Jun 26 08:02:30 2015 +0000 @@ -4,18 +4,42 @@ #include "NTPClient.h" #include "AlarmClock.h" -static AlarmClock alarmclock ( +extern void net_main(void const *av) ; + +AlarmClock alarmclock ( /* Segment 0 - 6, Dot */ D11,D6, D3, D1, D0, D10,D4, D2 , /* Digit 1 - 4 */ D5, D7, D9, D12, /* Alarm, Hour, Min, Tone */D13, D14, D15, PTC11 ) ; -static EthernetInterface eth; +static void ntp(char *site) +{ + NTPClient ntp; + + alarmclock.setLED(33, 33) ; + if (ntp.setTime(site) == 0) { + alarmclock.flashLED() ; + } else { + alarmclock.setLED(0, 0) ; + wait(30.0) ; + return ; + } +} void clock_main(void const *av) { + ntp("ntp.jst.mfeed.ad.jp") ; + alarmclock.start() ; + while(1){ + alarmclock.poll() ; + Thread::wait(100); + } +} + +int main() { + EthernetInterface eth; int ret ; - NTPClient ntp; + alarmclock.setLED(11, 11) ; ret = eth.init(); alarmclock.setLED(22, 22) ; @@ -24,23 +48,9 @@ if(ret == 0)break ; Thread::wait(10); } - alarmclock.setLED(33, 33) ; - if (ntp.setTime("ntp.jst.mfeed.ad.jp") == 0) { - alarmclock.flashLED() ; - } else { - alarmclock.setLED(0, 0) ; - wait(30.0) ; - return ; - } - alarmclock.start() ; - while(1){ - alarmclock.poll() ; - Thread::wait(100); - } -} - -int main() { - #define STACK_SIZE 16000 - Thread t(clock_main, NULL, osPriorityNormal, STACK_SIZE); + + #define NET_STACK 16000 + Thread t_clock(clock_main, NULL, osPriorityNormal); + Thread t_net (net_main, NULL, osPriorityNormal, NET_STACK ); while(1)Thread::wait(1000); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/picojson.lib Fri Jun 26 08:02:30 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mimil/code/picojson/#2bb500b021e2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wolfSSL.lib Fri Jun 26 08:02:30 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/wolfSSL/code/wolfSSL/#d92f9d21154c