/[projects]/smsdaemon/plugins/TogPlugin.cpp
ViewVC logotype

Contents of /smsdaemon/plugins/TogPlugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (show annotations) (download)
Mon Dec 15 09:27:56 2008 UTC (15 years, 5 months ago) by torben
File size: 2335 byte(s)
Make TogPlugin less sensible to editor conversion errors

1 #include "TogPlugin.h"
2 #include "ISmsTransceiver.h"
3 #include "Sms.h"
4
5 #include "TrainInfo.h"
6 #include "Util.h"
7
8 #include <iostream>
9 #include <sstream>
10
11 #include "Common.h"
12
13 using namespace std;
14
15
16 StationInfo::StationInfo(std::string c, std::string n)
17 {
18 code = Util::str_characters(c);
19 name = Util::str_characters(n);
20 }
21
22
23 TogPlugin::TogPlugin()
24 : Plugin("tog", "Henter tog info fra bane.dk")
25 {
26 _stationInfo["ar"] = StationInfo("AR","&Aring;rhus");
27 _stationInfo["la"] = StationInfo("LG","Lang&aring;");
28 _stationInfo["ul"] = StationInfo("UP","Ulstrup");
29 _stationInfo["bj"] = StationInfo("BJ","Bjerringbro");
30 _stationInfo["vi"] = StationInfo("VG","Viborg");
31 _stationInfo["ho"] = StationInfo("H&Oslash;","H&oslash;jslev");
32 _stationInfo["sk"] = StationInfo("SK","Skive");
33 }
34
35 string TogPlugin::GetRealStationCode(string code, string &stationName)
36 {
37 map<string, StationInfo>::iterator it = _stationInfo.find(code);
38
39 if (it != _stationInfo.end())
40 {
41 stationName = (*it).second.name;
42 return (*it).second.code;
43 }
44
45 return "";
46 }
47
48 string TogPlugin::CreateStationList()
49 {
50 ostringstream os;
51
52 os << "code - station\n";
53
54 map<string, StationInfo>::iterator it;
55
56 for (it = _stationInfo.begin(); it != _stationInfo.end(); ++it)
57 {
58 os << (*it).first << " - " << (*it).second.name << "\n";
59 }
60
61 return os.str();
62 }
63
64 void TogPlugin::Execute(ISmsTransceiver& modem, SMS& sms)
65 {
66 string data = sms.ExtractParameters();
67
68 data = Util::str_tolower(data);
69
70
71 if (data == "")
72 {
73 modem.SendSms(sms.GetSender(), "Usage: tog <stationskode>\ntog list - liste over stationer", false);
74 return;
75 }
76
77 if (data == "list")
78 {
79 modem.SendSms(sms.GetSender(), CreateStationList(), true);
80 return;
81 }
82
83 string stationName;
84 string code = GetRealStationCode(data, stationName);
85
86 if (code == "")
87 {
88 modem.SendSms(sms.GetSender(), "Unknown station", false);
89 return;
90 }
91
92 vector<TrainInfo> infos;
93 try
94 {
95 infos = GetTrainInfo( code , "" );
96 }
97 catch(std::exception& e)
98 {
99 modem.SendSms(sms.GetSender(), e.what(), false );
100 return;
101 }
102
103 ostringstream os;
104 os << stationName << ":\n";
105
106 for (unsigned int i=0; i< infos.size(); ++i)
107 {
108 TrainInfo& train = infos[i];
109 os << train.time << " ";
110 os << train.destination << " ";
111 os << train.status << " ";
112 os << train.note << "\n";
113 }
114
115 modem.SendSms(sms.GetSender(), os.str(), true);
116
117
118
119 }

  ViewVC Help
Powered by ViewVC 1.1.20