/[projects]/smsdaemon/plugins/TogPlugin.cpp
ViewVC logotype

Contents of /smsdaemon/plugins/TogPlugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (show annotations) (download)
Wed Jun 11 12:15:14 2008 UTC (15 years, 11 months ago) by torben
File size: 2092 byte(s)
TogPlugin is now working :)


1 #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 _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 }
23
24 string TogPlugin::GetRealStationCode(string code, string &stationName)
25 {
26 map<string, StationInfo>::iterator it = _stationInfo.find(code);
27
28 if (it != _stationInfo.end())
29 {
30 stationName = (*it).second.name;
31 return (*it).second.code;
32 }
33
34 return "";
35 }
36
37 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 void TogPlugin::Execute(IGsmModem& modem, SMS& sms)
54 {
55 string data = GetSmsData(sms);
56
57 data = Util::str_gsm2latin(data);;
58 data = Util::str_tolower(data);
59
60
61 if (data == "")
62 {
63 modem.SendSms(sms.sender, "Usage: tog <stationskode>\ntog list - liste over stationer");
64 return;
65 }
66
67 if (data == "list")
68 {
69 modem.SendSms(sms.sender, CreateStationList());
70 return;
71 }
72
73 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 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
93 ostringstream os;
94 os << stationName << ":\n";
95
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 }

  ViewVC Help
Powered by ViewVC 1.1.20