/[projects]/smsdaemon/SmsDaemon.cpp
ViewVC logotype

Contents of /smsdaemon/SmsDaemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 125 - (show annotations) (download)
Sat Dec 6 14:06:17 2008 UTC (15 years, 5 months ago) by torben
File size: 2235 byte(s)
no need to check for new sms or execute tasks more than once per second

1 #include "SmsDaemon.h"
2
3 #include <string>
4
5 #include <sstream>
6 #include "common.h"
7
8 #include "GsmModem.h"
9
10 #include "Plugin.h"
11 #include "kbhit.h"
12
13 #include "util.h"
14
15 #include "TaskManager.h"
16 #include "PluginManager.h"
17
18 #include "version.h"
19
20 using namespace std;
21
22 void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin)
23 {
24 ostringstream os;
25 os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms);
26 if (!hasPlugin)
27 os << " -- PLUGIN NOT FOUND";
28
29 Common::instance()->logMessage(os.str());
30 }
31
32
33 void SmsDaemon::CheckSms()
34 {
35 const int INTERVAL = 250; //no of milliseconds between sms checks
36 timeval now = Util::GetTimeOfDay();
37
38 if ( Util::mTimeDiff(_lastSmsCheck,now) < INTERVAL)
39 return;
40
41 _lastSmsCheck = now;
42
43 Common* cmn = Common::instance();
44
45 PluginManager* manager = cmn->GetPluginManager();
46 vector<SMS> sms = _modem.ReadSms();
47
48 for (unsigned int i=0; i<sms.size(); ++i)
49 {
50 string cmd = GetSmsCommand(sms[i]);
51
52 cmd = Util::str_tolower(cmd);
53
54 Plugin* pl = manager->GetPlugin(cmd);
55
56 CreateLogMessage(sms[i], pl != 0);
57
58 if (pl)
59 {
60 pl->Execute(_modem, sms[i]);
61 }
62 else
63 {
64 _modem.SendSms(sms[i].sender, "Unknown command!", false);
65 }
66
67 _modem.DeleteSms(sms[i].sms_index);
68 cmn->smsCounter.incomming++;
69 }
70 }
71
72
73 void SmsDaemon::MainLoop()
74 {
75 Common* cmn = Common::instance();
76 volatile bool& mainContinue = cmn->mainContinue;
77
78
79 mainContinue = true;
80
81 int loopcount = 0;
82 while (mainContinue)
83 {
84 if (loopcount > 100 )
85 {
86 CheckSms();
87
88 cmn->GetTaskManager()->ExecuteTasks(_modem);
89
90 loopcount = 0;
91 }
92
93 if (cmn->isDebug && kbhit())
94 break;
95
96 Util::Sleep(10);
97
98 }
99 }
100
101 void SmsDaemon::Start()
102 {
103 Common* cmn = Common::instance();
104
105
106 cmn->daemonStart = time(0);
107 _lastSmsCheck = Util::GetTimeOfDay();
108
109 cmn->logMessage("--------------------------------");
110 cmn->logMessage( VERSION );
111 cmn->logMessage( SVNVERSION );
112
113 cmn->GetTaskManager()->LoadTasks();
114 cmn->GetPluginManager()->LoadPlugins();
115 cmn->logMessage("SMS daemon started");
116
117 _modem.DeleteAllSms();
118
119 try
120 {
121 MainLoop();
122 }
123 catch (std::exception& e)
124 {
125 cmn->logMessage( e.what() );
126 }
127 catch (...)
128 {
129 cmn->logMessage( "Caught unknown exception" );
130 }
131
132 cmn->logMessage( cmn->getStatusMessage() );
133 }

  ViewVC Help
Powered by ViewVC 1.1.20