fix: load less from database on mqtt publish

This commit is contained in:
Tobias Reisinger 2020-08-28 23:26:22 +02:00
parent 6117548e32
commit 6728ef9264
6 changed files with 40 additions and 25 deletions

View file

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7) cmake_minimum_required (VERSION 3.7)
project(core project(core
VERSION 0.3.3 VERSION 0.3.4
LANGUAGES C) LANGUAGES C)
add_executable(core src/main.c) add_executable(core src/main.c)

View file

@ -42,6 +42,9 @@ controller_get_by_id(int id);
controller_t* controller_t*
controller_get_by_uid(uuid_t uid); controller_get_by_uid(uuid_t uid);
int
controller_get_id_for_uid(uuid_t uid);
controller_t** controller_t**
controller_get_all(); controller_get_all();

View file

@ -47,6 +47,9 @@ relay_free_list(relay_t **relays_list);
relay_t* relay_t*
relay_get_by_id(int id); relay_get_by_id(int id);
int
relay_get_id_for_controller(int controller_id, int relay_num);
relay_t* relay_t*
relay_get_for_controller(int controller_id, int relay_num); relay_get_for_controller(int controller_id, int relay_num);

View file

@ -31,13 +31,12 @@ handle_mqtt_publish_controller(char **topic_save, int controller_id, char *paylo
{ {
return; return;
} }
relay_t *relay = relay_get_for_controller(controller_id, relay_num); int relay_id = relay_get_id_for_controller(controller_id, relay_num);
if(!relay) if(!relay_id)
{ {
return; return;
} }
status_update_entry(relay->id, payload[0] == '1'); status_update_entry(relay_id, payload[0] == '1');
free(relay);
} }
} }
@ -64,11 +63,10 @@ handle_mqtt_publish(struct mg_mqtt_message *msg)
uuid_t controller_uid; uuid_t controller_uid;
if(uuid_parse(controller_uid_str, controller_uid) == 0) if(uuid_parse(controller_uid_str, controller_uid) == 0)
{ {
controller_t *controller = controller_get_by_uid(controller_uid); int controller_id = controller_get_id_for_uid(controller_uid);
if(controller) if(controller_id)
{ {
handle_mqtt_publish_controller(topic_save, controller->id, payload); handle_mqtt_publish_controller(topic_save, controller_id, payload);
controller_free(controller);
} }
} }
} }

View file

@ -324,6 +324,20 @@ controller_get_by_uid(uuid_t uid)
return result; 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_t**
controller_get_all() controller_get_all()
{ {

View file

@ -383,22 +383,6 @@ relay_get_by_id(int id)
return result; 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_t**
relay_get_all() 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_t**
relay_get_by_controller_id(int controller_id) relay_get_by_controller_id(int controller_id)
{ {