#include "messagesender.h"


MessageSender::MessageSender(QObject *parent, QString serverPath, QString key, QString id) :
    QObject(parent)
{
    waitting = false;
    hour = 255;
    minutes = 255;
    ServerPath = serverPath;
    UKey = key;
    SID = id;
    msgList.clear();
//    printf("sender path:%s\nkey:%s,id:%s\n",ServerPath.toUtf8().data(),UKey.toUtf8().data(),SID.toUtf8().data());
    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();
        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();
            printf("%s\n",msg.MSG.toUtf8().data());
            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);
            QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
            connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
        }else if(minutes!=QTime::currentTime().minute()){
            if(QTime::currentTime().minute()>2){
                waitting = true;
                minutes = QTime::currentTime().minute();
                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()));
            }
        }
    }
}
