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

Contents of /smsdaemon/SmsDaemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations) (download)
Sat Dec 6 15:13:34 2008 UTC (15 years, 5 months ago) by torben
File size: 2165 byte(s)
Revert r125 & r126 - and use some less aggressive timevalues

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 = 1000; //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 while (mainContinue)
82 {
83
84 CheckSms();
85
86 cmn->GetTaskManager()->ExecuteTasks(_modem);
87
88 if (cmn->isDebug && kbhit())
89 break;
90
91 Util::Sleep(100);
92
93 }
94 }
95
96 void SmsDaemon::Start()
97 {
98 Common* cmn = Common::instance();
99
100
101 cmn->daemonStart = time(0);
102 _lastSmsCheck = Util::GetTimeOfDay();
103
104 cmn->logMessage("--------------------------------");
105 cmn->logMessage( VERSION );
106 cmn->logMessage( SVNVERSION );
107
108 cmn->GetTaskManager()->LoadTasks();
109 cmn->GetPluginManager()->LoadPlugins();
110 cmn->logMessage("SMS daemon started");
111
112 _modem.DeleteAllSms();
113
114 try
115 {
116 MainLoop();
117 }
118 catch (std::exception& e)
119 {
120 cmn->logMessage( e.what() );
121 }
122 catch (...)
123 {
124 cmn->logMessage( "Caught unknown exception" );
125 }
126
127 cmn->logMessage( cmn->getStatusMessage() );
128 }

  ViewVC Help
Powered by ViewVC 1.1.20