fix: save id as blob, not as string
add: some progress to new discovery add: some tests remove: migrations. restarting for now
This commit is contained in:
parent
b4eec336a2
commit
51ab1d7982
22 changed files with 345 additions and 121 deletions
|
@ -63,14 +63,22 @@ configure_file("config.json" "config.json" COPYONLY)
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
add_executable(core ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${VIEWSRC} ${PLUGIN_SRC} ${MODEL_SRC} ${HELPER_SRC})
|
add_executable(core ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${VIEWSRC} ${PLUGIN_SRC} ${MODEL_SRC} ${HELPER_SRC})
|
||||||
|
|
||||||
add_custom_target(compiled_migrations ${CMAKE_CURRENT_SOURCE_DIR}/compile_migrations.sh)
|
|
||||||
add_dependencies(core compiled_migrations)
|
|
||||||
|
|
||||||
add_subdirectory(drogon)
|
add_subdirectory(drogon)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE drogon)
|
target_link_libraries(${PROJECT_NAME} PRIVATE drogon)
|
||||||
|
|
||||||
|
add_custom_target(migrations
|
||||||
|
COMMAND ./compile_migrations.sh
|
||||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
add_custom_target(run
|
add_custom_target(run
|
||||||
COMMAND core
|
COMMAND core
|
||||||
DEPENDS core
|
DEPENDS core
|
||||||
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_custom_target(test
|
||||||
|
COMMAND tavern-ci --tavern-beta-new-traceback ./tavern_tests
|
||||||
|
DEPENDS core
|
||||||
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||||
|
)
|
||||||
|
|
|
@ -23,9 +23,17 @@ controllers::get_all(const HttpRequestPtr &req, std::function<void(const HttpRes
|
||||||
|
|
||||||
void
|
void
|
||||||
controllers::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
controllers::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||||
const std::string& controller_id)
|
const std::string& controller_id_str)
|
||||||
{
|
{
|
||||||
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
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));
|
||||||
|
|
||||||
if(controllers[0])
|
if(controllers[0])
|
||||||
{
|
{
|
||||||
|
@ -47,7 +55,7 @@ void
|
||||||
controllers::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
controllers::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||||
const std::string &controller_tag)
|
const std::string &controller_tag)
|
||||||
{
|
{
|
||||||
controller_dbo **controllers = controller_dbo::get_by_simple("tag", controller_tag.c_str(), (intptr_t) &sqlite3_bind_text);
|
controller_dbo **controllers = controller_dbo::get_by_simple("tag", controller_tag.c_str(), (intptr_t) &sqlite3_bind_text, -1);
|
||||||
|
|
||||||
if(controllers[0])
|
if(controllers[0])
|
||||||
{
|
{
|
||||||
|
@ -66,9 +74,17 @@ controllers::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
controllers::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id)
|
controllers::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str)
|
||||||
{
|
{
|
||||||
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
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));
|
||||||
|
|
||||||
if(controllers[0])
|
if(controllers[0])
|
||||||
{
|
{
|
||||||
|
@ -92,11 +108,19 @@ controllers::delete_one_by_id(const HttpRequestPtr &req, std::function<void(cons
|
||||||
|
|
||||||
void
|
void
|
||||||
controllers::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
controllers::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||||
const std::string &controller_id)
|
const std::string &controller_id_str)
|
||||||
{
|
{
|
||||||
Json::Value body = *req->getJsonObject();
|
Json::Value body = *req->getJsonObject();
|
||||||
|
|
||||||
controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
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));
|
||||||
|
|
||||||
if(controllers[0])
|
if(controllers[0])
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,13 +21,13 @@ namespace api::v1
|
||||||
|
|
||||||
static void post_discover(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
|
static void post_discover(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
|
||||||
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
|
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
|
||||||
static void get_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
static void get_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id_str);
|
||||||
static void get_one_by_tag(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_tag);
|
static void get_one_by_tag(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_tag);
|
||||||
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id_str);
|
||||||
static void put_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
static void put_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
||||||
|
|
||||||
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id_str);
|
||||||
static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num);
|
static void 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);
|
||||||
static void put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num);
|
static void 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);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,14 @@
|
||||||
|
|
||||||
using namespace api::v1;
|
using namespace api::v1;
|
||||||
|
|
||||||
|
enum DISCOVERY_MAPPING
|
||||||
|
{
|
||||||
|
DISCOVERY_MAPPING_ID = 0,
|
||||||
|
DISCOVERY_MAPPING_NAME = 1,
|
||||||
|
DISCOVERY_MAPPING_COMMAND_PORT = 2,
|
||||||
|
DISCOVERY_MAPPING_RELAY_COUNT = 3,
|
||||||
|
};
|
||||||
|
|
||||||
void controllers::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
|
void controllers::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -95,12 +103,13 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
uuid_t uuid;
|
uuid_t discovered_id;
|
||||||
int uuid_size = sizeof(uuid_t);
|
int id_size = sizeof(uuid_t);
|
||||||
memcpy(&uuid, binn_object_blob(answer_payload, "uuid", &uuid_size), uuid_size);
|
memcpy(&discovered_id, binn_map_blob(answer_payload, DISCOVERY_MAPPING_ID, &id_size), id_size);
|
||||||
|
//strncpy(&id, binn_map_blob(answer_payload, DISCOVERY_MAPPING_ID, &uuid_size), uuid_size);
|
||||||
|
|
||||||
char uuid_str[38];
|
char uuid_str[38];
|
||||||
uuid_unparse(uuid, uuid_str);
|
uuid_unparse(discovered_id, uuid_str);
|
||||||
LOG_DEBUG << uuid_str;
|
LOG_DEBUG << uuid_str;
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -108,15 +117,13 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
|
||||||
|
|
||||||
Json::Value client_info;
|
Json::Value client_info;
|
||||||
|
|
||||||
const char *discovered_id = client_info["id"].asCString();
|
|
||||||
|
|
||||||
bool found_discovered_in_list = false;
|
bool found_discovered_in_list = false;
|
||||||
|
|
||||||
for(int i = 0; known_controllers[i] != nullptr; i++)
|
for(int i = 0; known_controllers[i] != nullptr; i++)
|
||||||
{
|
{
|
||||||
if(!found_discovered_in_list)
|
if(!found_discovered_in_list)
|
||||||
{
|
{
|
||||||
if(strcmp(known_controllers[i]->id, discovered_id) == 0)
|
if(uuid_compare(known_controllers[i]->id, discovered_id) == 0)
|
||||||
{
|
{
|
||||||
known_controllers[i]->active = true;
|
known_controllers[i]->active = true;
|
||||||
known_controllers[i]->update();
|
known_controllers[i]->update();
|
||||||
|
@ -135,7 +142,7 @@ void controllers::post_discover(const HttpRequestPtr &req, std::function<void(co
|
||||||
{
|
{
|
||||||
controller_dbo discovered_controller{};
|
controller_dbo discovered_controller{};
|
||||||
strcpy(discovered_controller.ip, inet_ntoa(addr.sin_addr));
|
strcpy(discovered_controller.ip, inet_ntoa(addr.sin_addr));
|
||||||
strcpy(discovered_controller.id, discovered_id);
|
memcpy(discovered_controller.id, discovered_id, sizeof(uuid_t));
|
||||||
strcpy(discovered_controller.name, client_info["name"].asCString());
|
strcpy(discovered_controller.name, client_info["name"].asCString());
|
||||||
discovered_controller.relay_count = client_info["relay_count"].asInt();
|
discovered_controller.relay_count = client_info["relay_count"].asInt();
|
||||||
discovered_controller.port = client_info["port"].asInt();
|
discovered_controller.port = client_info["port"].asInt();
|
||||||
|
|
|
@ -10,9 +10,17 @@ using namespace api::v1;
|
||||||
|
|
||||||
void
|
void
|
||||||
controllers::get_relays_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
controllers::get_relays_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||||
const std::string& controller_id)
|
const std::string& controller_id_str)
|
||||||
{
|
{
|
||||||
relay_dbo **all_controller_relays = relay_dbo::get_by_simple("controller_id", (void *) controller_id.c_str(), (intptr_t) sqlite3_bind_text);
|
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);
|
Json::Value all_relays_json(Json::arrayValue);
|
||||||
|
|
||||||
for(int i = 0; all_controller_relays[i] != nullptr; i++)
|
for(int i = 0; all_controller_relays[i] != nullptr; i++)
|
||||||
|
@ -29,10 +37,18 @@ controllers::get_relays_all(const HttpRequestPtr &req, std::function<void(const
|
||||||
|
|
||||||
void
|
void
|
||||||
controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id,
|
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str,
|
||||||
int relay_num)
|
int relay_num)
|
||||||
{
|
{
|
||||||
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id.c_str(), 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);
|
||||||
|
|
||||||
if(relay)
|
if(relay)
|
||||||
{
|
{
|
||||||
|
@ -53,10 +69,18 @@ controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
|
|
||||||
void
|
void
|
||||||
controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id,
|
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id_str,
|
||||||
int relay_num)
|
int relay_num)
|
||||||
{
|
{
|
||||||
if(!relay_dbo::valid_num_for_controller(controller_id.c_str(), 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;
|
||||||
|
}
|
||||||
|
if(!relay_dbo::valid_num_for_controller(controller_id, relay_num))
|
||||||
{
|
{
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
resp->setStatusCode(k400BadRequest);
|
resp->setStatusCode(k400BadRequest);
|
||||||
|
@ -64,15 +88,24 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id.c_str(), relay_num);
|
|
||||||
Json::Value body = *req->getJsonObject();
|
Json::Value body = *req->getJsonObject();
|
||||||
|
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);
|
||||||
|
|
||||||
bool db_action_result;
|
bool db_action_result;
|
||||||
|
|
||||||
if(relay)
|
if(relay)
|
||||||
{
|
{
|
||||||
strncpy(relay->name, body["name"].asCString(), 127);
|
strncpy(relay->name, body["name"].asCString(), 127);
|
||||||
strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 32);
|
uuid_copy(relay->active_schedule_id, active_schedule_id);
|
||||||
|
|
||||||
db_action_result = relay->update();
|
db_action_result = relay->update();
|
||||||
}
|
}
|
||||||
|
@ -81,8 +114,9 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
relay = new relay_dbo();
|
relay = new relay_dbo();
|
||||||
relay->number = relay_num;
|
relay->number = relay_num;
|
||||||
strncpy(relay->name, body["name"].asCString(), 127);
|
strncpy(relay->name, body["name"].asCString(), 127);
|
||||||
strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 32);
|
|
||||||
strncpy(relay->controller_id, controller_id.c_str(), 32);
|
uuid_copy(relay->active_schedule_id, active_schedule_id);
|
||||||
|
uuid_copy(relay->controller_id, controller_id);
|
||||||
|
|
||||||
relay->reload_active_schedule();
|
relay->reload_active_schedule();
|
||||||
|
|
||||||
|
@ -97,8 +131,8 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto schedules = schedule_dbo::get_by_simple("id", body["active_schedule"].asCString(), (intptr_t)&sqlite3_bind_text);
|
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.c_str(), (intptr_t)&sqlite3_bind_text);
|
auto controllers = controller_dbo::get_by_simple("id", controller_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
Json::Value payload;
|
Json::Value payload;
|
||||||
payload["target"] = relay_num;
|
payload["target"] = relay_num;
|
||||||
|
|
|
@ -26,7 +26,7 @@ void
|
||||||
relays::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
relays::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||||
const std::string &relay_tag)
|
const std::string &relay_tag)
|
||||||
{
|
{
|
||||||
relay_dbo **relays = relay_dbo::get_by_simple("tag", relay_tag.c_str(), (intptr_t)&sqlite3_bind_text);
|
relay_dbo **relays = relay_dbo::get_by_simple("tag", relay_tag.c_str(), (intptr_t)&sqlite3_bind_text, -1);
|
||||||
|
|
||||||
if(relays[0])
|
if(relays[0])
|
||||||
{
|
{
|
||||||
|
|
|
@ -25,9 +25,18 @@ schedules::get_all(const HttpRequestPtr &req, std::function<void(const HttpRespo
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
|
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id_str)
|
||||||
{
|
{
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
uuid_t schedule_id;
|
||||||
|
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
|
||||||
|
{
|
||||||
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
|
resp->setStatusCode(k400BadRequest);
|
||||||
|
callback(resp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
if(schedules[0])
|
if(schedules[0])
|
||||||
{
|
{
|
||||||
|
@ -46,9 +55,9 @@ schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
|
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id_str)
|
||||||
{
|
{
|
||||||
if(strcmp(schedule_id.c_str(), "off") == 0 || strcmp(schedule_id.c_str(), "on") == 0)
|
if(strcmp(schedule_id_str.c_str(), "off") == 0 || strcmp(schedule_id_str.c_str(), "on") == 0)
|
||||||
{
|
{
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
resp->setStatusCode(k403Forbidden);
|
resp->setStatusCode(k403Forbidden);
|
||||||
|
@ -57,7 +66,15 @@ schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
uuid_t schedule_id;
|
||||||
|
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
|
||||||
|
{
|
||||||
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
|
resp->setStatusCode(k400BadRequest);
|
||||||
|
callback(resp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
if(schedules[0])
|
if(schedules[0])
|
||||||
{
|
{
|
||||||
|
@ -88,7 +105,7 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
|
||||||
|
|
||||||
strncpy(new_schedule.name, body["name"].asCString(), 127);
|
strncpy(new_schedule.name, body["name"].asCString(), 127);
|
||||||
new_schedule.name[127] = '\0';
|
new_schedule.name[127] = '\0';
|
||||||
strncpy(new_schedule.id, drogon::utils::getUuid().c_str(), 32);
|
uuid_generate(new_schedule.id);
|
||||||
new_schedule.id[32] = '\0';
|
new_schedule.id[32] = '\0';
|
||||||
new_schedule.periods = helpers::parse_periods(body["periods"]);
|
new_schedule.periods = helpers::parse_periods(body["periods"]);
|
||||||
|
|
||||||
|
@ -107,9 +124,9 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
|
||||||
|
|
||||||
void
|
void
|
||||||
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||||
const std::string &schedule_id)
|
const std::string &schedule_id_str)
|
||||||
{
|
{
|
||||||
if(strcmp(schedule_id.c_str(), "off") == 0 || strcmp(schedule_id.c_str(), "on") == 0)
|
if(strcmp(schedule_id_str.c_str(), "off") == 0 || strcmp(schedule_id_str.c_str(), "on") == 0)
|
||||||
{
|
{
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
resp->setStatusCode(k403Forbidden);
|
resp->setStatusCode(k403Forbidden);
|
||||||
|
@ -118,9 +135,17 @@ schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value body = *req->jsonObject();
|
uuid_t schedule_id;
|
||||||
|
if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id))
|
||||||
|
{
|
||||||
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
|
resp->setStatusCode(k400BadRequest);
|
||||||
|
callback(resp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
Json::Value body = *req->jsonObject();
|
||||||
|
|
||||||
if(schedules[0])
|
if(schedules[0])
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,9 +21,17 @@ void valid_json::doFilter(const HttpRequestPtr &req,
|
||||||
is_valid &= body["name"].type() == Json::ValueType::stringValue;
|
is_valid &= body["name"].type() == Json::ValueType::stringValue;
|
||||||
is_valid &= body["active_schedule"].type() == Json::ValueType::stringValue;
|
is_valid &= body["active_schedule"].type() == Json::ValueType::stringValue;
|
||||||
|
|
||||||
|
uuid_t active_schedule_id;
|
||||||
|
if(schedule_dbo::parse_id(body["active_schedule"].asCString(), active_schedule_id))
|
||||||
|
{
|
||||||
|
auto res = drogon::HttpResponse::newHttpResponse();
|
||||||
|
res->setStatusCode(k400BadRequest);
|
||||||
|
fcb(res);
|
||||||
|
}
|
||||||
|
|
||||||
if(is_valid)
|
if(is_valid)
|
||||||
{
|
{
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", body["active_schedule"].asCString(), (intptr_t)&sqlite3_bind_text);
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
bool schedule_found = schedules[0] != nullptr;
|
bool schedule_found = schedules[0] != nullptr;
|
||||||
schedule_dbo::free_list(schedules);
|
schedule_dbo::free_list(schedules);
|
||||||
if(schedule_found)
|
if(schedule_found)
|
||||||
|
|
|
@ -22,11 +22,12 @@ namespace helpers
|
||||||
|
|
||||||
typedef struct sql_filter_builder
|
typedef struct sql_filter_builder
|
||||||
{
|
{
|
||||||
sql_filter_builder(const char *col_name, const void *value, intptr_t bind_func, const char *logic);
|
sql_filter_builder(const char *col_name, const void *value, intptr_t bind_func, int bind_func_param, const char *logic);
|
||||||
|
|
||||||
const char *col_name;
|
const char *col_name;
|
||||||
const void *value;
|
const void *value;
|
||||||
intptr_t bind_func;
|
intptr_t bind_func;
|
||||||
|
int bind_func_param;
|
||||||
const char *logic;
|
const char *logic;
|
||||||
} sql_filter_builder;
|
} sql_filter_builder;
|
||||||
|
|
||||||
|
|
|
@ -41,18 +41,22 @@ helpers::create_sql_filtered_query(const char *sql, sql_filter_builder **filters
|
||||||
}
|
}
|
||||||
if(filter->bind_func == (intptr_t)&sqlite3_bind_text)
|
if(filter->bind_func == (intptr_t)&sqlite3_bind_text)
|
||||||
{
|
{
|
||||||
sqlite3_bind_text(stmt, i + 1, (char*)filter->value, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, i + 1, (char*)filter->value, filter->bind_func_param, SQLITE_STATIC);
|
||||||
|
}
|
||||||
|
if(filter->bind_func == (intptr_t)&sqlite3_bind_blob)
|
||||||
|
{
|
||||||
|
sqlite3_bind_blob(stmt, i + 1, filter->value, filter->bind_func_param, SQLITE_STATIC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return stmt;
|
return stmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
helpers::sql_filter_builder::sql_filter_builder(const char *col_name, const void *value, intptr_t bind_func,
|
helpers::sql_filter_builder::sql_filter_builder(const char *col_name, const void *value, intptr_t bind_func, int bind_func_param, const char *logic)
|
||||||
const char *logic)
|
|
||||||
{
|
{
|
||||||
this->col_name = col_name;
|
this->col_name = col_name;
|
||||||
this->value = value;
|
this->value = value;
|
||||||
this->bind_func = bind_func;
|
this->bind_func = bind_func;
|
||||||
|
this->bind_func_param = bind_func_param;
|
||||||
this->logic = logic;
|
this->logic = logic;
|
||||||
}
|
}
|
|
@ -1,11 +1,12 @@
|
||||||
#include <helpers.h>
|
#include <helpers.h>
|
||||||
#include <globals.h>
|
#include <globals.h>
|
||||||
|
#include <uuid/uuid.h>
|
||||||
#include <drogon/trantor/trantor/utils/Logger.h>
|
#include <drogon/trantor/trantor/utils/Logger.h>
|
||||||
|
#include <drogon/utils/Utilities.h>
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include <sql/migration_0.h>
|
#include <sql/migration_0.h>
|
||||||
#include <sql/migration_1.h>
|
|
||||||
|
|
||||||
int
|
int
|
||||||
helpers::migrate_sql()
|
helpers::migrate_sql()
|
||||||
|
@ -41,15 +42,6 @@ helpers::migrate_sql()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
new_version_num = 1;
|
new_version_num = 1;
|
||||||
case 1:
|
|
||||||
LOG_INFO << "Migrating LEVEL 1";
|
|
||||||
rc = sqlite3_exec(globals::db, (const char *)sql_migration_1_sql, nullptr, nullptr, &err_msg);
|
|
||||||
if(rc != 0)
|
|
||||||
{
|
|
||||||
LOG_FATAL << "Couldn't migrate LEVEL 1 (" << err_msg << ")";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
new_version_num = 2;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <trantor/utils/Logger.h>
|
#include <trantor/utils/Logger.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <uuid/uuid.h>
|
||||||
#include <helpers.h>
|
#include <helpers.h>
|
||||||
#include "controller_dbo.h"
|
#include "controller_dbo.h"
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
|
@ -19,7 +20,7 @@ static bool controller_db_update_insert(controller_dbo *controller, sqlite3_stmt
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
sqlite3_bind_text(stmt, 1, controller->id, -1, SQLITE_STATIC);
|
sqlite3_bind_blob(stmt, 1, controller->id, sizeof(uuid_t), SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 2, controller->name, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 2, controller->name, -1, SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 3, controller->ip, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 3, controller->ip, -1, SQLITE_STATIC);
|
||||||
sqlite3_bind_int(stmt, 4, controller->active);
|
sqlite3_bind_int(stmt, 4, controller->active);
|
||||||
|
@ -56,7 +57,7 @@ controller_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
switch(name[1])
|
switch(name[1])
|
||||||
{
|
{
|
||||||
case 'd': // id
|
case 'd': // id
|
||||||
strncpy(new_controller->id, (const char*)sqlite3_column_text(stmt, i), 32);
|
uuid_copy(new_controller->id, (const unsigned char*)sqlite3_column_blob(stmt, i));
|
||||||
break;
|
break;
|
||||||
case 'p': // ip
|
case 'p': // ip
|
||||||
strncpy(new_controller->ip, (const char*)sqlite3_column_text(stmt, i), 16);
|
strncpy(new_controller->ip, (const char*)sqlite3_column_text(stmt, i), 16);
|
||||||
|
@ -104,7 +105,7 @@ controller_db_select(sqlite3_stmt *stmt)
|
||||||
if (s == SQLITE_ROW)
|
if (s == SQLITE_ROW)
|
||||||
{
|
{
|
||||||
controller_dbo *new_controller = controller_db_select_mapper(stmt);
|
controller_dbo *new_controller = controller_db_select_mapper(stmt);
|
||||||
new_controller->relays = relay_dbo::get_by_simple("controller_id", new_controller->id, (intptr_t)&sqlite3_bind_text);
|
new_controller->relays = relay_dbo::get_by_simple("controller_id", new_controller->id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
row++;
|
row++;
|
||||||
|
|
||||||
all_controllers = (controller_dbo**)realloc(all_controllers, sizeof(controller_dbo*) * (row + 1));
|
all_controllers = (controller_dbo**)realloc(all_controllers, sizeof(controller_dbo*) * (row + 1));
|
||||||
|
@ -158,7 +159,7 @@ controller_dbo::remove()
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
sqlite3_prepare_v2(globals::db, "DELETE FROM controllers WHERE id=?1;", -1, &stmt, nullptr);
|
sqlite3_prepare_v2(globals::db, "DELETE FROM controllers WHERE id=?1;", -1, &stmt, nullptr);
|
||||||
sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
|
sqlite3_bind_blob(stmt, 1, this->id, sizeof(uuid_t), SQLITE_STATIC);
|
||||||
rc = sqlite3_step(stmt);
|
rc = sqlite3_step(stmt);
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
|
|
||||||
|
@ -169,9 +170,11 @@ controller_dbo::remove()
|
||||||
Json::Value
|
Json::Value
|
||||||
controller_dbo::to_json()
|
controller_dbo::to_json()
|
||||||
{
|
{
|
||||||
|
char id_str[37];
|
||||||
|
uuid_unparse(this->id, id_str);
|
||||||
Json::Value controller_json;
|
Json::Value controller_json;
|
||||||
controller_json["name"] = this->name;
|
controller_json["name"] = this->name;
|
||||||
controller_json["id"] = this->id;
|
controller_json["id"] = id_str;
|
||||||
controller_json["ip"] = this->ip;
|
controller_json["ip"] = this->ip;
|
||||||
controller_json["relay_count"] = this->relay_count;
|
controller_json["relay_count"] = this->relay_count;
|
||||||
controller_json["active"] = this->active;
|
controller_json["active"] = this->active;
|
||||||
|
@ -199,7 +202,7 @@ controller_dbo::get_all()
|
||||||
}
|
}
|
||||||
|
|
||||||
controller_dbo**
|
controller_dbo**
|
||||||
controller_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
|
controller_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param)
|
||||||
{
|
{
|
||||||
helpers::sql_filter_builder *filters[1];
|
helpers::sql_filter_builder *filters[1];
|
||||||
helpers::sql_filter_builder filter
|
helpers::sql_filter_builder filter
|
||||||
|
@ -207,6 +210,7 @@ controller_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_
|
||||||
key,
|
key,
|
||||||
value,
|
value,
|
||||||
bind_func,
|
bind_func,
|
||||||
|
bind_func_param,
|
||||||
";"
|
";"
|
||||||
};
|
};
|
||||||
filters[0] = &filter;
|
filters[0] = &filter;
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define EMGAUWA_CORE_controller_DBO_H
|
#define EMGAUWA_CORE_controller_DBO_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <uuid/uuid.h>
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#include <json/value.h>
|
#include <json/value.h>
|
||||||
#include <helpers.h>
|
#include <helpers.h>
|
||||||
|
@ -11,7 +12,7 @@ class controller_dbo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
char id[33];
|
uuid_t id;
|
||||||
char name[128];
|
char name[128];
|
||||||
char ip[17];
|
char ip[17];
|
||||||
bool active;
|
bool active;
|
||||||
|
@ -35,7 +36,7 @@ public:
|
||||||
to_json();
|
to_json();
|
||||||
|
|
||||||
static controller_dbo**
|
static controller_dbo**
|
||||||
get_by_simple(const char *key, const void *value, intptr_t bind_func);
|
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
|
||||||
|
|
||||||
static controller_dbo**
|
static controller_dbo**
|
||||||
get_by(helpers::sql_filter_builder **filters);
|
get_by(helpers::sql_filter_builder **filters);
|
||||||
|
|
|
@ -14,8 +14,8 @@ static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
|
||||||
sqlite3_bind_int(stmt, 1, relay->id);
|
sqlite3_bind_int(stmt, 1, relay->id);
|
||||||
sqlite3_bind_int(stmt, 2, relay->number);
|
sqlite3_bind_int(stmt, 2, relay->number);
|
||||||
sqlite3_bind_text(stmt, 3, relay->name, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 3, relay->name, -1, SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 4, relay->active_schedule_id, -1, SQLITE_STATIC);
|
sqlite3_bind_blob(stmt, 4, relay->active_schedule_id, sizeof(uuid_t), SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 5, relay->controller_id, -1, SQLITE_STATIC);
|
sqlite3_bind_blob(stmt, 5, relay->controller_id, sizeof(uuid_t), SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 6, relay->tag, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 6, relay->tag, -1, SQLITE_STATIC);
|
||||||
|
|
||||||
rc = sqlite3_step(stmt);
|
rc = sqlite3_step(stmt);
|
||||||
|
@ -41,10 +41,10 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
switch(name[0])
|
switch(name[0])
|
||||||
{
|
{
|
||||||
case 'a': // active_schedule_id
|
case 'a': // active_schedule_id
|
||||||
strncpy(new_relay->active_schedule_id, (const char*)sqlite3_column_text(stmt, i), 32);
|
uuid_copy(new_relay->active_schedule_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
|
||||||
break;
|
break;
|
||||||
case 'c': // controller_id
|
case 'c': // controller_id
|
||||||
strncpy(new_relay->controller_id, (const char*)sqlite3_column_text(stmt, i), 32);
|
uuid_copy(new_relay->controller_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
new_relay->id = sqlite3_column_int(stmt, i);
|
new_relay->id = sqlite3_column_int(stmt, i);
|
||||||
|
@ -123,13 +123,16 @@ relay_db_select(sqlite3_stmt *stmt)
|
||||||
void
|
void
|
||||||
relay_dbo::reload_active_schedule()
|
relay_dbo::reload_active_schedule()
|
||||||
{
|
{
|
||||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", this->active_schedule_id, (intptr_t)&sqlite3_bind_text);
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", this->active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
if(!schedules[0])
|
if(!schedules[0])
|
||||||
{
|
{
|
||||||
free(schedules);
|
free(schedules);
|
||||||
schedules = schedule_dbo::get_by_simple("id", "off", (intptr_t)&sqlite3_bind_text);
|
uuid_t off_uuid;
|
||||||
strcpy(this->active_schedule_id, "off");
|
memset(off_uuid, 0, sizeof(uuid_t));
|
||||||
|
memcpy(off_uuid, "off", 3);
|
||||||
|
schedules = schedule_dbo::get_by_simple("id", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
uuid_copy(this->active_schedule_id, off_uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->active_schedule = schedules[0];
|
this->active_schedule = schedules[0];
|
||||||
|
@ -175,11 +178,15 @@ relay_dbo::remove()
|
||||||
Json::Value
|
Json::Value
|
||||||
relay_dbo::to_json()
|
relay_dbo::to_json()
|
||||||
{
|
{
|
||||||
|
char controller_id_str[37];
|
||||||
|
uuid_unparse(this->controller_id, controller_id_str);
|
||||||
|
char active_schedule_id_str[37];
|
||||||
|
uuid_unparse(this->active_schedule_id, active_schedule_id_str);
|
||||||
Json::Value relay_json;
|
Json::Value relay_json;
|
||||||
relay_json["name"] = this->name;
|
relay_json["name"] = this->name;
|
||||||
relay_json["number"] = this->number;
|
relay_json["number"] = this->number;
|
||||||
relay_json["active_schedule_id"] = this->active_schedule_id;
|
relay_json["active_schedule_id"] = active_schedule_id_str;
|
||||||
relay_json["controller_id"] = this->controller_id;
|
relay_json["controller_id"] = controller_id_str;
|
||||||
relay_json["active_schedule"] = this->active_schedule->to_json();
|
relay_json["active_schedule"] = this->active_schedule->to_json();
|
||||||
relay_json["tag"] = this->tag;
|
relay_json["tag"] = this->tag;
|
||||||
|
|
||||||
|
@ -197,7 +204,7 @@ relay_dbo::get_all()
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_dbo**
|
relay_dbo**
|
||||||
relay_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
|
relay_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param)
|
||||||
{
|
{
|
||||||
helpers::sql_filter_builder *filters[1];
|
helpers::sql_filter_builder *filters[1];
|
||||||
helpers::sql_filter_builder filter
|
helpers::sql_filter_builder filter
|
||||||
|
@ -205,6 +212,7 @@ relay_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
|
||||||
key,
|
key,
|
||||||
value,
|
value,
|
||||||
bind_func,
|
bind_func,
|
||||||
|
bind_func_param,
|
||||||
";"
|
";"
|
||||||
};
|
};
|
||||||
filters[0] = &filter;
|
filters[0] = &filter;
|
||||||
|
@ -221,19 +229,21 @@ relay_dbo::get_by(helpers::sql_filter_builder **filters)
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_dbo*
|
relay_dbo*
|
||||||
relay_dbo::get_relay_for_controller(const char *controller_id, int relay_num)
|
relay_dbo::get_relay_for_controller(uuid_t controller_id, int relay_num)
|
||||||
{
|
{
|
||||||
helpers::sql_filter_builder *filters[2];
|
helpers::sql_filter_builder *filters[2];
|
||||||
helpers::sql_filter_builder filter(
|
helpers::sql_filter_builder filter(
|
||||||
"number",
|
"number",
|
||||||
(void*)(intptr_t)relay_num,
|
(void*)(intptr_t)relay_num,
|
||||||
(intptr_t)&sqlite3_bind_int,
|
(intptr_t)&sqlite3_bind_int,
|
||||||
|
0,
|
||||||
"AND"
|
"AND"
|
||||||
);
|
);
|
||||||
helpers::sql_filter_builder filter2(
|
helpers::sql_filter_builder filter2(
|
||||||
"controller_id",
|
"controller_id",
|
||||||
(void*)controller_id,
|
(void*)controller_id,
|
||||||
(intptr_t)sqlite3_bind_text,
|
(intptr_t)sqlite3_bind_blob,
|
||||||
|
sizeof(uuid_t),
|
||||||
";"
|
";"
|
||||||
);
|
);
|
||||||
filters[0] = &filter;
|
filters[0] = &filter;
|
||||||
|
@ -247,9 +257,9 @@ relay_dbo::get_relay_for_controller(const char *controller_id, int relay_num)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
relay_dbo::valid_num_for_controller(const char *search_controller_id, int relay_num)
|
relay_dbo::valid_num_for_controller(uuid_t search_controller_id, int relay_num)
|
||||||
{
|
{
|
||||||
controller_dbo **controllers = controller_dbo::get_by_simple("id", search_controller_id, (intptr_t)&sqlite3_bind_text);
|
controller_dbo **controllers = controller_dbo::get_by_simple("id", search_controller_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||||
|
|
||||||
bool valid_id_and_num = controllers[0] && controllers[0]->relay_count > relay_num;
|
bool valid_id_and_num = controllers[0] && controllers[0]->relay_count > relay_num;
|
||||||
controller_dbo::free_list(controllers);
|
controller_dbo::free_list(controllers);
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#include <json/value.h>
|
#include <json/value.h>
|
||||||
|
#include <uuid/uuid.h>
|
||||||
#include <helpers.h>
|
#include <helpers.h>
|
||||||
#include "schedule_dbo.h"
|
#include "schedule_dbo.h"
|
||||||
|
|
||||||
|
@ -14,8 +15,8 @@ public:
|
||||||
int id;
|
int id;
|
||||||
char name[128];
|
char name[128];
|
||||||
int number;
|
int number;
|
||||||
char controller_id[33];
|
uuid_t controller_id;
|
||||||
char active_schedule_id[33];
|
uuid_t active_schedule_id;
|
||||||
char tag[64];
|
char tag[64];
|
||||||
schedule_dbo *active_schedule;
|
schedule_dbo *active_schedule;
|
||||||
|
|
||||||
|
@ -38,16 +39,16 @@ public:
|
||||||
free_list(relay_dbo **relays_list);
|
free_list(relay_dbo **relays_list);
|
||||||
|
|
||||||
static relay_dbo**
|
static relay_dbo**
|
||||||
get_by_simple(const char *key, const void *value, intptr_t bind_func);
|
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
|
||||||
|
|
||||||
static relay_dbo**
|
static relay_dbo**
|
||||||
get_by(helpers::sql_filter_builder **filters);
|
get_by(helpers::sql_filter_builder **filters);
|
||||||
|
|
||||||
static relay_dbo*
|
static relay_dbo*
|
||||||
get_relay_for_controller(const char *controller_id, int relay_num);
|
get_relay_for_controller(uuid_t controller_id, int relay_num);
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
valid_num_for_controller(const char *search_controller_id, int relay_num);
|
valid_num_for_controller(uuid_t search_controller_id, int relay_num);
|
||||||
|
|
||||||
static relay_dbo**
|
static relay_dbo**
|
||||||
get_all();
|
get_all();
|
||||||
|
|
|
@ -11,7 +11,7 @@ static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt
|
||||||
uint16_t *periods_blob = schedule->periods->to_db_blob();
|
uint16_t *periods_blob = schedule->periods->to_db_blob();
|
||||||
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
|
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
|
||||||
|
|
||||||
sqlite3_bind_text(stmt, 1, schedule->id, -1, SQLITE_STATIC);
|
sqlite3_bind_blob(stmt, 1, schedule->id, sizeof(uuid_t), SQLITE_STATIC);
|
||||||
sqlite3_bind_text(stmt, 2, schedule->name, -1, SQLITE_STATIC);
|
sqlite3_bind_text(stmt, 2, schedule->name, -1, SQLITE_STATIC);
|
||||||
sqlite3_bind_blob(stmt, 3, periods_blob, blob_size, SQLITE_STATIC);
|
sqlite3_bind_blob(stmt, 3, periods_blob, blob_size, SQLITE_STATIC);
|
||||||
|
|
||||||
|
@ -39,8 +39,7 @@ schedule_db_select_mapper(sqlite3_stmt *stmt)
|
||||||
switch(name[0])
|
switch(name[0])
|
||||||
{
|
{
|
||||||
case 'i': // id
|
case 'i': // id
|
||||||
strncpy(new_schedule->id, (const char*)sqlite3_column_text(stmt, i), 32);
|
uuid_copy(new_schedule->id, (const unsigned char*)sqlite3_column_blob(stmt, i));
|
||||||
new_schedule->id[32] = '\0';
|
|
||||||
break;
|
break;
|
||||||
case 'n': // name
|
case 'n': // name
|
||||||
strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127);
|
strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127);
|
||||||
|
@ -124,7 +123,7 @@ schedule_dbo::remove()
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
sqlite3_prepare_v2(globals::db, "DELETE FROM schedules WHERE id=?1;", -1, &stmt, nullptr);
|
sqlite3_prepare_v2(globals::db, "DELETE FROM schedules WHERE id=?1;", -1, &stmt, nullptr);
|
||||||
sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
|
sqlite3_bind_blob(stmt, 1, this->id, sizeof(uuid_t), SQLITE_STATIC);
|
||||||
rc = sqlite3_step(stmt);
|
rc = sqlite3_step(stmt);
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
|
@ -141,9 +140,11 @@ schedule_dbo::~schedule_dbo()
|
||||||
Json::Value
|
Json::Value
|
||||||
schedule_dbo::to_json()
|
schedule_dbo::to_json()
|
||||||
{
|
{
|
||||||
|
char id_str[37];
|
||||||
|
schedule_dbo::unparse_id(this->id, id_str);
|
||||||
Json::Value schedule_json;
|
Json::Value schedule_json;
|
||||||
schedule_json["name"] = this->name;
|
schedule_json["name"] = this->name;
|
||||||
schedule_json["id"] = this->id;
|
schedule_json["id"] = id_str;
|
||||||
schedule_json["periods"] = this->periods->to_json();
|
schedule_json["periods"] = this->periods->to_json();
|
||||||
|
|
||||||
return schedule_json;
|
return schedule_json;
|
||||||
|
@ -160,7 +161,7 @@ schedule_dbo::get_all()
|
||||||
}
|
}
|
||||||
|
|
||||||
schedule_dbo**
|
schedule_dbo**
|
||||||
schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
|
schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param)
|
||||||
{
|
{
|
||||||
helpers::sql_filter_builder *filters[1];
|
helpers::sql_filter_builder *filters[1];
|
||||||
helpers::sql_filter_builder filter
|
helpers::sql_filter_builder filter
|
||||||
|
@ -168,6 +169,7 @@ schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_fu
|
||||||
key,
|
key,
|
||||||
value,
|
value,
|
||||||
bind_func,
|
bind_func,
|
||||||
|
bind_func_param,
|
||||||
";"
|
";"
|
||||||
};
|
};
|
||||||
filters[0] = &filter;
|
filters[0] = &filter;
|
||||||
|
@ -195,3 +197,49 @@ schedule_dbo::free_list(schedule_dbo **schedules_list)
|
||||||
free(schedules_list);
|
free(schedules_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
schedule_dbo::parse_id(const char *id_str, uuid_t result)
|
||||||
|
{
|
||||||
|
if(strcmp("off", id_str) == 0)
|
||||||
|
{
|
||||||
|
memset(result, 0, sizeof(uuid_t));
|
||||||
|
memcpy(result, "off", 3);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(strcmp("on", id_str) == 0)
|
||||||
|
{
|
||||||
|
memset(result, 0, sizeof(uuid_t));
|
||||||
|
memcpy(result, "on", 2);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(uuid_parse(id_str, result))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
schedule_dbo::unparse_id(const uuid_t id, char *result)
|
||||||
|
{
|
||||||
|
uuid_t tmp_uuid;
|
||||||
|
|
||||||
|
memset(tmp_uuid, 0, sizeof(uuid_t));
|
||||||
|
memcpy(tmp_uuid, "off", 3);
|
||||||
|
if(uuid_compare(id, tmp_uuid) == 0)
|
||||||
|
{
|
||||||
|
strcpy(result, "off");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(tmp_uuid, 0, sizeof(uuid_t));
|
||||||
|
memcpy(tmp_uuid, "on", 2);
|
||||||
|
if(uuid_compare(id, tmp_uuid) == 0)
|
||||||
|
{
|
||||||
|
strcpy(result, "on");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uuid_unparse(id, result);
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
|
#include <uuid/uuid.h>
|
||||||
#include <json/value.h>
|
#include <json/value.h>
|
||||||
#include <helpers.h>
|
#include <helpers.h>
|
||||||
#include "period.h"
|
#include "period.h"
|
||||||
|
@ -12,7 +13,7 @@ class schedule_dbo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
char id[33];
|
uuid_t id;
|
||||||
char name[128];
|
char name[128];
|
||||||
period_list *periods;
|
period_list *periods;
|
||||||
|
|
||||||
|
@ -34,13 +35,19 @@ public:
|
||||||
free_list(schedule_dbo **schedules_list);
|
free_list(schedule_dbo **schedules_list);
|
||||||
|
|
||||||
static schedule_dbo**
|
static schedule_dbo**
|
||||||
get_by_simple(const char *key, const void *value, intptr_t bind_func);
|
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
|
||||||
|
|
||||||
static schedule_dbo**
|
static schedule_dbo**
|
||||||
get_by(helpers::sql_filter_builder **filters);
|
get_by(helpers::sql_filter_builder **filters);
|
||||||
|
|
||||||
static schedule_dbo**
|
static schedule_dbo**
|
||||||
get_all();
|
get_all();
|
||||||
|
|
||||||
|
static int
|
||||||
|
parse_id(const char *id_str, uuid_t result);
|
||||||
|
|
||||||
|
static void
|
||||||
|
unparse_id(const uuid_t id, char *result);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,8 @@ create table relays
|
||||||
controller_id VARCHAR(33) not null
|
controller_id VARCHAR(33) not null
|
||||||
references controllers (id),
|
references controllers (id),
|
||||||
active_schedule_id VARCHAR(33)
|
active_schedule_id VARCHAR(33)
|
||||||
references schedules
|
references schedules,
|
||||||
|
tag vARCHAR(64)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table schedules
|
create table schedules
|
||||||
|
@ -34,7 +35,9 @@ create table schedules
|
||||||
primary key
|
primary key
|
||||||
unique,
|
unique,
|
||||||
name VARCHAR(128),
|
name VARCHAR(128),
|
||||||
periods BLOB
|
periods BLOB,
|
||||||
|
tag vARCHAR(64)
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO schedules (id, name, periods) VALUES ('off', 'off', x'00');
|
INSERT INTO schedules (id, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00');
|
||||||
|
INSERT INTO schedules (id, name, periods) VALUES (x'6f6e0000000000000000000000000000', 'on', x'010000009F05');
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
alter table relays
|
|
||||||
add tag varchar(64);
|
|
||||||
|
|
||||||
alter table controllers
|
|
||||||
add tag varchar(64);
|
|
||||||
|
|
||||||
INSERT INTO schedules (id, name, periods) VALUES ('on', 'on', x'010000009F05');
|
|
||||||
|
|
25
tavern_tests/test_get_all.tavern.yaml
Normal file
25
tavern_tests/test_get_all.tavern.yaml
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
test_name: Test basic get all requests
|
||||||
|
|
||||||
|
stages:
|
||||||
|
- name: Get all schedules
|
||||||
|
request:
|
||||||
|
url: "http://localhost:5000/api/v1/schedules/"
|
||||||
|
method: GET
|
||||||
|
response:
|
||||||
|
status_code: 200
|
||||||
|
|
||||||
|
stages:
|
||||||
|
- name: Get all relays
|
||||||
|
request:
|
||||||
|
url: "http://localhost:5000/api/v1/relays/"
|
||||||
|
method: GET
|
||||||
|
response:
|
||||||
|
status_code: 200
|
||||||
|
|
||||||
|
stages:
|
||||||
|
- name: Get all controllers
|
||||||
|
request:
|
||||||
|
url: "http://localhost:5000/api/v1/controllers/"
|
||||||
|
method: GET
|
||||||
|
response:
|
||||||
|
status_code: 200
|
|
@ -1,9 +0,0 @@
|
||||||
test_name: Test basic requests
|
|
||||||
|
|
||||||
stages:
|
|
||||||
- name: Make sure we get any response
|
|
||||||
request:
|
|
||||||
url: http://localhost:5000/api/v1/schedules/
|
|
||||||
method: GET
|
|
||||||
response:
|
|
||||||
status_code: 200
|
|
39
tavern_tests/test_schedules_basic.tavern.yaml
Normal file
39
tavern_tests/test_schedules_basic.tavern.yaml
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
test_name: Test basic requests
|
||||||
|
|
||||||
|
stages:
|
||||||
|
- name: Make sure we get any response
|
||||||
|
request:
|
||||||
|
url: "http://localhost:5000/api/v1/schedules/"
|
||||||
|
method: GET
|
||||||
|
response:
|
||||||
|
status_code: 200
|
||||||
|
- name: post schedule, expect it to be echoed back
|
||||||
|
request:
|
||||||
|
method: POST
|
||||||
|
url: "http://localhost:5000/api/v1/schedules/"
|
||||||
|
json:
|
||||||
|
name: "hello"
|
||||||
|
periods:
|
||||||
|
- start: '00:10'
|
||||||
|
end: '00:20'
|
||||||
|
- start: '00:30'
|
||||||
|
end: '00:40'
|
||||||
|
- start: '00:50'
|
||||||
|
end: '01:00'
|
||||||
|
response:
|
||||||
|
status_code: 200
|
||||||
|
body:
|
||||||
|
name: "{tavern.request_vars.json.name}"
|
||||||
|
save:
|
||||||
|
body:
|
||||||
|
returned_name: name
|
||||||
|
returned_id: id
|
||||||
|
- name: get schedule, check name and some periods
|
||||||
|
request:
|
||||||
|
method: GET
|
||||||
|
url: "http://localhost:5000/api/v1/schedules/{returned_id}"
|
||||||
|
response:
|
||||||
|
status_code: 200
|
||||||
|
body:
|
||||||
|
name: "{returned_name}"
|
||||||
|
|
Loading…
Reference in a new issue