#include "stable.h"
#include "cmwcompother.h"
#include "cmwidget.h"
#include "CoatVolumeSensor.h"
CMWCompOther::CMWCompOther(QWidget *parent)
	: CMWidget(parent),
	ui(new Ui::CMWCompOther)
{
	ui->setupUi(this);
}

CMWCompOther::~CMWCompOther()
{
	delete ui;
}

void CMWCompOther::MTimeFresh()
{
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Driver_ID,
                            QObject::trUtf8("Measure volume"),
							QVariantList()
							<<COATVOLUME_SENSOR<<CCoatVolumeSensor::MeasureVolume
							<<0,0);
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Fresh pos"),
                          QVariantList()
                          << HGRIPPER_DRIVER << CDHGripperRS485::kFreshPos
                          << 0, 0);
	m_gSystem.start_test(SM_MAIN, msgqueue,false);

	CCoatVolumeSensor* pVolumeSensor=GET_CARD(COATVOLUME_SENSOR,CCoatVolumeSensor);
	ui->lblWeight->setText(QString::number(pVolumeSensor->m_dGrams, 'f', 0));

    CDriverCard* pGripper=GET_CARD(HGRIPPER_DRIVER, CDriverCard);
    ui->lblCurPos->setText(QString::number(pGripper->m_Variants.get_VData(Var_Motor_Pos_ID)->toInt()));
    ui->lblCurSpeed->setText(QString::number(pGripper->m_Variants.get_VData(Var_Motor_Default_speed_ID)->toInt()));

	CMeasHead* pMeas = GET_CHANNEL(DistanceSensor, DistanceSensor_Driver, CMeasHead);
	ui->lblDistance->setText(QString::number(pMeas->m_Variants.get_VData(Var_MeasHead_Result_ID)->toDouble()));
    
    CDriverCard* pDistanceSensor = GET_CARD(DistanceSensor_Driver, CDriverCard);
    ui->lblCalcThickness->setText(QString::number(pDistanceSensor->m_Variants.get_VData(Var_Distance_Measure_Thickness)->toDouble()));
}

void CMWCompOther::on_btnReadWeight_clicked()
{
    CCoatVolumeSensor* pVolumeSensor=GET_CARD(COATVOLUME_SENSOR,CCoatVolumeSensor);
    ui->lblWeight->setText(QString::number(pVolumeSensor->m_dGrams, 'f', 0));
}

void CMWCompOther::on_btnReadDistance_clicked()
{
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Driver_ID,
		QObject::trUtf8("Measure distance"),
		QVariantList()
		<< DistanceSensor_Driver << CMeasHeadDriver::FRESHRESULT
		<< DistanceSensor, 0);
	m_gSystem.start_test(SM_MAIN, msgqueue, false);
}

void CMWCompOther::MInit()
{
    CCoatVolumeSensor* pVolumeSensor=GET_CARD(COATVOLUME_SENSOR,CCoatVolumeSensor);
    Variant* pparam[7]={0};

	//皮重
	pparam[0] = pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_TareValue);
	//密度
	pparam[1]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_GlueDensity);

	pparam[2]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_AlarmDiffVolume);
	pparam[3]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_AlarmVolume);
	pparam[4]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_WarningVolume);
	pparam[5]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_ReplaceDate);
	pparam[6]= pVolumeSensor->m_Variants.get_Variant(Var_CoatVolume_InvalidDays);

	
    ui->widget->MParamInit_list(pparam,7);

    mchildwidgets[(uintptr_t)ui->widget]=ui->widget;
}

void CMWCompOther::on_btnReadBar_clicked()
{
    CBarcodeDriverInfoScan* pDriver = GET_CARD(BARCODE_DRIVER, CBarcodeDriverInfoScan);
	try{
		pDriver->GetBarCode(BARCODE);
		ui->txtBarcode->setText(pDriver->GetBarCode_String(BARCODE));
	}catch(const QString& ex)
	{
        m_gSystem.m_SysMsg.SetMsg(QString(ex), WARN_MSG_TYPE);
	}
}

void CMWCompOther::on_btnZero_clicked()
{
    V_MsgQueue msgqueue;

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                            QObject::trUtf8("Zero volume sensor"),
                            QVariantList()
                            <<COATVOLUME_SENSOR<<CCoatVolumeSensor::MSG_ZeroSensor
                            <<0,0);
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                            QObject::trUtf8("Measure volume"),
                            QVariantList()
                            <<COATVOLUME_SENSOR<<CCoatVolumeSensor::MeasureVolume
                            <<0,0);

    m_gSystem.start_test(SM_MAIN, msgqueue,false);

    QTimer::singleShot(300, [this](){
            this->ui->btnReadWeight->click();
       });
}

void CMWCompOther::on_btnSetValue_clicked()
{
    int iPos = ui->txtGripperPos->text().toInt();
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Move Gripper Pos"),
                          QVariantList()
                          << HGRIPPER_DRIVER << CDHGripperRS485::MOVEPOS
                          << iPos, 0);

    m_gSystem.start_test(SM_MAIN, msgqueue, false);
}

void CMWCompOther::on_btnSetSpeed_clicked()
{
    int iSpeed = ui->txtGripperSpeed->text().toInt();
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Set Speed"),
                          QVariantList()
                          << HGRIPPER_DRIVER << CDHGripperRS485::kSetSpeed
                          << iSpeed, 0);

    m_gSystem.start_test(SM_MAIN, msgqueue, false);
}

void CMWCompOther::on_btnGripperReset_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Reset Gripper"),
                          QVariantList()
                          << HGRIPPER_DRIVER << CDHGripperRS485::kReset, 0);

    m_gSystem.start_test(SM_MAIN, msgqueue, false);
}
