multicookcontroller.cpp 4.12 KB
#include "multicookcontroller.h"

#include "oven.h"
#include "haccp.h"

MultiCookController::MultiCookController(QObject *parent) : QObject(parent)
{
    state = Idle;

    connect(&checkTimer, SIGNAL(timeout()), SLOT(check()));
}

void MultiCookController::setContainer(MultiCookContainer *container)
{
    this->container = container;
    checkTimer.start(100);
}

bool MultiCookController::requireOpen()
{
    if (state == OpenDoor || state == Finished)
        return true;

    if (state != Running)
        return false;

    for (int i = 0; i < 10; i++)
    {
        MultiCook *cook = container->at(i);
        if (cook != Q_NULLPTR && cook->remainingTime() <= 0)
            return true;
    }

    return false;
}

bool MultiCookController::requireClose()
{
    if (state == Idle || state == OpenDoor)
        return false;

    return true;
}

void MultiCookController::check()
{
    switch (state)
    {
    case Idle:
        if (!container->isEmpty())
        {
            state = Preheating;

            HACCP::multiStart();
        }
        break;
    case Preheating:
        checkPreheating();
        break;
    case OpenDoor:
        if (Oven::getInstance()->door())
            state = CloseDoor;
        break;
    case CloseDoor:
        if (!Oven::getInstance()->door())
        {
            state = Running;
            container->start();
        }
        break;
    case Running:
        checkRunning();
        break;
    case Paused:
        checkPaused();
        break;
    case Finished:
        checkFinished();
        break;
    }
}

void MultiCookController::checkPreheating()
{
    Oven *oven = Oven::getInstance();
    if (container->isEmpty())
    {
        state = Idle;
        HACCP::done();
        oven->stop();
        return;
    }

    int temp = container->temperature();
    if (temp <= oven->currentTemp())
    {
        state = OpenDoor;
        oven->stop();
        return;
    }

    if (temp != oven->temp())
        oven->setTemp(temp);

    int hum = container->humidity();
    if (hum != oven->humidity())
        oven->setHumidity(hum);

    if (!oven->preheating())
        oven->startPreheating();
}

void MultiCookController::checkRunning()
{
    Oven *oven = Oven::getInstance();
    if (container->isEmpty())
    {
        state = Idle;
        HACCP::done();
        oven->stop();
        return;
    }

    if (container->isFinished())
    {
        state = Finished;
        HACCP::done();
        oven->stop();
        container->stop();
        return;
    }

    if (oven->door())
    {
        state = Paused;
        oven->stopCooking();
        container->pause();
        return;
    }

//    Define::Mode mode = container->mode();
//    switch (mode)
//    {
//    case Define::Invalid:
//        // Do nothing
//        break;
//    case Define::SteamMode:
//    case Define::CombiMode:
//    case Define::DryMode:
//        oven->setMode(mode);
//        break;
//    }

//    int temp = container->temperature();
//    if (temp != oven->temp())
//        oven->setTemp(temp);

//    int hum = container->humidity();
//    if (hum != oven->humidity())
//        oven->setHumidity(hum);

    oven->setMode(container->mode());
    oven->setTemp(container->temperature());
    oven->setHumidity(container->humidity());

    if (!oven->cooking())
    {
        oven->setTime(12 * 60 * 60);
        oven->startCooking();
    }
}

void MultiCookController::checkPaused()
{
    Oven *oven = Oven::getInstance();
    if (oven->door())
        return;

    for (int i = 0; i < 10; i++)
    {
        MultiCook *cook = container->at(i);
        if (cook != Q_NULLPTR && cook->remainingTime() <= 0)
        {
            container->remove(i);
            cook->deleteLater();
        }
    }

    state = Running;
    container->resume();
}

void MultiCookController::checkFinished()
{
    Oven *oven = Oven::getInstance();
    if (oven->door())
    {
        state = Idle;
        HACCP::done();

        for (int i = 0; i < 10; i++)
        {
            MultiCook *cook = container->at(i);
            if (cook != Q_NULLPTR)
            {
                container->remove(i);
                cook->deleteLater();
            }
        }
    }
}