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