--- smsdaemon/plugins/TogPlugin.cpp 2008/06/11 08:25:34 44 +++ smsdaemon/plugins/TogPlugin.cpp 2009/09/16 09:08:40 323 @@ -1,58 +1,107 @@ #include "TogPlugin.h" -#include "GsmModem.h" +#include "ISmsTransceiver.h" +#include "Sms.h" #include "TrainInfo.h" -#include "util.h" +#include "Util.h" #include #include +#include "Common.h" + using namespace std; +StationInfo::StationInfo(int i, std::string n) +{ + id = i; + name = Util::str_characters(n); +} + + TogPlugin::TogPlugin() - : Plugin("tog", "Henter tog info fra bane.dk") + : Plugin("tog", "Henter tog info fra bane.dk") { + _stationInfo["ar"] = StationInfo(278, "Århus"); + _stationInfo["la"] = StationInfo(122, "Langå"); + _stationInfo["ul"] = StationInfo(240, "Ulstrup"); + _stationInfo["bj"] = StationInfo(10, "Bjerringbro"); + _stationInfo["vi"] = StationInfo(253, "Viborg"); + _stationInfo["ho"] = StationInfo(94, "Højslev"); + _stationInfo["sk"] = StationInfo(190, "Skive"); } -string TogPlugin::GetRealStationCode(string code, string &stationName) +int TogPlugin::GetStationId(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.id; + } - return "BJ"; + return -1; } -void TogPlugin::Execute(IGsmModem& modem, SMS& sms) +string TogPlugin::CreateStationList() { - string data = GetSmsData(sms); -// Util::str_dump(data); - data = Util::str_gsm2latin(data);; -/* Util::str_dump(data); - - cout << "1: " << data << endl; - data = Util::convertToUnicode(data); - cout << "2: " << data << endl; - Util::str_dump(data);*/ - + ostringstream os; + + os << "code - station\n"; + + map::iterator it; + + for (it = _stationInfo.begin(); it != _stationInfo.end(); ++it) + { + os << (*it).first << " - " << (*it).second.name << "\n"; + } + + return os.str(); +} + +void TogPlugin::Execute(ISmsTransceiver& modem, SMS& sms) +{ + string data = sms.ExtractParameters(); + + data = Util::str_tolower(data); + + if (data == "") { - modem.SendSms(sms.sender, "Usage: tog "); - return; + 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); + int id = GetStationId(data, stationName); - if (code == "") + if (id < 0) { - modem.SendSms(sms.sender, "Unknown station"); + modem.SendSms(sms.GetSender(), "Unknown station", false); return; } - vector infos = GetTrainInfo( code , "" ); + vector infos; + try + { + infos = GetTrainInfo( id ); + } + catch (std::exception& e) + { + 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) { @@ -63,7 +112,7 @@ os << train.note << "\n"; } - modem.SendSms(sms.sender, Util::str_latin2gsm(os.str())); + modem.SendSms(sms.GetSender(), os.str(), true);