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

Annotation of /smsdaemon/plugins/TogPlugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 142 - (hide annotations) (download)
Sun Dec 7 15:51:02 2008 UTC (15 years, 5 months ago) by torben
File size: 2206 byte(s)
Refacerored SMS class

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

  ViewVC Help
Powered by ViewVC 1.1.20