From 14f93e44ce409350a92bb98904dd0c2a16099a32 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Thu, 27 Aug 2020 10:46:52 +0200
Subject: [PATCH 1/4] add: only allow pulse when on schedule

---
 CMakeLists.txt         |  2 +-
 include/models/relay.h |  1 -
 src/handlers/loop.c    | 27 +++++++++++++++------------
 src/models/relay.c     |  2 --
 4 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d27cf5e..ec9f33a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
 cmake_minimum_required (VERSION 3.7)
 project(controller
-        VERSION 0.3.4
+        VERSION 0.3.5
         LANGUAGES C)
 
 add_executable(controller src/main.c)
diff --git a/include/models/relay.h b/include/models/relay.h
index 6ffb258..bbac159 100644
--- a/include/models/relay.h
+++ b/include/models/relay.h
@@ -13,7 +13,6 @@ typedef struct
     int id;
     uint8_t number;
     int is_on;
-    int is_on_schedule;
     int pulse_timer;
     int sent_to_broker;
     char name[MAX_NAME_LENGTH + 1];
diff --git a/src/handlers/loop.c b/src/handlers/loop.c
index f2c6671..035e462 100644
--- a/src/handlers/loop.c
+++ b/src/handlers/loop.c
@@ -34,21 +34,25 @@ handler_loop(struct mg_connection *c_mqtt)
         int is_on = 0;
         
         int is_on_schedule = relay_is_on_schedule(relay, &time_now);
+        int pulse_relay = global_config.relay_configs[i].pulse_duration;
 
-        if(relay->is_on_schedule != is_on_schedule && relay->is_on_schedule != -1)
+        if(is_on_schedule)
         {
-            relay->pulse_timer = global_config.relay_configs[i].pulse_duration;
+            if(pulse_relay)
+            {
+                is_on = 1;
+                --relay->pulse_timer;
+                LOGGER_DEBUG("relay %d is pulsing for %d more seconds\n", i, relay->pulse_timer);
+            }
+            else
+            {
+                is_on = 1;
+                LOGGER_DEBUG("relay %d is active\n", i);
+            }
         }
-        if(is_on_schedule && !global_config.relay_configs[i].pulse_duration)
+        else
         {
-            is_on = 1;
-            LOGGER_DEBUG("relay %d is active\n", i);
-        }
-        if(relay->pulse_timer > 0)
-        {
-            is_on = 1;
-            --relay->pulse_timer;
-            LOGGER_DEBUG("relay %d is pulsing for %d more seconds\n", i, relay->pulse_timer);
+            relay->pulse_timer = 0;
         }
 
         if(relay->is_on != is_on)
@@ -68,7 +72,6 @@ handler_loop(struct mg_connection *c_mqtt)
             --relay->sent_to_broker;
         }
         relay->is_on = is_on;
-        relay->is_on_schedule = is_on_schedule;
 
         if(global_config.relay_configs[i].inverted)
         {
diff --git a/src/models/relay.c b/src/models/relay.c
index 903e1bf..38c3aa0 100644
--- a/src/models/relay.c
+++ b/src/models/relay.c
@@ -60,7 +60,6 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
     relay_reload_schedules(new_relay);
 
     new_relay->is_on = -1;
-    new_relay->is_on_schedule = -1;
     new_relay->pulse_timer = 0;
     new_relay->sent_to_broker = 0;
 
@@ -177,7 +176,6 @@ relay_create(uint8_t number)
     new_relay->name[0] = '\0';
 
     new_relay->is_on = -1;
-    new_relay->is_on_schedule = -1;
     new_relay->pulse_timer = 0;
     new_relay->sent_to_broker = 0;
 

From d82a6219f0682eef1e4b88adb8679eb66124c221 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Thu, 27 Aug 2020 12:01:49 +0200
Subject: [PATCH 2/4] fix: forgot pulse condition

---
 CMakeLists.txt      | 2 +-
 src/handlers/loop.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ec9f33a..9d07b24 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
 cmake_minimum_required (VERSION 3.7)
 project(controller
-        VERSION 0.3.5
+        VERSION 0.3.6
         LANGUAGES C)
 
 add_executable(controller src/main.c)
diff --git a/src/handlers/loop.c b/src/handlers/loop.c
index 035e462..f121bcd 100644
--- a/src/handlers/loop.c
+++ b/src/handlers/loop.c
@@ -38,7 +38,7 @@ handler_loop(struct mg_connection *c_mqtt)
 
         if(is_on_schedule)
         {
-            if(pulse_relay)
+            if(pulse_relay && relay->pulse_timer)
             {
                 is_on = 1;
                 --relay->pulse_timer;

From 25eab5d38ec867cb3834cd31acb6f75029a9cb5e Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Thu, 27 Aug 2020 13:22:10 +0200
Subject: [PATCH 3/4] fix: see last commit (I need tests)

---
 CMakeLists.txt      |  2 +-
 src/handlers/loop.c | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9d07b24..3223e1c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
 cmake_minimum_required (VERSION 3.7)
 project(controller
-        VERSION 0.3.6
+        VERSION 0.3.7
         LANGUAGES C)
 
 add_executable(controller src/main.c)
diff --git a/src/handlers/loop.c b/src/handlers/loop.c
index f121bcd..2076952 100644
--- a/src/handlers/loop.c
+++ b/src/handlers/loop.c
@@ -38,17 +38,17 @@ handler_loop(struct mg_connection *c_mqtt)
 
         if(is_on_schedule)
         {
+            if(!pulse_relay)
+            {
+                is_on = 1;
+                LOGGER_DEBUG("relay %d is active\n", i);
+            }
             if(pulse_relay && relay->pulse_timer)
             {
                 is_on = 1;
                 --relay->pulse_timer;
                 LOGGER_DEBUG("relay %d is pulsing for %d more seconds\n", i, relay->pulse_timer);
             }
-            else
-            {
-                is_on = 1;
-                LOGGER_DEBUG("relay %d is active\n", i);
-            }
         }
         else
         {

From 5afff53301cb8bb8af13e5a681591961aa76a4c3 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Sat, 27 May 2023 00:37:28 +0200
Subject: [PATCH 4/4] Remove unused lmdb.h

---
 include/models/controller.h | 1 -
 include/models/relay.h      | 1 -
 include/models/schedule.h   | 1 -
 src/main.c                  | 1 -
 4 files changed, 4 deletions(-)

diff --git a/include/models/controller.h b/include/models/controller.h
index 1cf4fad..10d5827 100644
--- a/include/models/controller.h
+++ b/include/models/controller.h
@@ -3,7 +3,6 @@
 
 #include <uuid/uuid.h>
 #include <stdint.h>
-#include <lmdb.h>
 
 #include <config.h>
 #include <models/relay.h>
diff --git a/include/models/relay.h b/include/models/relay.h
index bbac159..498ea30 100644
--- a/include/models/relay.h
+++ b/include/models/relay.h
@@ -3,7 +3,6 @@
 
 #include <stdint.h>
 #include <time.h>
-#include <lmdb.h>
 
 #include <constants.h>
 #include <models/schedule.h>
diff --git a/include/models/schedule.h b/include/models/schedule.h
index 212cd81..ae91e14 100644
--- a/include/models/schedule.h
+++ b/include/models/schedule.h
@@ -3,7 +3,6 @@
 
 #include <stdint.h>
 #include <uuid/uuid.h>
-#include <lmdb.h>
 
 #include <models/period.h>
 
diff --git a/src/main.c b/src/main.c
index 17149e2..a13c75e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,7 +2,6 @@
 #include <string.h>
 #include <stdio.h>
 #include <time.h>
-#include <lmdb.h>
 #include <signal.h>
 #include <syslog.h>