1 |
torben |
44 |
#include "TogPlugin.h" |
2 |
torben |
149 |
#include "ISmsTransceiver.h" |
3 |
torben |
90 |
#include "Sms.h" |
4 |
torben |
44 |
|
5 |
|
|
#include "TrainInfo.h" |
6 |
torben |
158 |
#include "Util.h" |
7 |
torben |
44 |
|
8 |
|
|
#include <iostream> |
9 |
|
|
#include <sstream> |
10 |
|
|
|
11 |
torben |
158 |
#include "Common.h" |
12 |
torben |
80 |
|
13 |
torben |
44 |
using namespace std; |
14 |
|
|
|
15 |
|
|
|
16 |
torben |
186 |
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 |
torben |
44 |
TogPlugin::TogPlugin() |
24 |
torben |
196 |
: Plugin("tog", "Henter tog info fra bane.dk") |
25 |
torben |
44 |
{ |
26 |
torben |
186 |
_stationInfo["ar"] = StationInfo("AR","Århus"); |
27 |
|
|
_stationInfo["la"] = StationInfo("LG","Langå"); |
28 |
torben |
52 |
_stationInfo["ul"] = StationInfo("UP","Ulstrup"); |
29 |
|
|
_stationInfo["bj"] = StationInfo("BJ","Bjerringbro"); |
30 |
|
|
_stationInfo["vi"] = StationInfo("VG","Viborg"); |
31 |
torben |
186 |
_stationInfo["ho"] = StationInfo("HØ","Højslev"); |
32 |
torben |
52 |
_stationInfo["sk"] = StationInfo("SK","Skive"); |
33 |
torben |
44 |
} |
34 |
|
|
|
35 |
|
|
string TogPlugin::GetRealStationCode(string code, string &stationName) |
36 |
|
|
{ |
37 |
torben |
52 |
map<string, StationInfo>::iterator it = _stationInfo.find(code); |
38 |
torben |
44 |
|
39 |
torben |
52 |
if (it != _stationInfo.end()) |
40 |
|
|
{ |
41 |
|
|
stationName = (*it).second.name; |
42 |
|
|
return (*it).second.code; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
return ""; |
46 |
torben |
44 |
} |
47 |
|
|
|
48 |
torben |
52 |
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 |
torben |
135 |
return os.str(); |
62 |
torben |
52 |
} |
63 |
|
|
|
64 |
torben |
149 |
void TogPlugin::Execute(ISmsTransceiver& modem, SMS& sms) |
65 |
torben |
44 |
{ |
66 |
torben |
142 |
string data = sms.ExtractParameters(); |
67 |
torben |
52 |
|
68 |
|
|
data = Util::str_tolower(data); |
69 |
torben |
44 |
|
70 |
torben |
109 |
|
71 |
torben |
44 |
if (data == "") |
72 |
|
|
{ |
73 |
torben |
142 |
modem.SendSms(sms.GetSender(), "Usage: tog <stationskode>\ntog list - liste over stationer", false); |
74 |
torben |
196 |
return; |
75 |
torben |
44 |
} |
76 |
|
|
|
77 |
torben |
52 |
if (data == "list") |
78 |
|
|
{ |
79 |
torben |
142 |
modem.SendSms(sms.GetSender(), CreateStationList(), true); |
80 |
torben |
52 |
return; |
81 |
|
|
} |
82 |
|
|
|
83 |
torben |
44 |
string stationName; |
84 |
|
|
string code = GetRealStationCode(data, stationName); |
85 |
|
|
|
86 |
|
|
if (code == "") |
87 |
|
|
{ |
88 |
torben |
142 |
modem.SendSms(sms.GetSender(), "Unknown station", false); |
89 |
torben |
44 |
return; |
90 |
|
|
} |
91 |
|
|
|
92 |
torben |
49 |
vector<TrainInfo> infos; |
93 |
|
|
try |
94 |
|
|
{ |
95 |
|
|
infos = GetTrainInfo( code , "" ); |
96 |
|
|
} |
97 |
torben |
196 |
catch (std::exception& e) |
98 |
torben |
49 |
{ |
99 |
torben |
142 |
modem.SendSms(sms.GetSender(), e.what(), false ); |
100 |
torben |
49 |
return; |
101 |
|
|
} |
102 |
torben |
44 |
|
103 |
|
|
ostringstream os; |
104 |
torben |
52 |
os << stationName << ":\n"; |
105 |
torben |
44 |
|
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 |
torben |
142 |
modem.SendSms(sms.GetSender(), os.str(), true); |
116 |
torben |
44 |
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
} |