From 606f2483a5ac3a92e5b2d74b53f6db9f141c0eb6 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 11 May 2020 14:50:25 +0200 Subject: [PATCH] add: forward schedule updates to relays --- endpoints/api_v1_schedules_STR.c | 13 ++++++ handlers/connection.c | 21 ++++++---- include/models/junction_relay_schedule.h | 3 ++ models/junction_relay_schedule.c | 51 +++++++++++++++++++----- models/schedule.c | 5 ++- 5 files changed, 75 insertions(+), 18 deletions(-) diff --git a/endpoints/api_v1_schedules_STR.c b/endpoints/api_v1_schedules_STR.c index a87d51a..ce065a0 100644 --- a/endpoints/api_v1_schedules_STR.c +++ b/endpoints/api_v1_schedules_STR.c @@ -2,8 +2,11 @@ #include #include #include +#include #include +#include #include +#include #include void @@ -153,6 +156,16 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct return; } + int *relays = junction_relay_schedule_get_relays_ids(schedule->id); + for(int i = 0; relays[i] != 0; ++i) + { + relay_t *relay = relay_get_by_id(relays[i]); + if(relay) + { + command_set_relay_schedule(relay); + } + } + cJSON *json_tag; cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags"); if(cJSON_IsArray(json_tags)) diff --git a/handlers/connection.c b/handlers/connection.c index a1a0ecc..af949cc 100644 --- a/handlers/connection.c +++ b/handlers/connection.c @@ -8,26 +8,33 @@ handler_connection(struct mg_connection *c, int ev, void *p) { if (ev == MG_EV_HTTP_REQUEST) { - LOG_DEBUG("new http request\n"); struct http_message *hm = (struct http_message *) p; + LOG_DEBUG("new http request for %.*s\n", hm->uri.len, hm->uri.p); endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &hm->method); if(endpoint && endpoint->func) { - endpoint->func(c, endpoint->args, p); - - for(int i = 0; i < endpoint->args_count; ++i) + if(endpoint->func) { - if(endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR) + endpoint->func(c, endpoint->args, p); + + for(int i = 0; i < endpoint->args_count; ++i) { - free((char*)endpoint->args[i].value.v_str); + if(endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR) + { + free((char*)endpoint->args[i].value.v_str); + } } } + else + { + mg_send_head(c, 501, 0, "Content-Type: text/plain"); + } } else { - mg_send_head(c, 501, 0, "Content-Type: text/plain"); + mg_send_head(c, 500, 0, "Content-Type: text/plain"); } //mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p); diff --git a/include/models/junction_relay_schedule.h b/include/models/junction_relay_schedule.h index 3e46914..dec7ac9 100644 --- a/include/models/junction_relay_schedule.h +++ b/include/models/junction_relay_schedule.h @@ -4,6 +4,9 @@ int junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id); +int* +junction_relay_schedule_get_relays_ids(int schedule_id); + int junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id); diff --git a/models/junction_relay_schedule.c b/models/junction_relay_schedule.c index 7fe56aa..d80eec6 100644 --- a/models/junction_relay_schedule.c +++ b/models/junction_relay_schedule.c @@ -30,15 +30,13 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id) return true; } -int -junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id) +static int* +get_ids(sqlite3_stmt *stmt) { - int result = 0; - sqlite3_stmt *stmt; + int *ids = malloc(sizeof(int)); + int new_id; - 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 row = 0; while(true) { @@ -47,7 +45,11 @@ junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id) s = sqlite3_step(stmt); if (s == SQLITE_ROW) { - result = sqlite3_column_int(stmt, 0); + new_id = sqlite3_column_int(stmt, 0); + row++; + + ids = (int*)realloc(ids, sizeof(int) * (row + 1)); + ids[row - 1] = new_id; } else { @@ -57,17 +59,46 @@ junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id) } else { - LOG_ERROR("error reading from database: %s", sqlite3_errstr(s)); - break; + 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) { diff --git a/models/schedule.c b/models/schedule.c index b10f200..deccb3b 100644 --- a/models/schedule.c +++ b/models/schedule.c @@ -134,7 +134,10 @@ schedule_save(schedule_t *schedule) } else { - schedule->id = sqlite3_last_insert_rowid(global_database); + if(!schedule->id) + { + schedule->id = sqlite3_last_insert_rowid(global_database); + } } return result; }