05ff72ce685c059585fa7a12d7cf8f36063e1183.svn-base 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "messagesender.h"
  2. MessageSender::MessageSender(QObject *parent, QString serverPath, QString key, QString id) :
  3. QObject(parent)
  4. {
  5. waitting = false;
  6. hour = 255;
  7. ServerPath = serverPath;
  8. UKey = key;
  9. SID = id;
  10. msgList.clear();
  11. timer = new QTimer(this);
  12. connect(timer,SIGNAL(timeout()),this,SLOT(time_out()));
  13. queryManager = new QNetworkAccessManager(this);
  14. connect(queryManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(query_finishedSlot(QNetworkReply*)));
  15. beepManager = new QNetworkAccessManager(this);
  16. connect(beepManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(beep_finishedSlot(QNetworkReply*)));
  17. time_out();
  18. timer->start(5000);
  19. }
  20. void MessageSender::appendMsg(Msg msg)
  21. {
  22. msgList.append(msg);
  23. }
  24. void MessageSender::reply_timeout()
  25. {
  26. waitting = false;
  27. }
  28. void MessageSender::query_finishedSlot(QNetworkReply *reply)
  29. {
  30. if(reply->error()==QNetworkReply::NoError){
  31. QByteArray qj = reply->readAll();
  32. // printf("%s\n",qj.data());
  33. emit sent(msgList.first().ID);
  34. msgList.removeFirst();
  35. }
  36. waitting = false;
  37. }
  38. void MessageSender::beep_finishedSlot(QNetworkReply *reply)
  39. {
  40. if(reply->error()==QNetworkReply::NoError){
  41. QByteArray qj = reply->readAll();
  42. }
  43. waitting = false;
  44. }
  45. void MessageSender::time_out()
  46. {
  47. if(!waitting){
  48. if(msgList.length()>0){
  49. waitting = true;
  50. Msg msg = msgList.first();
  51. QNetworkRequest *req = new QNetworkRequest(QUrl(ServerPath));
  52. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded; charset=UTF-8");
  53. req->setRawHeader("Accept","text/html, application/xhtml+xml, */*");
  54. req->setRawHeader("Accept-Language","zh-CN");
  55. req->setRawHeader("Connection","keep-alive");
  56. req->setRawHeader("Cache-Control"," no-cache");
  57. QByteArray postdata;
  58. postdata.append(msg.MSG.toUtf8());
  59. req->setRawHeader("Content-Length",QString("%1").arg(postdata.length()).toUtf8());
  60. QNetworkReply *reply = queryManager->post(*req,postdata);
  61. // printf("%s\n",postdata.data());
  62. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  63. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
  64. }else if(hour!=QTime::currentTime().hour()){
  65. if(QTime::currentTime().minute()>2){
  66. waitting = true;
  67. QNetworkRequest *req = new QNetworkRequest(QUrl(ServerPath));
  68. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded; charset=UTF-8");
  69. req->setRawHeader("Accept","text/html, application/xhtml+xml, */*");
  70. req->setRawHeader("Accept-Language","zh-CN");
  71. req->setRawHeader("Connection","keep-alive");
  72. req->setRawHeader("Cache-Control"," no-cache");
  73. QString postdata = QString("key=%1&json={\"jsonrpc\":\"2.0\",\"id\":\"%2\",\"method\":\"updateHeart\",\"params\":{\"deviceId\":\"%2\"}}")
  74. .arg(UKey).arg(SID);
  75. req->setRawHeader("Content-Length",QString("%1").arg(postdata.length()).toUtf8());
  76. QNetworkReply *reply = beepManager->post(*req,postdata.toUtf8());
  77. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  78. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
  79. }
  80. }
  81. }
  82. }