diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cf0074..7cfa064 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ endif() set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -latomic") +set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -g -latomic") ########## # If you include the drogon source code locally in your project, use this method to add drogon diff --git a/controllers/api_v1_controllers_relays.cc b/controllers/api_v1_controllers_relays.cc index fdf8cb4..9bc4b16 100644 --- a/controllers/api_v1_controllers_relays.cc +++ b/controllers/api_v1_controllers_relays.cc @@ -93,7 +93,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, Json::Value body = *req->getJsonObject(); uuid_t active_schedule_id; - if(schedule_dbo::parse_id(body["active_schedule"].asCString(), active_schedule_id)) + if(schedule_dbo::parse_uid(body["active_schedule"].asCString(), active_schedule_id)) { LOG_DEBUG << "bad active_schedule uuid"; auto resp = HttpResponse::newHttpResponse(); @@ -103,13 +103,17 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, } relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num); + schedule_dbo **schedule = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); bool db_action_result; if(relay) { strncpy(relay->name, body["name"].asCString(), 127); - uuid_copy(relay->active_schedule_id, active_schedule_id); + relay->active_schedule_id = schedule[0]->id; + uuid_copy(relay->controller_id, controller_id); + + relay->reload_active_schedule(); db_action_result = relay->update(); } @@ -119,7 +123,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, relay->number = relay_num; strncpy(relay->name, body["name"].asCString(), 127); - uuid_copy(relay->active_schedule_id, active_schedule_id); + relay->active_schedule_id = schedule[0]->id; uuid_copy(relay->controller_id, controller_id); relay->reload_active_schedule(); @@ -135,7 +139,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, } else { - auto schedules = schedule_dbo::get_by_simple("id", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); + auto schedules = schedule_dbo::get_by_simple("uid", 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)); uint16_t *periods = schedules[0]->periods->to_blob(); @@ -144,7 +148,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, binn_map_set_uint8(map, COMMAND_MAPPING_CODE, config::command_code_set_schedule); binn_map_set_uint8(map, COMMAND_MAPPING_RELAY_NUM, relay_num); - binn_map_set_blob(map, COMMAND_MAPPING_SCHEDULE_ID, schedules[0]->id, sizeof(uuid_t)); + binn_map_set_blob(map, COMMAND_MAPPING_SCHEDULE_ID, schedules[0]->uid, sizeof(uuid_t)); binn_map_set_uint16(map, COMMAND_MAPPING_PERIODS_COUNT, periods_count); // periods + 1 to skip length in periods[0] diff --git a/controllers/api_v1_schedules.cc b/controllers/api_v1_schedules.cc index 093a47e..8ec6b12 100644 --- a/controllers/api_v1_schedules.cc +++ b/controllers/api_v1_schedules.cc @@ -28,7 +28,7 @@ void schedules::get_one_by_id(const HttpRequestPtr &req, std::function &&callback, const std::string& schedule_id_str) { uuid_t schedule_id; - if(schedule_dbo::parse_id(schedule_id_str.c_str(), schedule_id)) + if(schedule_dbo::parse_uid(schedule_id_str.c_str(), schedule_id)) { auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k400BadRequest); @@ -36,7 +36,7 @@ schedules::get_one_by_id(const HttpRequestPtr &req, std::functionsetStatusCode(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("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t)); if(schedules[0]) { @@ -105,8 +105,8 @@ schedules::post_new(const HttpRequestPtr &req, std::functionsetStatusCode(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("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t)); Json::Value body = *req->jsonObject(); diff --git a/filters/relays_valid_json.cc b/filters/relays_valid_json.cc index 4d19830..57d7ecf 100644 --- a/filters/relays_valid_json.cc +++ b/filters/relays_valid_json.cc @@ -22,8 +22,9 @@ void valid_json::doFilter(const HttpRequestPtr &req, 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)) + if(schedule_dbo::parse_uid(body["active_schedule"].asCString(), active_schedule_id)) { + LOG_DEBUG << "parse_uid failed"; auto res = drogon::HttpResponse::newHttpResponse(); res->setStatusCode(k400BadRequest); fcb(res); @@ -31,7 +32,7 @@ void valid_json::doFilter(const HttpRequestPtr &req, if(is_valid) { - schedule_dbo **schedules = schedule_dbo::get_by_simple("id", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); + schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); bool schedule_found = schedules[0] != nullptr; schedule_dbo::free_list(schedules); if(schedule_found) @@ -42,6 +43,7 @@ void valid_json::doFilter(const HttpRequestPtr &req, } } //Check failed + LOG_DEBUG << "schedule not found"; auto res = drogon::HttpResponse::newHttpResponse(); res->setStatusCode(k400BadRequest); fcb(res); diff --git a/helpers/create_sql_filtered_query.cc b/helpers/create_sql_filtered_query.cc index 4e635ca..9b577e6 100644 --- a/helpers/create_sql_filtered_query.cc +++ b/helpers/create_sql_filtered_query.cc @@ -37,7 +37,7 @@ helpers::create_sql_filtered_query(const char *sql, sql_filter_builder **filters if(filter->bind_func == (intptr_t)&sqlite3_bind_int) { - sqlite3_bind_int(stmt, i + 1, (int)(intptr_t)filter->value); + sqlite3_bind_int(stmt, i + 1, *((int*)filter->value)); } if(filter->bind_func == (intptr_t)&sqlite3_bind_text) { diff --git a/models/relay_dbo.cc b/models/relay_dbo.cc index 16d460c..c6d0e1c 100644 --- a/models/relay_dbo.cc +++ b/models/relay_dbo.cc @@ -14,7 +14,7 @@ static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt) sqlite3_bind_int(stmt, 1, relay->id); sqlite3_bind_int(stmt, 2, relay->number); sqlite3_bind_text(stmt, 3, relay->name, -1, SQLITE_STATIC); - sqlite3_bind_blob(stmt, 4, relay->active_schedule_id, sizeof(uuid_t), SQLITE_STATIC); + sqlite3_bind_int(stmt, 4, relay->active_schedule_id); sqlite3_bind_blob(stmt, 5, relay->controller_id, sizeof(uuid_t), SQLITE_STATIC); sqlite3_bind_text(stmt, 6, relay->tag, -1, SQLITE_STATIC); @@ -40,8 +40,8 @@ relay_db_select_mapper(sqlite3_stmt *stmt) const char *name = sqlite3_column_name(stmt, i); switch(name[0]) { - case 'a': // active_schedule_id - uuid_copy(new_relay->active_schedule_id, (const unsigned char*)sqlite3_column_blob(stmt, i)); + case 'a': // active_schedule_dbid + new_relay->active_schedule_id = sqlite3_column_int(stmt, i); break; case 'c': // controller_id uuid_copy(new_relay->controller_id, (const unsigned char*)sqlite3_column_blob(stmt, i)); @@ -123,7 +123,7 @@ relay_db_select(sqlite3_stmt *stmt) void relay_dbo::reload_active_schedule() { - schedule_dbo **schedules = schedule_dbo::get_by_simple("id", this->active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); + schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &this->active_schedule_id, (intptr_t)&sqlite3_bind_int, 0); if(!schedules[0]) { @@ -131,8 +131,11 @@ relay_dbo::reload_active_schedule() uuid_t off_uuid; 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); + schedules = schedule_dbo::get_by_simple("uid", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t)); + if(schedules[0]) + { + this->active_schedule_id = schedules[0]->id; + } } this->active_schedule = schedules[0]; @@ -180,12 +183,12 @@ 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); + char active_schedule_uid_str[37]; + schedule_dbo::unparse_uid(this->active_schedule->uid, active_schedule_uid_str); Json::Value relay_json; relay_json["name"] = this->name; relay_json["number"] = this->number; - relay_json["active_schedule_id"] = active_schedule_id_str; + relay_json["active_schedule_id"] = active_schedule_uid_str; relay_json["controller_id"] = controller_id_str; relay_json["active_schedule"] = this->active_schedule->to_json(); relay_json["tag"] = this->tag; @@ -234,7 +237,7 @@ relay_dbo::get_relay_for_controller(uuid_t controller_id, int relay_num) helpers::sql_filter_builder *filters[2]; helpers::sql_filter_builder filter( "number", - (void*)(intptr_t)relay_num, + &relay_num, (intptr_t)&sqlite3_bind_int, 0, "AND" diff --git a/models/relay_dbo.h b/models/relay_dbo.h index eced1ce..6250d70 100644 --- a/models/relay_dbo.h +++ b/models/relay_dbo.h @@ -16,7 +16,7 @@ public: char name[128]; int number; uuid_t controller_id; - uuid_t active_schedule_id; + int active_schedule_id; char tag[64]; schedule_dbo *active_schedule; diff --git a/models/schedule_dbo.cc b/models/schedule_dbo.cc index c84b8ab..ad4c367 100644 --- a/models/schedule_dbo.cc +++ b/models/schedule_dbo.cc @@ -11,9 +11,10 @@ static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt uint16_t *periods_blob = schedule->periods->to_blob(); int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1); - sqlite3_bind_blob(stmt, 1, schedule->id, sizeof(uuid_t), 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_int(stmt, 1, schedule->id); + sqlite3_bind_blob(stmt, 2, schedule->uid, sizeof(uuid_t), SQLITE_STATIC); + sqlite3_bind_text(stmt, 3, schedule->name, -1, SQLITE_STATIC); + sqlite3_bind_blob(stmt, 4, periods_blob, blob_size, SQLITE_STATIC); rc = sqlite3_step(stmt); @@ -39,7 +40,7 @@ schedule_db_select_mapper(sqlite3_stmt *stmt) switch(name[0]) { case 'i': // id - uuid_copy(new_schedule->id, (const unsigned char*)sqlite3_column_blob(stmt, i)); + new_schedule->id = sqlite3_column_int(stmt, i); break; case 'n': // name strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127); @@ -48,6 +49,9 @@ schedule_db_select_mapper(sqlite3_stmt *stmt) case 'p': // periods new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i)); break; + case 'u': // uid + uuid_copy(new_schedule->uid, (const unsigned char*)sqlite3_column_blob(stmt, i)); + break; default: // ignore columns not implemented break; } @@ -101,7 +105,7 @@ schedule_dbo::update() { sqlite3_stmt *stmt; - sqlite3_prepare_v2(globals::db, "UPDATE schedules SET name = ?2, periods = ?3 WHERE id=?1;", -1, &stmt, nullptr); + sqlite3_prepare_v2(globals::db, "UPDATE schedules SET uid = ?2, name = ?3, periods = ?4 WHERE id=?1;", -1, &stmt, nullptr); return schedule_db_update_insert(this, stmt); } @@ -111,7 +115,7 @@ schedule_dbo::insert() { sqlite3_stmt *stmt; - sqlite3_prepare_v2(globals::db, "INSERT INTO schedules(id, name, periods) values (?1, ?2, ?3);", -1, &stmt, nullptr); + sqlite3_prepare_v2(globals::db, "INSERT INTO schedules(uid, name, periods) values (?2, ?3, ?4);", -1, &stmt, nullptr); return schedule_db_update_insert(this, stmt); } @@ -123,7 +127,7 @@ schedule_dbo::remove() int rc; sqlite3_prepare_v2(globals::db, "DELETE FROM schedules WHERE id=?1;", -1, &stmt, nullptr); - sqlite3_bind_blob(stmt, 1, this->id, sizeof(uuid_t), SQLITE_STATIC); + sqlite3_bind_int(stmt, 1, this->id); rc = sqlite3_step(stmt); sqlite3_finalize(stmt); @@ -141,7 +145,7 @@ Json::Value schedule_dbo::to_json() { char id_str[37]; - schedule_dbo::unparse_id(this->id, id_str); + schedule_dbo::unparse_uid(this->uid, id_str); Json::Value schedule_json; schedule_json["name"] = this->name; schedule_json["id"] = id_str; @@ -198,22 +202,22 @@ schedule_dbo::free_list(schedule_dbo **schedules_list) } int -schedule_dbo::parse_id(const char *id_str, uuid_t result) +schedule_dbo::parse_uid(const char *uid_str, uuid_t result) { - if(strcmp("off", id_str) == 0) + if(strcmp("off", uid_str) == 0) { memset(result, 0, sizeof(uuid_t)); memcpy(result, "off", 3); return 0; } - if(strcmp("on", id_str) == 0) + if(strcmp("on", uid_str) == 0) { memset(result, 0, sizeof(uuid_t)); memcpy(result, "on", 2); return 0; } - if(uuid_parse(id_str, result)) + if(uuid_parse(uid_str, result)) { return 1; } @@ -221,13 +225,13 @@ schedule_dbo::parse_id(const char *id_str, uuid_t result) } void -schedule_dbo::unparse_id(const uuid_t id, char *result) +schedule_dbo::unparse_uid(const uuid_t uid, 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) + if(uuid_compare(uid, tmp_uuid) == 0) { strcpy(result, "off"); return; @@ -235,11 +239,11 @@ schedule_dbo::unparse_id(const uuid_t id, char *result) memset(tmp_uuid, 0, sizeof(uuid_t)); memcpy(tmp_uuid, "on", 2); - if(uuid_compare(id, tmp_uuid) == 0) + if(uuid_compare(uid, tmp_uuid) == 0) { strcpy(result, "on"); return; } - uuid_unparse(id, result); + uuid_unparse(uid, result); } diff --git a/models/schedule_dbo.h b/models/schedule_dbo.h index 9f4a743..93579eb 100644 --- a/models/schedule_dbo.h +++ b/models/schedule_dbo.h @@ -13,7 +13,8 @@ class schedule_dbo { public: - uuid_t id; + int id; + uuid_t uid; char name[128]; period_list *periods; @@ -44,10 +45,10 @@ public: get_all(); static int - parse_id(const char *id_str, uuid_t result); + parse_uid(const char *uid_str, uuid_t result); static void - unparse_id(const uuid_t id, char *result); + unparse_uid(const uuid_t uid, char *result); }; diff --git a/sql/migration_0.sql b/sql/migration_0.sql index 2d12d9d..cb66692 100644 --- a/sql/migration_0.sql +++ b/sql/migration_0.sql @@ -18,27 +18,34 @@ create table controllers create table relays ( - id INTEGER not null + id INTEGER + not null primary key unique, name VARCHAR(128), - number INTEGER not null, - controller_id VARCHAR(33) not null + number INTEGER + not null, + controller_id VARCHAR(33) + not null references controllers (id), - active_schedule_id VARCHAR(33) - references schedules, + active_schedule_id int + references schedules (id), tag vARCHAR(64) ); create table schedules ( - id VARCHAR(33) not null + id INTEGER + not null primary key unique, + uid VARCHAR(33) + not null + unique, name VARCHAR(128), periods BLOB, tag vARCHAR(64) ); -INSERT INTO schedules (id, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00'); -INSERT INTO schedules (id, name, periods) VALUES (x'6f6e0000000000000000000000000000', 'on', x'010000009F05'); +INSERT INTO schedules (uid, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00'); +INSERT INTO schedules (uid, name, periods) VALUES (x'6f6e0000000000000000000000000000', 'on', x'010000009F05');