add: more debugging
add: more options for testing
This commit is contained in:
parent
0efbd2a22f
commit
e84d54f562
23 changed files with 130 additions and 63 deletions
src/endpoints
api_v1_controllers.capi_v1_controllers_STR.capi_v1_controllers_STR_relays.capi_v1_controllers_STR_relays_INT.capi_v1_controllers_STR_relays_INT_pulse.capi_v1_controllers_discover.capi_v1_relays.capi_v1_relays_tag_STR.capi_v1_schedules.capi_v1_schedules_STR.capi_v1_schedules_tag_STR.capi_v1_tags_STR.c
|
@ -17,9 +17,10 @@ api_v1_controllers_GET(struct mg_connection *nc, struct http_message *hm, endpoi
|
|||
|
||||
cJSON *json = cJSON_CreateArray();
|
||||
|
||||
LOGGER_DEBUG("filling json array\n");
|
||||
for(int i = 0; all_controllers[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_controller = cJSON_CreateRaw(controller_to_json(all_controllers[i]));
|
||||
cJSON *json_controller = controller_to_json(all_controllers[i]);
|
||||
|
||||
cJSON_AddItemToArray(json, json_controller);
|
||||
}
|
||||
|
|
|
@ -35,8 +35,9 @@ api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, en
|
|||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
LOGGER_DEBUG("returning controller for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
cJSON *json = cJSON_CreateRaw(controller_to_json(controller));
|
||||
cJSON *json = controller_to_json(controller);
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
|
@ -69,6 +70,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
|
|||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
LOGGER_DEBUG("starting overwrite for controller %s\n", args[0].value.v_str);
|
||||
|
||||
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||
|
||||
|
@ -87,6 +89,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
|
|||
{
|
||||
strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH);
|
||||
controller->name[MAX_NAME_LENGTH] = '\0';
|
||||
LOGGER_DEBUG("new controller name: %s\n", controller->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -108,6 +111,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
|
|||
{
|
||||
strncpy(controller->ip, json_ip->valuestring, IP_LENGTH);
|
||||
controller->ip[IP_LENGTH] = '\0';
|
||||
LOGGER_DEBUG("new controller ip: %s\n", controller->ip);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -138,9 +142,10 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
|
|||
endpoint_response_text(response, 500, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
LOGGER_DEBUG("saved controller %s\n", args[0].value.v_str);
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = cJSON_CreateRaw(controller_to_json(controller));
|
||||
json = controller_to_json(controller);
|
||||
|
||||
command_set_controller_name(controller);
|
||||
|
||||
|
@ -187,6 +192,7 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
|
|||
}
|
||||
else
|
||||
{
|
||||
LOGGER_DEBUG("deleted controller %s\n", args[0].value.v_str);
|
||||
endpoint_response_text(response, 200, "", 0);
|
||||
}
|
||||
controller_free(controller);
|
||||
|
|
|
@ -37,9 +37,10 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *nc, struct http_message
|
|||
|
||||
cJSON *json = cJSON_CreateArray();
|
||||
|
||||
LOGGER_DEBUG("returning all relays for controller %s\n", args[0].value.v_str);
|
||||
for(int i = 0; all_relays[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(all_relays[i]));
|
||||
cJSON *json_relay = relay_to_json(all_relays[i]);
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
}
|
||||
|
|
|
@ -47,7 +47,8 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess
|
|||
return;
|
||||
}
|
||||
|
||||
cJSON *json = cJSON_CreateRaw(relay_to_json(relay));
|
||||
LOGGER_DEBUG("returning relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||
cJSON *json = relay_to_json(relay);
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
|
@ -82,10 +83,20 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
return;
|
||||
}
|
||||
|
||||
if(args[1].value.v_int > controller->relay_count)
|
||||
{
|
||||
LOGGER_DEBUG("relay num %d is too high for %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||
|
||||
static const char content[] = "relay number is too high for this controller";
|
||||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int);
|
||||
|
||||
if(!relay)
|
||||
{
|
||||
LOGGER_DEBUG("relay num %d not found for %s - creating default relay\n", args[1].value.v_int, args[0].value.v_str);
|
||||
relay = malloc(sizeof(relay_t));
|
||||
relay->id = 0;
|
||||
relay->number = args[1].value.v_int;
|
||||
|
@ -106,6 +117,8 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
|
||||
}
|
||||
|
||||
LOGGER_DEBUG("overwriting relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||
|
||||
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||
|
||||
if(json == NULL)
|
||||
|
@ -120,6 +133,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
{
|
||||
strncpy(relay->name, json_name->valuestring, MAX_NAME_LENGTH);
|
||||
relay->name[MAX_NAME_LENGTH] = '\0';
|
||||
LOGGER_DEBUG("new name: %s\n", relay->name);
|
||||
}
|
||||
|
||||
|
||||
|
@ -154,6 +168,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
|
||||
schedule_free(relay->schedules[schedule_position]);
|
||||
relay->schedules[schedule_position] = schedule_get_by_uid_or_off(target_uid);
|
||||
LOGGER_DEBUG("new schedule[%d]: %s\n", schedule_position, relay->schedules[schedule_position]);
|
||||
|
||||
++schedule_position;
|
||||
}
|
||||
|
@ -182,6 +197,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
return;
|
||||
}
|
||||
relay->schedules[day_of_week] = schedule_get_by_uid_or_off(target_uid);
|
||||
LOGGER_DEBUG("new active schedule: %s\n", relay->schedules[day_of_week]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -200,6 +216,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
|
||||
if(cJSON_IsArray(json_tags))
|
||||
{
|
||||
LOGGER_DEBUG("cleaning tags");
|
||||
junction_tag_remove_for_relay(relay->id);
|
||||
}
|
||||
cJSON_ArrayForEach(json_tag, json_tags)
|
||||
|
@ -220,8 +237,9 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = cJSON_CreateRaw(relay_to_json(relay));
|
||||
json = relay_to_json(relay);
|
||||
|
||||
LOGGER_DEBUG("commanding schedules");
|
||||
command_set_relay_schedule(relay);
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
|
|
|
@ -57,9 +57,11 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht
|
|||
{
|
||||
duration = json_duration->valueint & 0xFF;
|
||||
}
|
||||
LOGGER_DEBUG("pulsing with custom duration %d\n", duration);
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
LOGGER_DEBUG("commanding pulse to relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||
command_pulse(relay, duration);
|
||||
|
||||
endpoint_response_text(response, 200, "", 0);
|
||||
|
|
|
@ -127,6 +127,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
|
|||
int16_t payload[1];
|
||||
payload[0] = discover_server_port;
|
||||
|
||||
LOGGER_DEBUG("sending udp broadcast\n");
|
||||
if(send_udp_broadcast("255.255.255.255", global_config.discovery_port, payload, sizeof(payload)) < 0)
|
||||
{
|
||||
LOGGER_ERR("failed to send UDP broadcast\n");
|
||||
|
@ -149,7 +150,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
|
|||
|
||||
controller_t **known_controllers = controller_get_all();
|
||||
|
||||
while(true)
|
||||
for(;;)
|
||||
{
|
||||
addr_size = sizeof(their_addr);
|
||||
|
||||
|
@ -198,6 +199,8 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
|
|||
continue;
|
||||
}
|
||||
|
||||
LOGGER_DEBUG("received info for discovered controller\n");
|
||||
|
||||
uuid_t discovered_id;
|
||||
|
||||
mpack_tree_t tree;
|
||||
|
@ -225,6 +228,8 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
|
|||
{
|
||||
if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0)
|
||||
{
|
||||
LOGGER_DEBUG("rediscovered a known controller\n");
|
||||
|
||||
known_controllers[i]->active = 1;
|
||||
strncpy(known_controllers[i]->name, discovered_name, discovered_name_len);
|
||||
known_controllers[i]->name[discovered_name_len] = '\0';
|
||||
|
@ -246,6 +251,8 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
|
|||
|
||||
if(!found_discovered_in_list)
|
||||
{
|
||||
LOGGER_DEBUG("discovered a new controller\n");
|
||||
|
||||
controller_t *discovered_controller = malloc(sizeof(controller_t));
|
||||
discovered_controller->id = 0;
|
||||
strcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr));
|
||||
|
@ -299,9 +306,9 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
|
|||
}
|
||||
for(int i = 0; known_controllers[i] != NULL; i++)
|
||||
{
|
||||
LOGGER_DEBUG("lost controller %s at %s\n", known_controllers[i]->name, known_controllers[i]->ip);
|
||||
known_controllers[i]->active = false;
|
||||
controller_save(known_controllers[i]);
|
||||
LOGGER_DEBUG("lost: %s\n", known_controllers[i]->name);
|
||||
}
|
||||
controller_free_list(known_controllers);
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ api_v1_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_ar
|
|||
|
||||
for(int i = 0; all_relays[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(all_relays[i]));
|
||||
cJSON *json_relay = relay_to_json(all_relays[i]);
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
}
|
||||
|
|
|
@ -38,9 +38,10 @@ api_v1_relays_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, end
|
|||
|
||||
if(!relay)
|
||||
{
|
||||
LOGGER_DEBUG("failed to get relay %d for tag %s\n", relays_ids[i], args[0].value.v_str);
|
||||
continue;
|
||||
}
|
||||
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relay));
|
||||
cJSON *json_relay = relay_to_json(relay);
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
|
||||
|
|
|
@ -145,7 +145,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
|
|||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = cJSON_CreateRaw(schedule_to_json(new_schedule));
|
||||
json = schedule_to_json(new_schedule);
|
||||
|
||||
endpoint_response_json(response, 201, json);
|
||||
cJSON_Delete(json);
|
||||
|
@ -165,7 +165,7 @@ api_v1_schedules_GET(struct mg_connection *nc, struct http_message *hm, endpoint
|
|||
|
||||
for(int i = 0; all_schedules[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(all_schedules[i]));
|
||||
cJSON *json_schedule = schedule_to_json(all_schedules[i]);
|
||||
|
||||
cJSON_AddItemToArray(json, json_schedule);
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endp
|
|||
return;
|
||||
}
|
||||
|
||||
cJSON *json = cJSON_CreateRaw(schedule_to_json(schedule));
|
||||
cJSON *json = schedule_to_json(schedule);
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
|
@ -176,7 +176,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp
|
|||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = cJSON_CreateRaw(schedule_to_json(schedule));
|
||||
json = schedule_to_json(schedule);
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
|
|
|
@ -40,7 +40,7 @@ api_v1_schedules_tag_STR_GET(struct mg_connection *nc, struct http_message *hm,
|
|||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(schedule));
|
||||
cJSON *json_schedule = schedule_to_json(schedule);
|
||||
|
||||
cJSON_AddItemToArray(json, json_schedule);
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_
|
|||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relay));
|
||||
cJSON *json_relay = relay_to_json(relay);
|
||||
|
||||
cJSON_AddItemToArray(json_relays, json_relay);
|
||||
|
||||
|
@ -66,7 +66,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_
|
|||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(schedule));
|
||||
cJSON *json_schedule = schedule_to_json(schedule);
|
||||
|
||||
cJSON_AddItemToArray(json_schedules, json_schedule);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue