REFRACTOR
This commit is contained in:
		
							parent
							
								
									b68cdd224e
								
							
						
					
					
						commit
						f2a40ca330
					
				
					 3 changed files with 38 additions and 144 deletions
				
			
		
							
								
								
									
										32
									
								
								config.c
									
										
									
									
									
								
							
							
						
						
									
										32
									
								
								config.c
									
										
									
									
									
								
							| 
						 | 
					@ -11,20 +11,8 @@ config_t global_config;
 | 
				
			||||||
     ini_string_match_ii(KEY, disp->data, disp->format))
 | 
					     ini_string_match_ii(KEY, disp->data, disp->format))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
config_load(IniDispatch *disp, void *config_void)
 | 
					config_load_log_level(IniDispatch *disp, config_t *config)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    config_t *config = (config_t*)config_void;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if(disp->type == INI_KEY)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        if(CONFINI_IS_KEY("core", "database"))
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            config->database = malloc(sizeof(char) * (strlen(disp->value) + 1));
 | 
					 | 
				
			||||||
            strcpy(config->database, disp->value);
 | 
					 | 
				
			||||||
            return 0;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if(CONFINI_IS_KEY("core", "log-level"))
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
    if(strcasecmp(disp->value, "trace") == 0)
 | 
					    if(strcasecmp(disp->value, "trace") == 0)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        config->log_level = LOG_LEVEL_TRACE;
 | 
					        config->log_level = LOG_LEVEL_TRACE;
 | 
				
			||||||
| 
						 | 
					@ -57,6 +45,24 @@ config_load(IniDispatch *disp, void *config_void)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    LOG_WARN("invalid log-level '%s'\n", disp->value);
 | 
					    LOG_WARN("invalid log-level '%s'\n", disp->value);
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int
 | 
				
			||||||
 | 
					config_load(IniDispatch *disp, void *config_void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    config_t *config = (config_t*)config_void;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if(disp->type == INI_KEY)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if(CONFINI_IS_KEY("core", "database"))
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            config->database = malloc(sizeof(char) * (strlen(disp->value) + 1));
 | 
				
			||||||
 | 
					            strcpy(config->database, disp->value);
 | 
				
			||||||
 | 
					            return 0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if(CONFINI_IS_KEY("core", "log-level"))
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return config_load_log_level(disp, config);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if(CONFINI_IS_KEY("core", "discovery-port"))
 | 
					        if(CONFINI_IS_KEY("core", "discovery-port"))
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,22 +1,10 @@
 | 
				
			||||||
#ifndef CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H
 | 
					#ifndef CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H
 | 
				
			||||||
#define CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H
 | 
					#define CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int
 | 
					 | 
				
			||||||
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int*
 | 
					 | 
				
			||||||
junction_relay_schedule_get_relays_ids(int schedule_id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id);
 | 
					junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int
 | 
					 | 
				
			||||||
junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
junction_relay_schedule_remove_for_relay(int relay_id);
 | 
					junction_relay_schedule_remove_for_relay(int relay_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int
 | 
					 | 
				
			||||||
junction_relay_schedule_remove_for_schedule(int schedule_id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#endif /* CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H */
 | 
					#endif /* CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -30,91 +30,6 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int*
 | 
					 | 
				
			||||||
get_ids(sqlite3_stmt *stmt)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    int *ids = malloc(sizeof(int));
 | 
					 | 
				
			||||||
    int new_id;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    int row = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    while(true)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        int s;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        s = sqlite3_step(stmt);
 | 
					 | 
				
			||||||
        if (s == SQLITE_ROW)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            new_id = sqlite3_column_int(stmt, 0);
 | 
					 | 
				
			||||||
            row++;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            ids = (int*)realloc(ids, sizeof(int) * (row + 1));
 | 
					 | 
				
			||||||
            ids[row - 1] = new_id;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        else
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            if (s == SQLITE_DONE)
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            else
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                LOG_ERROR("error selecting junction ids from database: %s\n", sqlite3_errstr(s));
 | 
					 | 
				
			||||||
                sqlite3_finalize(stmt);
 | 
					 | 
				
			||||||
                return NULL;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    sqlite3_finalize(stmt);
 | 
					 | 
				
			||||||
    ids[row] = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return ids;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int
 | 
					 | 
				
			||||||
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    sqlite3_stmt *stmt;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    sqlite3_prepare_v2(global_database, "SELECT schedule_id FROM junction_relay_schedule WHERE weekday=?1 AND relay_id=?2 LIMIT 1;", -1, &stmt, NULL);
 | 
					 | 
				
			||||||
    sqlite3_bind_int(stmt, 1, weekday);
 | 
					 | 
				
			||||||
    sqlite3_bind_int(stmt, 2, relay_id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    int *id_list = get_ids(stmt); 
 | 
					 | 
				
			||||||
    int result = id_list[0];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    free(id_list);
 | 
					 | 
				
			||||||
    return result;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int*
 | 
					 | 
				
			||||||
junction_relay_schedule_get_relays_ids(int schedule_id)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    sqlite3_stmt *stmt;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    sqlite3_prepare_v2(global_database, "SELECT relay_id FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, NULL);
 | 
					 | 
				
			||||||
    sqlite3_bind_int(stmt, 1, schedule_id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return get_ids(stmt);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int
 | 
					 | 
				
			||||||
junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    sqlite3_stmt *stmt;
 | 
					 | 
				
			||||||
    int rc;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    sqlite3_prepare_v2(global_database, "DELETE FROM junction_relay_schedule WHERE weekday=?1 AND schedule_id=?2 AND relay_id=?3;", -1, &stmt, NULL);
 | 
					 | 
				
			||||||
    sqlite3_bind_int(stmt, 1, weekday);
 | 
					 | 
				
			||||||
    sqlite3_bind_int(stmt, 2, schedule_id);
 | 
					 | 
				
			||||||
    sqlite3_bind_int(stmt, 3, relay_id);
 | 
					 | 
				
			||||||
    rc = sqlite3_step(stmt);
 | 
					 | 
				
			||||||
    sqlite3_finalize(stmt);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return rc == SQLITE_DONE;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
junction_relay_schedule_remove_for_relay(int relay_id)
 | 
					junction_relay_schedule_remove_for_relay(int relay_id)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -128,18 +43,3 @@ junction_relay_schedule_remove_for_relay(int relay_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return rc == SQLITE_DONE;
 | 
					    return rc == SQLITE_DONE;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
int
 | 
					 | 
				
			||||||
junction_relay_schedule_remove_for_schedule(int schedule_id)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    sqlite3_stmt *stmt;
 | 
					 | 
				
			||||||
    int rc;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    sqlite3_prepare_v2(global_database, "DELETE FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, NULL);
 | 
					 | 
				
			||||||
    sqlite3_bind_int(stmt, 1, schedule_id);
 | 
					 | 
				
			||||||
    rc = sqlite3_step(stmt);
 | 
					 | 
				
			||||||
    sqlite3_finalize(stmt);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return rc == SQLITE_DONE;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue