2019-07-20 21:33:17 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <models/relay_dbo.h>
|
2019-07-20 22:29:05 +00:00
|
|
|
#include <helpers.h>
|
2019-07-20 21:33:17 +00:00
|
|
|
#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)
|
|
|
|
{
|
|
|
|
|
2019-07-20 22:29:05 +00:00
|
|
|
relay_dbo **all_device_relays = relay_dbo::get_by_simple("device_id", (void *) device_id.c_str(), (intptr_t) sqlite3_bind_text);
|
2019-07-20 21:33:17 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-07-20 22:29:05 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
auto relays = relay_dbo::get_by(filters);
|
2019-07-20 21:33:17 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|