remove: lmdb

add: sqlite
add: new commands
This commit is contained in:
Tobias Reisinger 2020-08-24 16:00:08 +02:00
parent a37bdc4870
commit 9602e6e937
33 changed files with 1403 additions and 752 deletions

View file

@ -1,19 +1,40 @@
#ifndef CONTROLLER_DATABASE_H
#define CONTROLLER_DATABASE_H
#include <lmdb.h>
#include <sqlite3.h>
extern sqlite3 *global_database;
/**
* @brief Setup lmdb database enviroment
*
* Creates, sets max dbs and opens an MDB_env instance.
* Will return on success, but exit program on failure.
*
* @param mdb_env Source variable will be set to new MDB_env
*/
void
database_setup(MDB_env **mdb_env, config_t *config);
database_init();
extern MDB_env *global_mdb_env;
void
database_free();
void
database_migrate();
int
database_transaction_begin();
void
database_transaction_commit();
void
database_transaction_rollback();
int
database_helper_get_id(sqlite3_stmt *stmt);
int*
database_helper_get_ids(sqlite3_stmt *stmt);
char*
database_helper_get_string(sqlite3_stmt *stmt);
char**
database_helper_get_strings(sqlite3_stmt *stmt);
#endif /* CONTROLLER_DATABASE_H */

View file

@ -15,31 +15,6 @@ typedef enum
DISCOVERY_MAPPING_RELAY_COUNT = 3,
} discovery_mapping_t;
typedef enum
{
COMMAND_MAPPING_CODE = 0,
COMMAND_MAPPING_NAME = 1,
COMMAND_MAPPING_RELAY_NUM = 2,
COMMAND_MAPPING_SCHEDULES_ARRAY = 3,
COMMAND_MAPPING_SCHEDULE_ID = 4,
COMMAND_MAPPING_PERIODS_COUNT = 5,
COMMAND_MAPPING_PERIODS_BLOB = 6,
COMMAND_MAPPING_PULSE_DURATION = 7,
} control_mapping_t;
typedef enum
{
COMMAND_CODE_GET_TIME = 1,
COMMAND_CODE_GET_ID = 2,
COMMAND_CODE_SET_NAME = 100,
COMMAND_CODE_GET_NAME = 101,
COMMAND_CODE_SET_SCHEDULE = 102,
COMMAND_CODE_GET_SCHEDULE = 103,
COMMAND_CODE_SET_RELAY_NAME = 104,
COMMAND_CODE_GET_RELAY_NAME = 105,
COMMAND_CODE_PULSE = 200,
} command_code_t;
typedef enum
{
RELAY_DRIVER_NONE,

View file

@ -1,13 +1,6 @@
#ifndef CONTROLLER_MACROS_H
#define CONTROLLER_MACROS_H
#include <colors.h>
#include <logger.h>
#define STRLEN(s) ((sizeof(s)/sizeof(s[0])) - sizeof(s[0]))
#ifndef SOURCE_PATH_SIZE
#define SOURCE_PATH_SIZE 0
#endif
#define __FILENAME__ (__FILE__ + SOURCE_PATH_SIZE)
#endif //CONTROLLER_MACROS_H
#endif /* CONTROLLER_MACROS_H */

View file

@ -13,10 +13,12 @@
*/
typedef struct
{
int id;
/**
* @brief A unique UUID for this controller
*/
uuid_t id;
uuid_t uid;
/**
* @brief The name of this controller
*
@ -27,14 +29,7 @@ typedef struct
* @brief The command port the controller was bound to
*/
uint16_t command_port;
/**
* @brief The discovery port the controller receives broadcasts on
*/
uint16_t discovery_port;
/**
* @brief Amount of relays available to this controller
*/
uint8_t relay_count;
relay_t **relays;
} controller_t;
@ -71,7 +66,7 @@ controller_create(void);
* @return A loaded or new instance of controller or NULL on database error
*/
controller_t*
controller_load(MDB_env *mdb_env);
controller_load();
/**
* @brief Save a controller to the database
@ -82,7 +77,7 @@ controller_load(MDB_env *mdb_env);
* @return Indicator to show success (0) or failure (!0)
*/
int
controller_save(controller_t *controller, MDB_env *mdb_env);
controller_save();
/**
* @brief Sets a name to a controller.

View file

@ -0,0 +1,13 @@
#ifndef CONTROLLER_MODELS_JUNCTION_RELAY_SCHEDULE_H
#define CONTROLLER_MODELS_JUNCTION_RELAY_SCHEDULE_H
int
junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id);
int
junction_relay_schedule_remove_for_relay(int relay_id);
int
junction_relay_schedule_insert_weekdays(int relay_id, int *schedule_ids);
#endif /* CONTROLLER_MODELS_JUNCTION_RELAY_SCHEDULE_H */

View file

@ -10,10 +10,7 @@ typedef struct
uint16_t end;
} period_t;
period_t*
period_create(uint16_t start, uint16_t end);
int
period_includes_time(period_t *period, struct tm *time_struct);
period_includes_time(period_t period, struct tm *time_struct);
#endif /* CONTROLLER_PERIOD_H */

View file

@ -10,6 +10,7 @@
typedef struct
{
int id;
uint8_t number;
int is_on;
int is_on_schedule;
@ -33,26 +34,14 @@ relay_create(uint8_t number);
void
relay_set_name(relay_t *relay, const char *name);
/**
* @brief Load a relay for database or create a new one
*
* @param mdb_env An opened MDB_env to load from
*
* @return A loaded or new instance of relay
*/
relay_t*
relay_load(MDB_env *mdb_env, uint8_t num);
relay_load(uint8_t number);
/**
* @brief Save a relay to the database
*
* @param relay Instance of a relay
* @param mdb_env Already created MDB_env
*
* @return Indicator to show success (0) or failure (!0)
*/
int
relay_save(relay_t *relay, MDB_env *mdb_env);
relay_save(relay_t *relay);
void
relay_reload_schedules(relay_t *relay);
int
relay_is_on_schedule(relay_t *relay, struct tm *time_struct);

View file

@ -9,10 +9,11 @@
typedef struct
{
uuid_t id;
int id;
uuid_t uid;
uint8_t weekday;
uint16_t length;
period_t **periods;
period_t *periods;
} schedule_t;
/**
@ -25,13 +26,16 @@ typedef enum
} db_key_schedule_e;
schedule_t*
schedule_create(uuid_t id, uint8_t weekday, uint16_t length, uint16_t *periods_blob);
schedule_t*
schedule_load(MDB_env *mdb_env, uint8_t relay_num, uint8_t weekday);
schedule_create(uuid_t uid, uint16_t length, uint16_t *periods_blob);
int
schedule_save(schedule_t *schedule, uint8_t relay_num, MDB_env *mdb_env);
schedule_save(schedule_t *schedule);
schedule_t*
schedule_get_by_uid(uuid_t uid);
schedule_t**
schedule_get_relay_weekdays(int relay_id);
uint16_t*
schedule_periods_to_blob(schedule_t *schedule);
@ -39,7 +43,16 @@ schedule_periods_to_blob(schedule_t *schedule);
void
schedule_free(schedule_t *schedule);
void
schedule_free_list(schedule_t **schedules_list);
void
schedule_debug(schedule_t *schedule);
int
schedule_uid_parse(const char *uid_str, uuid_t result);
void
schedule_uid_unparse(const uuid_t uid, char *result);
#endif /* CONTROLLER_SCHEDULE_H */

View file

@ -6,6 +6,6 @@
#include <models/controller.h>
void
runner_test(controller_t *controller);
runner_test();
#endif /* CONTROLLER_RUNNERS_H */

0
include/sql/.gitkeep Normal file
View file

96
include/sql/migration_0.h Normal file
View file

@ -0,0 +1,96 @@
unsigned char sql_migration_0_sql[] = {
0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x20, 0x74, 0x61, 0x62, 0x6c, 0x65,
0x20, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x73,
0x0a, 0x28, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x64, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x49, 0x4e, 0x54, 0x45, 0x47, 0x45, 0x52, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x50,
0x52, 0x49, 0x4d, 0x41, 0x52, 0x59, 0x20, 0x4b, 0x45, 0x59, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x41,
0x55, 0x54, 0x4f, 0x49, 0x4e, 0x43, 0x52, 0x45, 0x4d, 0x45, 0x4e, 0x54,
0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x75, 0x69, 0x64, 0x20, 0x20, 0x20,
0x20, 0x20, 0x42, 0x4c, 0x4f, 0x42, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x4e, 0x4f, 0x54, 0x20, 0x4e,
0x55, 0x4c, 0x4c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x55, 0x4e, 0x49, 0x51, 0x55, 0x45, 0x2c, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x56, 0x41, 0x52, 0x43,
0x48, 0x41, 0x52, 0x28, 0x31, 0x32, 0x38, 0x29, 0x2c, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x5f, 0x70, 0x6f,
0x72, 0x74, 0x20, 0x20, 0x20, 0x20, 0x49, 0x4e, 0x54, 0x45, 0x47, 0x45,
0x52, 0x0a, 0x29, 0x3b, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65,
0x20, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x72, 0x65, 0x6c, 0x61, 0x79,
0x73, 0x0a, 0x28, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6e, 0x75, 0x6d, 0x62,
0x65, 0x72, 0x20, 0x20, 0x49, 0x4e, 0x54, 0x45, 0x47, 0x45, 0x52, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x50, 0x52, 0x49, 0x4d, 0x41, 0x52, 0x59, 0x20, 0x4b, 0x45, 0x59, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x4e, 0x4f, 0x54, 0x20, 0x4e, 0x55, 0x4c, 0x4c, 0x2c, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x20, 0x20, 0x20, 0x56, 0x41,
0x52, 0x43, 0x48, 0x41, 0x52, 0x28, 0x31, 0x32, 0x38, 0x29, 0x0a, 0x29,
0x3b, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x20, 0x74, 0x61,
0x62, 0x6c, 0x65, 0x20, 0x73, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65,
0x73, 0x0a, 0x28, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x64, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x49, 0x4e, 0x54, 0x45, 0x47, 0x45, 0x52, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x50, 0x52, 0x49, 0x4d, 0x41, 0x52, 0x59, 0x20, 0x4b, 0x45, 0x59, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x41, 0x55, 0x54, 0x4f, 0x49, 0x4e, 0x43, 0x52, 0x45, 0x4d, 0x45, 0x4e,
0x54, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x75, 0x69, 0x64, 0x20, 0x20,
0x20, 0x20, 0x20, 0x42, 0x4c, 0x4f, 0x42, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x4e, 0x4f, 0x54, 0x20,
0x4e, 0x55, 0x4c, 0x4c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x55, 0x4e, 0x49, 0x51, 0x55, 0x45, 0x2c,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x20, 0x20,
0x20, 0x56, 0x41, 0x52, 0x43, 0x48, 0x41, 0x52, 0x28, 0x31, 0x32, 0x38,
0x29, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x70, 0x65, 0x72, 0x69, 0x6f,
0x64, 0x73, 0x20, 0x42, 0x4c, 0x4f, 0x42, 0x0a, 0x29, 0x3b, 0x0a, 0x0a,
0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x20, 0x74, 0x61, 0x62, 0x6c, 0x65,
0x20, 0x6a, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x72, 0x65,
0x6c, 0x61, 0x79, 0x5f, 0x73, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c, 0x65,
0x0a, 0x28, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x77, 0x65, 0x65, 0x6b, 0x64,
0x61, 0x79, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x53,
0x4d, 0x41, 0x4c, 0x4c, 0x49, 0x4e, 0x54, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x4e, 0x4f, 0x54, 0x20, 0x4e, 0x55, 0x4c, 0x4c,
0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x6c, 0x61, 0x79, 0x5f,
0x69, 0x64, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x49, 0x4e,
0x54, 0x45, 0x47, 0x45, 0x52, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x52, 0x45, 0x46, 0x45, 0x52, 0x45, 0x4e, 0x43, 0x45, 0x53,
0x20, 0x72, 0x65, 0x6c, 0x61, 0x79, 0x73, 0x20, 0x28, 0x69, 0x64, 0x29,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x4f, 0x4e, 0x20,
0x44, 0x45, 0x4c, 0x45, 0x54, 0x45, 0x20, 0x43, 0x41, 0x53, 0x43, 0x41,
0x44, 0x45, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x73, 0x63, 0x68, 0x65,
0x64, 0x75, 0x6c, 0x65, 0x5f, 0x69, 0x64, 0x20, 0x20, 0x20, 0x20, 0x20,
0x49, 0x4e, 0x54, 0x45, 0x47, 0x45, 0x52, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x44, 0x45, 0x46, 0x41, 0x55, 0x4c, 0x54, 0x20,
0x31, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x52, 0x45,
0x46, 0x45, 0x52, 0x45, 0x4e, 0x43, 0x45, 0x53, 0x20, 0x73, 0x63, 0x68,
0x65, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x20, 0x28, 0x69, 0x64, 0x29, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x4f, 0x4e, 0x20, 0x44,
0x45, 0x4c, 0x45, 0x54, 0x45, 0x20, 0x53, 0x45, 0x54, 0x20, 0x44, 0x45,
0x46, 0x41, 0x55, 0x4c, 0x54, 0x0a, 0x29, 0x3b, 0x0a, 0x0a, 0x49, 0x4e,
0x53, 0x45, 0x52, 0x54, 0x20, 0x49, 0x4e, 0x54, 0x4f, 0x20, 0x73, 0x63,
0x68, 0x65, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x20, 0x28, 0x75, 0x69, 0x64,
0x2c, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x70, 0x65, 0x72, 0x69,
0x6f, 0x64, 0x73, 0x29, 0x20, 0x56, 0x41, 0x4c, 0x55, 0x45, 0x53, 0x20,
0x28, 0x78, 0x27, 0x36, 0x66, 0x36, 0x36, 0x36, 0x36, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x27,
0x2c, 0x20, 0x27, 0x6f, 0x66, 0x66, 0x27, 0x2c, 0x20, 0x78, 0x27, 0x30,
0x30, 0x27, 0x29, 0x3b, 0x0a, 0x49, 0x4e, 0x53, 0x45, 0x52, 0x54, 0x20,
0x49, 0x4e, 0x54, 0x4f, 0x20, 0x73, 0x63, 0x68, 0x65, 0x64, 0x75, 0x6c,
0x65, 0x73, 0x20, 0x28, 0x75, 0x69, 0x64, 0x2c, 0x20, 0x6e, 0x61, 0x6d,
0x65, 0x2c, 0x20, 0x70, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x73, 0x29, 0x20,
0x56, 0x41, 0x4c, 0x55, 0x45, 0x53, 0x20, 0x28, 0x78, 0x27, 0x36, 0x66,
0x36, 0x65, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x27, 0x2c, 0x20, 0x20, 0x27, 0x6f,
0x6e, 0x27, 0x2c, 0x20, 0x78, 0x27, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x39, 0x46, 0x30, 0x35, 0x27, 0x29, 0x3b, 0x0a, 0x00
};
unsigned int sql_migration_0_sql_len = 1114;