2019-07-22 20:06:13 +00:00
|
|
|
#include <models/controller_dbo.h>
|
2019-08-04 21:44:38 +00:00
|
|
|
#include <config.h>
|
2019-07-22 20:06:13 +00:00
|
|
|
#include "api_v1_controllers.h"
|
|
|
|
using namespace api::v1;
|
|
|
|
|
|
|
|
void
|
|
|
|
controllers::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
|
|
|
|
{
|
|
|
|
controller_dbo **all_controllers = controller_dbo::get_all();
|
|
|
|
Json::Value all_controllers_json(Json::arrayValue);
|
|
|
|
|
|
|
|
for(int i = 0; all_controllers[i] != nullptr; i++)
|
|
|
|
{
|
|
|
|
all_controllers_json.append(all_controllers[i]->to_json());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(all_controllers_json);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
|
|
|
|
controller_dbo::free_list(all_controllers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-24 21:33:35 +00:00
|
|
|
controllers::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
2020-02-23 19:06:14 +00:00
|
|
|
const std::string& controller_id_str)
|
2019-07-22 20:06:13 +00:00
|
|
|
{
|
2020-02-23 19:06:14 +00:00
|
|
|
uuid_t controller_id;
|
|
|
|
if(uuid_parse(controller_id_str.c_str(), controller_id))
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k400BadRequest);
|
|
|
|
callback(resp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
2019-07-22 20:06:13 +00:00
|
|
|
|
|
|
|
if(controllers[0])
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(controllers[0]->to_json());
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
2019-09-24 21:33:35 +00:00
|
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
controller_dbo::free_list(controllers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
controllers::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
|
|
|
const std::string &controller_tag)
|
|
|
|
{
|
2020-02-23 19:06:14 +00:00
|
|
|
controller_dbo **controllers = controller_dbo::get_by_simple("tag", controller_tag.c_str(), (intptr_t) &sqlite3_bind_text, -1);
|
2019-09-24 21:33:35 +00:00
|
|
|
|
|
|
|
if(controllers[0])
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(controllers[0]->to_json());
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
2019-07-22 20:06:13 +00:00
|
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
controller_dbo::free_list(controllers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-23 19:06:14 +00:00
|
|
|
controllers::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str)
|
2019-07-22 20:06:13 +00:00
|
|
|
{
|
2020-02-23 19:06:14 +00:00
|
|
|
uuid_t controller_id;
|
|
|
|
if(uuid_parse(controller_id_str.c_str(), controller_id))
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k400BadRequest);
|
|
|
|
callback(resp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
2019-07-22 20:06:13 +00:00
|
|
|
|
|
|
|
if(controllers[0])
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
if(!controllers[0]->remove())
|
|
|
|
{
|
|
|
|
resp->setStatusCode(k500InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
controller_dbo::free_list(controllers);
|
|
|
|
}
|
2019-07-29 21:59:16 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
controllers::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
2020-02-23 19:06:14 +00:00
|
|
|
const std::string &controller_id_str)
|
2019-07-29 21:59:16 +00:00
|
|
|
{
|
|
|
|
Json::Value body = *req->getJsonObject();
|
|
|
|
|
2020-02-23 19:06:14 +00:00
|
|
|
uuid_t controller_id;
|
|
|
|
if(uuid_parse(controller_id_str.c_str(), controller_id))
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k400BadRequest);
|
|
|
|
callback(resp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
2019-07-29 21:59:16 +00:00
|
|
|
|
|
|
|
if(controllers[0])
|
|
|
|
{
|
2019-07-30 11:22:19 +00:00
|
|
|
strncpy(controllers[0]->name, body["name"].asCString(), 127);
|
|
|
|
strncpy(controllers[0]->ip, body["ip"].asCString(), 16);
|
2019-07-29 21:59:16 +00:00
|
|
|
|
|
|
|
controllers[0]->name[127] = '\0';
|
|
|
|
controllers[0]->ip[16] = '\0';
|
|
|
|
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
if(controllers[0]->update())
|
|
|
|
{
|
2019-08-04 21:44:38 +00:00
|
|
|
controllers[0]->command(config::command_code_set_name, controllers[0]->name);
|
2019-07-29 21:59:16 +00:00
|
|
|
resp = HttpResponse::newHttpJsonResponse(controllers[0]->to_json());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resp->setStatusCode(k500InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
controller_dbo::free_list(controllers);
|
2020-02-23 19:06:14 +00:00
|
|
|
}
|