Daemon.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include "Daemon.h"
  5. #include "Message.h"
  6. #include <QCoreApplication>
  7. #include <QDir>
  8. #include <QDebug>
  9. #include <QTimer>
  10. #include <QRemoteServiceRegister>
  11. #include <QServiceManager>
  12. Daemon::Daemon(QObject *parent) :
  13. QObject(parent)
  14. {
  15. reset();
  16. // Daemon settings
  17. m_settings = new QSettings("nokia","whowheredaemon",this);
  18. readSettings();
  19. // For logging messages into text
  20. #ifdef WRITE_LOG_TO_FILE
  21. QString path("C:\\Data\\whowheredaemon.dat");
  22. m_file = new QFile(path);
  23. // Delete old log file
  24. if (QFile::exists(path)) {
  25. QFile::remove(path);
  26. }
  27. // Open new log
  28. m_file->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
  29. m_outStream.setDevice(m_file);
  30. #endif
  31. // SMS message listening / sending
  32. m_message = new Message(this);
  33. QObject::connect(m_message, SIGNAL(friendAskLocationSMS(QString)), this,
  34. SLOT(friendAskLocationSMS(QString)));
  35. // Create GPS
  36. createGPS();
  37. // Start GPS
  38. if (m_daemonEnabled) {
  39. QTimer::singleShot(5000,this,SLOT(startGps()));
  40. }
  41. log("WhoWhereDaemon Started");
  42. }
  43. Daemon::~Daemon()
  44. {
  45. // Stop GPS
  46. if (m_location) {
  47. m_location->stopUpdates();
  48. delete m_location;
  49. }
  50. #ifdef WRITE_LOG_TO_FILE
  51. m_file->close();
  52. delete m_file;
  53. #endif
  54. }
  55. void Daemon::readSettings()
  56. {
  57. m_daemonEnabled = m_settings->value("daemonEnabled",QVariant(true)).toBool();
  58. m_daemonLogEnabled = m_settings->value("daemonLogEnabled",QVariant(true)).toBool();
  59. m_keepGpsRunning = m_settings->value("daemonGpsAlive",QVariant(false)).toBool();
  60. m_accuracy = m_settings->value("daemonGpsAccuracy",QVariant(20.0)).toDouble();
  61. }
  62. void Daemon::saveSettings()
  63. {
  64. m_settings->setValue("daemonEnabled",QVariant(m_daemonEnabled));
  65. m_settings->setValue("daemonLogEnabled",QVariant(m_daemonLogEnabled));
  66. m_settings->setValue("daemonGpsAlive",QVariant(m_keepGpsRunning));
  67. m_settings->setValue("daemonGpsAccuracy",QVariant(m_accuracy));
  68. m_settings->sync();
  69. }
  70. void Daemon::createGPS()
  71. {
  72. if (!m_location)
  73. {
  74. m_location = QGeoPositionInfoSource::createDefaultSource(this);
  75. m_location->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);
  76. m_location->setUpdateInterval(10*1000);
  77. // System has some positioning source found
  78. if (m_location) {
  79. QObject::connect(m_location, SIGNAL(positionUpdated(QGeoPositionInfo)), this,
  80. SLOT(positionUpdated(QGeoPositionInfo)));
  81. QObject::connect(m_location, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
  82. }
  83. else {
  84. // System has not any positioning source
  85. log("Device has not any positioning source. WhoWhereDaemon is exiting");
  86. // Exit application
  87. QCoreApplication::exit();
  88. }
  89. }
  90. }
  91. void Daemon::deleteGPS()
  92. {
  93. emit gpsClosed();
  94. if (m_location) {
  95. m_location->stopUpdates();
  96. }
  97. delete m_location;
  98. m_location = 0;
  99. log("GPS stopped");
  100. }
  101. void Daemon::startGps()
  102. {
  103. emit gpsInitialized();
  104. createGPS();
  105. m_location->startUpdates();
  106. log("GPS started");
  107. }
  108. void Daemon::log(QString str)
  109. {
  110. qDebug() << str;
  111. if (m_daemonLogEnabled) {
  112. emit daemonLog(str);
  113. }
  114. #ifdef WRITE_LOG_TO_FILE
  115. str.append("\n");
  116. m_outStream << str;
  117. m_outStream.flush();
  118. #endif
  119. }
  120. void Daemon::friendAskLocationSMS(QString friendPhoneNumber)
  121. {
  122. // Friend asks location
  123. //log(QString("Send location to number %1 asks your location").arg(friendPhoneNumber));
  124. if (m_daemonEnabled) {
  125. // Store requester phone number
  126. m_askingLocation = friendPhoneNumber;
  127. // Read your location
  128. if (!m_YougeoPosInfo.isValid()) {
  129. startGps();
  130. } else {
  131. // You have location, send it
  132. m_message->sendLocationSMS("RES:", m_YougeoPosInfo, m_askingLocation);
  133. // Reset position and requester
  134. reset();
  135. }
  136. }
  137. }
  138. void Daemon::updateTimeout()
  139. {
  140. log("Waiting GPS location...");
  141. }
  142. void Daemon::positionUpdated(QGeoPositionInfo gpsPos)
  143. {
  144. qreal haMeters = 0;
  145. if(gpsPos.isValid()) {
  146. haMeters = gpsPos.attribute(QGeoPositionInfo::HorizontalAccuracy);
  147. log(QString("Location accuracy is %1 m").arg(haMeters));
  148. } else {
  149. log("Location updated, but not valid");
  150. }
  151. if (!m_daemonEnabled) {
  152. deleteGPS();
  153. return;
  154. }
  155. if(gpsPos.isValid()) {
  156. m_YougeoPosInfo = gpsPos;
  157. if (haMeters < m_accuracy && haMeters != 0) {
  158. emit gpsLocationReceived();
  159. if (!m_keepGpsRunning) {
  160. deleteGPS();
  161. log(QString("GPS stopped. Accuracy was %1 m").arg(haMeters));
  162. }
  163. // Is there location request?
  164. if (!m_askingLocation.isEmpty()) {
  165. // Send location response SMS
  166. m_message->sendLocationSMS("RES:", m_YougeoPosInfo, m_askingLocation);
  167. log(QString("SMS sent to number %1").arg(m_askingLocation));
  168. // Reset position and requester
  169. reset();
  170. }
  171. }
  172. }
  173. }
  174. void Daemon::reset()
  175. {
  176. // Reset position and requester
  177. m_askingLocation = "";
  178. m_YougeoPosInfo.setCoordinate(QGeoCoordinate());
  179. }
  180. void Daemon::enableDaemon(QVariant enable)
  181. {
  182. m_daemonEnabled = enable.toBool();
  183. if (m_daemonEnabled) {
  184. // Reset position and requester
  185. reset();
  186. // Start GPS
  187. startGps();
  188. log("Client enables WhoWhereDaemon");
  189. } else {
  190. deleteGPS();
  191. log("Client disables WhoWhereDaemon");
  192. }
  193. saveSettings();
  194. }
  195. void Daemon::killDaemon()
  196. {
  197. log("Client is killing the WhoWhereDaemon");
  198. QCoreApplication::exit();
  199. }
  200. void Daemon::keepGpsRunning(QVariant running)
  201. {
  202. m_keepGpsRunning = running.toBool();
  203. if (m_keepGpsRunning) {
  204. log("Client keeps GPS running");
  205. }else {
  206. log("Client allows GPS to shutdown");
  207. }
  208. saveSettings();
  209. }
  210. void Daemon::gpsAccuracy(QVariant meters)
  211. {
  212. m_accuracy = meters.toDouble();
  213. log(QString("Client sets GPS accuracy to %1 meters").arg(m_accuracy));
  214. saveSettings();
  215. }
  216. void Daemon::sendLocationTo(QVariant number)
  217. {
  218. friendAskLocationSMS(number.toString());
  219. log(QString("Client sends GPS location to number %1").arg(number.toString()));
  220. }
  221. void Daemon::enableDaemonLog(QVariant enable)
  222. {
  223. m_daemonLogEnabled = enable.toBool();
  224. if (m_daemonLogEnabled) {
  225. log("Client enables WhoWhereDaemon log");
  226. } else {
  227. log("Client disables WhoWhereDaemon log");
  228. }
  229. saveSettings();
  230. }
  231. QVariant Daemon::isDaemonEnabled()
  232. {
  233. log("Client asks is daemon enabled");
  234. return QVariant(m_daemonEnabled);
  235. }
  236. QVariant Daemon::isLogEnabled()
  237. {
  238. log("Client asks is log enabled");
  239. return QVariant(m_daemonLogEnabled);
  240. }
  241. QVariant Daemon::isGpsRunningEnabled()
  242. {
  243. log("Client asks is GPS enabled");
  244. return QVariant(m_keepGpsRunning);
  245. }
  246. QVariant Daemon::getGpsAccuracy()
  247. {
  248. log("Client asks GPS accuracy");
  249. return QVariant(m_accuracy);
  250. }
  251. void Daemon::storeSettings()
  252. {
  253. saveSettings();
  254. }