add: commands

fix: timezone problem
This commit is contained in:
Tobias Reisinger 2020-05-07 01:38:13 +02:00
parent 0edb16a2d5
commit 10e41ca166
11 changed files with 209 additions and 21 deletions

View file

@ -1,4 +1,5 @@
#include <cJSON.h>
#include <command.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
@ -114,16 +115,24 @@ api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struc
cJSON_Delete(json);
json = controller_to_json(controller);
int result = command_set_controller_name(controller);
int status_code = 200;
if(result)
{
status_code = 504;
}
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print controller json\n");
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_send_head(c, status_code, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "{}");
}
else
{
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_send_head(c, status_code, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "%s", json_str);
free(json_str);
}

View file

@ -1,4 +1,5 @@
#include <cJSON.h>
#include <command.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
@ -104,7 +105,9 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
{
relay->schedules[i] = schedule_get_by_uid(tmp_uuid);
}
relay->active_schedule = schedule_get_by_uid(tmp_uuid);
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
}
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
@ -169,7 +172,10 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
cJSON *json_active_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_active_schedule, "id");
if(cJSON_IsString(json_active_schedule_uid) && json_active_schedule_uid->valuestring)
{
int day_of_week = helper_get_weekday(time(NULL));
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
int day_of_week = helper_get_weekday(time_struct);
schedule_free(relay->schedules[day_of_week]);
uuid_t target_uid;
@ -220,16 +226,24 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
cJSON_Delete(json);
json = relay_to_json(relay);
int result = command_set_relay_schedule(relay);
int status_code = 200;
if(result)
{
status_code = 504;
}
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print relay json\n");
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_send_head(c, status_code, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "{}");
}
else
{
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_send_head(c, status_code, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "%s", json_str);
free(json_str);
}

View file

@ -244,6 +244,7 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
discovered_controller->active = 1;
controller_save(discovered_controller);
controller_free(discovered_controller);
}
mpack_tree_destroy(&tree);
free(answer_payload);

View file

@ -83,6 +83,9 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
LOG_ERROR("error before: %s\n", error_ptr);
}
cJSON_Delete(json);
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "{}");
return;
}
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");