core-legacy/controllers/api_v1_controllers_relays.cc

269 lines
8.3 KiB
C++
Raw Normal View History

#include <netdb.h>
#include <models/relay_dbo.h>
2019-07-20 22:29:05 +00:00
#include <helpers.h>
2020-04-23 23:33:34 +00:00
#include <mpack.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>
2020-04-28 19:50:19 +00:00
#include <models/junction_tag_dbo.h>
#include <models/tag_dbo.h>
2019-07-29 20:02:38 +00:00
#include <config.h>
2020-04-18 19:29:52 +00:00
#include <enums.h>
2019-07-22 20:06:13 +00:00
#include "api_v1_controllers.h"
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,
const std::string& controller_id_str)
{
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));
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-22 20:06:13 +00:00
all_relays_json.append(all_controller_relays[i]->to_json());
}
auto resp = HttpResponse::newHttpJsonResponse(all_relays_json);
callback(resp);
2019-07-22 20:06:13 +00:00
relay_dbo::free_list(all_controller_relays);
}
void
2019-07-22 20:06:13 +00:00
controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str,
int relay_num)
{
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-21 22:00:07 +00:00
auto resp = HttpResponse::newHttpJsonResponse(relay->to_json());
callback(resp);
2019-07-21 22:00:07 +00:00
delete relay;
}
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,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str,
int relay_num)
{
uuid_t controller_id;
if(uuid_parse(controller_id_str.c_str(), controller_id))
{
2020-04-18 19:29:52 +00:00
LOG_DEBUG << "bad uuid";
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
{
2020-04-18 19:29:52 +00:00
LOG_DEBUG << "invalid num for controller";
2019-07-21 22:00:07 +00:00
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
Json::Value body = *req->getJsonObject();
2020-04-28 19:50:19 +00:00
bool set_name = body["name"].type() == Json::ValueType::stringValue;
bool set_tags = body["tags"].type() == Json::ValueType::arrayValue;
bool set_schedules = body["schedules"].type() == Json::ValueType::arrayValue;
bool set_active_schedule = body["active_schedule"].type() == Json::ValueType::objectValue;
2020-04-28 19:50:19 +00:00
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num);
schedule_dbo **schedule_list;
schedule_dbo *active_schedule;
schedule_dbo *schedules[7];
if(set_schedules)
2020-04-23 15:00:12 +00:00
{
2020-04-28 19:50:19 +00:00
uuid_t schedules_ids[7];
for(int i = 0; i < 7; ++i)
2020-04-23 15:00:12 +00:00
{
2020-04-28 19:50:19 +00:00
if(schedule_dbo::parse_uid(body["schedules"][i]["id"].asCString(), schedules_ids[i]))
{
LOG_DEBUG << "parse_uid failed for schedule " << i;
auto res = drogon::HttpResponse::newHttpResponse();
res->setStatusCode(k400BadRequest);
callback(res);
return;
}
}
for(int i = 0; i < 7; ++i)
{
schedule_list = schedule_dbo::get_by_simple("uid", schedules_ids[i], (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
schedules[i] = schedule_list[0];
free(schedule_list);
2020-04-23 15:00:12 +00:00
}
}
2020-04-28 19:50:19 +00:00
if(set_active_schedule)
2020-04-23 15:00:12 +00:00
{
2020-04-28 19:50:19 +00:00
uuid_t active_schedule_id;
if(schedule_dbo::parse_uid(body["active_schedule"]["id"].asCString(), active_schedule_id))
{
LOG_DEBUG << "bad active_schedule uuid";
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
schedule_list = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
active_schedule = schedule_list[0];
2020-04-23 15:00:12 +00:00
free(schedule_list);
}
2019-07-21 22:00:07 +00:00
2020-04-28 19:50:19 +00:00
if(!relay)
2019-07-21 22:00:07 +00:00
{
2020-04-28 19:50:19 +00:00
relay = new relay_dbo();
relay->number = relay_num;
2020-04-19 00:44:35 +00:00
uuid_copy(relay->controller_id, controller_id);
2020-04-23 15:00:12 +00:00
for(int i = 0; i < 7; ++i)
{
2020-04-28 19:50:19 +00:00
relay->schedules[i] = schedule_dbo::get_by_id_or_off(0);
2020-04-23 15:00:12 +00:00
}
2019-07-21 22:00:07 +00:00
}
2020-04-28 19:50:19 +00:00
if(set_name)
2019-07-21 22:00:07 +00:00
{
2019-07-30 11:22:19 +00:00
strncpy(relay->name, body["name"].asCString(), 127);
2020-04-28 19:50:19 +00:00
}
2020-04-28 19:50:19 +00:00
if(set_tags)
{
junction_tag_dbo::remove_for_relay(relay->id);
for(int i = 0; i < body["tags"].size(); ++i)
{
const char *tag = body["tags"][i].asCString();
int tag_id = tag_dbo::get_id(tag);
if(tag_id == 0)
{
tag_dbo::save(tag_id, tag);
tag_id = tag_dbo::get_id(tag);
}
junction_tag_dbo::insert(tag_id, relay->id, 0);
}
}
if(set_schedules)
{
2020-04-23 15:00:12 +00:00
for(int i = 0; i < 7; ++i)
{
relay->schedules[i] = schedules[i];
}
2020-04-28 19:50:19 +00:00
relay->active_schedule = schedules[helpers::get_day_of_week()];
}
2020-04-23 15:00:12 +00:00
2020-04-28 19:50:19 +00:00
if(set_active_schedule)
{
2020-04-23 15:00:12 +00:00
relay->schedules[helpers::get_day_of_week()] = active_schedule;
relay->active_schedule = active_schedule;
2019-07-21 22:00:07 +00:00
}
2020-04-28 19:50:19 +00:00
if(!relay->save())
2019-07-21 22:00:07 +00:00
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k500InternalServerError);
callback(resp);
}
else
{
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
2020-04-23 23:33:34 +00:00
char* data;
size_t size;
mpack_writer_t writer;
mpack_writer_init_growable(&writer, &data, &size);
2020-04-24 22:49:32 +00:00
// 3 = code, relay num, relay name, schedules(array)
mpack_start_map(&writer, 3);
2020-04-23 23:33:34 +00:00
mpack_write_uint(&writer, COMMAND_MAPPING_CODE);
mpack_write_u8(&writer, config::command_code_set_schedule);
2020-04-24 22:49:32 +00:00
2020-04-23 23:33:34 +00:00
mpack_write_uint(&writer, COMMAND_MAPPING_RELAY_NUM);
2020-04-24 22:49:32 +00:00
mpack_write_u8(&writer, relay->number);
mpack_write_uint(&writer, COMMAND_MAPPING_SCHEDULES_ARRAY);
// 7 = days of week
mpack_start_array(&writer, 7);
for(int i = 0; i < 7; ++i)
{
uint16_t *periods = relay->schedules[i]->periods->to_blob();
uint16_t periods_count = periods[0];
// 3 = code, relaynum, schedules(array)
mpack_start_map(&writer, 3);
mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_COUNT);
mpack_write_u16(&writer, periods_count);
mpack_write_uint(&writer, COMMAND_MAPPING_SCHEDULE_ID);
2020-04-28 19:50:19 +00:00
mpack_write_bin(&writer, (char*)relay->schedules[0]->uid, sizeof(uuid_t));
2020-04-24 22:49:32 +00:00
mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_BLOB);
// periods + 1 to skip length in periods[0]
// periods_count * 2 because each uint16_t is a timestamp. 2 are start and end
mpack_write_bin(&writer, (char*)(periods + 1), sizeof(uint16_t) * periods_count * 2);
mpack_finish_map(&writer);
free(periods);
}
mpack_finish_array(&writer);
2020-04-23 23:33:34 +00:00
mpack_finish_map(&writer);
// finish writing
if (mpack_writer_destroy(&writer) != mpack_ok) {
LOG_ERROR << "an error occurred encoding the data";
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k500InternalServerError);
callback(resp);
return;
}
2020-04-18 19:29:52 +00:00
2020-04-23 23:33:34 +00:00
controllers[0]->command(config::command_code_set_name, data, size);
2020-04-18 19:29:52 +00:00
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
controller_dbo::free_list(controllers);
2019-07-21 22:00:07 +00:00
}
2019-07-21 22:00:07 +00:00
delete relay;
}