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 |
loopcount++; |
97 |
Util::Sleep(10); |
98 |
|
99 |
} |
100 |
} |
101 |
|
102 |
void SmsDaemon::Start() |
103 |
{ |
104 |
Common* cmn = Common::instance(); |
105 |
|
106 |
|
107 |
cmn->daemonStart = time(0); |
108 |
_lastSmsCheck = Util::GetTimeOfDay(); |
109 |
|
110 |
cmn->logMessage("--------------------------------"); |
111 |
cmn->logMessage( VERSION ); |
112 |
cmn->logMessage( SVNVERSION ); |
113 |
|
114 |
cmn->GetTaskManager()->LoadTasks(); |
115 |
cmn->GetPluginManager()->LoadPlugins(); |
116 |
cmn->logMessage("SMS daemon started"); |
117 |
|
118 |
_modem.DeleteAllSms(); |
119 |
|
120 |
try |
121 |
{ |
122 |
MainLoop(); |
123 |
} |
124 |
catch (std::exception& e) |
125 |
{ |
126 |
cmn->logMessage( e.what() ); |
127 |
} |
128 |
catch (...) |
129 |
{ |
130 |
cmn->logMessage( "Caught unknown exception" ); |
131 |
} |
132 |
|
133 |
cmn->logMessage( cmn->getStatusMessage() ); |
134 |
} |