#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();

    //车头图片转化
    m_PicToBase64Model_CarPic = new PicToBase64Model_CarPic();
    m_PicToBase64Model_CarPic_thread = new QThread;
    m_PicToBase64Model_CarPic->moveToThread(m_PicToBase64Model_CarPic_thread);
    m_PicToBase64Model_CarPic_thread->start();
    //车牌转换
    m_PicToBase64Model_CarPlnumPic = new PicToBase64Model_CarPlnumPic();
    m_PicToBase64Model_CarPlnumPic_thread = new QThread;
    m_PicToBase64Model_CarPlnumPic->moveToThread(m_PicToBase64Model_CarPlnumPic_thread);
    m_PicToBase64Model_CarPlnumPic_thread->start();
    Func_PicToBase64Model();
}

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();
            emit SendToPicToBase64Model_CarPic(data1);
            emit SendToPicToBase64Model_CarPlnumPic(data2);
        }

    });
}

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

void Bridge::Func_PicToBase64Model()
{
    ///发送转换信号
    connect(this,&Bridge::SendToPicToBase64Model_CarPic,m_PicToBase64Model_CarPic,&PicToBase64Model_CarPic::RecvFromBridge_picPath);
    connect(this,&Bridge::SendToPicToBase64Model_CarPlnumPic,m_PicToBase64Model_CarPlnumPic,&PicToBase64Model_CarPlnumPic::RecvFromBridge_picPath);
    ///接收转换结果
    connect(m_PicToBase64Model_CarPic,&PicToBase64Model_CarPic::SendToBridge_Base64data,this,[=](QByteArray data){
        if(!data.isEmpty()){
            qDebug()<<"车头转换成功"<<data.size();
        }
    });
    connect(m_PicToBase64Model_CarPlnumPic,&PicToBase64Model_CarPlnumPic::SendToBridge_Base64data,this,[=](QByteArray data){
        if(!data.isEmpty()){
            qDebug()<<"车牌转换成功"<<data.size();
        }
    });
}
