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

Contents of /smsdaemon/plugins/TogPlugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 135 - (show annotations) (download)
Sun Dec 7 08:58:15 2008 UTC (15 years, 5 months ago) by torben
File size: 2173 byte(s)
It is the duty of GsmModem::SendSms to correctly encode the message

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

  ViewVC Help
Powered by ViewVC 1.1.20