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