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