Compare commits

...

6 commits

8 changed files with 21 additions and 26 deletions

View file

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7) cmake_minimum_required (VERSION 3.7)
project(controller project(controller
VERSION 0.3.2 VERSION 0.3.7
LANGUAGES C) LANGUAGES C)
add_executable(controller src/main.c) add_executable(controller src/main.c)

View file

@ -3,7 +3,6 @@
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <stdint.h> #include <stdint.h>
#include <lmdb.h>
#include <config.h> #include <config.h>
#include <models/relay.h> #include <models/relay.h>

View file

@ -3,7 +3,6 @@
#include <stdint.h> #include <stdint.h>
#include <time.h> #include <time.h>
#include <lmdb.h>
#include <constants.h> #include <constants.h>
#include <models/schedule.h> #include <models/schedule.h>
@ -13,7 +12,6 @@ typedef struct
int id; int id;
uint8_t number; uint8_t number;
int is_on; int is_on;
int is_on_schedule;
int pulse_timer; int pulse_timer;
int sent_to_broker; int sent_to_broker;
char name[MAX_NAME_LENGTH + 1]; char name[MAX_NAME_LENGTH + 1];

View file

@ -3,7 +3,6 @@
#include <stdint.h> #include <stdint.h>
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <lmdb.h>
#include <models/period.h> #include <models/period.h>

View file

@ -93,7 +93,6 @@ handler_command_relay_name_set(mpack_node_t map)
return; return;
} }
relay_set_name(global_controller->relays[relay_num], relay_name); relay_set_name(global_controller->relays[relay_num], relay_name);
relay_debug(global_controller->relays[relay_num]);
LOGGER_DEBUG("setting new name %s for relay %d\n", relay_name, relay_num); LOGGER_DEBUG("setting new name %s for relay %d\n", relay_name, relay_num);
relay_save(global_controller->relays[relay_num]); relay_save(global_controller->relays[relay_num]);
} }
@ -118,7 +117,6 @@ handler_command_schedule_update(mpack_node_t map)
} }
schedule_save(new_schedule); schedule_save(new_schedule);
schedule_debug(new_schedule);
int *relay_ids = junction_relay_schedule_get_relay_ids_with_schedule(new_schedule->id); int *relay_ids = junction_relay_schedule_get_relay_ids_with_schedule(new_schedule->id);
@ -178,14 +176,11 @@ handler_command_relay_schedules_set(mpack_node_t map)
} }
schedule_save(new_schedule); schedule_save(new_schedule);
schedule_debug(new_schedule);
relay_debug(target_relay);
junction_relay_schedule_insert(i, target_relay->id, new_schedule->id); junction_relay_schedule_insert(i, target_relay->id, new_schedule->id);
} }
relay_reload_schedules(target_relay); relay_reload_schedules(target_relay);
relay_debug(target_relay);
database_transaction_commit(); database_transaction_commit();
} }

View file

@ -34,21 +34,25 @@ handler_loop(struct mg_connection *c_mqtt)
int is_on = 0; int is_on = 0;
int is_on_schedule = relay_is_on_schedule(relay, &time_now); 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;
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);
}
} }
if(is_on_schedule && !global_config.relay_configs[i].pulse_duration) else
{ {
is_on = 1; relay->pulse_timer = 0;
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);
} }
if(relay->is_on != is_on) if(relay->is_on != is_on)
@ -60,11 +64,14 @@ handler_loop(struct mg_connection *c_mqtt)
sprintf(topic_buf, "controller/%s/relay/%u", controller_uid, i); sprintf(topic_buf, "controller/%s/relay/%u", controller_uid, i);
sprintf(payload_buf, "%u", is_on); sprintf(payload_buf, "%u", is_on);
mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf)); mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf));
relay->sent_to_broker = 1; relay->sent_to_broker = (rand() % 45) + 15;
LOGGER_DEBUG("sent relay %d status (%d) to mqtt broker\n", i, is_on); LOGGER_DEBUG("sent relay %d status (%d) to mqtt broker\n", i, is_on);
} }
if(relay->sent_to_broker)
{
--relay->sent_to_broker;
}
relay->is_on = is_on; relay->is_on = is_on;
relay->is_on_schedule = is_on_schedule;
if(global_config.relay_configs[i].inverted) if(global_config.relay_configs[i].inverted)
{ {

View file

@ -2,7 +2,6 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <lmdb.h>
#include <signal.h> #include <signal.h>
#include <syslog.h> #include <syslog.h>

View file

@ -60,7 +60,6 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
relay_reload_schedules(new_relay); relay_reload_schedules(new_relay);
new_relay->is_on = -1; new_relay->is_on = -1;
new_relay->is_on_schedule = -1;
new_relay->pulse_timer = 0; new_relay->pulse_timer = 0;
new_relay->sent_to_broker = 0; new_relay->sent_to_broker = 0;
@ -177,7 +176,6 @@ relay_create(uint8_t number)
new_relay->name[0] = '\0'; new_relay->name[0] = '\0';
new_relay->is_on = -1; new_relay->is_on = -1;
new_relay->is_on_schedule = -1;
new_relay->pulse_timer = 0; new_relay->pulse_timer = 0;
new_relay->sent_to_broker = 0; new_relay->sent_to_broker = 0;