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

Contents of /smsdaemon/plugins/TogPlugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 80 - (show annotations) (download)
Sun Jun 15 18:57:00 2008 UTC (15 years, 11 months ago) by torben
File size: 2149 byte(s)
util.cpp system() allways returns -1 in daemon mode (take handle of that)

TogPlugin.cpp: pass the exception message back to the user.

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 #include "common.h"
11
12 using namespace std;
13
14
15 TogPlugin::TogPlugin()
16 : 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)
27 {
28 map<string, StationInfo>::iterator it = _stationInfo.find(code);
29
30 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)
56 {
57 string data = GetSmsData(sms);
58
59 data = Util::str_gsm2latin(data);;
60 data = Util::str_tolower(data);
61
62
63 if (data == "")
64 {
65 modem.SendSms(sms.sender, "Usage: tog <stationskode>\ntog list - liste over stationer", false);
66 return;
67 }
68
69 if (data == "list")
70 {
71 modem.SendSms(sms.sender, CreateStationList(), true);
72 return;
73 }
74
75 string stationName;
76 string code = GetRealStationCode(data, stationName);
77
78 if (code == "")
79 {
80 modem.SendSms(sms.sender, "Unknown station", false);
81 return;
82 }
83
84 vector<TrainInfo> infos;
85 try
86 {
87 infos = GetTrainInfo( code , "" );
88 }
89 catch(std::exception& e)
90 {
91 modem.SendSms(sms.sender, e.what(), false );
92 return;
93 }
94
95 ostringstream os;
96 os << stationName << ":\n";
97
98 for (unsigned int i=0; i< infos.size(); ++i)
99 {
100 TrainInfo& train = infos[i];
101 os << train.time << " ";
102 os << train.destination << " ";
103 os << train.status << " ";
104 os << train.note << "\n";
105 }
106
107 modem.SendSms(sms.sender, Util::str_latin2gsm(os.str()), true);
108
109
110
111 }

  ViewVC Help
Powered by ViewVC 1.1.20