add: forward schedule updates to relays

This commit is contained in:
Tobias Reisinger 2020-05-11 14:50:25 +02:00
parent 574a384d2f
commit 606f2483a5
5 changed files with 75 additions and 18 deletions

View file

@ -2,8 +2,11 @@
#include <constants.h> #include <constants.h>
#include <endpoints/api_v1_schedules.h> #include <endpoints/api_v1_schedules.h>
#include <logger.h> #include <logger.h>
#include <command.h>
#include <models/junction_tag.h> #include <models/junction_tag.h>
#include <models/junction_relay_schedule.h>
#include <models/schedule.h> #include <models/schedule.h>
#include <models/relay.h>
#include <models/tag.h> #include <models/tag.h>
void void
@ -153,6 +156,16 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
return; 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_tag;
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags"); cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
if(cJSON_IsArray(json_tags)) if(cJSON_IsArray(json_tags))

View file

@ -8,26 +8,33 @@ handler_connection(struct mg_connection *c, int ev, void *p)
{ {
if (ev == MG_EV_HTTP_REQUEST) if (ev == MG_EV_HTTP_REQUEST)
{ {
LOG_DEBUG("new http request\n");
struct http_message *hm = (struct http_message *) p; 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); endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &hm->method);
if(endpoint && endpoint->func) if(endpoint && endpoint->func)
{ {
endpoint->func(c, endpoint->args, p); if(endpoint->func)
for(int i = 0; i < endpoint->args_count; ++i)
{ {
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 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); //mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p);

View file

@ -4,6 +4,9 @@
int int
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id); 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);

View file

@ -30,15 +30,13 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
return true; return true;
} }
int static int*
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id) get_ids(sqlite3_stmt *stmt)
{ {
int result = 0; int *ids = malloc(sizeof(int));
sqlite3_stmt *stmt; 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); int row = 0;
sqlite3_bind_int(stmt, 1, weekday);
sqlite3_bind_int(stmt, 2, relay_id);
while(true) while(true)
{ {
@ -47,7 +45,11 @@ junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
s = sqlite3_step(stmt); s = sqlite3_step(stmt);
if (s == SQLITE_ROW) 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 else
{ {
@ -57,17 +59,46 @@ junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
} }
else else
{ {
LOG_ERROR("error reading from database: %s", sqlite3_errstr(s)); LOG_ERROR("error selecting junction ids from database: %s\n", sqlite3_errstr(s));
break; sqlite3_finalize(stmt);
return NULL;
} }
} }
} }
sqlite3_finalize(stmt); 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; 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 int
junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id) junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id)
{ {

View file

@ -134,7 +134,10 @@ schedule_save(schedule_t *schedule)
} }
else else
{ {
schedule->id = sqlite3_last_insert_rowid(global_database); if(!schedule->id)
{
schedule->id = sqlite3_last_insert_rowid(global_database);
}
} }
return result; return result;
} }