add: basic relay structure

add: improved sql calls
This commit is contained in:
Tobias Reisinger 2019-07-20 23:33:17 +02:00
parent a38a6e63b3
commit ac61c26d56
12 changed files with 426 additions and 77 deletions

View file

@ -24,15 +24,13 @@ devices::get_all(const HttpRequestPtr &req, std::function<void(const HttpRespons
void
devices::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id)
{
device_dbo *device = device_dbo::get_one_by("id", device_id.c_str());
device_dbo **devices = device_dbo::get_by_simple("id", device_id.c_str(), (intptr_t) &sqlite3_bind_text);
if(device)
if(devices[0])
{
auto resp = HttpResponse::newHttpJsonResponse(device->to_json());
auto resp = HttpResponse::newHttpJsonResponse(devices[0]->to_json());
callback(resp);
free(device);
}
else
{
@ -41,24 +39,23 @@ devices::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpR
callback(resp);
}
device_dbo::free_list(devices);
}
void
devices::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id)
{
device_dbo *device = device_dbo::get_one_by("id", device_id.c_str());
device_dbo **devices = device_dbo::get_by_simple("id", device_id.c_str(), (intptr_t) &sqlite3_bind_text);
if(device)
if(devices[0])
{
auto resp = HttpResponse::newHttpResponse();
if(!device->remove())
if(!devices[0]->remove())
{
resp->setStatusCode(k500InternalServerError);
}
callback(resp);
free(device);
}
else
{
@ -67,4 +64,5 @@ devices::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const Ht
callback(resp);
}
device_dbo::free_list(devices);
}

View file

@ -10,19 +10,23 @@ namespace api
public:
METHOD_LIST_BEGIN
METHOD_ADD(devices::post_discover, "/discover", Post);
METHOD_ADD(devices::get_all,"/",Get);
METHOD_ADD(devices::get_one_by_id,"/{1}",Get);
METHOD_ADD(devices::delete_one_by_id,"/{1}",Delete);
//METHOD_ADD(Devices::get_relays_all,"/{1}/relays",Get);
//METHOD_ADD(Devices::get_relays_one,"/{1}/relays/{2}",Get);
METHOD_ADD(devices::get_all, "/", Get);
METHOD_ADD(devices::get_one_by_id, "/{1}", Get);
METHOD_ADD(devices::delete_one_by_id, "/{1}", Delete);
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_LIST_END
static void post_discover(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback);
static void get_all(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback);
static void get_one_by_id(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,const std::string& device_id);
static void delete_one_by_id(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,const std::string& device_id);
//void get_relays_all(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string device_id);
//void get_relays_one(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string device_id,std::string relay_id);
static void post_discover(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
static void get_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& device_id);
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& device_id);
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& device_id);
static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& device_id, int relay_num);
static void put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& device_id, int relay_num);
};
}
}

View file

@ -0,0 +1,57 @@
#include <netdb.h>
#include <models/relay_dbo.h>
#include "api_v1_devices.h"
using namespace api::v1;
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);
for(int i = 0; all_device_relays[i] != nullptr; i++)
{
all_relays_json.append(all_device_relays[i]->to_json());
}
auto resp = HttpResponse::newHttpJsonResponse(all_relays_json);
callback(resp);
relay_dbo::free_list(all_device_relays);
}
void
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)
{
relay_dbo **relays = relay_dbo::get_by_simple("id", (void *) (intptr_t) relay_num, (intptr_t) &sqlite3_bind_int);
if(relays[0])
{
auto resp = HttpResponse::newHttpJsonResponse(relays[0]->to_json());
callback(resp);
}
else
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k404NotFound);
callback(resp);
}
relay_dbo::free_list(relays);
}
void
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)
{
}

View file

@ -27,15 +27,13 @@ schedules::get_all(const HttpRequestPtr &req, std::function<void(const HttpRespo
void
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
{
schedule_dbo *schedule = schedule_dbo::get_one_by("id", schedule_id.c_str());
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
if(schedule)
if(schedules[0])
{
auto resp = HttpResponse::newHttpJsonResponse(schedule->to_json());
auto resp = HttpResponse::newHttpJsonResponse(schedules[0]->to_json());
callback(resp);
delete schedule;
}
else
{
@ -44,24 +42,23 @@ schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
callback(resp);
}
schedule_dbo::free_list(schedules);
}
void
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
{
schedule_dbo *schedule = schedule_dbo::get_one_by("id", schedule_id.c_str());
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
if(schedule)
if(schedules[0])
{
auto resp = HttpResponse::newHttpResponse();
if(!schedule->remove())
if(!schedules[0]->remove())
{
resp->setStatusCode(k500InternalServerError);
}
callback(resp);
delete schedule;
}
else
{
@ -70,6 +67,7 @@ schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const
callback(resp);
}
schedule_dbo::free_list(schedules);
}
void
@ -99,20 +97,21 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
}
void
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string &schedule_id)
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &schedule_id)
{
Json::Value body = *req->jsonObject();
schedule_dbo *schedule = schedule_dbo::get_one_by("id", schedule_id.c_str());
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
if(schedule)
if(schedules[0])
{
strncpy(schedule->name, body["name"].asCString(), 127);
schedule->name[127] = '\0';
delete schedule->periods;
schedule->periods = helpers::parse_periods(body["periods"]);
strncpy(schedules[0]->name, body["name"].asCString(), 127);
schedules[0]->name[127] = '\0';
delete schedules[0]->periods;
schedules[0]->periods = helpers::parse_periods(body["periods"]);
if(!schedule->update())
if(!schedules[0]->update())
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k500InternalServerError);
@ -120,11 +119,9 @@ schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
}
else
{
auto resp = HttpResponse::newHttpJsonResponse(schedule->to_json());
auto resp = HttpResponse::newHttpJsonResponse(schedules[0]->to_json());
callback(resp);
}
delete schedule;
}
else
{
@ -133,5 +130,5 @@ schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
callback(resp);
}
schedule_dbo::free_list(schedules);
}