gastestwindow.cpp 7.61 KB
#include "gastestwindow.h"
#include "ui_gastestwindow.h"

#include <QKeyEvent>
#include <QDebug>

#include "soundplayer.h"
#include "config.h"

GasTestWindow::GasTestWindow(Target target, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GasTestWindow), target(target)
{
    ui->setupUi(this);

    ui->clockContainer->setParent(ui->upperStack);
    setAttribute(Qt::WA_DeleteOnClose);

    switch (target)
    {
    case Upper:
        ui->titleLabel->setText(tr("서비스단계(엔지니어모드) > 기능테스트 > 연소가스동작 > 건열송풍기(상)"));
        break;
    case Lower:
        ui->titleLabel->setText(tr("서비스단계(엔지니어모드) > 기능테스트 > 연소가스동작 > 건열송풍기(하)"));
        break;
    case Steam:
        ui->titleLabel->setText(tr("서비스단계(엔지니어모드) > 기능테스트 > 연소가스동작 > 스팀용송풍기"));
        break;
    }

    udp = UdpHandler::getInstance();
    udp->set(TG_OVEN_MODE, 4);
    connect(udp, SIGNAL(changed()), this, SLOT(onDataChanged()));

    stopTimer.setInterval(120 * 1000);
    connect(&stopTimer, SIGNAL(timeout()), SLOT(stop()));

    foreach (QPushButton *button, findChildren<QPushButton *>())
        connect(button, &QPushButton::pressed, SoundPlayer::playClick);
}

GasTestWindow::~GasTestWindow()
{
    stop();

    delete ui;
}

void GasTestWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        pushed = focusWidget();
        break;
    case 0x01000030:    // Turn right
        onEncoderRight();
        break;
    }
}

void GasTestWindow::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        if (focusWidget() == pushed)
            onEncoderClicked(pushed);

        pushed = NULL;
        break;
    case 0x01000030:    // Turn right
        onEncoderRight();
        break;
    }
}

void GasTestWindow::updateView()
{
    ui->rpmLabel->setText(QString("%1 rpm").arg(currentRpm()));

    switch (phase)
    {
    case Min:
        ui->minButton->setText(isRunning() ? "STOP" : "START");
        ui->maxButton->setText("START");
        break;
    case Max:
        ui->minButton->setText("START");
        ui->maxButton->setText(isRunning() ? "STOP" : "START");
        break;
    default:
        ui->minButton->setText("START");
        ui->maxButton->setText("START");
    }
}

void GasTestWindow::onDataChanged()
{
    switch (phase)
    {
    case MinStopping:
    case MaxStopping:
        if (!isRunning())
            phase = Idle;
    default:
        break;
    }

    updateView();
}

void GasTestWindow::startMin()
{
    switch (target)
    {
    case Upper:
        runUpper(minRpm());
        break;
    case Lower:
        runLower(minRpm());
        break;
    case Steam:
        runSteam(minRpm());
        break;
    }

    stopTimer.start();

    phase = Min;
}

void GasTestWindow::startMax()
{
    switch (target)
    {
    case Upper:
        runUpper(maxRpm());
        break;
    case Lower:
        runLower(maxRpm());
        break;
    case Steam:
        runSteam(maxRpm());
        break;
    }

    stopTimer.start();

    phase = Max;
}

void GasTestWindow::stop()
{
    if (stopTimer.isActive())
        stopTimer.stop();

    switch (target)
    {
    case Upper:
        stopUpper();
        break;
    case Lower:
        stopLower();
        break;
    case Steam:
        stopSteam();
        break;
    }

    switch (phase)
    {
    case Min:
        phase = MinStopping;
        break;
    case Max:
        phase = MaxStopping;
        break;
    default:
        break;
    }
}

void GasTestWindow::runUpper(int rpm)
{
    udp->set(TG_BUNNER1_RPM, rpm);
    udp->turnOn(TG_BUNNER1_MANUAL);
    udp->turnOn(TG_BUNNER1_FAN);
    udp->turnOn(TG_MANUAL_BURNER1);

    udp->set(TG_FAN1_DIRECTOIN, 0);
    udp->set(TG_FAN1_RPM, 1175);
    udp->turnOn(TG_FAN1_MANUAL);
    udp->turnOn(TG_MANUAL_FAN1);
}

void GasTestWindow::runLower(int rpm)
{
    udp->set(TG_BUNNER2_RPM, rpm);
    udp->turnOn(TG_BUNNER2_MANUAL);
    udp->turnOn(TG_BUNNER2_FAN);
    udp->turnOn(TG_MANUAL_BURNER2);

    udp->set(TG_FAN2_DIRECTOIN, 0);
    udp->set(TG_FAN2_RPM, 1175);
    udp->turnOn(TG_FAN2_MANUAL);
    udp->turnOn(TG_MANUAL_FAN2);
}

void GasTestWindow::runSteam(int rpm)
{
    udp->set(TG_BUNNER3_RPM, rpm);
    udp->turnOn(TG_BUNNER3_MANUAL);
    udp->turnOn(TG_BUNNER3_FAN);
    udp->turnOn(TG_MANUAL_BURNER3);
}

void GasTestWindow::stopUpper()
{
    udp->turnOff(TG_MANUAL_BURNER1);
    udp->turnOff(TG_BUNNER1_MANUAL);
    udp->turnOff(TG_BUNNER1_FAN);

    udp->turnOff(TG_MANUAL_FAN1);
    udp->turnOff(TG_FAN1_MANUAL);
}

void GasTestWindow::stopLower()
{
    udp->turnOff(TG_MANUAL_BURNER2);
    udp->turnOff(TG_BUNNER2_MANUAL);
    udp->turnOff(TG_BUNNER2_FAN);

    udp->turnOff(TG_MANUAL_FAN2);
    udp->turnOff(TG_FAN2_MANUAL);
}

void GasTestWindow::stopSteam()
{
    udp->turnOff(TG_MANUAL_BURNER3);
    udp->turnOff(TG_BUNNER3_MANUAL);
    udp->turnOff(TG_BUNNER3_FAN);
}

int GasTestWindow::minRpm()
{
    Config *config = Config::getInstance();

    switch (target)
    {
    case Upper:
    case Lower:
        if (isHalf())
            return config->getConfigValue(Define::config_burner23_pwr1_half_rpm).d32;
        else
            return config->getConfigValue(Define::config_burner23_pwr1_normal_rpm).d32;
    case Steam:
    default:
        if (isHalf())
            return config->getConfigValue(Define::config_burner1_pwr1_half_rpm).d32;
        else
            return config->getConfigValue(Define::config_burner1_pwr1_normal_rpm).d32;
    }
}

int GasTestWindow::maxRpm()
{
    Config *config = Config::getInstance();

    switch (target)
    {
    case Upper:
    case Lower:
        if (isHalf())
            return config->getConfigValue(Define::config_burner23_pwr2_half_rpm).d32;
        else
            return config->getConfigValue(Define::config_burner23_pwr2_normal_rpm).d32;
    case Steam:
    default:
        if (isHalf())
            return config->getConfigValue(Define::config_burner1_pwr2_half_rpm).d32;
        else
            return config->getConfigValue(Define::config_burner1_pwr2_normal_rpm).d32;
    }
}

bool GasTestWindow::isHalf()
{
    return Config::getInstance()->getConfigValue(Define::config_set_half_energy).d32 > 0;
}

bool GasTestWindow::isRunning()
{
    switch (target)
    {
    case Upper:
        return udp->burner1() || udp->burnerFan1();
    case Lower:
        return udp->burner2() || udp->burnerFan2();
    case Steam:
        return udp->burner3() || udp->burnerFan3();
    default:
        return false;
    }
}

int GasTestWindow::currentRpm()
{
    switch (target)
    {
    case Upper:
        return udp->getData().burner1_current_rpm;
    case Lower:
        return udp->getData().burner2_current_rpm;
    case Steam:
        return udp->getData().burner3_current_rpm;
    default:
        return 0;
    }
}

void GasTestWindow::on_minButton_clicked()
{
    if (isRunning() && (phase == Min || phase == MinStopping))
        stop();
    else
        startMin();
}

void GasTestWindow::on_maxButton_clicked()
{
    if (isRunning() && (phase == Max || phase == MaxStopping))
        stop();
    else
        startMax();
}

void GasTestWindow::on_backButton_clicked()
{
    close();
}

void GasTestWindow::onEncoderLeft()
{
    focusPreviousChild();
}

void GasTestWindow::onEncoderRight()
{
    focusNextChild();
}

void GasTestWindow::onEncoderClicked(QWidget *clicked)
{
    QPushButton *b = qobject_cast<QPushButton *>(clicked);
    if (b)
        b->click();
}