remove: lmdb

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

View file

@ -1,13 +1,13 @@
cmake_minimum_required (VERSION 3.7)
project(controller
VERSION 0.2.10
VERSION 0.3.0
LANGUAGES C)
add_executable(controller src/main.c)
target_link_libraries(controller -lwiringPi)
target_link_libraries(controller -lwiringPiDev)
target_link_libraries(controller -llmdb)
target_link_libraries(controller -lsqlite3)
target_link_libraries(controller -luuid)
option(WIRING_PI_DEBUG "Use WiringPi Debugging Tool (OFF)" OFF)
@ -30,14 +30,22 @@ aux_source_directory(src/drivers DRIVERS_SRC)
aux_source_directory(src/runners RUNNERS_SRC)
aux_source_directory(vendor VENDOR_SRC)
add_dependencies(controller sql)
configure_file("controller.ini" "controller.ini" COPYONLY)
configure_file("version.h.in" "version.h" @ONLY)
target_sources(controller PRIVATE ${SRC_DIR} ${MODELS_SRC} ${HELPERS_SRC} ${HANDLERS_SRC} ${DRIVERS_SRC} ${RUNNERS_SRC} ${VENDOR_SRC})
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor)
target_include_directories(controller PRIVATE ${CMAKE_BINARY_DIR})
add_custom_target(sql
COMMAND ./compile_sql.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_custom_target(run
COMMAND ./controller start
DEPENDS controller

33
compile_sql.sh Executable file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env sh
to_header () {
if [ -f ./include/sql/$1.h ]
then
if [ -z $(find ./sql/$1.sql -newer ./include/sql/$1.h -print) ]
then
return
fi
fi
echo "Compiling $1"
xxd -i sql/$1.sql | sed 's/\([0-9a-f]\)$/\0, 0x00/' > ./include/sql/$1.h
}
cd "$(dirname "$0")";
migration_num=0;
while [ -f ./sql/migration_$migration_num.sql ]
do
if [ -f ./include/migrations/$migration_num.sql.h ]
then
if [ -z $(find ./sql/migration_$migration_num.sql -newer ./include/sql/migration_$migration_num.h -print) ]
then
migration_num=$((migration_num+1))
continue
fi
fi
to_header "migration_$migration_num"
migration_num=$((migration_num+1))
done
cd $PWD;

View file

@ -10,7 +10,7 @@ mqtt-host = localhost
relay-count = 10
relays-init = 1
database = controller_db.lmdb
database = controller.sqlite
log-level = debug
log-file = stdout

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;

47
sql/migration_0.sql Normal file
View file

@ -0,0 +1,47 @@
create table controllers
(
id INTEGER
PRIMARY KEY
AUTOINCREMENT,
uid BLOB
NOT NULL
UNIQUE,
name VARCHAR(128),
command_port INTEGER
);
create table relays
(
number INTEGER
PRIMARY KEY
NOT NULL,
name VARCHAR(128)
);
create table schedules
(
id INTEGER
PRIMARY KEY
AUTOINCREMENT,
uid BLOB
NOT NULL
UNIQUE,
name VARCHAR(128),
periods BLOB
);
create table junction_relay_schedule
(
weekday SMALLINT
NOT NULL,
relay_id INTEGER
REFERENCES relays (id)
ON DELETE CASCADE,
schedule_id INTEGER
DEFAULT 1
REFERENCES schedules (id)
ON DELETE SET DEFAULT
);
INSERT INTO schedules (uid, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00');
INSERT INTO schedules (uid, name, periods) VALUES (x'6f6e0000000000000000000000000000', 'on', x'010000009F05');

View file

@ -11,7 +11,7 @@ struct mg_connection*
connection_discovery_bind(struct mg_mgr *mgr)
{
char address[32];
sprintf(address, "udp://0.0.0.0:%u", global_controller->discovery_port);
sprintf(address, "udp://0.0.0.0:%u", global_config.discovery_port);
struct mg_connection *c = mg_bind(mgr, address, handler_discovery);
return c;
}

View file

@ -1,32 +1,264 @@
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <lmdb.h>
#include <stdlib.h>
#include <logger.h>
#include <config.h>
#include <constants.h>
#include <database.h>
#include <sql/migration_0.h>
sqlite3 *global_database;
static int in_transaction;
void
database_setup(MDB_env **mdb_env, config_t *config)
database_init()
{
int err;
if(mdb_env_create(mdb_env) != 0)
int rc = sqlite3_open(global_config.database, &global_database);
if(rc)
{
LOGGER_CRIT("Can't create mdb handle");
LOGGER_CRIT("can't open database: %s\n", sqlite3_errmsg(global_database));
exit(1);
}
if((err = mdb_env_set_maxdbs(*mdb_env, MDB_MAXDBS)) != 0)
{
LOGGER_CRIT("mdb_env_set_maxdbs error %s\n", mdb_strerror(err));
exit(1);
}
database_migrate();
if(mdb_env_open(*mdb_env, config->database, MDB_NOSUBDIR, 0700) != 0)
sqlite3_exec(global_database, "PRAGMA foreign_keys = ON", 0, 0, 0);
in_transaction = 0;
}
void
database_free()
{
sqlite3_close(global_database);
}
void
database_migrate()
{
uint16_t version_num = 0;
int s, rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "PRAGMA user_version;", -1, &stmt, NULL);
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
LOGGER_CRIT("Can't open mdb file");
exit(1);
version_num = sqlite3_column_int(stmt, 0);
}
else
{
version_num = 0;
}
uint16_t new_version_num = version_num;
char* err_msg;
sqlite3_finalize(stmt);
switch(version_num)
{
case 0:
LOGGER_INFO("migrating LEVEL 0\n");
rc = sqlite3_exec(global_database, (const char *)sql_migration_0_sql, NULL, NULL, &err_msg);
if(rc)
{
LOGGER_CRIT("couldn't migrate LEVEL 0 (%s)\n", err_msg);
exit(1);
}
new_version_num = 1;
default:
break;
}
char pragma_query[32];
sprintf(pragma_query, "PRAGMA user_version=%d;", new_version_num);
sqlite3_exec(global_database, pragma_query, 0, 0, 0);
LOGGER_DEBUG("storing new user_version %d\n", new_version_num);
return;
}
int
database_transaction_begin()
{
if(!in_transaction)
{
LOGGER_DEBUG("beginning transaction\n");
sqlite3_exec(global_database, "BEGIN TRANSACTION;", NULL, NULL, NULL);
in_transaction = 1;
return 1;
}
return 0;
}
void
database_transaction_commit()
{
LOGGER_DEBUG("commiting transaction\n");
sqlite3_exec(global_database, "COMMIT TRANSACTION;", NULL, NULL, NULL);
in_transaction = 0;
}
void
database_transaction_rollback()
{
LOGGER_DEBUG("rolling back transaction\n");
sqlite3_exec(global_database, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
in_transaction = 0;
}
int
database_helper_get_id(sqlite3_stmt *stmt)
{
int result = 0;
for(;;)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
result = sqlite3_column_int(stmt, 0);
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOGGER_ERR("error selecting id from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return 0;
}
}
}
sqlite3_finalize(stmt);
return result;
}
int*
database_helper_get_ids(sqlite3_stmt *stmt)
{
int *result = malloc(sizeof(int));
int new_id;
int row = 0;
for(;;)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
new_id = sqlite3_column_int(stmt, 0);
if(new_id != 0) // found row for other target (relay <> schedule)
{
row++;
result = (int*)realloc(result, sizeof(int) * (row + 1));
result[row - 1] = new_id;
}
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOGGER_ERR("error selecting ids from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return NULL;
}
}
}
sqlite3_finalize(stmt);
result[row] = 0;
return result;
}
char*
database_helper_get_string(sqlite3_stmt *stmt)
{
char *result = NULL;
for(;;)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
const char *found_string = (const char *)sqlite3_column_text(stmt, 0);
result = (char*)malloc(sizeof(char) * (strlen(found_string) + 1));
strcpy(result, found_string);
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOGGER_ERR("error selecting string from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return NULL;
}
}
}
sqlite3_finalize(stmt);
return result;
}
char**
database_helper_get_strings(sqlite3_stmt *stmt)
{
char **result = malloc(sizeof(char*));
int row = 0;
for(;;)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
const char *new_string = (const char *)sqlite3_column_text(stmt, 0);
int new_string_len = strlen(new_string);
row++;
result = (char**)realloc(result, sizeof(char*) * (row + 1));
result[row - 1] = malloc(sizeof(char) * (new_string_len + 1));
strcpy(result[row - 1], new_string);
}
else
{
if(s == SQLITE_DONE)
{
break;
}
else
{
LOGGER_ERR("error selecting strings from database: %s\n", sqlite3_errstr(s));
break;
}
}
}
sqlite3_finalize(stmt);
result[row] = NULL;
return result;
}

View file

@ -14,16 +14,52 @@
#include <database.h>
#include <handlers.h>
#include <helpers.h>
#include <enums.h>
#include <mpack.h>
#include <models/controller.h>
#include <models/relay.h>
#include <models/schedule.h>
#include <models/junction_relay_schedule.h>
typedef enum
{
COMMAND_CODE_CONTROLLER_ID_GET = 0,
COMMAND_CODE_CONTROLLER_TIME_GET = 1,
COMMAND_CODE_CONTROLLER_NAME_SET = 2,
COMMAND_CODE_CONTROLLER_NAME_GET = 3,
COMMAND_CODE_RELAY_SCHEDULES_SET = 100,
COMMAND_CODE_RELAY_SCHEDULES_GET = 101,
COMMAND_CODE_RELAY_NAME_SET = 102,
COMMAND_CODE_RELAY_NAME_GET = 103,
COMMAND_CODE_RELAY_PULSE = 200,
COMMAND_CODE_SCHEDULE_UPDATE = 300
} command_code_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;
static void
handler_command_pulse(mpack_node_t map, controller_t *controller)
handler_command_relay_pulse(mpack_node_t map)
{
int relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
relay_t *target_relay = controller->relays[relay_num];
if(relay_num > global_config.relay_count)
{
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config.relay_count);
return;
}
relay_t *target_relay = global_controller->relays[relay_num];
(void)target_relay;
int duration = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_PULSE_DURATION));
@ -36,56 +72,92 @@ handler_command_pulse(mpack_node_t map, controller_t *controller)
}
static void
handler_command_set_name(mpack_node_t map, controller_t *controller)
handler_command_controller_name_set(mpack_node_t map)
{
char name_buffer[MAX_NAME_LENGTH + 1];
mpack_node_copy_cstr(mpack_node_map_uint(map, COMMAND_MAPPING_NAME), name_buffer, MAX_NAME_LENGTH + 1);
controller_set_name(controller, name_buffer);
controller_set_name(global_controller, name_buffer);
LOGGER_DEBUG("setting new name %s for controller\n", name_buffer);
controller_save();
}
static void
handler_command_set_schedule(mpack_node_t map, controller_t *controller)
handler_command_relay_name_set(mpack_node_t map)
{
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
LOGGER_DEBUG("setting schedules for relay %d\n", relay_num);
const char *relay_name = mpack_node_str(mpack_node_map_uint(map, COMMAND_MAPPING_NAME));
relay_t *target_relay = controller->relays[relay_num];
if(relay_num > global_config.relay_count)
{
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config.relay_count);
return;
}
relay_set_name(global_controller->relays[relay_num], relay_name);
relay_debug(global_controller->relays[relay_num]);
LOGGER_DEBUG("setting new name %s for relay %d\n", relay_name, relay_num);
relay_save(global_controller->relays[relay_num]);
}
static void
handler_command_schedule_update(mpack_node_t map)
{
uuid_t schedule_uid;
memcpy(schedule_uid, mpack_node_data(mpack_node_map_uint(map, COMMAND_MAPPING_SCHEDULE_ID)), sizeof(uuid_t));
uint16_t periods_count = mpack_node_u16(mpack_node_map_uint(map, COMMAND_MAPPING_PERIODS_COUNT));
uint16_t *periods = (uint16_t*)mpack_node_bin_data(mpack_node_map_uint(map, COMMAND_MAPPING_PERIODS_BLOB));
schedule_t *schedule = schedule_get_by_uid(schedule_uid);
schedule_t *new_schedule = schedule_create(schedule_uid, periods_count, periods);
if(schedule)
{
new_schedule->id = schedule->id;
}
schedule_save(new_schedule);
schedule_free(schedule);
schedule_free(new_schedule);
}
static void
handler_command_relay_schedules_set(mpack_node_t map)
{
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
if(relay_num > global_config.relay_count)
{
LOGGER_WARNING("relay %d is not available (relay count: %d\n", relay_num, global_config.relay_count);
return;
}
LOGGER_DEBUG("setting schedules for relay %d\n", relay_num);
relay_t *target_relay = global_controller->relays[relay_num];
database_transaction_begin();
junction_relay_schedule_remove_for_relay(target_relay->id);
mpack_node_t schedules_array = mpack_node_map_uint(map, COMMAND_MAPPING_SCHEDULES_ARRAY);
for(int i = 0; i < 7; ++i)
{
mpack_node_t schedules_map = mpack_node_array_at(schedules_array, i);
mpack_node_t schedule_map = mpack_node_array_at(schedules_array, i);
uuid_t schedule_id;
memcpy(schedule_id, mpack_node_data(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_SCHEDULE_ID)), sizeof(uuid_t));
handler_command_schedule_update(schedule_map);
uint16_t periods_count = mpack_node_u16(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_PERIODS_COUNT));
uint16_t *periods = (uint16_t*)mpack_node_bin_data(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_PERIODS_BLOB));
uuid_t schedule_uid;
memcpy(schedule_uid, mpack_node_data(mpack_node_map_uint(schedule_map, COMMAND_MAPPING_SCHEDULE_ID)), sizeof(uuid_t));
if(target_relay->schedules[i])
{
schedule_free(target_relay->schedules[i]);
}
target_relay->schedules[i] = schedule_create(schedule_id, i, periods_count, periods);
schedule_t *schedule = schedule_get_by_uid(schedule_uid);
junction_relay_schedule_insert(i, target_relay->id, schedule->id);
}
relay_reload_schedules(target_relay);
relay_debug(target_relay);
}
static void
handler_command_set_relay_name(mpack_node_t map, controller_t *controller)
{
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
const char *relay_name = mpack_node_str(mpack_node_map_uint(map, COMMAND_MAPPING_NAME));
if(relay_num < controller->relay_count)
{
relay_set_name(controller->relays[relay_num], relay_name);
}
relay_debug(controller->relays[relay_num]);
LOGGER_DEBUG("setting new name %s for relay %d\n", relay_name, relay_num);
database_transaction_commit();
}
void
@ -110,33 +182,36 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
mpack_tree_parse(&tree);
mpack_node_t root = mpack_tree_root(&tree);
uint8_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
uint16_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
LOGGER_DEBUG("received command %d\n", command_code);
switch(command_code)
{
case COMMAND_CODE_GET_TIME:
case COMMAND_CODE_CONTROLLER_ID_GET:
break;
case COMMAND_CODE_GET_ID:
case COMMAND_CODE_CONTROLLER_TIME_GET:
break;
case COMMAND_CODE_SET_NAME:
handler_command_set_name(root, global_controller);
case COMMAND_CODE_CONTROLLER_NAME_SET:
handler_command_controller_name_set(root);
break;
case COMMAND_CODE_GET_NAME:
case COMMAND_CODE_CONTROLLER_NAME_GET:
break;
case COMMAND_CODE_SET_SCHEDULE:
handler_command_set_schedule(root, global_controller);
case COMMAND_CODE_RELAY_SCHEDULES_SET:
handler_command_relay_schedules_set(root);
break;
case COMMAND_CODE_GET_SCHEDULE:
case COMMAND_CODE_RELAY_SCHEDULES_GET:
break;
case COMMAND_CODE_SET_RELAY_NAME:
handler_command_set_relay_name(root, global_controller);
case COMMAND_CODE_RELAY_NAME_SET:
handler_command_relay_name_set(root);
break;
case COMMAND_CODE_GET_RELAY_NAME:
case COMMAND_CODE_RELAY_NAME_GET:
break;
case COMMAND_CODE_PULSE:
handler_command_pulse(root, global_controller);
case COMMAND_CODE_RELAY_PULSE:
handler_command_relay_pulse(root);
break;
case COMMAND_CODE_SCHEDULE_UPDATE:
handler_command_schedule_update(root);
break;
default:
LOGGER_ERR("received invalid command\n");
@ -146,5 +221,4 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
{
LOGGER_WARNING("error when destroying mpack tree\n");
}
controller_save(global_controller, global_mdb_env);
}

View file

@ -49,11 +49,11 @@ handler_discovery(struct mg_connection *c, int ev, void *ev_data)
mpack_start_map(&writer, 4);
mpack_write_uint(&writer, DISCOVERY_MAPPING_ID);
mpack_write_bin(&writer, (char*)global_controller->id, sizeof(uuid_t));
mpack_write_bin(&writer, (char*)global_controller->uid, sizeof(uuid_t));
mpack_write_uint(&writer, DISCOVERY_MAPPING_COMMAND_PORT);
mpack_write_u16(&writer, global_controller->command_port);
mpack_write_uint(&writer, DISCOVERY_MAPPING_RELAY_COUNT);
mpack_write_u8(&writer, global_controller->relay_count);
mpack_write_u8(&writer, global_config.relay_count);
mpack_write_uint(&writer, DISCOVERY_MAPPING_NAME);
mpack_write_cstr(&writer, global_controller->name);
mpack_finish_map(&writer);

View file

@ -19,7 +19,7 @@ handler_loop(struct mg_connection *c_mqtt)
char topic_buf[100];
char payload_buf[2];
char controller_uid[UUID_STR_LEN];
uuid_unparse(global_controller->id, controller_uid);
uuid_unparse(global_controller->uid, controller_uid);
struct tm time_last, time_now;
time_t timestamp;
@ -28,7 +28,7 @@ handler_loop(struct mg_connection *c_mqtt)
localtime_r(&timestamp, &time_last);
timestamp = time(NULL);
localtime_r(&timestamp, &time_now);
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
for(uint_fast8_t i = 0; i < global_config.relay_count; ++i)
{
relay_t *relay = global_controller->relays[i];
int is_on = 0;

Some files were not shown because too many files have changed in this diff Show more