123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #include "messagesender.h"
- MessageSender::MessageSender(QObject *parent, QString serverPath, QString key, QString id) :
- QObject(parent)
- {
- waitting = false;
- hour = 255;
- ServerPath = serverPath;
- UKey = key;
- SID = id;
- msgList.clear();
- timer = new QTimer(this);
- connect(timer,SIGNAL(timeout()),this,SLOT(time_out()));
- queryManager = new QNetworkAccessManager(this);
- connect(queryManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(query_finishedSlot(QNetworkReply*)));
- beepManager = new QNetworkAccessManager(this);
- connect(beepManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(beep_finishedSlot(QNetworkReply*)));
- time_out();
- timer->start(5000);
- }
- void MessageSender::appendMsg(Msg msg)
- {
- msgList.append(msg);
- }
- void MessageSender::reply_timeout()
- {
- waitting = false;
- }
- void MessageSender::query_finishedSlot(QNetworkReply *reply)
- {
- if(reply->error()==QNetworkReply::NoError){
- QByteArray qj = reply->readAll();
- // printf("%s\n",qj.data());
- emit sent(msgList.first().ID);
- msgList.removeFirst();
- }
- waitting = false;
- }
- void MessageSender::beep_finishedSlot(QNetworkReply *reply)
- {
- if(reply->error()==QNetworkReply::NoError){
- QByteArray qj = reply->readAll();
- }
- waitting = false;
- }
- void MessageSender::time_out()
- {
- if(!waitting){
- if(msgList.length()>0){
- waitting = true;
- Msg msg = msgList.first();
- QNetworkRequest *req = new QNetworkRequest(QUrl(ServerPath));
- req->setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded; charset=UTF-8");
- req->setRawHeader("Accept","text/html, application/xhtml+xml, */*");
- req->setRawHeader("Accept-Language","zh-CN");
- req->setRawHeader("Connection","keep-alive");
- req->setRawHeader("Cache-Control"," no-cache");
- QByteArray postdata;
- postdata.append(msg.MSG.toUtf8());
- req->setRawHeader("Content-Length",QString("%1").arg(postdata.length()).toUtf8());
- QNetworkReply *reply = queryManager->post(*req,postdata);
- // printf("%s\n",postdata.data());
- QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
- connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
- }else if(hour!=QTime::currentTime().hour()){
- if(QTime::currentTime().minute()>2){
- waitting = true;
- QNetworkRequest *req = new QNetworkRequest(QUrl(ServerPath));
- req->setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded; charset=UTF-8");
- req->setRawHeader("Accept","text/html, application/xhtml+xml, */*");
- req->setRawHeader("Accept-Language","zh-CN");
- req->setRawHeader("Connection","keep-alive");
- req->setRawHeader("Cache-Control"," no-cache");
- QString postdata = QString("key=%1&json={\"jsonrpc\":\"2.0\",\"id\":\"%2\",\"method\":\"updateHeart\",\"params\":{\"deviceId\":\"%2\"}}")
- .arg(UKey).arg(SID);
- req->setRawHeader("Content-Length",QString("%1").arg(postdata.length()).toUtf8());
- QNetworkReply *reply = beepManager->post(*req,postdata.toUtf8());
- QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
- connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
- }
- }
- }
- }
|