From fd8461350e18ba5f13cbffe10c2112d8c25de065 Mon Sep 17 00:00:00 2001
From: victor <taehoon@falinux.com>
Date: Tue, 18 Apr 2017 17:02:56 +0900
Subject: [PATCH] =?UTF-8?q?=EC=98=A4=EB=B8=90=20=EA=B8=B0=EB=8A=A5=20?=
 =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=88=98=EC=A0=95=20=20-=20?=
 =?UTF-8?q?=EB=8C=90=ED=8D=BC=20/=20=EA=B0=80=EC=8A=B5=20=EA=B8=B0?=
 =?UTF-8?q?=EB=8A=A5=20=EB=B0=98=EB=B3=B5=20=EA=B0=80=EB=8A=A5=ED=95=98?=
 =?UTF-8?q?=EA=B2=8C=20=EC=B6=94=EA=B0=80=20=20-=20=EC=A4=91=EC=8B=AC=20?=
 =?UTF-8?q?=EC=98=A8=EB=8F=84=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80?=
 =?UTF-8?q?=EC=82=AC=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20=20=20=20?=
 =?UTF-8?q?-=20=EC=A4=91=EC=8B=AC=20=EC=98=A8=EB=8F=84=EA=B0=80=20?=
 =?UTF-8?q?=ED=98=84=EC=9E=AC=20=EC=98=A8=EB=8F=84=EC=99=80=2020=EB=8F=84?=
 =?UTF-8?q?=20=EC=9D=B4=EC=83=81=203=EC=B4=88=20=EC=9D=B4=EC=83=81=20?=
 =?UTF-8?q?=EC=B0=A8=EC=9D=B4=EB=82=A0=20=EB=95=8C=20=EC=9C=A0=ED=9A=A8=20?=
 =?UTF-8?q?=20-=20=EC=8B=B1=EA=B8=80=ED=86=A4=20=EC=98=A4=EB=B8=8C?=
 =?UTF-8?q?=EC=A0=9D=ED=8A=B8=EB=A1=9C=20=EB=B3=80=EA=B2=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/gui/oven_control/mainwindow.cpp |  13 +-
 app/gui/oven_control/oven.cpp       | 304 +++++++++++++++++++++++++++++++++++-
 app/gui/oven_control/oven.h         |  53 ++++++-
 3 files changed, 355 insertions(+), 15 deletions(-)

diff --git a/app/gui/oven_control/mainwindow.cpp b/app/gui/oven_control/mainwindow.cpp
index 239723a..24d601b 100644
--- a/app/gui/oven_control/mainwindow.cpp
+++ b/app/gui/oven_control/mainwindow.cpp
@@ -21,7 +21,7 @@ MainWindow::MainWindow(QWidget *parent) :
     //for singletone event debug
     pcombiButton = ui->helpButton;
 
-    oven = new Oven(this);
+    oven = Oven::getInstance();
 
     OvenController *interface = new OvenController(this, oven);
     oven->setInterface(interface);
@@ -73,27 +73,31 @@ void MainWindow::on_configButton_clicked()
     FunctionTestWindow *w = new FunctionTestWindow(this, udp);
     w->setWindowModality(Qt::WindowModal);
     w->showFullScreen();
+    w->raise();
 }
 
 void MainWindow::on_poultryButton_clicked()
 {
-    AutoCookSelectionWindow *w = new AutoCookSelectionWindow(this, oven, Cook::Poultry);
+    AutoCookSelectionWindow *w = new AutoCookSelectionWindow(this, oven, Define::Poultry);
     w->setWindowModality(Qt::WindowModal);
     w->showFullScreen();
+    w->raise();
 }
 
 void MainWindow::on_meatButton_clicked()
 {
-    AutoCookSelectionWindow *w = new AutoCookSelectionWindow(this, oven, Cook::Meat);
+    AutoCookSelectionWindow *w = new AutoCookSelectionWindow(this, oven, Define::Meat);
     w->setWindowModality(Qt::WindowModal);
     w->showFullScreen();
+    w->raise();
 }
 
 void MainWindow::on_breadButton_clicked()
 {
-    AutoCookSelectionWindow *w = new AutoCookSelectionWindow(this, oven, Cook::Bread);
+    AutoCookSelectionWindow *w = new AutoCookSelectionWindow(this, oven, Define::Bread);
     w->setWindowModality(Qt::WindowModal);
     w->showFullScreen();
+    w->raise();
 }
 
 void MainWindow::on_washButton_clicked()
@@ -101,4 +105,5 @@ void MainWindow::on_washButton_clicked()
     WashWindow *w = new WashWindow(this, udp);
     w->setWindowModality(Qt::WindowModal);
     w->showFullScreen();
+    w->raise();
 }
diff --git a/app/gui/oven_control/oven.cpp b/app/gui/oven_control/oven.cpp
index 3b936fe..2daf051 100644
--- a/app/gui/oven_control/oven.cpp
+++ b/app/gui/oven_control/oven.cpp
@@ -1,8 +1,11 @@
 #include "oven.h"
 
 #include <QtDebug>
+#include <QTime>
 #include <cmath>
 
+Oven *Oven::instance = 0;
+
 Oven::Oven(QObject *parent) : QObject(parent)
 {
     interface = NULL;
@@ -12,13 +15,30 @@ Oven::Oven(QObject *parent) : QObject(parent)
     currentInterTemp_ = 0;
 
     cookingTimer.setSingleShot(true);
-    connect(&cookingTimer, SIGNAL(timeout()), this, SLOT(stopCooking()));
+    connect(&cookingTimer, SIGNAL(timeout()), SLOT(stopCooking()));
 
     damperTimer.setSingleShot(true);
-    connect(&damperTimer, SIGNAL(timeout()), this, SLOT(closeDamper()));
+    connect(&damperTimer, SIGNAL(timeout()), SLOT(closeDamper()));
 
     humidificationTimer.setSingleShot(true);
-    connect(&humidificationTimer, SIGNAL(timeout()), this, SLOT(stopHumidification()));
+    connect(&humidificationTimer, SIGNAL(timeout()), SLOT(stopHumidification()));
+
+    damperRepeatRuntimeTimer.setSingleShot(true);
+    connect(&damperRepeatRuntimeTimer, SIGNAL(timeout()), SLOT(idleRepeatDamper()));
+
+    damperRepeatDelayTimer.setSingleShot(true);
+    connect(&damperRepeatDelayTimer, SIGNAL(timeout()), SLOT(runRepeatDamper()));
+
+    humidificationRepeatRuntimeTimer.setSingleShot(true);
+    connect(&humidificationRepeatRuntimeTimer, SIGNAL(timeout()), SLOT(idleRepeatHumidification()));
+
+    humidificationRepeatDelayTimer.setSingleShot(true);
+    connect(&humidificationRepeatDelayTimer, SIGNAL(timeout()), SLOT(runRepeatHumidification()));
+
+    isInterTempValid_ = false;
+
+    humidificationRepeat = false;
+    damperRepeat = false;
 }
 
 void Oven::setInterface(OvenInterface *interface)
@@ -241,6 +261,11 @@ bool Oven::setFan_(int speed)
     return true;
 }
 
+bool Oven::isInterTempValid()
+{
+    return isInterTempValid_ && interTempValidTime.elapsed() > 3000;
+}
+
 bool Oven::cookingStartable()
 {
     if (/*door() || */cooking() || time() <= 0)
@@ -393,15 +418,22 @@ void Oven::stopCooldown()
     }
 }
 
-void Oven::startHumidification()
+void Oven::startHumidification(int secs)
 {
     if (humidificationStartable())
     {
-        humidification_ = true;
-        humidificationTimer.start(10 * 60 * 1000);
-        interface->startHumidification();
+        if (humidificationRepeat)
+        {
 
-        emit changed(this);
+        }
+        else
+        {
+            humidification_ = true;
+            humidificationTimer.start(secs * 1000);
+            interface->startHumidification();
+
+            emit changed(this);
+        }
     }
 }
 
@@ -419,6 +451,123 @@ void Oven::stopHumidification()
     }
 }
 
+void Oven::repeatHumidification(int runtime, int delay, int count)
+{
+    humidificationRepeatInfinitely = count == 0;
+    humidificationRepeatRuntime = runtime * 1000;
+    humidificationRepeatDelay = delay * 1000;
+    humidificationRepeatCount = count;
+
+    if (humidificationRepeat)
+    {
+        if (humidificationRepeatPaused)
+        {
+            if (humidificationRepeatPausedOnRuntime)
+                humidificationRepeatRemainingTime = humidificationRepeatRuntime;
+            else
+                humidificationRepeatRemainingTime = humidificationRepeatDelay;
+        }
+        else
+        {
+            runRepeatHumidification();
+        }
+    }
+    else
+    {
+        humidificationRepeat = true;
+
+        if (humidification())
+        {
+            humidificationRepeatPaused = true;
+            humidificationRepeatPausedOnRuntime = false;
+            humidificationRepeatRemainingTime = humidificationRepeatDelay;
+        }
+        else
+        {
+            humidificationRepeatPaused = false;
+            humidificationRepeatRemainingTime = 0;
+            runRepeatHumidification();
+        }
+    }
+}
+
+void Oven::stopRepeatHumidification()
+{
+    if (!humidificationRepeat)
+        return;
+
+    humidificationRepeat = false;
+
+    humidificationRepeatRuntimeTimer.stop();
+    humidificationRepeatDelayTimer.stop();
+
+    if (humidification())
+        stopHumidification();
+}
+
+void Oven::pauseRepeatHumidification()
+{
+    if (!humidificationRepeat)
+        return;
+
+    humidificationRepeatPaused = true;
+
+    if (humidificationRepeatRuntimeTimer.isActive())
+    {
+        humidificationRepeatPausedOnRuntime = true;
+        humidificationRepeatRemainingTime = humidificationRepeatRuntimeTimer.remainingTime();
+        humidificationRepeatRuntimeTimer.stop();
+    }
+    else
+    {
+        humidificationRepeatPausedOnRuntime = false;
+        humidificationRepeatRemainingTime = humidificationRepeatDelayTimer.remainingTime();
+        humidificationRepeatDelayTimer.stop();
+    }
+}
+
+void Oven::resumeRepeatHumidification()
+{
+    if (!humidificationRepeat || !humidificationRepeatPaused)
+        return;
+
+    humidificationRepeatPaused = false;
+
+    if (humidificationRepeatPausedOnRuntime)
+    {
+        humidificationRepeatRuntimeTimer.start(humidificationRepeatRemainingTime);
+
+        humidification_ = true;
+        interface->startHumidification();
+        emit changed(this);
+    }
+    else
+    {
+        humidificationRepeatDelayTimer.start(humidificationRepeatRemainingTime);
+    }
+}
+
+void Oven::runRepeatHumidification()
+{
+    if (humidificationRepeatInfinitely || humidificationRepeatCount-- > 0)
+    {
+        humidificationRepeatRuntimeTimer.start(humidificationRepeatRuntime);
+
+        humidification_ = true;
+        interface->startHumidification();
+        emit changed(this);
+    }
+}
+
+void Oven::idleRepeatHumidification()
+{
+    humidificationRepeatDelayTimer.start(humidificationRepeatDelay);
+
+    humidification_ = false;
+    interface->stopHumidification();
+    emit changed(this);
+}
+
 void Oven::openDamper(int secs)
 {
     if (!damper())
@@ -446,6 +595,123 @@ void Oven::closeDamper()
     }
 }
 
+void Oven::repeatDamper(int runtime, int delay, int count)
+{
+    damperRepeatInfinitely = count == 0;
+    damperRepeatRuntime = runtime * 1000;
+    damperRepeatDelay = delay * 1000;
+    damperRepeatCount = count;
+
+    if (damperRepeat)
+    {
+        if (damperRepeatPaused)
+        {
+            if (damperRepeatPausedOnRuntime)
+                damperRepeatRemainingTime = damperRepeatRuntime;
+            else
+                damperRepeatRemainingTime = damperRepeatDelay;
+        }
+        else
+        {
+            runRepeatDamper();
+        }
+    }
+    else
+    {
+        damperRepeat = true;
+
+        if (damper())
+        {
+            damperRepeatPaused = true;
+            damperRepeatPausedOnRuntime = false;
+            damperRepeatRemainingTime = damperRepeatDelay;
+        }
+        else
+        {
+            damperRepeatPaused = false;
+            damperRepeatRemainingTime = 0;
+            runRepeatDamper();
+        }
+    }
+}
+
+void Oven::stopRepeatDamper()
+{
+    if (!damperRepeat)
+        return;
+
+    damperRepeat = false;
+
+    damperRepeatRuntimeTimer.stop();
+    damperRepeatDelayTimer.stop();
+
+    if (damper())
+        closeDamper();
+}
+
+void Oven::pauseRepeatDamper()
+{
+    if (!damperRepeat)
+        return;
+
+    damperRepeatPaused = true;
+
+    if (damperRepeatRuntimeTimer.isActive())
+    {
+        damperRepeatPausedOnRuntime = true;
+        damperRepeatRemainingTime = damperRepeatRuntimeTimer.remainingTime();
+        damperRepeatRuntimeTimer.stop();
+    }
+    else
+    {
+        damperRepeatPausedOnRuntime = false;
+        damperRepeatRemainingTime = damperRepeatDelayTimer.remainingTime();
+        damperRepeatDelayTimer.stop();
+    }
+}
+
+void Oven::resumeRepeatDamper()
+{
+    if (!damperRepeat || !damperRepeatPaused)
+        return;
+
+    damperRepeatPaused = false;
+
+    if (damperRepeatPausedOnRuntime)
+    {
+        damperRepeatRuntimeTimer.start(damperRepeatRemainingTime);
+
+        damper_ = true;
+        interface->closeDamper();
+        emit changed(this);
+    }
+    else
+    {
+        damperRepeatDelayTimer.start(damperRepeatRemainingTime);
+    }
+}
+
+void Oven::runRepeatDamper()
+{
+    if (damperRepeatInfinitely || damperRepeatCount-- > 0)
+    {
+        damperRepeatRuntimeTimer.start(damperRepeatRuntime);
+
+        damper_ = true;
+        interface->openDamper();
+        emit changed(this);
+    }
+}
+
+void Oven::idleRepeatDamper()
+{
+    damperRepeatDelayTimer.start(damperRepeatDelay);
+
+    damper_ = false;
+    interface->closeDamper();
+    emit changed(this);
+}
+
 void Oven::setCurrentHumidity(int percentage)
 {
     if (currentHumidity() != percentage)
@@ -469,6 +735,21 @@ void Oven::setCurrentInterTemp(int celsius)
     if (currentInterTemp() != celsius)
     {
         currentInterTemp_ = celsius;
+
+        if (isInterTempValid_)
+        {
+            if (currentInterTemp_ == currentTemp_)
+                isInterTempValid_ = false;
+        }
+        else
+        {
+            if (currentInterTemp_ + 20 < currentTemp_)
+            {
+                isInterTempValid_ = true;
+                interTempValidTime = QTime::currentTime();
+            }
+        }
+
         emit changed(this);
     }
 }
@@ -556,6 +837,10 @@ void Oven::onDoorOpened()
     if (cooking())
     {
         stopCooking();
+
+        if (damperRepeat)
+            pauseRepeatDamper();
+
         openDamper(7);
     }
 }
@@ -571,4 +856,7 @@ void Oven::onDoorClosed()
 
     if (damper())
         closeDamper();
+
+    if (damperRepeat)
+        resumeRepeatDamper();
 }
diff --git a/app/gui/oven_control/oven.h b/app/gui/oven_control/oven.h
index ce1c10f..5cbe67f 100644
--- a/app/gui/oven_control/oven.h
+++ b/app/gui/oven_control/oven.h
@@ -3,6 +3,7 @@
 
 #include <QObject>
 #include <QTimer>
+#include <QTime>
 
 class OvenInterface : public QObject
 {
@@ -71,6 +72,30 @@ private:
     int currentTemp_;
     int currentInterTemp_;
 
+    bool isInterTempValid_;
+    QTime interTempValidTime;
+
+    bool damperRepeat;
+    bool damperRepeatInfinitely;
+    int damperRepeatRuntime;
+    int damperRepeatDelay;
+    int damperRepeatCount;
+    QTimer damperRepeatRuntimeTimer;
+    QTimer damperRepeatDelayTimer;
+    bool damperRepeatPaused;
+    bool damperRepeatPausedOnRuntime;
+    int damperRepeatRemainingTime;
+
+    bool humidificationRepeat;
+    bool humidificationRepeatInfinitely;
+    int humidificationRepeatRuntime;
+    int humidificationRepeatDelay;
+    int humidificationRepeatCount;
+    QTimer humidificationRepeatRuntimeTimer;
+    QTimer humidificationRepeatDelayTimer;
+    bool humidificationRepeatPaused;
+    bool humidificationRepeatPausedOnRuntime;
+    int humidificationRepeatRemainingTime;
 
     bool paused_;
 
@@ -81,9 +106,17 @@ private:
 
     OvenInterface *interface;
 
+    static Oven *instance;
+
 public:
     explicit Oven(QObject *parent = 0);
 
+    static Oven *getInstance() {
+        if (instance == 0)
+            instance = new Oven();
+
+        return instance;
+    }
 
     Mode mode() { return mode_; }
     int humidity() { return humidity_; }
@@ -104,6 +137,8 @@ public:
     int currentHumidity() { return currentHumidity_; }
     int currentInterTemp() { return currentInterTemp_; }
 
+    bool isInterTempValid();
+
     bool cookingStartable();
     bool preheatingStartable();
     bool cooldownStartable();
@@ -139,12 +174,22 @@ public slots:
     void stopPreheating();
     void startCooldown();
     void stopCooldown();
-    void startHumidification();
+    void startHumidification(int secs = 600);
     void stopHumidification();
 
     void openDamper(int secs = 300);
     void closeDamper();
 
+    void repeatHumidification(int runtime, int delay, int count = 0);
+    void stopRepeatHumidification();
+    void pauseRepeatHumidification();
+    void resumeRepeatHumidification();
+
+    void repeatDamper(int runtime, int delay, int count = 0);
+    void stopRepeatDamper();
+    void pauseRepeatDamper();
+    void resumeRepeatDamper();
+
     void setCurrentHumidity(int percentage);
     void setCurrentTemp(int celsius);
     void setCurrentInterTemp(int celsius);
@@ -159,12 +204,14 @@ private slots:
     bool setInterTemp_(int celsius);
     bool setFan_(int speed);
 
+    void runRepeatHumidification();
+    void idleRepeatHumidification();
 
+    void runRepeatDamper();
+    void idleRepeatDamper();
 
     void onDoorOpened();
     void onDoorClosed();
 };
 
-
-
 #endif // OVEN_H
-- 
2.1.4