multicookcontainer.cpp 2.93 KB
#include "multicookcontainer.h"

MultiCookContainer::MultiCookContainer(QObject *parent) : QObject(parent)
{
    for (int i = 0; i < 10; i++)
        list.append(Q_NULLPTR);

    isEmpty_ = true;
    state = Idle;
}

void MultiCookContainer::add(int slot, MultiCook *cook)
{
    list.replace(slot, cook);
    if (isEmpty_)
    {
        mode_ = cook->mode();
        isEmpty_ = false;
    }

    switch (state)
    {
    case Idle:
        // Do nothing
        break;
    case Running:
        cook->start();
        break;
    case Paused:
        cook->start();
        cook->pause();
        break;
    }
}

void MultiCookContainer::remove(int slot)
{
    MultiCook *cook = list.at(slot);
    if (cook == Q_NULLPTR)
        return;

    cook->stop();

    list.replace(slot, Q_NULLPTR);

    isEmpty_ = true;
    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR)
        {
            isEmpty_ = false;
            break;
        }

    if (isEmpty_)
    {
        mode_ = Define::InvalidMode;
        state = Idle;
    }
}

MultiCook *MultiCookContainer::at(int slot)
{
    return list.at(slot);
}

bool MultiCookContainer::isEmpty()
{
    return isEmpty_;
}

bool MultiCookContainer::isFinished()
{
    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR && c->remainingTime() > 0)
            return false;

    return true;
}

Define::Mode MultiCookContainer::mode()
{
    return mode_;
}

int MultiCookContainer::temperature()
{
    int count = 0;
    int temp = 0;
    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR && c->remainingTime() > 0)
        {
            temp += c->temperature();
            count++;
        }

    if (count == 0)
        return -1;

    return temp / count;
}

int MultiCookContainer::humidity()
{
    int count = 0;
    int hum = 0;
    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR && c->remainingTime() > 0)
        {
            hum += c->humidity();
            count++;
        }

    if (count == 0)
        return -1;

    return hum / count;
}

int MultiCookContainer::remainingTime()
{
    int max = 0;
    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR)
            max = qMax(max, c->remainingTime());

    return max;
}

void MultiCookContainer::start()
{
    if (isEmpty_ || state != Idle)
        return;

    state = Running;

    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR)
            c->start();
}

void MultiCookContainer::stop()
{
    if (isEmpty_ || state != Running)
        return;

    state = Idle;

    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR)
            c->stop();
}

void MultiCookContainer::pause()
{
    if (isEmpty_ || state != Running)
        return;

    state = Paused;

    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR)
            c->pause();
}

void MultiCookContainer::resume()
{
    if (isEmpty_ || state != Paused)
        return;

    state = Running;

    foreach (MultiCook *c, list)
        if (c != Q_NULLPTR)
            c->resume();
}