burnertestwindow.cpp 4.67 KB
#include "burnertestwindow.h"
#include "ui_burnertestwindow.h"

#include <QTimer>
#include <QKeyEvent>

#include "soundplayer.h"

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

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

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

    udp = UdpHandler::getInstance();

    steamTimer.setInterval(5 * 60 * 1000);
    upperTimer.setInterval(5 * 60 * 1000);
    lowerTimer.setInterval(5 * 60 * 1000);

    connect(&steamTimer, SIGNAL(timeout()), this, SLOT(steamOff()));
    connect(&upperTimer, SIGNAL(timeout()), this, SLOT(upperOff()));
    connect(&lowerTimer, SIGNAL(timeout()), this, SLOT(lowerOff()));

    connect(this->udp, SIGNAL(changed()), this, SLOT(onDataChanged()));

    udp->set(TG_OVEN_MODE, 4);
    udp->turnOn(TG_MANUAL_BURNER1);
    udp->turnOn(TG_MANUAL_BURNER2);
    udp->turnOn(TG_MANUAL_BURNER3);

    onDataChanged();
}

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

void BurnerTestWindow::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 BurnerTestWindow::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 BurnerTestWindow::onDataChanged()
{
    oven_state_t oven;
    udp->fillData(oven);

    int steam = oven.onoff_state1 & 0x0004;
    int upper = oven.onoff_state1 & 0x0001;
    int lower = oven.onoff_state1 & 0x0002;

    if (steam)
        ui->steamBurnerButton->setText("STOP");
    else
        ui->steamBurnerButton->setText("START");

    if (upper)
        ui->upperBurnerButton->setText("STOP");
    else
        ui->upperBurnerButton->setText("START");

    if (lower)
        ui->lowerBurnerButton->setText("STOP");
    else
        ui->lowerBurnerButton->setText("START");

    QString buf;
    ui->t1->setText(buf.sprintf("%d℃", oven.sensor1));
    ui->t5->setText(buf.sprintf("%d℃", oven.sensor5));

    if (steam && oven.curr_humidity >= 100)
    {
        steamTimer.stop();
        steamOff();
    }

    if ((upper || lower) && oven.curr_heat >= 150)
    {
        upperTimer.stop();
        lowerTimer.stop();
        upperOff();
        lowerOff();
    }
}

void BurnerTestWindow::on_steamBurnerButton_clicked()
{
    oven_state_t oven;
    udp->fillData(oven);

    int sw = oven.onoff_state1 & 0x0004;
    if (sw)
    {
        steamTimer.stop();
        steamOff();
    }
    else
    {
        steamTimer.start();
        steamOn();
    }
}

void BurnerTestWindow::on_upperBurnerButton_clicked()
{
    oven_state_t oven;
    udp->fillData(oven);

    int sw = oven.onoff_state1 & 0x0001;
    if (sw)
    {
        upperTimer.stop();
        upperOff();
    }
    else
    {
        upperTimer.start();
        upperOn();
    }
}

void BurnerTestWindow::on_lowerBurnerButton_clicked()
{
    oven_state_t oven;
    udp->fillData(oven);

    int sw = oven.onoff_state1 & 0x0002;
    if (sw)
    {
        lowerTimer.stop();
        lowerOff();
    }
    else
    {
        lowerTimer.start();
        lowerOn();
    }
}

void BurnerTestWindow::on_backButton_clicked()
{
    udp->turnOff(TG_BUNNER1_MANUAL);
    udp->turnOff(TG_BUNNER2_MANUAL);
    udp->turnOff(TG_BUNNER3_MANUAL);
    udp->turnOff(TG_MANUAL_BURNER1);
    udp->turnOff(TG_MANUAL_BURNER2);
    udp->turnOff(TG_MANUAL_BURNER3);

    close();
}

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

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

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

void BurnerTestWindow::steamOn()
{
    udp->turnOn(TG_BUNNER3_MANUAL);
}

void BurnerTestWindow::steamOff()
{
    udp->turnOff(TG_BUNNER3_MANUAL);
}

void BurnerTestWindow::upperOn()
{
    udp->turnOn(TG_BUNNER1_MANUAL);
}

void BurnerTestWindow::upperOff()
{
    udp->turnOff(TG_BUNNER1_MANUAL);
}

void BurnerTestWindow::lowerOn()
{
    udp->turnOn(TG_BUNNER2_MANUAL);
}

void BurnerTestWindow::lowerOff()
{
    udp->turnOff(TG_BUNNER2_MANUAL);
}