diff --git a/CMakeLists.txt b/CMakeLists.txt index 3223e1c..0eee6a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project(controller - VERSION 0.3.7 + VERSION 0.3.1 LANGUAGES C) add_executable(controller src/main.c) diff --git a/include/models/controller.h b/include/models/controller.h index 10d5827..1cf4fad 100644 --- a/include/models/controller.h +++ b/include/models/controller.h @@ -3,6 +3,7 @@ #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 498ea30..6ffb258 100644 --- a/include/models/relay.h +++ b/include/models/relay.h @@ -3,6 +3,7 @@ #include <stdint.h> #include <time.h> +#include <lmdb.h> #include <constants.h> #include <models/schedule.h> @@ -12,6 +13,7 @@ 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/include/models/schedule.h b/include/models/schedule.h index ae91e14..212cd81 100644 --- a/include/models/schedule.h +++ b/include/models/schedule.h @@ -3,6 +3,7 @@ #include <stdint.h> #include <uuid/uuid.h> +#include <lmdb.h> #include <models/period.h> diff --git a/src/handlers/command.c b/src/handlers/command.c index d2c04c9..b511e02 100644 --- a/src/handlers/command.c +++ b/src/handlers/command.c @@ -93,6 +93,7 @@ handler_command_relay_name_set(mpack_node_t map) return; } 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); relay_save(global_controller->relays[relay_num]); } @@ -117,6 +118,7 @@ handler_command_schedule_update(mpack_node_t map) } schedule_save(new_schedule); + schedule_debug(new_schedule); int *relay_ids = junction_relay_schedule_get_relay_ids_with_schedule(new_schedule->id); @@ -159,28 +161,19 @@ handler_command_relay_schedules_set(mpack_node_t map) { mpack_node_t schedule_map = mpack_node_array_at(schedules_array, i); + handler_command_schedule_update(schedule_map); + uuid_t schedule_uid; memcpy(schedule_uid, mpack_node_data(mpack_node_map_uint(schedule_map, COMMAND_MAPPING_SCHEDULE_ID)), sizeof(uuid_t)); - uint16_t periods_count = mpack_node_u16(mpack_node_map_uint(schedule_map, COMMAND_MAPPING_PERIODS_COUNT)); - uint16_t *periods = (uint16_t*)mpack_node_bin_data(mpack_node_map_uint(schedule_map, COMMAND_MAPPING_PERIODS_BLOB)); - schedule_t *schedule = schedule_get_by_uid(schedule_uid); - schedule_t *new_schedule = schedule_create(schedule_uid, periods_count, periods); - - if(schedule) - { - new_schedule->id = schedule->id; - schedule_free(schedule); - } - - schedule_save(new_schedule); - - junction_relay_schedule_insert(i, target_relay->id, new_schedule->id); + relay_debug(target_relay); + junction_relay_schedule_insert(i, target_relay->id, schedule->id); } relay_reload_schedules(target_relay); + relay_debug(target_relay); database_transaction_commit(); } diff --git a/src/handlers/loop.c b/src/handlers/loop.c index 2076952..76a5999 100644 --- a/src/handlers/loop.c +++ b/src/handlers/loop.c @@ -34,25 +34,21 @@ 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(is_on_schedule) + if(relay->is_on_schedule != is_on_schedule && relay->is_on_schedule != -1) { - 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); - } + relay->pulse_timer = global_config.relay_configs[i].pulse_duration; } - else + if(is_on_schedule && !global_config.relay_configs[i].pulse_duration) { - relay->pulse_timer = 0; + 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); } if(relay->is_on != is_on) @@ -64,14 +60,11 @@ handler_loop(struct mg_connection *c_mqtt) sprintf(topic_buf, "controller/%s/relay/%u", controller_uid, i); sprintf(payload_buf, "%u", is_on); mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf)); - relay->sent_to_broker = (rand() % 45) + 15; + relay->sent_to_broker = 1; 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_schedule = is_on_schedule; if(global_config.relay_configs[i].inverted) { diff --git a/src/main.c b/src/main.c index a13c75e..17149e2 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include <string.h> #include <stdio.h> #include <time.h> +#include <lmdb.h> #include <signal.h> #include <syslog.h> diff --git a/src/models/relay.c b/src/models/relay.c index 38c3aa0..903e1bf 100644 --- a/src/models/relay.c +++ b/src/models/relay.c @@ -60,6 +60,7 @@ 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; @@ -176,6 +177,7 @@ 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; diff --git a/src/models/schedule.c b/src/models/schedule.c index 4c279c9..d895637 100644 --- a/src/models/schedule.c +++ b/src/models/schedule.c @@ -296,7 +296,7 @@ schedule_debug(schedule_t *schedule) return; } char uuid_str[UUID_STR_LEN]; - schedule_uid_unparse(schedule->uid, uuid_str); + uuid_unparse(schedule->uid, uuid_str); LOGGER_DEBUG("(1/3) %s @ %p\n", uuid_str, (void*)schedule); LOGGER_DEBUG("(2/3) id: %3d; period count: %3d\n", schedule->id, schedule->periods_count);