fix: load less from database on mqtt publish
This commit is contained in:
		
							parent
							
								
									6117548e32
								
							
						
					
					
						commit
						6728ef9264
					
				
					 6 changed files with 40 additions and 25 deletions
				
			
		| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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()
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue