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

Annotation of /smsdaemon/plugins/TogPlugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 182 - (hide annotations) (download)
Fri Dec 12 13:23:01 2008 UTC (15 years, 5 months ago) by torben
File size: 2190 byte(s)
The transceiver should make sure the text is latin 1. It is not something the plugin  should care about!

1 torben 44 #include "TogPlugin.h"
2 torben 149 #include "ISmsTransceiver.h"
3 torben 90 #include "Sms.h"
4 torben 44
5     #include "TrainInfo.h"
6 torben 158 #include "Util.h"
7 torben 44
8     #include <iostream>
9     #include <sstream>
10    
11 torben 158 #include "Common.h"
12 torben 80
13 torben 44 using namespace std;
14    
15    
16     TogPlugin::TogPlugin()
17     : Plugin("tog", "Henter tog info fra bane.dk")
18     {
19 torben 149 _stationInfo["ar"] = StationInfo("AR","�rhus");
20     _stationInfo["la"] = StationInfo("LG","Lang�");
21 torben 52 _stationInfo["ul"] = StationInfo("UP","Ulstrup");
22     _stationInfo["bj"] = StationInfo("BJ","Bjerringbro");
23     _stationInfo["vi"] = StationInfo("VG","Viborg");
24 torben 149 _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 149 void TogPlugin::Execute(ISmsTransceiver& modem, SMS& sms)
58 torben 44 {
59 torben 142 string data = sms.ExtractParameters();
60 torben 52
61     data = Util::str_tolower(data);
62 torben 44
63 torben 109
64 torben 44 if (data == "")
65     {
66 torben 142 modem.SendSms(sms.GetSender(), "Usage: tog <stationskode>\ntog list - liste over stationer", false);
67 torben 44 return;
68     }
69    
70 torben 52 if (data == "list")
71     {
72 torben 142 modem.SendSms(sms.GetSender(), CreateStationList(), true);
73 torben 52 return;
74     }
75    
76 torben 44 string stationName;
77     string code = GetRealStationCode(data, stationName);
78    
79     if (code == "")
80     {
81 torben 142 modem.SendSms(sms.GetSender(), "Unknown station", false);
82 torben 44 return;
83     }
84    
85 torben 49 vector<TrainInfo> infos;
86     try
87     {
88     infos = GetTrainInfo( code , "" );
89     }
90 torben 80 catch(std::exception& e)
91 torben 49 {
92 torben 142 modem.SendSms(sms.GetSender(), e.what(), false );
93 torben 49 return;
94     }
95 torben 44
96     ostringstream os;
97 torben 52 os << stationName << ":\n";
98 torben 44
99     for (unsigned int i=0; i< infos.size(); ++i)
100     {
101     TrainInfo& train = infos[i];
102     os << train.time << " ";
103     os << train.destination << " ";
104     os << train.status << " ";
105     os << train.note << "\n";
106     }
107    
108 torben 142 modem.SendSms(sms.GetSender(), os.str(), true);
109 torben 44
110    
111    
112     }

  ViewVC Help
Powered by ViewVC 1.1.20