#include "writecore.h"
#include "../DataWriteDog/writeshm.h"

WriteCore::WriteCore(QObject *parent) : QObject(parent)
{
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&WriteCore::time_out);
    timer->start(1000);

    dbthread = new DatabaseThread(this);
    dbthread->start();
    connect(dbthread,&DatabaseThread::mqtt_conf,this,&WriteCore::mqtt_data);
    connect(dbthread,&DatabaseThread::dbdata_log,this,&WriteCore::dbdata_log);

    dbeventthread = new DatabaseEventThread(this);
    dbeventthread->start();
    connect(dbeventthread,&DatabaseEventThread::dbdata_log,this,&WriteCore::dbdata_log);

    logthread = new LogThread(this);
    logthread->start();

    mqthread = new MqttDataThread(this);
    connect(mqthread,&MqttDataThread::mq_log,this,&WriteCore::mq_log);
    connect(mqthread,&MqttDataThread::sendMqttData,this,&WriteCore::sendMqttData);

    dealmqthread = new DealMqttThread(this);
    connect(dealmqthread,&DealMqttThread::dmq_log,this,&WriteCore::dmq_log);
    connect(dealmqthread,&DealMqttThread::dmqSql,this,&WriteCore::appendSql);

    mqeventthread = new MqttEventDataThread(this);
    connect(mqeventthread,&MqttEventDataThread::mq_log,this,&WriteCore::mq_log);
    connect(mqeventthread,&MqttEventDataThread::sendMqttData,this,&WriteCore::sendMqttEventData);

    dealmqeventthread = new DealMqttEventThread(this);
    connect(dealmqeventthread,&DealMqttEventThread::dmq_log,this,&WriteCore::dmq_log);
    connect(dealmqeventthread,&DealMqttEventThread::dmqSql,this,&WriteCore::appendEventSql);

    mqalarmthread = new MqttAlarmDataThread(this);
    connect(mqalarmthread,&MqttAlarmDataThread::mq_log,this,&WriteCore::mq_log);
    connect(mqalarmthread,&MqttAlarmDataThread::sendMqttData,this,&WriteCore::sendMqttAlarmData);

    dealmqalarmthread = new DealMqttAlarmThread(this);
    connect(dealmqalarmthread,&DealMqttAlarmThread::dmq_log,this,&WriteCore::dmq_log);
    connect(dealmqalarmthread,&DealMqttAlarmThread::dmqSql,this,&WriteCore::appendEventSql);
}

WriteCore::~WriteCore()
{
    dbthread->stop();
    dbeventthread->stop();
    logthread->stop();
    dealmqthread->stop();
    dealmqeventthread->stop();
    dealmqalarmthread->stop();
}

void WriteCore::start()
{
    mqthread->start();
    dealmqthread->start();
    mqeventthread->start();
    dealmqeventthread->start();
    mqalarmthread->start();
    dealmqalarmthread->start();
}

void WriteCore::time_out()
{
//    dataWriteShm->processStatus[1].t_time = QDateTime::currentDateTime().toTime_t();

}

void WriteCore::mqtt_data(QString ip,QString port,QString username,QString password)
{
    mqthread->mqtt_conf(ip,port,username,password);
    mqeventthread->mqtt_conf(ip,port,username,password);
    mqalarmthread->mqtt_conf(ip,port,username,password);

}

void WriteCore::dbdata_log(QString log)
{
    logthread->appendData(log);
}

void WriteCore::mq_log(QString log)
{
    logthread->appendData(log);
}

void WriteCore::sendMqttData(QString topic,QByteArray data)
{
    dealmqthread->appendMqttData(MqttData(topic,data));
}

void WriteCore::sendMqttEventData(QString topic,QByteArray data)
{
    dealmqeventthread->appendMqttData(MqttEventData(topic,data));
}

void WriteCore::sendMqttAlarmData(QString topic,QByteArray data)
{
    dealmqalarmthread->appendMqttData(MqttAlarmData(topic,data));
}

void WriteCore::dmq_log(QString log)
{
    logthread->appendData(log);
}

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

void WriteCore::appendEventSql(QString sql)
{
    dbeventthread->appendSql(sql);
}






