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

Contents of /smsdaemon/SmsDaemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 132 - (show annotations) (download)
Sun Dec 7 00:59:05 2008 UTC (15 years, 5 months ago) by torben
File size: 2203 byte(s)
Added spooling (queing) function, with a standalone application(smsqueue) to put new messages 
into the spool dir.

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

  ViewVC Help
Powered by ViewVC 1.1.20