diff --git a/CMakeLists.txt b/CMakeLists.txt index a3c7f10..a763453 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project(core - VERSION 0.3.3 + VERSION 0.3.4 LANGUAGES C) add_executable(core src/main.c) diff --git a/include/models/controller.h b/include/models/controller.h index 028c63b..e144a0d 100644 --- a/include/models/controller.h +++ b/include/models/controller.h @@ -42,6 +42,9 @@ controller_get_by_id(int id); controller_t* controller_get_by_uid(uuid_t uid); +int +controller_get_id_for_uid(uuid_t uid); + controller_t** controller_get_all(); diff --git a/include/models/relay.h b/include/models/relay.h index 9826f30..529db7f 100644 --- a/include/models/relay.h +++ b/include/models/relay.h @@ -47,6 +47,9 @@ relay_free_list(relay_t **relays_list); relay_t* relay_get_by_id(int id); +int +relay_get_id_for_controller(int controller_id, int relay_num); + relay_t* relay_get_for_controller(int controller_id, int relay_num); diff --git a/src/handlers/mqtt.c b/src/handlers/mqtt.c index d7200fe..7f26ffc 100644 --- a/src/handlers/mqtt.c +++ b/src/handlers/mqtt.c @@ -31,13 +31,12 @@ handle_mqtt_publish_controller(char **topic_save, int controller_id, char *paylo { return; } - relay_t *relay = relay_get_for_controller(controller_id, relay_num); - if(!relay) + int relay_id = relay_get_id_for_controller(controller_id, relay_num); + if(!relay_id) { return; } - status_update_entry(relay->id, payload[0] == '1'); - free(relay); + status_update_entry(relay_id, payload[0] == '1'); } } @@ -64,11 +63,10 @@ handle_mqtt_publish(struct mg_mqtt_message *msg) uuid_t controller_uid; if(uuid_parse(controller_uid_str, controller_uid) == 0) { - controller_t *controller = controller_get_by_uid(controller_uid); - if(controller) + int controller_id = controller_get_id_for_uid(controller_uid); + if(controller_id) { - handle_mqtt_publish_controller(topic_save, controller->id, payload); - controller_free(controller); + handle_mqtt_publish_controller(topic_save, controller_id, payload); } } } diff --git a/src/models/controller.c b/src/models/controller.c index 6797c56..807853c 100644 --- a/src/models/controller.c +++ b/src/models/controller.c @@ -324,6 +324,20 @@ controller_get_by_uid(uuid_t uid) return result; } +int +controller_get_id_for_uid(uuid_t uid) +{ + char uuid_str[UUID_STR_LEN]; + uuid_unparse(uid, uuid_str); + LOGGER_DEBUG("getting controller id [uid=%s] from database\n", uuid_str); + sqlite3_stmt *stmt; + + sqlite3_prepare_v2(global_database, "SELECT id FROM controllers WHERE uid = ?1;", -1, &stmt, NULL); + sqlite3_bind_blob(stmt, 1, uid, sizeof(uuid_t), SQLITE_STATIC); + + return database_helper_get_id(stmt); +} + controller_t** controller_get_all() { diff --git a/src/models/relay.c b/src/models/relay.c index d2ca8b9..df196f0 100644 --- a/src/models/relay.c +++ b/src/models/relay.c @@ -383,22 +383,6 @@ relay_get_by_id(int id) return result; } -relay_t* -relay_get_by_uid(uuid_t uid) -{ - sqlite3_stmt *stmt; - - sqlite3_prepare_v2(global_database, "SELECT * FROM relays WHERE uid = ?1;", -1, &stmt, NULL); - sqlite3_bind_blob(stmt, 1, uid, sizeof(uuid_t), SQLITE_STATIC); - - relay_t **sql_result = relay_db_select(stmt); - - relay_t *result = sql_result[0]; - free(sql_result); - - return result; -} - relay_t** relay_get_all() { @@ -441,6 +425,19 @@ relay_get_for_controller(int controller_id, int relay_num) } +int +relay_get_id_for_controller(int controller_id, int relay_num) +{ + LOGGER_DEBUG("getting relay id [controller_id=%d, relay_num=%d] from database\n", controller_id, relay_num); + sqlite3_stmt *stmt; + + sqlite3_prepare_v2(global_database, "SELECT id FROM relays WHERE controller_id = ?1 AND number = ?2;", -1, &stmt, NULL); + sqlite3_bind_int(stmt, 1, controller_id); + sqlite3_bind_int(stmt, 2, relay_num); + + return database_helper_get_id(stmt); +} + relay_t** relay_get_by_controller_id(int controller_id) {