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