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

Annotation of /smsdaemon/plugins/TogPlugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 80 - (hide 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 torben 44 #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 torben 80 #include "common.h"
11    
12 torben 44 using namespace std;
13    
14    
15     TogPlugin::TogPlugin()
16     : Plugin("tog", "Henter tog info fra bane.dk")
17     {
18 torben 52 _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 torben 44 }
25    
26     string TogPlugin::GetRealStationCode(string code, string &stationName)
27     {
28 torben 52 map<string, StationInfo>::iterator it = _stationInfo.find(code);
29 torben 44
30 torben 52 if (it != _stationInfo.end())
31     {
32     stationName = (*it).second.name;
33     return (*it).second.code;
34     }
35    
36     return "";
37 torben 44 }
38    
39 torben 52 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 torben 44 void TogPlugin::Execute(IGsmModem& modem, SMS& sms)
56     {
57     string data = GetSmsData(sms);
58 torben 52
59 torben 44 data = Util::str_gsm2latin(data);;
60 torben 52 data = Util::str_tolower(data);
61 torben 44
62    
63     if (data == "")
64     {
65 torben 63 modem.SendSms(sms.sender, "Usage: tog <stationskode>\ntog list - liste over stationer", false);
66 torben 44 return;
67     }
68    
69 torben 52 if (data == "list")
70     {
71 torben 63 modem.SendSms(sms.sender, CreateStationList(), true);
72 torben 52 return;
73     }
74    
75 torben 44 string stationName;
76     string code = GetRealStationCode(data, stationName);
77    
78     if (code == "")
79     {
80 torben 63 modem.SendSms(sms.sender, "Unknown station", false);
81 torben 44 return;
82     }
83    
84 torben 49 vector<TrainInfo> infos;
85     try
86     {
87     infos = GetTrainInfo( code , "" );
88     }
89 torben 80 catch(std::exception& e)
90 torben 49 {
91 torben 80 modem.SendSms(sms.sender, e.what(), false );
92 torben 49 return;
93     }
94 torben 44
95     ostringstream os;
96 torben 52 os << stationName << ":\n";
97 torben 44
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 torben 63 modem.SendSms(sms.sender, Util::str_latin2gsm(os.str()), true);
108 torben 44
109    
110    
111     }

  ViewVC Help
Powered by ViewVC 1.1.20