1 |
torben |
88 |
#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 |
torben |
92 |
#include "TaskManager.h" |
16 |
|
|
#include "PluginManager.h" |
17 |
|
|
|
18 |
torben |
94 |
#include "version.h" |
19 |
|
|
|
20 |
torben |
88 |
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 |
torben |
95 |
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 |
torben |
88 |
Common* cmn = Common::instance(); |
44 |
|
|
|
45 |
torben |
92 |
PluginManager* manager = cmn->GetPluginManager(); |
46 |
torben |
88 |
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 |
torben |
92 |
Plugin* pl = manager->GetPlugin(cmd); |
55 |
torben |
88 |
|
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 |
torben |
125 |
int loopcount = 0; |
82 |
torben |
88 |
while (mainContinue) |
83 |
|
|
{ |
84 |
torben |
125 |
if (loopcount > 100 ) |
85 |
|
|
{ |
86 |
|
|
CheckSms(); |
87 |
torben |
88 |
|
88 |
torben |
125 |
cmn->GetTaskManager()->ExecuteTasks(_modem); |
89 |
torben |
88 |
|
90 |
torben |
125 |
loopcount = 0; |
91 |
|
|
} |
92 |
torben |
88 |
|
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 |
torben |
95 |
_lastSmsCheck = Util::GetTimeOfDay(); |
108 |
torben |
88 |
|
109 |
|
|
cmn->logMessage("--------------------------------"); |
110 |
torben |
94 |
cmn->logMessage( VERSION ); |
111 |
torben |
101 |
cmn->logMessage( SVNVERSION ); |
112 |
torben |
88 |
|
113 |
torben |
92 |
cmn->GetTaskManager()->LoadTasks(); |
114 |
|
|
cmn->GetPluginManager()->LoadPlugins(); |
115 |
torben |
88 |
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 |
|
|
} |