From 7edd4f6b48993956f6206b0e325feb3c874de7e8 Mon Sep 17 00:00:00 2001
From: Siwat Sirichai <siwat@siwatinc.com>
Date: Sat, 22 Jun 2024 15:42:42 +0700
Subject: [PATCH] gym lock initial implementation

---
 .gitignore     |  1 +
 src/config.hpp |  5 ++++
 src/main.cpp   | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.hpp   |  6 ++++-
 4 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 89cc49c..fb3ca12 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@
 .vscode/c_cpp_properties.json
 .vscode/launch.json
 .vscode/ipch
+.vscode/settings.json
diff --git a/src/config.hpp b/src/config.hpp
index ff8f991..9c86da7 100644
--- a/src/config.hpp
+++ b/src/config.hpp
@@ -29,6 +29,11 @@
 #define GIRLS_TOILET_LIGHT_RELAY_PIN 6
 #define BOYS_TOILET_LIGHT_RELAY_PIN 7
 
+/***********************************************
+ *              IoT Configuration              *
+ ***********************************************/
+#define TOPIC_TOILET_LIGHT_LOCK "/toiletlight/lock"
+#define TOPIC_GYM_LIGHT_LOCK "/gymlight/lock"
 
 /***********************************************
  *            Display Configuration            *
diff --git a/src/main.cpp b/src/main.cpp
index 6ff7173..37f89ca 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -8,6 +8,8 @@ Button rightOnButton(&espmega.inputs, RIGHT_ON_BUTTON_PIN);
 Button rightOffButton(&espmega.inputs, RIGHT_OFF_BUTTON_PIN);
 Button leftOnButton(&espmega.inputs, LEFT_ON_BUTTON_PIN);
 Button leftOffButton(&espmega.inputs, LEFT_OFF_BUTTON_PIN);
+SmartVariable toiletLightsLock;
+SmartVariable gymLightsLock;
 
 /************************************************
  *            End of Global Variables           *
@@ -66,9 +68,38 @@ void setup()
   espmega.iot->registerMqttCallback(handle_mqtt_message);
   internalDisplayOTA.begin("/intdisp", espmega.display, espmega.webServer);
   button_init();
+  smartvar_init();
   ESP_LOGI("CUD IoT OS", "Initialization Complete");
 }
 
+void smartvar_init() {
+  // Initialize Smart Variables
+  ESP_LOGV("CUD IoT OS", "Initializing Smart Variables");
+  toiletLightsLock.begin(5);
+  // Construct the topic
+  char* toilet_lock_topic = (char*)malloc(strlen(TOPIC_TOILET_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 5);
+  sprintf(toilet_lock_topic, "%s%s", espmega.iot->getMqttConfig()->base_topic, TOPIC_TOILET_LIGHT_LOCK);
+  char* toilet_lock_set_topic = (char*)malloc(strlen(TOPIC_TOILET_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 15);
+  sprintf(toilet_lock_set_topic, "%s%s/set", espmega.iot->getMqttConfig()->base_topic, TOPIC_TOILET_LIGHT_LOCK);
+  char* toilet_lock_request_topic = (char*)malloc(strlen(TOPIC_TOILET_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 15);
+  sprintf(toilet_lock_request_topic, "%s%s/request", espmega.iot->getMqttConfig()->base_topic, TOPIC_TOILET_LIGHT_LOCK);
+  toiletLightsLock.enableIoT(espmega.iot, toilet_lock_topic);
+  toiletLightsLock.enableSetValue(toilet_lock_set_topic);
+  toiletLightsLock.enableValueRequest(toilet_lock_request_topic);
+
+  gymLightsLock.begin(5);
+  // Construct the topic
+  char* gym_lock_topic = (char*)malloc(strlen(TOPIC_GYM_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 5);
+  sprintf(gym_lock_topic, "%s%s", espmega.iot->getMqttConfig()->base_topic, TOPIC_GYM_LIGHT_LOCK);
+  char* gym_lock_set_topic = (char*)malloc(strlen(TOPIC_GYM_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 15);
+  sprintf(gym_lock_set_topic, "%s%s/set", espmega.iot->getMqttConfig()->base_topic, TOPIC_GYM_LIGHT_LOCK);
+  char* gym_lock_request_topic = (char*)malloc(strlen(TOPIC_GYM_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 15);
+  sprintf(gym_lock_request_topic, "%s%s/request", espmega.iot->getMqttConfig()->base_topic, TOPIC_GYM_LIGHT_LOCK);
+  gymLightsLock.enableIoT(espmega.iot, gym_lock_topic);
+  gymLightsLock.enableSetValue(gym_lock_set_topic);
+  gymLightsLock.enableValueRequest(gym_lock_request_topic);
+}
+
 void button_init()
 {
   // Initialize buttons
@@ -78,10 +109,14 @@ void button_init()
   rightOnButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS);
   rightOnButton.setShortPressCallback([]()
                                       { 
+                                        if (get_gym_light_lock())
+                                          return;
                                         // Turn on right light
                                         espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH); });
   rightOnButton.setLongPressCallback([]()
                                      { 
+                                      if (get_gym_light_lock())
+                                        return;
                                         // Turn on both lights
                                         espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH);
                                         espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); });
@@ -92,10 +127,14 @@ void button_init()
   rightOffButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS);
   rightOffButton.setShortPressCallback([]()
                                        { 
+                                        if (get_gym_light_lock())
+                                          return;
                                         // Turn off right light
                                         espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, LOW); });
   rightOffButton.setLongPressCallback([]()
                                       { 
+                                        if (get_gym_light_lock())
+                                          return;
                                         // Turn off both lights
                                         espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, LOW);
                                         espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, LOW); });
@@ -106,10 +145,14 @@ void button_init()
   leftOnButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS);
   leftOnButton.setShortPressCallback([]()
                                      { 
+                                      if (get_gym_light_lock())
+                                        return;
                                         // Turn on left light
                                         espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); });
   leftOnButton.setLongPressCallback([]()
                                     { 
+                                      if (get_gym_light_lock())
+                                        return;
                                       // Turn on both lights
                                       espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH);
                                       espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); });
@@ -151,18 +194,34 @@ void handle_input_change(uint8_t pin, bool state)
   switch (pin)
   {
   case RIGHT_STADIUM_LIGHT_SWITCH_PIN:
+    if (get_gym_light_lock())
+    {
+      return;
+    }
     espmega.outputs.digitalWrite(RIGHT_STADIUM_LIGHT_RELAY_PIN,
                                  !espmega.outputs.getState(RIGHT_STADIUM_LIGHT_RELAY_PIN));
     break;
   case LEFT_STADIUM_LIGHT_SWITCH_PIN:
+    if (get_gym_light_lock())
+    {
+      return;
+    }
     espmega.outputs.digitalWrite(LEFT_STADIUM_LIGHT_RELAY_PIN,
                                  !espmega.outputs.getState(LEFT_STADIUM_LIGHT_RELAY_PIN));
     break;
   case GIRLS_TOILET_LIGHT_SWITCH_PIN:
+    if (get_toilet_light_lock())
+    {
+      return;
+    }
     espmega.outputs.digitalWrite(GIRLS_TOILET_LIGHT_RELAY_PIN,
                                  !espmega.outputs.getState(GIRLS_TOILET_LIGHT_RELAY_PIN));
     break;
   case BOYS_TOILET_LIGHT_SWITCH_PIN:
+    if (get_toilet_light_lock())
+    {
+      return;
+    }
     espmega.outputs.digitalWrite(BOYS_TOILET_LIGHT_RELAY_PIN,
                                  !espmega.outputs.getState(BOYS_TOILET_LIGHT_RELAY_PIN));
     break;
@@ -171,6 +230,16 @@ void handle_input_change(uint8_t pin, bool state)
   }
 }
 
+bool get_toilet_light_lock()
+{
+  return toiletLightsLock.getIntValue() == 1;
+}
+
+bool get_gym_light_lock()
+{
+  return gymLightsLock.getIntValue() == 1;
+}
+
 void handle_mqtt_message(char *topic, char *payload)
 {
   // Do nothing
diff --git a/src/main.hpp b/src/main.hpp
index 1229bbc..450ba1f 100644
--- a/src/main.hpp
+++ b/src/main.hpp
@@ -12,6 +12,7 @@
 #include <ESPMegaProOS.hpp>
 #include <ETH.h>
 #include <ESPMegaDisplayOTA.hpp>
+#include <SmartVariable.hpp>
 #include "button.hpp"
 #include "config.hpp"
 
@@ -23,4 +24,7 @@ void loop();
 void send_stop_bytes(HardwareSerial &uart);
 void handle_input_change(uint8_t pin, bool state);
 void handle_mqtt_message(char *topic, char *payload);
-void button_init();
\ No newline at end of file
+void button_init();
+void smartvar_init();
+bool get_toilet_light_lock();
+bool get_gym_light_lock();
\ No newline at end of file