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-22 20:06:13 +00:00
|
|
|
#include <models/controller_dbo.h>
|
2019-07-29 20:02:38 +00:00
|
|
|
#include <models/schedule_dbo.h>
|
|
|
|
#include <config.h>
|
2019-07-22 20:06:13 +00:00
|
|
|
#include "api_v1_controllers.h"
|
2019-07-20 21:33:17 +00:00
|
|
|
|
|
|
|
using namespace api::v1;
|
|
|
|
|
|
|
|
void
|
2019-07-22 20:06:13 +00:00
|
|
|
controllers::get_relays_all(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-20 21:33:17 +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;
|
|
|
|
}
|
|
|
|
relay_dbo **all_controller_relays = relay_dbo::get_by_simple("controller_id", (void *) controller_id, (intptr_t) sqlite3_bind_blob, sizeof(uuid_t));
|
2019-07-20 21:33:17 +00:00
|
|
|
Json::Value all_relays_json(Json::arrayValue);
|
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
for(int i = 0; all_controller_relays[i] != nullptr; i++)
|
2019-07-20 21:33:17 +00:00
|
|
|
{
|
2019-07-22 20:06:13 +00:00
|
|
|
all_relays_json.append(all_controller_relays[i]->to_json());
|
2019-07-20 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(all_relays_json);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
|
2019-07-22 20:06:13 +00:00
|
|
|
relay_dbo::free_list(all_controller_relays);
|
2019-07-20 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-22 20:06:13 +00:00
|
|
|
controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
2020-02-23 19:06:14 +00:00
|
|
|
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str,
|
2019-07-20 21:33:17 +00:00
|
|
|
int relay_num)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num);
|
2019-07-21 22:00:07 +00:00
|
|
|
|
|
|
|
if(relay)
|
2019-07-20 21:33:17 +00:00
|
|
|
{
|
2019-07-21 22:00:07 +00:00
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(relay->to_json());
|
2019-07-20 21:33:17 +00:00
|
|
|
|
|
|
|
callback(resp);
|
2019-07-21 22:00:07 +00:00
|
|
|
|
|
|
|
delete relay;
|
2019-07-20 21:33:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-22 20:06:13 +00:00
|
|
|
controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
2020-02-23 19:06:14 +00:00
|
|
|
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str,
|
2019-07-20 21:33:17 +00:00
|
|
|
int relay_num)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
if(!relay_dbo::valid_num_for_controller(controller_id, relay_num))
|
2019-07-21 22:00:07 +00:00
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k400BadRequest);
|
|
|
|
callback(resp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value body = *req->getJsonObject();
|
2020-02-23 19:06:14 +00:00
|
|
|
uuid_t active_schedule_id;
|
|
|
|
if(!schedule_dbo::parse_id(body["active_schedule"].asCString(), active_schedule_id))
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k400BadRequest);
|
|
|
|
callback(resp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num);
|
2019-07-21 22:00:07 +00:00
|
|
|
|
|
|
|
bool db_action_result;
|
|
|
|
|
|
|
|
if(relay)
|
|
|
|
{
|
2019-07-30 11:22:19 +00:00
|
|
|
strncpy(relay->name, body["name"].asCString(), 127);
|
2020-02-23 19:06:14 +00:00
|
|
|
uuid_copy(relay->active_schedule_id, active_schedule_id);
|
2019-07-21 22:00:07 +00:00
|
|
|
|
|
|
|
db_action_result = relay->update();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relay = new relay_dbo();
|
|
|
|
relay->number = relay_num;
|
2019-07-30 11:22:19 +00:00
|
|
|
strncpy(relay->name, body["name"].asCString(), 127);
|
2020-02-23 19:06:14 +00:00
|
|
|
|
|
|
|
uuid_copy(relay->active_schedule_id, active_schedule_id);
|
|
|
|
uuid_copy(relay->controller_id, controller_id);
|
2019-07-21 22:00:07 +00:00
|
|
|
|
2019-09-08 17:34:37 +00:00
|
|
|
relay->reload_active_schedule();
|
|
|
|
|
2019-07-21 22:00:07 +00:00
|
|
|
db_action_result = relay->insert();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!db_action_result)
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k500InternalServerError);
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-23 19:06:14 +00:00
|
|
|
auto schedules = schedule_dbo::get_by_simple("id", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
|
|
|
auto controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
2019-07-29 20:02:38 +00:00
|
|
|
|
|
|
|
Json::Value payload;
|
|
|
|
payload["target"] = relay_num;
|
|
|
|
payload["schedule"] = schedules[0]->to_json();
|
|
|
|
|
|
|
|
Json::StreamWriterBuilder json_writer;
|
|
|
|
|
|
|
|
controllers[0]->command(config::command_code_set_schedule, Json::writeString(json_writer, payload).c_str());
|
|
|
|
|
2019-07-21 22:00:07 +00:00
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(relay->to_json());
|
|
|
|
callback(resp);
|
2019-07-29 20:02:38 +00:00
|
|
|
|
|
|
|
schedule_dbo::free_list(schedules);
|
|
|
|
controller_dbo::free_list(controllers);
|
2019-07-21 22:00:07 +00:00
|
|
|
}
|
2019-07-20 21:33:17 +00:00
|
|
|
|
2019-07-21 22:00:07 +00:00
|
|
|
delete relay;
|
2020-02-23 19:06:14 +00:00
|
|
|
}
|