fix: all the bugs with the new database and commanding
This commit is contained in:
parent
9602e6e937
commit
924633f272
12 changed files with 99 additions and 149 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,5 +1,4 @@
|
||||||
.idea/
|
|
||||||
|
|
||||||
docs/
|
docs/
|
||||||
build/
|
build/
|
||||||
cmake-build-debug/
|
|
||||||
|
include/sql/*.h
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project(controller
|
project(controller
|
||||||
VERSION 0.3.0
|
VERSION 0.3.1
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
add_executable(controller src/main.c)
|
add_executable(controller src/main.c)
|
||||||
|
|
|
@ -10,4 +10,7 @@ junction_relay_schedule_remove_for_relay(int relay_id);
|
||||||
int
|
int
|
||||||
junction_relay_schedule_insert_weekdays(int relay_id, int *schedule_ids);
|
junction_relay_schedule_insert_weekdays(int relay_id, int *schedule_ids);
|
||||||
|
|
||||||
|
int*
|
||||||
|
junction_relay_schedule_get_relay_ids_with_schedule(int schedule_id);
|
||||||
|
|
||||||
#endif /* CONTROLLER_MODELS_JUNCTION_RELAY_SCHEDULE_H */
|
#endif /* CONTROLLER_MODELS_JUNCTION_RELAY_SCHEDULE_H */
|
||||||
|
|
|
@ -12,7 +12,7 @@ typedef struct
|
||||||
int id;
|
int id;
|
||||||
uuid_t uid;
|
uuid_t uid;
|
||||||
uint8_t weekday;
|
uint8_t weekday;
|
||||||
uint16_t length;
|
uint16_t periods_count;
|
||||||
period_t *periods;
|
period_t *periods;
|
||||||
} schedule_t;
|
} schedule_t;
|
||||||
|
|
||||||
|
|
|
@ -1,96 +0,0 @@
|
||||||
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;
|
|
|
@ -12,8 +12,10 @@ create table controllers
|
||||||
|
|
||||||
create table relays
|
create table relays
|
||||||
(
|
(
|
||||||
number INTEGER
|
id INTEGER
|
||||||
PRIMARY KEY
|
PRIMARY KEY
|
||||||
|
AUTOINCREMENT,
|
||||||
|
number INTEGER
|
||||||
NOT NULL,
|
NOT NULL,
|
||||||
name VARCHAR(128)
|
name VARCHAR(128)
|
||||||
);
|
);
|
||||||
|
|
|
@ -114,9 +114,26 @@ handler_command_schedule_update(mpack_node_t map)
|
||||||
if(schedule)
|
if(schedule)
|
||||||
{
|
{
|
||||||
new_schedule->id = schedule->id;
|
new_schedule->id = schedule->id;
|
||||||
|
schedule_free(schedule);
|
||||||
}
|
}
|
||||||
|
|
||||||
schedule_save(new_schedule);
|
schedule_save(new_schedule);
|
||||||
schedule_free(schedule);
|
schedule_debug(new_schedule);
|
||||||
|
|
||||||
|
int *relay_ids = junction_relay_schedule_get_relay_ids_with_schedule(new_schedule->id);
|
||||||
|
|
||||||
|
for(int i = 0; i < global_config.relay_count; ++i)
|
||||||
|
{
|
||||||
|
for(int j = 0; relay_ids[j] != 0; ++j)
|
||||||
|
{
|
||||||
|
if(global_controller->relays[i]->id == relay_ids[j])
|
||||||
|
{
|
||||||
|
relay_reload_schedules(global_controller->relays[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(relay_ids);
|
||||||
schedule_free(new_schedule);
|
schedule_free(new_schedule);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,6 +168,7 @@ handler_command_relay_schedules_set(mpack_node_t map)
|
||||||
|
|
||||||
schedule_t *schedule = schedule_get_by_uid(schedule_uid);
|
schedule_t *schedule = schedule_get_by_uid(schedule_uid);
|
||||||
|
|
||||||
|
relay_debug(target_relay);
|
||||||
junction_relay_schedule_insert(i, target_relay->id, schedule->id);
|
junction_relay_schedule_insert(i, target_relay->id, schedule->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +200,7 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
|
||||||
mpack_tree_parse(&tree);
|
mpack_tree_parse(&tree);
|
||||||
mpack_node_t root = mpack_tree_root(&tree);
|
mpack_node_t root = mpack_tree_root(&tree);
|
||||||
|
|
||||||
uint16_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
|
uint16_t command_code = mpack_node_u16(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
|
||||||
|
|
||||||
LOGGER_DEBUG("received command %d\n", command_code);
|
LOGGER_DEBUG("received command %d\n", command_code);
|
||||||
|
|
||||||
|
@ -221,4 +239,6 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
|
||||||
{
|
{
|
||||||
LOGGER_WARNING("error when destroying mpack tree\n");
|
LOGGER_WARNING("error when destroying mpack tree\n");
|
||||||
}
|
}
|
||||||
|
LOGGER_DEBUG("done with command %d - closing connection\n", command_code);
|
||||||
|
c->flags |= MG_F_SEND_AND_CLOSE;
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,6 @@ main(int argc, const char** argv)
|
||||||
if(!global_controller)
|
if(!global_controller)
|
||||||
{
|
{
|
||||||
global_controller = controller_create();
|
global_controller = controller_create();
|
||||||
|
|
||||||
controller_save();
|
controller_save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,9 +129,13 @@ main(int argc, const char** argv)
|
||||||
connection_mqtt_connect(&mgr);
|
connection_mqtt_connect(&mgr);
|
||||||
struct mg_connection *c_command = connection_command_bind(&mgr);
|
struct mg_connection *c_command = connection_command_bind(&mgr);
|
||||||
|
|
||||||
global_controller->command_port = helper_get_port(c_command->sock);
|
if(global_controller->command_port == 0)
|
||||||
|
{
|
||||||
|
global_controller->command_port = helper_get_port(c_command->sock);
|
||||||
|
controller_save();
|
||||||
|
}
|
||||||
|
|
||||||
controller_save();
|
controller_debug(global_controller);
|
||||||
|
|
||||||
helper_drop_privileges();
|
helper_drop_privileges();
|
||||||
|
|
||||||
|
|
|
@ -60,10 +60,11 @@ controller_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
uint8_t i;
|
uint8_t i;
|
||||||
for(i = 0; i < global_config.relay_count; ++i)
|
for(i = 0; i < global_config.relay_count; ++i)
|
||||||
{
|
{
|
||||||
new_controller->relays[i] = relay_load(new_controller->id);
|
new_controller->relays[i] = relay_load(i);
|
||||||
if(!new_controller->relays[i])
|
if(!new_controller->relays[i])
|
||||||
{
|
{
|
||||||
new_controller->relays[i] = relay_create(i);
|
new_controller->relays[i] = relay_create(i);
|
||||||
|
relay_save(new_controller->relays[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new_controller;
|
return new_controller;
|
||||||
|
@ -206,10 +207,11 @@ controller_create(void)
|
||||||
uint8_t i;
|
uint8_t i;
|
||||||
for(i = 0; i < global_config.relay_count; ++i)
|
for(i = 0; i < global_config.relay_count; ++i)
|
||||||
{
|
{
|
||||||
new_controller->relays[i] = relay_load(new_controller->id);
|
new_controller->relays[i] = relay_load(i);
|
||||||
if(!new_controller->relays[i])
|
if(!new_controller->relays[i])
|
||||||
{
|
{
|
||||||
new_controller->relays[i] = relay_create(i);
|
new_controller->relays[i] = relay_create(i);
|
||||||
|
relay_save(new_controller->relays[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
|
||||||
rc = sqlite3_step(stmt);
|
rc = sqlite3_step(stmt);
|
||||||
if (rc != SQLITE_DONE)
|
if (rc != SQLITE_DONE)
|
||||||
{
|
{
|
||||||
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
|
LOGGER_ERR("error inserting data: %s\n", sqlite3_errmsg(global_database));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,3 +88,15 @@ junction_relay_schedule_remove_for_relay(int relay_id)
|
||||||
|
|
||||||
return rc == SQLITE_DONE;
|
return rc == SQLITE_DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int*
|
||||||
|
junction_relay_schedule_get_relay_ids_with_schedule(int schedule_id)
|
||||||
|
{
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
|
sqlite3_prepare_v2(global_database, "SELECT DISTINCT relay_id FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, NULL);
|
||||||
|
sqlite3_bind_int(stmt, 1, schedule_id);
|
||||||
|
|
||||||
|
return database_helper_get_ids(stmt);
|
||||||
|
}
|
||||||
|
|
|
@ -55,20 +55,14 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memset(new_relay->schedules, 0, sizeof(schedule_t*) * 7);
|
||||||
|
relay_reload_schedules(new_relay);
|
||||||
|
|
||||||
schedule_t **schedules = schedule_get_relay_weekdays(new_relay->id);
|
new_relay->is_on = -1;
|
||||||
for(int i = 0; i < 7; ++i)
|
new_relay->is_on_schedule = -1;
|
||||||
{
|
new_relay->pulse_timer = 0;
|
||||||
if(schedules[i] == NULL)
|
new_relay->sent_to_broker = 0;
|
||||||
{
|
|
||||||
LOGGER_ERR("got only %d/7 schedules for relay_id %d\n", i, new_relay->id);
|
|
||||||
relay_free(new_relay);
|
|
||||||
free(schedules);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
new_relay->schedules[i] = schedules[i];
|
|
||||||
}
|
|
||||||
free(schedules); // don't free list, because contents are kept in relay->schedules
|
|
||||||
|
|
||||||
return new_relay;
|
return new_relay;
|
||||||
}
|
}
|
||||||
|
@ -119,11 +113,11 @@ relay_save(relay_t *relay)
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
if(relay->id)
|
if(relay->id)
|
||||||
{
|
{
|
||||||
sqlite3_prepare_v2(global_database, "UPDATE relays set number = ?2, name = ?3, controller_id = ?4 WHERE id = ?1;", -1, &stmt, NULL);
|
sqlite3_prepare_v2(global_database, "UPDATE relays set number = ?2, name = ?3 WHERE id = ?1;", -1, &stmt, NULL);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sqlite3_prepare_v2(global_database, "INSERT INTO relays(number, name, controller_id) values (?2, ?3, ?4);", -1, &stmt, NULL);
|
sqlite3_prepare_v2(global_database, "INSERT INTO relays(number, name) values (?2, ?3);", -1, &stmt, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = db_update_insert(relay, stmt);
|
int result = db_update_insert(relay, stmt);
|
||||||
|
@ -149,6 +143,7 @@ relay_save(relay_t *relay)
|
||||||
if(relay->id == 0)
|
if(relay->id == 0)
|
||||||
{
|
{
|
||||||
relay->id = sqlite3_last_insert_rowid(global_database);
|
relay->id = sqlite3_last_insert_rowid(global_database);
|
||||||
|
LOGGER_DEBUG("new relay - new id: %d\n", relay->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGGER_DEBUG("cleaning relay_schedule junction\n");
|
LOGGER_DEBUG("cleaning relay_schedule junction\n");
|
||||||
|
@ -177,6 +172,7 @@ relay_create(uint8_t number)
|
||||||
{
|
{
|
||||||
relay_t *new_relay = malloc(sizeof(relay_t));
|
relay_t *new_relay = malloc(sizeof(relay_t));
|
||||||
|
|
||||||
|
new_relay->id = 0;
|
||||||
new_relay->number = number;
|
new_relay->number = number;
|
||||||
new_relay->name[0] = '\0';
|
new_relay->name[0] = '\0';
|
||||||
|
|
||||||
|
@ -217,23 +213,31 @@ void
|
||||||
relay_reload_schedules(relay_t *relay)
|
relay_reload_schedules(relay_t *relay)
|
||||||
{
|
{
|
||||||
schedule_t **schedules = schedule_get_relay_weekdays(relay->id);
|
schedule_t **schedules = schedule_get_relay_weekdays(relay->id);
|
||||||
|
|
||||||
|
uuid_t off_id;
|
||||||
|
memset(off_id, 0, sizeof(uuid_t));
|
||||||
|
memcpy(off_id, "off", 3);
|
||||||
|
|
||||||
|
int fill_with_off = 0;
|
||||||
for(int i = 0; i < 7; ++i)
|
for(int i = 0; i < 7; ++i)
|
||||||
{
|
{
|
||||||
if(schedules[i] == NULL)
|
if(schedules[i] == NULL || fill_with_off)
|
||||||
{
|
{
|
||||||
LOGGER_ERR("got only %d/7 schedules for relay_id %d\n", i, relay->id);
|
LOGGER_WARNING("got only %d/7 schedules for relay_id %d\n", i, relay->id);
|
||||||
relay_free(relay);
|
relay->schedules[i] = schedule_get_by_uid(off_id);
|
||||||
schedule_free_list(schedules);
|
|
||||||
return;
|
fill_with_off = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(relay->schedules[i])
|
||||||
|
{
|
||||||
|
schedule_free(relay->schedules[i]);
|
||||||
|
}
|
||||||
|
relay->schedules[i] = schedules[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for(int i = 0; i < 7; ++i)
|
|
||||||
{
|
|
||||||
schedule_free(relay->schedules[i]);
|
|
||||||
relay->schedules[i] = schedules[i];
|
|
||||||
}
|
|
||||||
free(schedules); // don't free list, because contents are kept in relay->schedules
|
free(schedules); // don't free list, because contents are kept in relay->schedules
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,12 +252,12 @@ int
|
||||||
relay_is_on_schedule(relay_t *relay, struct tm *time_struct)
|
relay_is_on_schedule(relay_t *relay, struct tm *time_struct)
|
||||||
{
|
{
|
||||||
schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)];
|
schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)];
|
||||||
if(schedule->length == 0)
|
if(schedule->periods_count == 0)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(uint16_t i = 0; i < schedule->length; ++i)
|
for(uint16_t i = 0; i < schedule->periods_count; ++i)
|
||||||
{
|
{
|
||||||
if(period_includes_time(schedule->periods[i], time_struct))
|
if(period_includes_time(schedule->periods[i], time_struct))
|
||||||
{
|
{
|
||||||
|
@ -271,8 +275,8 @@ relay_debug(relay_t *relay)
|
||||||
LOGGER_DEBUG("relay is NULL\n");
|
LOGGER_DEBUG("relay is NULL\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOGGER_DEBUG("(1/3) %d @ %p\n", relay->number, (void*)relay);
|
LOGGER_DEBUG("(1/3) %3d @ %p\n", relay->number, (void*)relay);
|
||||||
LOGGER_DEBUG("(2/3) name: %s\n", relay->name);
|
LOGGER_DEBUG("(2/3) id: %3d; name: %s\n", relay->id, relay->name);
|
||||||
LOGGER_DEBUG("(3/3) schedules @ %p:\n", (void*)relay->schedules);
|
LOGGER_DEBUG("(3/3) schedules @ %p:\n", (void*)relay->schedules);
|
||||||
for(int i = 0; i < 7; ++i)
|
for(int i = 0; i < 7; ++i)
|
||||||
{
|
{
|
||||||
|
|
|
@ -44,6 +44,7 @@ schedule_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
break;
|
break;
|
||||||
case 'p': // periods
|
case 'p': // periods
|
||||||
periods_blob = sqlite3_column_blob(stmt, i);
|
periods_blob = sqlite3_column_blob(stmt, i);
|
||||||
|
new_schedule->periods_count = periods_blob[0];
|
||||||
new_schedule->periods = malloc(sizeof(period_t) * periods_blob[0]);
|
new_schedule->periods = malloc(sizeof(period_t) * periods_blob[0]);
|
||||||
|
|
||||||
for(int i = 0; i < periods_blob[0]; ++i)
|
for(int i = 0; i < periods_blob[0]; ++i)
|
||||||
|
@ -186,9 +187,10 @@ schedule_create(uuid_t uid, uint16_t length, uint16_t *periods_blob)
|
||||||
{
|
{
|
||||||
schedule_t *new_schedule = malloc(sizeof(schedule_t));
|
schedule_t *new_schedule = malloc(sizeof(schedule_t));
|
||||||
|
|
||||||
|
new_schedule->id = 0;
|
||||||
memmove(new_schedule->uid, uid, sizeof(uuid_t));
|
memmove(new_schedule->uid, uid, sizeof(uuid_t));
|
||||||
|
|
||||||
new_schedule->length = length;
|
new_schedule->periods_count = length;
|
||||||
new_schedule->periods = NULL;
|
new_schedule->periods = NULL;
|
||||||
|
|
||||||
if(length)
|
if(length)
|
||||||
|
@ -208,10 +210,10 @@ schedule_create(uuid_t uid, uint16_t length, uint16_t *periods_blob)
|
||||||
uint16_t*
|
uint16_t*
|
||||||
schedule_periods_to_blob(schedule_t *schedule)
|
schedule_periods_to_blob(schedule_t *schedule)
|
||||||
{
|
{
|
||||||
uint16_t *periods_blob = malloc(sizeof(uint16_t) * ((2 * schedule->length) + 1));
|
uint16_t *periods_blob = malloc(sizeof(uint16_t) * ((2 * schedule->periods_count) + 1));
|
||||||
periods_blob[0] = schedule->length;
|
periods_blob[0] = schedule->periods_count;
|
||||||
|
|
||||||
for(uint16_t i = 0; i < schedule->length; ++i)
|
for(uint16_t i = 0; i < schedule->periods_count; ++i)
|
||||||
{
|
{
|
||||||
|
|
||||||
periods_blob[1 + (i * 2)] = schedule->periods[i].start;
|
periods_blob[1 + (i * 2)] = schedule->periods[i].start;
|
||||||
|
@ -296,14 +298,13 @@ schedule_debug(schedule_t *schedule)
|
||||||
char uuid_str[UUID_STR_LEN];
|
char uuid_str[UUID_STR_LEN];
|
||||||
uuid_unparse(schedule->uid, uuid_str);
|
uuid_unparse(schedule->uid, uuid_str);
|
||||||
LOGGER_DEBUG("(1/3) %s @ %p\n", uuid_str, (void*)schedule);
|
LOGGER_DEBUG("(1/3) %s @ %p\n", uuid_str, (void*)schedule);
|
||||||
LOGGER_DEBUG("(2/4) period count: %3d\n", schedule->length);
|
LOGGER_DEBUG("(2/3) id: %3d; period count: %3d\n", schedule->id, schedule->periods_count);
|
||||||
LOGGER_DEBUG("(3/4) weekday: %3d\n", schedule->weekday);
|
|
||||||
|
|
||||||
// one block: "HH:MM-HH:MM, " --> size: 13 (14 with '\0')
|
// one block: "HH:MM-HH:MM, " --> size: 13 (14 with '\0')
|
||||||
char *periods_debug_str = malloc(sizeof(char) * ((schedule->length * 13) + 1));
|
char *periods_debug_str = malloc(sizeof(char) * ((schedule->periods_count * 13) + 1));
|
||||||
periods_debug_str[0] = '\0';
|
periods_debug_str[0] = '\0';
|
||||||
|
|
||||||
for(uint16_t i = 0; i < schedule->length; ++i)
|
for(uint16_t i = 0; i < schedule->periods_count; ++i)
|
||||||
{
|
{
|
||||||
sprintf(
|
sprintf(
|
||||||
periods_debug_str + (13 * i),
|
periods_debug_str + (13 * i),
|
||||||
|
@ -315,7 +316,7 @@ schedule_debug(schedule_t *schedule)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGGER_DEBUG("(4/4) periods: %s\n", periods_debug_str);
|
LOGGER_DEBUG("(3/3) periods: %s\n", periods_debug_str);
|
||||||
|
|
||||||
free(periods_debug_str);
|
free(periods_debug_str);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue