#include "bridge.h"
#include <QDebug>
extern QString DB_RFIDComName;
extern QString DB_RFIDComBund;
//extern
//extern
//extern
Bridge::Bridge(QObject *parent) : QObject(parent)
{
    qDebug()<<"主线程:"<< QThread::currentThreadId();
    //声明读卡器模块和线程
    m_ReadEPCControl = new ReadEPCControl();
    m_ReadEPCControl_thread = new QThread;
    m_ReadEPCControl->moveToThread(m_ReadEPCControl_thread);
    m_ReadEPCControl_thread->start();
    Func_ReadEPCControl();

    //声明硬件控制模块
    m_EqptControl = new EqptControl();
    m_EqptControl_thread = new QThread;
    m_EqptControl->moveToThread(m_EqptControl_thread);
    m_EqptControl_thread->start();
    Func_EqptControl();

    //声明抓拍机控制模块
    m_CameraControl = new CameraControl();
    m_CameraControl_thread = new QThread;
    m_CameraControl->moveToThread(m_CameraControl_thread);
    m_CameraControl_thread->start();
    Func_CameraControl();

    //声明监控机控制模块
    m_HkVideoControl = new HkVideoControl();
    m_HkVideoControl_thread = new QThread;
    m_HkVideoControl->moveToThread(m_HkVideoControl_thread);
    m_HkVideoControl_thread->start();
    Func_HkVideoControl();
}

void Bridge::Func_ReadEPCControl()
{
    connect(this,&Bridge::SendToReadEPCControl_Connect,m_ReadEPCControl,&ReadEPCControl::setComm);
    connect(m_ReadEPCControl,&ReadEPCControl::SendRFIDStatus,this,[=](bool sta){
        if(sta){
            emit SendToWindowRFIDStatus(sta);
        }
    });
    connect(m_ReadEPCControl,&ReadEPCControl::SendToBridge,this,[=](QByteArray data){
        QString str;
        str = data.toHex();
        //qDebug()<< str;
        if(str.size() == 16){
            str = str.mid(8,4);
            //qDebug()<< str;
            bool ok;
            int hexnum = str.toInt(&ok,16); // 表示以16进制方式读取字符串
            emit SendToWindow(QString::number(hexnum));
        }
    });
    connect(this,SIGNAL(SendToReadEPCControl_Close()),m_ReadEPCControl,SIGNAL(SendToSelf_Close()));
}

void Bridge::Func_EqptControl()
{
    connect(this,&Bridge::SendToEqptControl_Connect,m_EqptControl,&EqptControl::setComm);
    connect(this,SIGNAL(SendToEqptControl_HTrunkUp()),m_EqptControl,SIGNAL(SendToSelf_HeadTrunkUp()));
    connect(this,SIGNAL(SendToEqptControl_HTrunkDown()),m_EqptControl,SIGNAL(SendToSelf_HeadTrunkDown()));
    connect(this,SIGNAL(SendToEqptControl_SnapByHand()),m_EqptControl,SIGNAL(SendToSelf_SnapByHand()));
}

void Bridge::Func_CameraControl()
{//抓拍机控制
    connect(this,&Bridge::SendToCameraControl_Connect,m_CameraControl,&CameraControl::OnClickedLogin);
    connect(m_CameraControl,&CameraControl::SendToBridge_LoginOk,this,[=](){
        qDebug()<<"抓拍机回复登陆成功信号";
    });
    connect(m_CameraControl,&CameraControl::SendToBridge_CarPic,this,
            [=](QString data1,QString data2,QString data3){
        if(data3.isNull()){
            //无车牌
            qDebug()<<"无车牌";
            //is_carPlnum = false;
        }
        if(!data3.isNull()){
            //有车牌
            qDebug()<<"有车牌";
            //is_carPlnum = true;
            qDebug()<<data3;
            // m_plateNum = data3.toStdString();
        }

    });
}

void Bridge::Func_HkVideoControl()
{
    connect(this,&Bridge::SendToHkVideoControl_Connect,m_HkVideoControl,&HkVideoControl::HkVideo_Login);    //启动监控线程
}
