add: relay api endpoints

This commit is contained in:
Tobias Reisinger 2019-07-22 00:00:07 +02:00
parent a399050892
commit 9981eeabcb
10 changed files with 165 additions and 32 deletions

View file

@ -16,7 +16,7 @@ namespace api
METHOD_ADD(devices::get_relays_all, "/{1}/relays/", Get);
METHOD_ADD(devices::get_relays_one_by_id_and_num, "/{1}/relays/{2}", Get);
METHOD_ADD(devices::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, "filters::json_required");
METHOD_ADD(devices::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, "filters::json_required", "filters::relays::valid_json");
METHOD_LIST_END
static void post_discover(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);

View file

@ -1,6 +1,7 @@
#include <netdb.h>
#include <models/relay_dbo.h>
#include <helpers.h>
#include <models/device_dbo.h>
#include "api_v1_devices.h"
using namespace api::v1;
@ -9,7 +10,6 @@ void
devices::get_relays_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string& device_id)
{
relay_dbo **all_device_relays = relay_dbo::get_by_simple("device_id", (void *) device_id.c_str(), (intptr_t) sqlite3_bind_text);
Json::Value all_relays_json(Json::arrayValue);
@ -30,31 +30,15 @@ devices::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id,
int relay_num)
{
helpers::sql_filter_builder *filters[2];
helpers::sql_filter_builder filter
{
"number",
(void*)(intptr_t)relay_num,
(intptr_t)&sqlite3_bind_int,
"AND"
};
helpers::sql_filter_builder filter2
{
"device_id",
(void*)device_id.c_str(),
(intptr_t)sqlite3_bind_text,
";"
};
filters[0] = &filter;
filters[1] = &filter2;
relay_dbo *relay = relay_dbo::get_relay_for_device(device_id.c_str(), relay_num);
auto relays = relay_dbo::get_by(filters);
if(relays[0])
if(relay)
{
auto resp = HttpResponse::newHttpJsonResponse(relays[0]->to_json());
auto resp = HttpResponse::newHttpJsonResponse(relay->to_json());
callback(resp);
delete relay;
}
else
{
@ -63,7 +47,6 @@ devices::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
callback(resp);
}
relay_dbo::free_list(relays);
}
void
@ -71,5 +54,48 @@ devices::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id,
int relay_num)
{
if(!relay_dbo::valid_num_for_device(device_id.c_str(), relay_num))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
relay_dbo *relay = relay_dbo::get_relay_for_device(device_id.c_str(), relay_num);
Json::Value body = *req->getJsonObject();
bool db_action_result;
if(relay)
{
strncpy(relay->name, body["name"].asCString(), 128);
strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 33);
db_action_result = relay->update();
}
else
{
relay = new relay_dbo();
relay->number = relay_num;
strncpy(relay->name, body["name"].asCString(), 128);
strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 33);
strncpy(relay->device_id, device_id.c_str(), 33);
db_action_result = relay->insert();
}
if(!db_action_result)
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k500InternalServerError);
callback(resp);
}
else
{
auto resp = HttpResponse::newHttpJsonResponse(relay->to_json());
callback(resp);
}
delete relay;
}

View file

@ -0,0 +1,34 @@
#include <models/schedule_dbo.h>
#include "relays_valid_json.h"
using namespace drogon;
using namespace filters::relays;
void valid_json::doFilter(const HttpRequestPtr &req,
FilterCallback &&fcb,
FilterChainCallback &&fccb)
{
Json::Value body = *req->jsonObject();
bool is_valid = true;
is_valid &= body["name"].type() == Json::ValueType::stringValue;
is_valid &= body["active_schedule"].type() == Json::ValueType::stringValue;
if(is_valid)
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", body["active_schedule"].asCString(), (intptr_t)&sqlite3_bind_text);
bool schedule_found = schedules[0] != nullptr;
schedule_dbo::free_list(schedules);
if(schedule_found)
{
//Passed
fccb();
return;
}
}
//Check failed
auto res = drogon::HttpResponse::newHttpResponse();
res->setStatusCode(k400BadRequest);
fcb(res);
}

View file

@ -0,0 +1,20 @@
#pragma once
#include <drogon/HttpFilter.h>
using namespace drogon;
namespace filters
{
namespace relays
{
class valid_json : public HttpFilter<valid_json>
{
public:
valid_json() = default;
void doFilter(const HttpRequestPtr &req,
FilterCallback &&fcb,
FilterChainCallback &&fccb) override;
};
}
}

View file

@ -11,8 +11,6 @@ void valid_json::doFilter(const HttpRequestPtr &req,
bool is_valid = true;
is_valid &= body.isMember("name");
is_valid &= body.isMember("periods");
is_valid &= body["name"].type() == Json::ValueType::stringValue;
is_valid &= body["periods"].type() == Json::ValueType::arrayValue;

View file

@ -20,11 +20,13 @@ namespace helpers
period_list*
parse_periods(Json::Value periods_json);
typedef struct s_sql_filter_builder
typedef struct sql_filter_builder
{
sql_filter_builder(const char *col_name, const void *value, intptr_t bind_func, const char *logic);
const char *col_name;
const void *value;
const intptr_t bind_func;
intptr_t bind_func;
const char *logic;
} sql_filter_builder;

View file

@ -47,3 +47,12 @@ helpers::create_sql_filtered_query(const char *sql, sql_filter_builder **filters
return stmt;
}
helpers::sql_filter_builder::sql_filter_builder(const char *col_name, const void *value, intptr_t bind_func,
const char *logic)
{
this->col_name = col_name;
this->value = value;
this->bind_func = bind_func;
this->logic = logic;
}

View file

@ -32,7 +32,7 @@ static bool device_db_update_insert(device_dbo *device, sqlite3_stmt *stmt)
static device_dbo*
device_db_select_mapper(sqlite3_stmt *stmt)
{
auto *new_device = (device_dbo*)malloc(sizeof(device_dbo));
auto *new_device = new device_dbo();
for(int i = 0; i < sqlite3_column_count(stmt); i++)
{
const char *name = sqlite3_column_name(stmt, i);

View file

@ -5,6 +5,7 @@
#include <helpers.h>
#include "relay_dbo.h"
#include "globals.h"
#include "device_dbo.h"
static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
{
@ -31,7 +32,7 @@ static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
static relay_dbo*
relay_db_select_mapper(sqlite3_stmt *stmt)
{
auto *new_relay = (relay_dbo*)malloc(sizeof(relay_dbo));
auto *new_relay = new relay_dbo();
for(int i = 0; i < sqlite3_column_count(stmt); i++)
{
const char *name = sqlite3_column_name(stmt, i);
@ -120,7 +121,7 @@ relay_dbo::insert()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(id, number, name, active_schedule_id, device_id) values (?1, ?2, ?3, ?4, ?5);", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, active_schedule_id, device_id) values (?2, ?3, ?4, ?5);", -1, &stmt, nullptr);
return relay_db_update_insert(this, stmt);
}
@ -187,6 +188,43 @@ relay_dbo::get_by(helpers::sql_filter_builder **filters)
return relay_db_select(stmt);
}
relay_dbo*
relay_dbo::get_relay_for_device(const char *device_id, int relay_num)
{
helpers::sql_filter_builder *filters[2];
helpers::sql_filter_builder filter(
"number",
(void*)(intptr_t)relay_num,
(intptr_t)&sqlite3_bind_int,
"AND"
);
helpers::sql_filter_builder filter2(
"device_id",
(void*)device_id,
(intptr_t)sqlite3_bind_text,
";"
);
filters[0] = &filter;
filters[1] = &filter2;
auto relays = relay_dbo::get_by(filters);
relay_dbo *relay = relays[0];
free(relays);
return relay;
}
bool
relay_dbo::valid_num_for_device(const char *search_device_id, int relay_num)
{
device_dbo **devices = device_dbo::get_by_simple("id", search_device_id, (intptr_t)&sqlite3_bind_text);
bool valid_id_and_num = devices[0] && devices[0]->relay_count > relay_num;
device_dbo::free_list(devices);
return valid_id_and_num;
}
void
relay_dbo::free_list(relay_dbo **relays_list)
{

View file

@ -37,6 +37,12 @@ public:
static relay_dbo**
get_by(helpers::sql_filter_builder **filters);
static relay_dbo*
get_relay_for_device(const char *device_id, int relay_num);
static bool
valid_num_for_device(const char *search_device_id, int relay_num);
static relay_dbo**
get_all();
};