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

void ElevatorCore::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 = 'ladder-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 = 'ladder-record-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 = 'ladder-state-hk'");
    qry = db.exec(sql);
    while (qry.next()) {
        httpurl1 = qry.value(0).toString();
        httpkey1 = qry.value(1).toString();
        httpsignature1 = qry.value(2).toString();
    }

}

bool ElevatorCore::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;
}

ElevatorCore::ElevatorCore(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("[agelevator] open yt_conf.db failed"));
    }
    if(shm_load()){
        logthread->appendData(QString("[agelevator] shm load success"));
    }

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

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

    httpthread = new HttpThread(this);
    connect(httpthread,&HttpThread::dataLog,this,&ElevatorCore::dataLog);
    connect(httpthread,&HttpThread::mqttData,this,&ElevatorCore::mqtt_data);
    connect(httpthread,&HttpThread::mqttEvent,this,&ElevatorCore::mqtt_event);
    httpthread->app_info(httpurl,httpkey,httpsignature,httpurl1,httpkey1,httpsignature1);

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

}


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

void ElevatorCore::start()
{
    dbthread->start();
    devinfoserver->start();

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

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

void ElevatorCore::mqtt_data(QString mqtt_msg)
{
    printf("test3333 [%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 ElevatorCore::mqtt_event(QString mqtt_msg)
{
    printf("test3333 mqtt event [%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(mqttEventIdx++,"event-collector",mqtt_msg.toUtf8()));
    if(mqttEventIdx > 9999){
        mqttEventIdx = 1;
    }
}

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




