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 |
88 |
using namespace std; |
19 |
|
|
|
20 |
|
|
|
21 |
|
|
void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin) |
22 |
|
|
{ |
23 |
|
|
ostringstream os; |
24 |
|
|
os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); |
25 |
|
|
if (!hasPlugin) |
26 |
|
|
os << " -- PLUGIN NOT FOUND"; |
27 |
|
|
|
28 |
|
|
Common::instance()->logMessage(os.str()); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
void SmsDaemon::CheckSms() |
33 |
|
|
{ |
34 |
|
|
Common* cmn = Common::instance(); |
35 |
|
|
|
36 |
torben |
92 |
PluginManager* manager = cmn->GetPluginManager(); |
37 |
torben |
88 |
vector<SMS> sms = _modem.ReadSms(); |
38 |
|
|
|
39 |
|
|
for (unsigned int i=0; i<sms.size(); ++i) |
40 |
|
|
{ |
41 |
|
|
string cmd = GetSmsCommand(sms[i]); |
42 |
|
|
|
43 |
|
|
cmd = Util::str_tolower(cmd); |
44 |
|
|
|
45 |
torben |
92 |
Plugin* pl = manager->GetPlugin(cmd); |
46 |
torben |
88 |
|
47 |
|
|
CreateLogMessage(sms[i], pl != 0); |
48 |
|
|
|
49 |
|
|
if (pl) |
50 |
|
|
{ |
51 |
|
|
pl->Execute(_modem, sms[i]); |
52 |
|
|
} |
53 |
|
|
else |
54 |
|
|
{ |
55 |
|
|
_modem.SendSms(sms[i].sender, "Unknown command!", false); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
_modem.DeleteSms(sms[i].sms_index); |
59 |
|
|
cmn->smsCounter.incomming++; |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
void SmsDaemon::MainLoop() |
65 |
|
|
{ |
66 |
|
|
Common* cmn = Common::instance(); |
67 |
|
|
volatile bool& mainContinue = cmn->mainContinue; |
68 |
|
|
|
69 |
|
|
|
70 |
|
|
mainContinue = true; |
71 |
|
|
|
72 |
|
|
while (mainContinue) |
73 |
|
|
{ |
74 |
|
|
|
75 |
|
|
CheckSms(); |
76 |
|
|
|
77 |
torben |
92 |
cmn->GetTaskManager()->ExecuteTasks(_modem); |
78 |
torben |
88 |
|
79 |
|
|
if (cmn->isDebug && kbhit()) |
80 |
|
|
break; |
81 |
|
|
|
82 |
|
|
Util::Sleep(10); |
83 |
|
|
|
84 |
|
|
} |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
void SmsDaemon::Start() |
88 |
|
|
{ |
89 |
|
|
Common* cmn = Common::instance(); |
90 |
|
|
|
91 |
|
|
|
92 |
|
|
cmn->daemonStart = time(0); |
93 |
|
|
|
94 |
|
|
cmn->logMessage("--------------------------------"); |
95 |
|
|
|
96 |
torben |
92 |
cmn->GetTaskManager()->LoadTasks(); |
97 |
|
|
cmn->GetPluginManager()->LoadPlugins(); |
98 |
torben |
88 |
cmn->logMessage("SMS daemon started"); |
99 |
|
|
|
100 |
|
|
_modem.DeleteAllSms(); |
101 |
|
|
|
102 |
|
|
try |
103 |
|
|
{ |
104 |
|
|
MainLoop(); |
105 |
|
|
} |
106 |
|
|
catch (std::exception& e) |
107 |
|
|
{ |
108 |
|
|
cmn->logMessage( e.what() ); |
109 |
|
|
} |
110 |
|
|
catch (...) |
111 |
|
|
{ |
112 |
|
|
cmn->logMessage( "Caught unknown exception" ); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
cmn->logMessage( cmn->getStatusMessage() ); |
116 |
|
|
} |