1 |
torben |
88 |
#include "SmsDaemon.h" |
2 |
|
|
|
3 |
|
|
#include <string> |
4 |
|
|
|
5 |
|
|
#include <sstream> |
6 |
torben |
132 |
#include <stdlib.h> |
7 |
|
|
|
8 |
torben |
88 |
#include "common.h" |
9 |
|
|
|
10 |
torben |
145 |
#include "IGsmModem.h" |
11 |
torben |
88 |
|
12 |
|
|
#include "Plugin.h" |
13 |
|
|
#include "kbhit.h" |
14 |
|
|
|
15 |
|
|
#include "util.h" |
16 |
|
|
|
17 |
torben |
92 |
#include "TaskManager.h" |
18 |
|
|
#include "PluginManager.h" |
19 |
|
|
|
20 |
torben |
94 |
#include "version.h" |
21 |
|
|
|
22 |
torben |
88 |
using namespace std; |
23 |
|
|
|
24 |
|
|
void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin) |
25 |
|
|
{ |
26 |
|
|
ostringstream os; |
27 |
torben |
142 |
os << "Recieved sms from " << sms.GetSender() << " ; command=" << sms.ExtractCommand(); |
28 |
torben |
88 |
if (!hasPlugin) |
29 |
|
|
os << " -- PLUGIN NOT FOUND"; |
30 |
|
|
|
31 |
|
|
Common::instance()->logMessage(os.str()); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
void SmsDaemon::CheckSms() |
36 |
|
|
{ |
37 |
torben |
128 |
const int INTERVAL = 1000; //no of milliseconds between sms checks |
38 |
torben |
95 |
timeval now = Util::GetTimeOfDay(); |
39 |
|
|
|
40 |
|
|
if ( Util::mTimeDiff(_lastSmsCheck,now) < INTERVAL) |
41 |
|
|
return; |
42 |
|
|
|
43 |
|
|
_lastSmsCheck = now; |
44 |
|
|
|
45 |
torben |
88 |
Common* cmn = Common::instance(); |
46 |
|
|
|
47 |
torben |
92 |
PluginManager* manager = cmn->GetPluginManager(); |
48 |
torben |
88 |
vector<SMS> sms = _modem.ReadSms(); |
49 |
|
|
|
50 |
|
|
for (unsigned int i=0; i<sms.size(); ++i) |
51 |
|
|
{ |
52 |
torben |
142 |
string cmd = sms[i].ExtractCommand(); |
53 |
torben |
88 |
|
54 |
|
|
cmd = Util::str_tolower(cmd); |
55 |
|
|
|
56 |
torben |
92 |
Plugin* pl = manager->GetPlugin(cmd); |
57 |
torben |
88 |
|
58 |
|
|
CreateLogMessage(sms[i], pl != 0); |
59 |
|
|
|
60 |
|
|
if (pl) |
61 |
|
|
{ |
62 |
|
|
pl->Execute(_modem, sms[i]); |
63 |
|
|
} |
64 |
|
|
else |
65 |
|
|
{ |
66 |
torben |
142 |
_modem.SendSms(sms[i].GetSender(), "Unknown command!", false); |
67 |
torben |
88 |
} |
68 |
|
|
|
69 |
torben |
142 |
_modem.DeleteSms(sms[i].GetIndex()); |
70 |
torben |
88 |
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 |
torben |
128 |
CheckSms(); |
87 |
torben |
88 |
|
88 |
torben |
128 |
cmn->GetTaskManager()->ExecuteTasks(_modem); |
89 |
torben |
88 |
|
90 |
|
|
if (cmn->isDebug && kbhit()) |
91 |
|
|
break; |
92 |
|
|
|
93 |
torben |
128 |
Util::Sleep(100); |
94 |
torben |
88 |
|
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
void SmsDaemon::Start() |
99 |
|
|
{ |
100 |
|
|
Common* cmn = Common::instance(); |
101 |
|
|
|
102 |
torben |
132 |
srand(time(0)); |
103 |
torben |
88 |
|
104 |
|
|
cmn->daemonStart = time(0); |
105 |
torben |
95 |
_lastSmsCheck = Util::GetTimeOfDay(); |
106 |
torben |
88 |
|
107 |
|
|
cmn->logMessage("--------------------------------"); |
108 |
torben |
94 |
cmn->logMessage( VERSION ); |
109 |
torben |
101 |
cmn->logMessage( SVNVERSION ); |
110 |
torben |
88 |
|
111 |
torben |
92 |
cmn->GetTaskManager()->LoadTasks(); |
112 |
|
|
cmn->GetPluginManager()->LoadPlugins(); |
113 |
torben |
88 |
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 |
|
|
} |