#include "FreqReplicatorForm.h"
#include "ui_FreqReplicatorForm.h"
#include "DeviceHubWindow.h"

FreqReplicatorForm::FreqReplicatorForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FreqReplicatorForm)
{
    ui->setupUi(this);
}

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

void FreqReplicatorForm::on_freqRepButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    FreqReplicator * device = (FreqReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("10").at(devIndex);

    // mock测试工作模式则产生一条mock数据
    if (SettingConfig::getInstance().WORK_MODE == "mock")
    {
        device->mockReceivData();
    }
}

void FreqReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData)
{
    QString currentDevCode = ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->currentData().toJsonObject().find("deviceNo")->toString();
    if (currentDevCode != frameData->devCode)
    {
        return;
    }

    // 当前显示的设备编号
    if (frameData->frameType == "1001")
    {
        FreqReplicatorStatusDto * statusFrameDto = (FreqReplicatorStatusDto *) frameData;
        ui->frABStatus->setText(statusFrameDto->abOrCd);
        ui->label_ts->setText(statusFrameDto->timestamp);

        QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject();
        QJsonDocument doc;

        doc.setArray(dataObj.find("inValid")->toArray());
        ui->frInStatus->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));

        doc.setArray(dataObj.find("outValid")->toArray());
        ui->frOutStatus->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));
    } else if (frameData->frameType == "1002")
    {
        FreqReplicatorStatusDto * statusFrameDto = (FreqReplicatorStatusDto *) frameData;
        ui->frCDStatus->setText(statusFrameDto->abOrCd);
        QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject();
        QJsonDocument doc;

        doc.setArray(dataObj.find("inValid")->toArray());
        ui->frInStatusCD->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));

        doc.setArray(dataObj.find("outValid")->toArray());
        ui->frOutStatusCD->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));
    }
}
