ac61c26d56
add: improved sql calls
57 lines
No EOL
1.7 KiB
C++
57 lines
No EOL
1.7 KiB
C++
#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)
|
|
{
|
|
|
|
} |