#include "SmsDaemon.h" #include #include #include "common.h" #include "GsmModem.h" #include "Plugin.h" #include "kbhit.h" #include "util.h" #include "TaskManager.h" #include "PluginManager.h" #include "version.h" using namespace std; void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin) { ostringstream os; os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); if (!hasPlugin) os << " -- PLUGIN NOT FOUND"; Common::instance()->logMessage(os.str()); } void SmsDaemon::CheckSms() { const int INTERVAL = 250; //no of milliseconds between sms checks timeval now = Util::GetTimeOfDay(); if ( Util::mTimeDiff(_lastSmsCheck,now) < INTERVAL) return; _lastSmsCheck = now; Common* cmn = Common::instance(); PluginManager* manager = cmn->GetPluginManager(); vector sms = _modem.ReadSms(); for (unsigned int i=0; iGetPlugin(cmd); CreateLogMessage(sms[i], pl != 0); if (pl) { pl->Execute(_modem, sms[i]); } else { _modem.SendSms(sms[i].sender, "Unknown command!", false); } _modem.DeleteSms(sms[i].sms_index); cmn->smsCounter.incomming++; } } void SmsDaemon::MainLoop() { Common* cmn = Common::instance(); volatile bool& mainContinue = cmn->mainContinue; mainContinue = true; while (mainContinue) { CheckSms(); cmn->GetTaskManager()->ExecuteTasks(_modem); if (cmn->isDebug && kbhit()) break; Util::Sleep(10); } } void SmsDaemon::Start() { Common* cmn = Common::instance(); cmn->daemonStart = time(0); _lastSmsCheck = Util::GetTimeOfDay(); cmn->logMessage("--------------------------------"); cmn->logMessage( VERSION ); cmn->logMessage( SVNVER ); cmn->GetTaskManager()->LoadTasks(); cmn->GetPluginManager()->LoadPlugins(); cmn->logMessage("SMS daemon started"); _modem.DeleteAllSms(); try { MainLoop(); } catch (std::exception& e) { cmn->logMessage( e.what() ); } catch (...) { cmn->logMessage( "Caught unknown exception" ); } cmn->logMessage( cmn->getStatusMessage() ); }