#include "videocore.h"
#include "../AGBoxDog/boxshm.h"
AGBoxShm *agBoxShm;

void VideoCore::shm_init(){
    QSqlQuery qry;
    QString sql = QString("select mqtt_ip,mqtt_port,user_name,pass_word from yt_t_mqtt where item_name = 'data-agbox'");
    qry = db.exec(sql);
    while (qry.next()) {
        ip = qry.value(0).toString();
        port = qry.value(1).toString();
        username = qry.value(2).toString();
        password = qry.value(3).toString();
    }
    sql.clear();
    qry.clear();
    sql = QString("select url,app_key,app_signature from yt_t_platforminfo where type = 'video-search-hk'");
    qry = db.exec(sql);
    while (qry.next()) {
        devinfourl = qry.value(0).toString();
        devinfokey = qry.value(1).toString();
        devinfosignature = qry.value(2).toString();
    }
    sql.clear();
    qry.clear();
    sql = QString("select url,app_key,app_signature from yt_t_platforminfo where type = 'video-get-hk'");
    qry = db.exec(sql);
    while (qry.next()) {
       httpurl = qry.value(0).toString();
       httpkey = qry.value(1).toString();
       httpsignature = qry.value(2).toString();
    }
    sql.clear();
    qry.clear();
    sql = QString("select url,app_key,app_signature from yt_t_platforminfo where type = 'video-preview-hk'");
    qry = db.exec(sql);
    while (qry.next()) {
       tcpserverurl = qry.value(0).toString();
       tcpserverkey = qry.value(1).toString();
       tcpserversignature = qry.value(2).toString();
    }
    sql.clear();
    qry.clear();
    sql = QString("select url,app_key,app_signature from yt_t_platforminfo where type = 'video-control-hk'");
    qry = db.exec(sql);
    while (qry.next()) {
       tcpserver1url = qry.value(0).toString();
       tcpserver1key = qry.value(1).toString();
       tcpserver1signature = qry.value(2).toString();
    }
}

bool VideoCore::shm_load(){
    key_t key;
    int shmid;
    if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
        return false;
    }
    if((shmid=shmget(key,sizeof(AGBoxShm),IPC_CREAT|0666))==-1){
        return false;
    }
    agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));

    shm_init();
    return true;
}


VideoCore::VideoCore(QObject *parent) : QObject(parent)
{
    logthread = new LogThread(this);
    logthread->start();

    db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
    db.setDatabaseName(QString("/opt/db/yt_conf.db"));

    if(!db.open()){
        logthread->appendData(QString("[agvideo] open yt_conf.db failed"));
    }
    if(shm_load()){
        logthread->appendData(QString("[agvideo] shm load success"));
    }


    mqttIdx = 1;
    m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
    connect(m_client,&QMQTT::Client::connected,this,&VideoCore::onConnected);
    m_client->setUsername(username);
    m_client->setPassword(password.toLatin1());
    m_client->setCleanSession(true);
    m_client->connectToHost();


    httpthread = new HttpThread(this);
    connect(httpthread,&HttpThread::dataLog,this,&VideoCore::dataLog);
    connect(httpthread,&HttpThread::mqttData,this,&VideoCore::mqtt_data);
    httpthread->app_info(httpurl,httpkey,httpsignature);

    tcpserver = new TcpServer(this);
    connect(tcpserver,&TcpServer::tcp_log,this,&VideoCore::dataLog);
    tcpserver->app_info(tcpserverurl,tcpserverkey,tcpserversignature);

    tcpserver1 = new TcpServer1(this);
    connect(tcpserver1,&TcpServer1::tcp_log,this,&VideoCore::dataLog);
    tcpserver1->app_info(tcpserver1url,tcpserver1key,tcpserver1signature);

    devinfoserver = new DevInfoTcpServer(this);
    connect(devinfoserver,&DevInfoTcpServer::tcp_log,this,&VideoCore::dataLog);
    connect(devinfoserver,&DevInfoTcpServer::append_sql,this,&VideoCore::appendSql);
    devinfoserver->app_info(devinfourl,devinfokey,devinfosignature);

    dbthread = new DatabaseThread(this,&db);
    connect(dbthread,&DatabaseThread::dbdata_log,this,&VideoCore::dataLog);

}

VideoCore::~VideoCore()
{
    logthread->stop();
    httpthread->stop();
    dbthread->stop();
}

void VideoCore::start()
{
    tcpserver->start();
    tcpserver1->start();
    devinfoserver->start();
    dbthread->start();
}

void VideoCore::onConnected()
{
    logthread->appendData(QString("mqtt onConnected"));
}

void VideoCore::dataLog(QString log)
{
    logthread->appendData(log);
}

void VideoCore::mqtt_data(QString mqtt_msg)
{
    printf("test1111 [%s]\n",mqtt_msg.toUtf8().data());
    if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
        m_client->connectToHost();
    }
    m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
    if(mqttIdx > 9999){
        mqttIdx = 1;
    }
}

void VideoCore::appendSql(QString sql)
{
    dbthread->appendSql(sql);
    shm_init();
}




