--- smsdaemon/plugins/TogPlugin.cpp 2008/06/11 10:21:47 49 +++ smsdaemon/plugins/TogPlugin.cpp 2008/12/07 15:51:02 142 @@ -1,5 +1,6 @@ #include "TogPlugin.h" -#include "GsmModem.h" +#include "IGsmModem.h" +#include "Sms.h" #include "TrainInfo.h" #include "util.h" @@ -7,45 +8,78 @@ #include #include +#include "common.h" + using namespace std; TogPlugin::TogPlugin() : Plugin("tog", "Henter tog info fra bane.dk") { + _stationInfo["ar"] = StationInfo("AR","Århus"); + _stationInfo["la"] = StationInfo("LG","Langå"); + _stationInfo["ul"] = StationInfo("UP","Ulstrup"); + _stationInfo["bj"] = StationInfo("BJ","Bjerringbro"); + _stationInfo["vi"] = StationInfo("VG","Viborg"); + _stationInfo["ho"] = StationInfo("HØ","Højslev"); + _stationInfo["sk"] = StationInfo("SK","Skive"); } string TogPlugin::GetRealStationCode(string code, string &stationName) { - code = Util::str_tolower(code); + map::iterator it = _stationInfo.find(code); + + if (it != _stationInfo.end()) + { + stationName = (*it).second.name; + return (*it).second.code; + } + + return ""; +} + +string TogPlugin::CreateStationList() +{ + ostringstream os; + + os << "code - station\n"; - return "BJ"; + map::iterator it; + + for (it = _stationInfo.begin(); it != _stationInfo.end(); ++it) + { + os << (*it).first << " - " << (*it).second.name << "\n"; + } + + return os.str(); } void TogPlugin::Execute(IGsmModem& modem, SMS& sms) { - string data = GetSmsData(sms); -// Util::str_dump(data); + string data = sms.ExtractParameters(); + data = Util::str_gsm2latin(data);; -/* Util::str_dump(data); + data = Util::str_tolower(data); + - cout << "1: " << data << endl; - data = Util::convertToUnicode(data); - cout << "2: " << data << endl; - Util::str_dump(data);*/ - if (data == "") { - modem.SendSms(sms.sender, "Usage: tog "); + modem.SendSms(sms.GetSender(), "Usage: tog \ntog list - liste over stationer", false); return; } + if (data == "list") + { + modem.SendSms(sms.GetSender(), CreateStationList(), true); + return; + } + string stationName; string code = GetRealStationCode(data, stationName); if (code == "") { - modem.SendSms(sms.sender, "Unknown station"); + modem.SendSms(sms.GetSender(), "Unknown station", false); return; } @@ -54,14 +88,14 @@ { infos = GetTrainInfo( code , "" ); } - catch(...) + catch(std::exception& e) { - modem.SendSms(sms.sender, "Connection timeout"); + modem.SendSms(sms.GetSender(), e.what(), false ); return; } ostringstream os; - os << stationName << "\n"; + os << stationName << ":\n"; for (unsigned int i=0; i< infos.size(); ++i) { @@ -72,7 +106,7 @@ os << train.note << "\n"; } - modem.SendSms(sms.sender, Util::str_latin2gsm(os.str())); + modem.SendSms(sms.GetSender(), os.str(), true);