1 |
#include "TogPlugin.h" |
#include "TogPlugin.h" |
2 |
#include "GsmModem.h" |
#include "ISmsTransceiver.h" |
3 |
|
#include "Sms.h" |
4 |
|
|
5 |
#include "TrainInfo.h" |
#include "TrainInfo.h" |
6 |
#include "util.h" |
#include "Util.h" |
7 |
|
|
8 |
#include <iostream> |
#include <iostream> |
9 |
#include <sstream> |
#include <sstream> |
10 |
|
|
11 |
|
#include "Common.h" |
12 |
|
|
13 |
using namespace std; |
using namespace std; |
14 |
|
|
15 |
|
|
16 |
|
StationInfo::StationInfo(int i, std::string n) |
17 |
|
{ |
18 |
|
id = i; |
19 |
|
name = Util::str_characters(n); |
20 |
|
} |
21 |
|
|
22 |
|
|
23 |
TogPlugin::TogPlugin() |
TogPlugin::TogPlugin() |
24 |
: Plugin("tog", "Henter tog info fra bane.dk") |
: Plugin("tog", "Henter tog info fra bane.dk") |
25 |
{ |
{ |
26 |
|
_stationInfo["ar"] = StationInfo(278, "Århus"); |
27 |
|
_stationInfo["la"] = StationInfo(122, "Langå"); |
28 |
|
_stationInfo["ul"] = StationInfo(240, "Ulstrup"); |
29 |
|
_stationInfo["bj"] = StationInfo(10, "Bjerringbro"); |
30 |
|
_stationInfo["vi"] = StationInfo(253, "Viborg"); |
31 |
|
_stationInfo["ho"] = StationInfo(94, "Højslev"); |
32 |
|
_stationInfo["sk"] = StationInfo(190, "Skive"); |
33 |
} |
} |
34 |
|
|
35 |
string TogPlugin::GetRealStationCode(string code, string &stationName) |
int TogPlugin::GetStationId(string code, string &stationName) |
36 |
{ |
{ |
37 |
code = Util::str_tolower(code); |
map<string, StationInfo>::iterator it = _stationInfo.find(code); |
38 |
|
|
39 |
|
if (it != _stationInfo.end()) |
40 |
|
{ |
41 |
|
stationName = (*it).second.name; |
42 |
|
return (*it).second.id; |
43 |
|
} |
44 |
|
|
45 |
|
return -1; |
46 |
|
} |
47 |
|
|
48 |
|
string TogPlugin::CreateStationList() |
49 |
|
{ |
50 |
|
ostringstream os; |
51 |
|
|
52 |
return "BJ"; |
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(IGsmModem& modem, SMS& sms) |
void TogPlugin::Execute(ISmsTransceiver& modem, SMS& sms) |
65 |
{ |
{ |
66 |
string data = GetSmsData(sms); |
string data = sms.ExtractParameters(); |
67 |
// Util::str_dump(data); |
|
68 |
data = Util::str_gsm2latin(data);; |
data = Util::str_tolower(data); |
69 |
/* Util::str_dump(data); |
|
70 |
|
|
|
cout << "1: " << data << endl; |
|
|
data = Util::convertToUnicode(data); |
|
|
cout << "2: " << data << endl; |
|
|
Util::str_dump(data);*/ |
|
|
|
|
71 |
if (data == "") |
if (data == "") |
72 |
{ |
{ |
73 |
modem.SendSms(sms.sender, "Usage: tog <stationskode>"); |
modem.SendSms(sms.GetSender(), "Usage: tog <stationskode>\ntog list - liste over stationer", false); |
74 |
return; |
return; |
75 |
|
} |
76 |
|
|
77 |
|
if (data == "list") |
78 |
|
{ |
79 |
|
modem.SendSms(sms.GetSender(), CreateStationList(), true); |
80 |
|
return; |
81 |
} |
} |
82 |
|
|
83 |
string stationName; |
string stationName; |
84 |
string code = GetRealStationCode(data, stationName); |
int id = GetStationId(data, stationName); |
85 |
|
|
86 |
if (code == "") |
if (id < 0) |
87 |
{ |
{ |
88 |
modem.SendSms(sms.sender, "Unknown station"); |
modem.SendSms(sms.GetSender(), "Unknown station", false); |
89 |
return; |
return; |
90 |
} |
} |
91 |
|
|
92 |
vector<TrainInfo> infos; |
vector<TrainInfo> infos; |
93 |
try |
try |
94 |
{ |
{ |
95 |
infos = GetTrainInfo( code , "" ); |
infos = GetTrainInfo( id ); |
96 |
} |
} |
97 |
catch(...) |
catch (std::exception& e) |
98 |
{ |
{ |
99 |
modem.SendSms(sms.sender, "Connection timeout"); |
modem.SendSms(sms.GetSender(), e.what(), false ); |
100 |
return; |
return; |
101 |
} |
} |
102 |
|
|
103 |
ostringstream os; |
ostringstream os; |
104 |
os << stationName << "\n"; |
os << stationName << ":\n"; |
105 |
|
|
106 |
for (unsigned int i=0; i< infos.size(); ++i) |
for (unsigned int i=0; i< infos.size(); ++i) |
107 |
{ |
{ |
112 |
os << train.note << "\n"; |
os << train.note << "\n"; |
113 |
} |
} |
114 |
|
|
115 |
modem.SendSms(sms.sender, Util::str_latin2gsm(os.str())); |
modem.SendSms(sms.GetSender(), os.str(), true); |
116 |
|
|
117 |
|
|
118 |
|
|