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

Contents of /smsdaemon/SmsDaemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 95 - (show annotations) (download)
Mon Jun 16 12:21:30 2008 UTC (15 years, 11 months ago) by torben
File size: 2160 byte(s)
Set an interval between sms reads from modem.

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

  ViewVC Help
Powered by ViewVC 1.1.20