add: pulse endpoint

This commit is contained in:
Tobias Reisinger 2020-06-27 18:31:36 +02:00
parent c41dd7e9fa
commit fb66480873
9 changed files with 135 additions and 3 deletions

View file

@ -0,0 +1,68 @@
#include <cJSON.h>
#include <macros.h>
#include <command.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/controller.h>
#include <models/relay.h>
#include <models/tag.h>
void
api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)hm;
(void)nc;
uuid_t target_uid;
if(uuid_parse(args[0].value.v_str, target_uid))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
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)
{
LOG_DEBUG("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str);
static const char content[] = "no relay for this controller found";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
uint8_t duration = 0;
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
if(json)
{
cJSON *json_duration = cJSON_GetObjectItemCaseSensitive(json, "duration");
if(cJSON_IsNumber(json_duration) && json_duration->valueint)
{
duration = json_duration->valueint & 0xFF;
}
cJSON_Delete(json);
}
command_pulse(relay, duration);
endpoint_response_text(response, 200, "", 0);
relay_free(relay);
controller_free(controller);
}