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

Contents of /smsdaemon/SmsDaemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 151 - (show annotations) (download)
Mon Dec 8 10:42:04 2008 UTC (15 years, 5 months ago) by torben
File size: 2389 byte(s)
Added a transceiver which uses smstools for interfaceing with the gsm modem.
-> http://smstools3.kekekasvi.com/


1 #include "SmsDaemon.h"
2
3 #include <string>
4
5 #include <sstream>
6 #include <stdlib.h>
7
8 #include "common.h"
9
10 #include "ISmsTransceiver.h"
11
12 #include "Plugin.h"
13 #include "kbhit.h"
14
15 #include "util.h"
16
17 #include "TaskManager.h"
18 #include "PluginManager.h"
19 #include "ConfigFile.h"
20
21 #include "version.h"
22
23 using namespace std;
24
25 void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin)
26 {
27 ostringstream os;
28 os << "Recieved sms from " << sms.GetSender() << " ; command=" << sms.ExtractCommand();
29 if (!hasPlugin)
30 os << " -- PLUGIN NOT FOUND";
31
32 Common::instance()->logMessage(os.str());
33 }
34
35
36 void SmsDaemon::CheckSms()
37 {
38 const int INTERVAL = 1000; //no of milliseconds between sms checks
39 timeval now = Util::GetTimeOfDay();
40
41 if ( Util::mTimeDiff(_lastSmsCheck,now) < INTERVAL)
42 return;
43
44 _lastSmsCheck = now;
45
46 Common* cmn = Common::instance();
47
48 PluginManager* manager = cmn->GetPluginManager();
49 vector<SMS> sms = _modem.ReadSms();
50
51 for (unsigned int i=0; i<sms.size(); ++i)
52 {
53 string cmd = sms[i].ExtractCommand();
54
55 cmd = Util::str_tolower(cmd);
56
57 Plugin* pl = manager->GetPlugin(cmd);
58
59 CreateLogMessage(sms[i], pl != 0);
60
61 if (pl)
62 {
63 pl->Execute(_modem, sms[i]);
64 }
65 else
66 {
67 _modem.SendSms(sms[i].GetSender(), "Unknown command!", false);
68 }
69
70 _modem.DeleteSms(sms[i].GetIndex());
71 cmn->smsCounter.incomming++;
72 }
73 }
74
75
76 void SmsDaemon::MainLoop()
77 {
78 Common* cmn = Common::instance();
79 volatile bool& mainContinue = cmn->mainContinue;
80
81
82 mainContinue = true;
83
84 while (mainContinue)
85 {
86
87 CheckSms();
88
89 cmn->GetTaskManager()->ExecuteTasks(_modem);
90
91 if (cmn->isDebug && kbhit())
92 break;
93
94 Util::Sleep(100);
95
96 }
97 }
98
99 void SmsDaemon::Start()
100 {
101 Common* cmn = Common::instance();
102 string transceiver = cmn->GetConfigfile()->GetValue("smsdaemon","transceiver");
103
104 srand(time(0));
105
106 cmn->daemonStart = time(0);
107 _lastSmsCheck = Util::GetTimeOfDay();
108
109 cmn->logMessage("--------------------------------");
110 cmn->logMessage( VERSION );
111 cmn->logMessage( SVNVERSION );
112 cmn->logMessage( string("Transceiver: ") + transceiver );
113
114 cmn->GetTaskManager()->LoadTasks();
115 cmn->GetPluginManager()->LoadPlugins();
116 cmn->logMessage("SMS daemon started");
117
118 _modem.DeleteAllSms();
119
120 try
121 {
122 MainLoop();
123 }
124 catch (std::exception& e)
125 {
126 cmn->logMessage( e.what() );
127 }
128 catch (...)
129 {
130 cmn->logMessage( "Caught unknown exception" );
131 }
132
133 cmn->logMessage( cmn->getStatusMessage() );
134 }

  ViewVC Help
Powered by ViewVC 1.1.20