Newer
Older
ZXSSCJ / DeviceHub / DeviceHubWindow.cpp
tan yue on 17 Nov 2021 6 KB 20211117 some form bug fixed
#include "DeviceHubWindow.h"
#include "ui_DeviceHubWindow.h"

#include <QTimer>
#include <QMessageBox>
#include <QDesktopWidget>
#include <QStandardItemModel>

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

    // 无边框
    this->setWindowFlags(Qt::FramelessWindowHint);

    // 窗口大小为占满一屏
    QRect screenRect = QApplication::desktop()->screenGeometry();
    resize(screenRect.width(), screenRect.height());

    // 将窗口移动到左上角
    move(0, 0);
    ui->exitButt->move(screenRect.width() - 80, 10);
    ui->minButt->move(screenRect.width() - 140, 10);
    ui->line->setGeometry(0, 59, screenRect.width(), 1);

    // 设置stackWidget的大小
    ui->stackedWidget->setGeometry(0, 60, screenRect.width(), screenRect.height() - 60);

    // add forms
    freqTunForm = new FrequencyTuningForm(this);
    ui->stackedWidget->addWidget(freqTunForm);

    signGenForm = new SignalGeneratorForm(this);
    ui->stackedWidget->addWidget(signGenForm);

    tmSwitForm = new TimeSwitcherForm(this);
    ui->stackedWidget->addWidget(tmSwitForm);

    freqSwitForm = new FreqSwitcherForm(this);
    ui->stackedWidget->addWidget(freqSwitForm);

    // kafka consumer
    kafkaConsumer = new QKafkaConsumer(this);
    kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS);
    kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC);
    kafkaConsumer->createConsumer();
    kafkaConsumer->start();

    // init device type and device list
    httpReq = new HttpRequestController(this);
    // 1. 获取访问接口需要的token
    int retCode = this->initHttpToken();
    if (retCode != 200)
    {
        QMessageBox::information(this, "错误", "获取http请求的token失败,程序即将退出");

        QTimer::singleShot(1000, qApp, SLOT(quit()));
    }
    // 2. 获取字典值:设备类型
    retCode = this->initDictDeviceTypes();
    this->initAllDeviceList();
}

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

QComboBox * DeviceHubWindow::getDevTypeSelect()
{
    return ui->devTypeSelect;
}
QComboBox * DeviceHubWindow::getDevSelect()
{
    return ui->devSelect;
}

void DeviceHubWindow::on_minButt_clicked()
{
    setWindowState(Qt::WindowMinimized | windowState());
}

void DeviceHubWindow::on_exitButt_clicked()
{
    QApplication::exit(0);
}

void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
    currentDevType = ui->devTypeSelect->currentData().toString();

    //
    QList<QJsonObject> typeDevList = ConstCache::getInstance().deviceList.value(currentDevType);
    ui->devSelect->clear();
    for (int var = 0; var < typeDevList.size(); ++var) {
        QJsonObject devItem = typeDevList.at(var);
        ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem);
    }

    // 5. 设置下拉框的样式
    QStandardItemModel * model = qobject_cast<QStandardItemModel*>(ui->devSelect->model());
    for (int i = 0; i < model->rowCount(); ++i)
    {
        QStandardItem * item = model->item(i);
        item->setSizeHint({ 0, 30 });
    }
}

void DeviceHubWindow::on_devSelect_currentIndexChanged(int index)
{
    currentDevIndex = index;
}

int DeviceHubWindow::initHttpToken()
{
    QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID,
                                                       SettingConfig::getInstance().APP_KEY);
    return response.find("code")->toInt();
}

int DeviceHubWindow::initDictDeviceTypes()
{
    QJsonObject response = httpReq->initDictDeviceType();

    if (response.find("code")->toInt() == 200)
    {
        ConstCache::getInstance().deviceTypes.clear();

        QJsonArray typeArray = response.find("data")->toArray();
        for (int i = 3; i < typeArray.size(); i++)
        {
            QJsonObject typeItem = typeArray.at(i).toObject();
            ui->devTypeSelect->addItem(typeItem.find("name")->toString(), typeItem.find("value")->toString());

            ConstCache::getInstance().deviceTypes.insert(typeItem.find("value")->toString(), typeItem.find("name")->toString());
        }
    }

    // 5. 设置下拉框的样式
    QStandardItemModel * model = qobject_cast<QStandardItemModel*>(ui->devTypeSelect->model());
    for (int i = 0; i < model->rowCount(); ++i)
    {
        QStandardItem * item = model->item(i);
        item->setSizeHint({ 0, 30 });
    }

    //
    ui->devTypeSelect->setCurrentIndex(model->rowCount() - 1);

    return response.find("code")->toInt();
}

void DeviceHubWindow::initAllDeviceList()
{
    QMapIterator<QString, QString> it(ConstCache::getInstance().deviceTypes);
    while (it.hasNext())
    {
        it.next();
        QString devType = it.key();
        QJsonObject resultObj = httpReq->initDeviceList(devType);
        if (resultObj.find("code")->toInt() == 200)
        {
            QList<QJsonObject> typeDevList;
            QJsonArray data = resultObj.find("data")->toArray();
            QList<DeviceBase *> singleTypeDevList;
            for (int i = 0; i < data.size(); i++)
            {
                QJsonObject item = data.at(i).toObject();
                typeDevList.append(item);
                qDebug() << item;

                DeviceBase * devicePtr = DeviceBase::deviceFactory(devType, this);
                std::cout << typeid (devicePtr).name() << std::endl;
                if (devicePtr != 0)
                {
                    devicePtr->setDevCode(item.find("deviceNo")->toString());
                    devicePtr->setDeviceId(item.find("deviceId")->toString());
                    devicePtr->setComName(item.find("linkComName")->toString());
                    devicePtr->setBaudRate(item.find("baudRate")->toString().toInt());

                    singleTypeDevList.append(devicePtr);

                    devicePtr->initSerialPort();
                }
            }
            this->allTypeDevList.insert(devType, singleTypeDevList);

            ConstCache::getInstance().deviceList.insert(devType, typeDevList);
        }
    }

    ui->devTypeSelect->setCurrentIndex(0);
}