From 368df26ad707bec62caaeba948849788f0eb607d Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 22 Jul 2019 22:06:13 +0200 Subject: [PATCH] fix: rename "device" to "controller" --- controllers/api_v1_controllers.cc | 68 ++++++ ...{api_v1_devices.h => api_v1_controllers.h} | 26 +-- ...over.cc => api_v1_controllers_discover.cc} | 60 ++--- controllers/api_v1_devices.cc | 68 ------ controllers/api_v1_devices_relays.cc | 32 +-- controllers/api_v1_schedules.h | 4 +- main.cc | 2 +- models/controller_dbo.cc | 207 ++++++++++++++++++ models/{device_dbo.h => controller_dbo.h} | 16 +- models/device_dbo.cc | 207 ------------------ models/relay_dbo.cc | 28 +-- models/relay_dbo.h | 6 +- 12 files changed, 362 insertions(+), 362 deletions(-) create mode 100644 controllers/api_v1_controllers.cc rename controllers/{api_v1_devices.h => api_v1_controllers.h} (56%) rename controllers/{api_v1_devices_discover.cc => api_v1_controllers_discover.cc} (66%) delete mode 100644 controllers/api_v1_devices.cc create mode 100644 models/controller_dbo.cc rename models/{device_dbo.h => controller_dbo.h} (63%) delete mode 100644 models/device_dbo.cc diff --git a/controllers/api_v1_controllers.cc b/controllers/api_v1_controllers.cc new file mode 100644 index 0000000..5199771 --- /dev/null +++ b/controllers/api_v1_controllers.cc @@ -0,0 +1,68 @@ +#include +#include +#include "api_v1_controllers.h" +using namespace api::v1; + +void +controllers::get_all(const HttpRequestPtr &req, std::function &&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 +controllers::get_one_by_id(const HttpRequestPtr &req, std::function &&callback, const std::string& controller_id) +{ + controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text); + + if(controllers[0]) + { + auto resp = HttpResponse::newHttpJsonResponse(controllers[0]->to_json()); + + callback(resp); + } + else + { + auto resp = HttpResponse::newHttpResponse(); + resp->setStatusCode(k404NotFound); + + callback(resp); + } + controller_dbo::free_list(controllers); +} + +void +controllers::delete_one_by_id(const HttpRequestPtr &req, std::function &&callback, const std::string& controller_id) +{ + controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text); + + 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); +} diff --git a/controllers/api_v1_devices.h b/controllers/api_v1_controllers.h similarity index 56% rename from controllers/api_v1_devices.h rename to controllers/api_v1_controllers.h index 3f840d6..db37532 100644 --- a/controllers/api_v1_devices.h +++ b/controllers/api_v1_controllers.h @@ -5,28 +5,28 @@ namespace api { namespace v1 { - class devices:public drogon::HttpController + class controllers:public drogon::HttpController { public: METHOD_LIST_BEGIN - METHOD_ADD(devices::post_discover, "/discover", Post); - METHOD_ADD(devices::get_all, "/", Get); - METHOD_ADD(devices::get_one_by_id, "/{1}", Get); - METHOD_ADD(devices::delete_one_by_id, "/{1}", Delete); + METHOD_ADD(controllers::post_discover, "/discover", Post); + METHOD_ADD(controllers::get_all, "/", Get); + METHOD_ADD(controllers::get_one_by_id, "/{1}", Get); + METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete); - METHOD_ADD(devices::get_relays_all, "/{1}/relays/", Get); - METHOD_ADD(devices::get_relays_one_by_id_and_num, "/{1}/relays/{2}", Get); - METHOD_ADD(devices::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, "filters::json_required", "filters::relays::valid_json"); + METHOD_ADD(controllers::get_relays_all, "/{1}/relays/", Get); + METHOD_ADD(controllers::get_relays_one_by_id_and_num, "/{1}/relays/{2}", Get); + METHOD_ADD(controllers::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, "filters::json_required", "filters::relays::valid_json"); METHOD_LIST_END static void post_discover(const HttpRequestPtr& req, std::function &&callback); static void get_all(const HttpRequestPtr& req, std::function &&callback); - static void get_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& device_id); - static void delete_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& device_id); + static void get_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id); + static void delete_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id); - static void get_relays_all(const HttpRequestPtr& req, std::function &&callback, const std::string& device_id); - static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function &&callback, const std::string& device_id, int relay_num); - static void put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function &&callback, const std::string& device_id, int relay_num); + static void get_relays_all(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id); + static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id, int relay_num); + static void put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id, int relay_num); }; } } diff --git a/controllers/api_v1_devices_discover.cc b/controllers/api_v1_controllers_discover.cc similarity index 66% rename from controllers/api_v1_devices_discover.cc rename to controllers/api_v1_controllers_discover.cc index 0ebab5c..6f19c91 100644 --- a/controllers/api_v1_devices_discover.cc +++ b/controllers/api_v1_controllers_discover.cc @@ -3,11 +3,11 @@ #include #include #include -#include -#include "api_v1_devices.h" +#include +#include "api_v1_controllers.h" using namespace api::v1; -void devices::post_discover(const HttpRequestPtr &req, std::function &&callback) +void controllers::post_discover(const HttpRequestPtr &req, std::function &&callback) { int discover_server_socket = helpers::bind_tcp_server("0.0.0.0", "0", config::discover_max_client_backlog); @@ -43,7 +43,7 @@ void devices::post_discover(const HttpRequestPtr &req, std::functionid, discovered_id) == 0) + if(strcmp(known_controllers[i]->id, discovered_id) == 0) { - known_devices[i]->active = true; - known_devices[i]->update(); - free(known_devices[i]); + known_controllers[i]->active = true; + known_controllers[i]->update(); + free(known_controllers[i]); found_discovered_in_list = true; - known_devices[i] = known_devices[i + 1]; + known_controllers[i] = known_controllers[i + 1]; } } else { - known_devices[i] = known_devices[i + 1]; + known_controllers[i] = known_controllers[i + 1]; } } if(!found_discovered_in_list) { - device_dbo discovered_device{}; - strcpy(discovered_device.ip, inet_ntoa(addr.sin_addr)); - strcpy(discovered_device.id, discovered_id); - strcpy(discovered_device.name, client_info["name"].asCString()); - discovered_device.relay_count = client_info["relay_count"].asInt(); - discovered_device.port = client_info["port"].asInt(); - discovered_device.active = true; + controller_dbo discovered_controller{}; + strcpy(discovered_controller.ip, inet_ntoa(addr.sin_addr)); + strcpy(discovered_controller.id, discovered_id); + strcpy(discovered_controller.name, client_info["name"].asCString()); + discovered_controller.relay_count = client_info["relay_count"].asInt(); + discovered_controller.port = client_info["port"].asInt(); + discovered_controller.active = true; - discovered_device.insert(); + discovered_controller.insert(); } discover_answer_buf[0] = config::discover_code_accept; @@ -142,27 +142,27 @@ void devices::post_discover(const HttpRequestPtr &req, std::functionactive = false; - known_devices[i]->update(); - LOG_DEBUG << "Lost: " << known_devices[i]->name; + known_controllers[i]->active = false; + known_controllers[i]->update(); + LOG_DEBUG << "Lost: " << known_controllers[i]->name; } - device_dbo::free_list(known_devices); + controller_dbo::free_list(known_controllers); - device_dbo **all_devices = device_dbo::get_all(); - Json::Value all_devices_json(Json::arrayValue); + controller_dbo **all_controllers = controller_dbo::get_all(); + Json::Value all_controllers_json(Json::arrayValue); - for(int i = 0; all_devices[i] != nullptr; i++) + for(int i = 0; all_controllers[i] != nullptr; i++) { - all_devices_json.append(all_devices[i]->to_json()); + all_controllers_json.append(all_controllers[i]->to_json()); } - auto resp = HttpResponse::newHttpJsonResponse(all_devices_json); + auto resp = HttpResponse::newHttpJsonResponse(all_controllers_json); callback(resp); - device_dbo::free_list(all_devices); + controller_dbo::free_list(all_controllers); } diff --git a/controllers/api_v1_devices.cc b/controllers/api_v1_devices.cc deleted file mode 100644 index d8aaa4f..0000000 --- a/controllers/api_v1_devices.cc +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include "api_v1_devices.h" -using namespace api::v1; - -void -devices::get_all(const HttpRequestPtr &req, std::function &&callback) -{ - device_dbo **all_devices = device_dbo::get_all(); - Json::Value all_devices_json(Json::arrayValue); - - for(int i = 0; all_devices[i] != nullptr; i++) - { - all_devices_json.append(all_devices[i]->to_json()); - } - - auto resp = HttpResponse::newHttpJsonResponse(all_devices_json); - - callback(resp); - - device_dbo::free_list(all_devices); -} - -void -devices::get_one_by_id(const HttpRequestPtr &req, std::function &&callback, const std::string& device_id) -{ - device_dbo **devices = device_dbo::get_by_simple("id", device_id.c_str(), (intptr_t) &sqlite3_bind_text); - - if(devices[0]) - { - auto resp = HttpResponse::newHttpJsonResponse(devices[0]->to_json()); - - callback(resp); - } - else - { - auto resp = HttpResponse::newHttpResponse(); - resp->setStatusCode(k404NotFound); - - callback(resp); - } - device_dbo::free_list(devices); -} - -void -devices::delete_one_by_id(const HttpRequestPtr &req, std::function &&callback, const std::string& device_id) -{ - device_dbo **devices = device_dbo::get_by_simple("id", device_id.c_str(), (intptr_t) &sqlite3_bind_text); - - if(devices[0]) - { - auto resp = HttpResponse::newHttpResponse(); - if(!devices[0]->remove()) - { - resp->setStatusCode(k500InternalServerError); - } - - callback(resp); - } - else - { - auto resp = HttpResponse::newHttpResponse(); - resp->setStatusCode(k404NotFound); - - callback(resp); - } - device_dbo::free_list(devices); -} diff --git a/controllers/api_v1_devices_relays.cc b/controllers/api_v1_devices_relays.cc index 6603480..0d1c26a 100644 --- a/controllers/api_v1_devices_relays.cc +++ b/controllers/api_v1_devices_relays.cc @@ -1,36 +1,36 @@ #include #include #include -#include -#include "api_v1_devices.h" +#include +#include "api_v1_controllers.h" using namespace api::v1; void -devices::get_relays_all(const HttpRequestPtr &req, std::function &&callback, - const std::string& device_id) +controllers::get_relays_all(const HttpRequestPtr &req, std::function &&callback, + const std::string& controller_id) { - relay_dbo **all_device_relays = relay_dbo::get_by_simple("device_id", (void *) device_id.c_str(), (intptr_t) sqlite3_bind_text); + relay_dbo **all_controller_relays = relay_dbo::get_by_simple("controller_id", (void *) controller_id.c_str(), (intptr_t) sqlite3_bind_text); Json::Value all_relays_json(Json::arrayValue); - for(int i = 0; all_device_relays[i] != nullptr; i++) + for(int i = 0; all_controller_relays[i] != nullptr; i++) { - all_relays_json.append(all_device_relays[i]->to_json()); + all_relays_json.append(all_controller_relays[i]->to_json()); } auto resp = HttpResponse::newHttpJsonResponse(all_relays_json); callback(resp); - relay_dbo::free_list(all_device_relays); + relay_dbo::free_list(all_controller_relays); } void -devices::get_relays_one_by_id_and_num(const HttpRequestPtr &req, - std::function &&callback, const std::string& device_id, +controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req, + std::function &&callback, const std::string& controller_id, int relay_num) { - relay_dbo *relay = relay_dbo::get_relay_for_device(device_id.c_str(), relay_num); + relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id.c_str(), relay_num); if(relay) { @@ -50,11 +50,11 @@ devices::get_relays_one_by_id_and_num(const HttpRequestPtr &req, } void -devices::put_relays_one_by_id_and_num(const HttpRequestPtr &req, - std::function &&callback, const std::string& device_id, +controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, + std::function &&callback, const std::string& controller_id, int relay_num) { - if(!relay_dbo::valid_num_for_device(device_id.c_str(), relay_num)) + if(!relay_dbo::valid_num_for_controller(controller_id.c_str(), relay_num)) { auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k400BadRequest); @@ -62,7 +62,7 @@ devices::put_relays_one_by_id_and_num(const HttpRequestPtr &req, return; } - relay_dbo *relay = relay_dbo::get_relay_for_device(device_id.c_str(), relay_num); + relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id.c_str(), relay_num); Json::Value body = *req->getJsonObject(); bool db_action_result; @@ -80,7 +80,7 @@ devices::put_relays_one_by_id_and_num(const HttpRequestPtr &req, relay->number = relay_num; strncpy(relay->name, body["name"].asCString(), 128); strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 33); - strncpy(relay->device_id, device_id.c_str(), 33); + strncpy(relay->controller_id, controller_id.c_str(), 33); db_action_result = relay->insert(); } diff --git a/controllers/api_v1_schedules.h b/controllers/api_v1_schedules.h index fd37132..a600a8f 100644 --- a/controllers/api_v1_schedules.h +++ b/controllers/api_v1_schedules.h @@ -14,8 +14,8 @@ namespace api METHOD_ADD(schedules::get_one_by_id, "/{1}", Get); METHOD_ADD(schedules::delete_one_by_id, "/{1}", Delete); METHOD_ADD(schedules::put_one_by_id, "/{1}", Put, "filters::json_required", "filters::schedules::valid_json"); - //METHOD_ADD(Devices::get_relays_all,"/{1}/relays",Get); - //METHOD_ADD(Devices::get_relays_one,"/{1}/relays/{2}",Get); + //METHOD_ADD(controllers::get_relays_all,"/{1}/relays",Get); + //METHOD_ADD(controllers::get_relays_one,"/{1}/relays/{2}",Get); METHOD_LIST_END static void post_new(const HttpRequestPtr& req, std::function &&callback); diff --git a/main.cc b/main.cc index 9a89e9d..58c6fd5 100644 --- a/main.cc +++ b/main.cc @@ -1,7 +1,7 @@ #include #include -#include +#include #include #include "globals.h" diff --git a/models/controller_dbo.cc b/models/controller_dbo.cc new file mode 100644 index 0000000..982944b --- /dev/null +++ b/models/controller_dbo.cc @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include +#include "controller_dbo.h" +#include "globals.h" + +static bool controller_db_update_insert(controller_dbo *controller, sqlite3_stmt *stmt) +{ + int rc; + + sqlite3_bind_text(stmt, 1, controller->id, -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_int(stmt, 4, controller->active); + sqlite3_bind_int(stmt, 5, controller->port); + sqlite3_bind_int(stmt, 6, controller->relay_count); + + rc = sqlite3_step(stmt); + if (rc != SQLITE_DONE) + { + printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db)); + return false; + } + + sqlite3_finalize(stmt); + + return true; +} + +static controller_dbo* +controller_db_select_mapper(sqlite3_stmt *stmt) +{ + auto *new_controller = new controller_dbo(); + for(int i = 0; i < sqlite3_column_count(stmt); i++) + { + const char *name = sqlite3_column_name(stmt, i); + switch(name[0]) + { + case 'a': // active + new_controller->active = (bool)sqlite3_column_int(stmt, i); + break; + case 'i': + switch(name[1]) + { + case 'd': // id + strncpy(new_controller->id, (const char*)sqlite3_column_text(stmt, i), 32); + break; + case 'p': // ip + strncpy(new_controller->ip, (const char*)sqlite3_column_text(stmt, i), 16); + break; + default: // ignore columns not implemented + break; + } + break; + case 'n': // name + strncpy(new_controller->name, (const char*)sqlite3_column_text(stmt, i), 127); + break; + case 'p': // port + new_controller->port = sqlite3_column_int(stmt, i); + break; + case 'r': // relay_count + new_controller->relay_count = sqlite3_column_int(stmt, i); + break; + default: // ignore columns not implemented + break; + } + } + return new_controller; +} + +static controller_dbo** +controller_db_select(sqlite3_stmt *stmt) +{ + auto **all_controllers = (controller_dbo**)malloc(sizeof(controller_dbo*)); + + int row = 0; + + while(true) + { + int s; + + s = sqlite3_step(stmt); + if (s == SQLITE_ROW) + { + controller_dbo *new_controller = controller_db_select_mapper(stmt); + row++; + + all_controllers = (controller_dbo**)realloc(all_controllers, sizeof(controller_dbo*) * (row + 1)); + all_controllers[row - 1] = new_controller; + } + else + { + if (s == SQLITE_DONE) + { + break; + } + else + { + LOG_ERROR << "Error Selecting controllers from database"; + sqlite3_finalize(stmt); + return nullptr; + } + } + } + + sqlite3_finalize(stmt); + all_controllers[row] = nullptr; + + return all_controllers; +} + +bool +controller_dbo::update() +{ + sqlite3_stmt *stmt; + + sqlite3_prepare_v2(globals::db, "UPDATE controllers set name = ?2, ip = ?3, active = ?4, port = ?5, relay_count = ?6 WHERE id = ?1;", -1, &stmt, nullptr); + + return controller_db_update_insert(this, stmt); +} + +bool +controller_dbo::insert() +{ + sqlite3_stmt *stmt; + + sqlite3_prepare_v2(globals::db, "INSERT INTO controllers(id, name, ip, active, port, relay_count) values (?1, ?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr); + + return controller_db_update_insert(this, stmt); +} + +bool +controller_dbo::remove() +{ + sqlite3_stmt *stmt; + int rc; + + sqlite3_prepare_v2(globals::db, "DELETE FROM controllers WHERE id=?1;", -1, &stmt, nullptr); + sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC); + rc = sqlite3_step(stmt); + sqlite3_finalize(stmt); + + return rc == SQLITE_DONE; + +} + +Json::Value +controller_dbo::to_json() +{ + Json::Value controller_json; + controller_json["name"] = this->name; + controller_json["id"] = this->id; + controller_json["ip"] = this->ip; + controller_json["port"] = this->port; + controller_json["relay_count"] = this->relay_count; + controller_json["active"] = this->active; + + return controller_json; +} + +controller_dbo** +controller_dbo::get_all() +{ + sqlite3_stmt *stmt; + + sqlite3_prepare_v2(globals::db, "SELECT * FROM controllers;", -1, &stmt, nullptr); + + return controller_db_select(stmt); +} + +controller_dbo** +controller_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func) +{ + helpers::sql_filter_builder *filters[1]; + helpers::sql_filter_builder filter + { + key, + value, + bind_func, + ";" + }; + filters[0] = &filter; + sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM controllers WHERE", filters); + + return controller_db_select(stmt); +} + +controller_dbo** +controller_dbo::get_by(helpers::sql_filter_builder **filters) +{ + sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM controllers WHERE", filters); + + return controller_db_select(stmt); +} + +void +controller_dbo::free_list(controller_dbo **controllers_list) +{ + for(int i = 0; controllers_list[i] != nullptr; i++) + { + free(controllers_list[i]); + } + free(controllers_list); +} + diff --git a/models/device_dbo.h b/models/controller_dbo.h similarity index 63% rename from models/device_dbo.h rename to models/controller_dbo.h index 89db361..ed6060f 100644 --- a/models/device_dbo.h +++ b/models/controller_dbo.h @@ -1,12 +1,12 @@ -#ifndef EMGAUWA_CORE_DEVICE_DBO_H -#define EMGAUWA_CORE_DEVICE_DBO_H +#ifndef EMGAUWA_CORE_controller_DBO_H +#define EMGAUWA_CORE_controller_DBO_H #include #include #include #include -class device_dbo +class controller_dbo { public: @@ -30,17 +30,17 @@ public: to_json(); static void - free_list(device_dbo **devices_list); + free_list(controller_dbo **controllers_list); - static device_dbo** + static controller_dbo** get_by_simple(const char *key, const void *value, intptr_t bind_func); - static device_dbo** + static controller_dbo** get_by(helpers::sql_filter_builder **filters); - static device_dbo** + static controller_dbo** get_all(); }; -#endif //EMGAUWA_CORE_DEVICE_DBO_H +#endif //EMGAUWA_CORE_controller_DBO_H diff --git a/models/device_dbo.cc b/models/device_dbo.cc deleted file mode 100644 index 8c9938e..0000000 --- a/models/device_dbo.cc +++ /dev/null @@ -1,207 +0,0 @@ -#include -#include -#include -#include -#include -#include "device_dbo.h" -#include "globals.h" - -static bool device_db_update_insert(device_dbo *device, sqlite3_stmt *stmt) -{ - int rc; - - sqlite3_bind_text(stmt, 1, device->id, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, device->name, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 3, device->ip, -1, SQLITE_STATIC); - sqlite3_bind_int(stmt, 4, device->active); - sqlite3_bind_int(stmt, 5, device->port); - sqlite3_bind_int(stmt, 6, device->relay_count); - - rc = sqlite3_step(stmt); - if (rc != SQLITE_DONE) - { - printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db)); - return false; - } - - sqlite3_finalize(stmt); - - return true; -} - -static device_dbo* -device_db_select_mapper(sqlite3_stmt *stmt) -{ - auto *new_device = new device_dbo(); - for(int i = 0; i < sqlite3_column_count(stmt); i++) - { - const char *name = sqlite3_column_name(stmt, i); - switch(name[0]) - { - case 'a': // active - new_device->active = (bool)sqlite3_column_int(stmt, i); - break; - case 'i': - switch(name[1]) - { - case 'd': // id - strncpy(new_device->id, (const char*)sqlite3_column_text(stmt, i), 32); - break; - case 'p': // ip - strncpy(new_device->ip, (const char*)sqlite3_column_text(stmt, i), 16); - break; - default: // ignore columns not implemented - break; - } - break; - case 'n': // name - strncpy(new_device->name, (const char*)sqlite3_column_text(stmt, i), 127); - break; - case 'p': // port - new_device->port = sqlite3_column_int(stmt, i); - break; - case 'r': // relay_count - new_device->relay_count = sqlite3_column_int(stmt, i); - break; - default: // ignore columns not implemented - break; - } - } - return new_device; -} - -static device_dbo** -device_db_select(sqlite3_stmt *stmt) -{ - auto **all_devices = (device_dbo**)malloc(sizeof(device_dbo*)); - - int row = 0; - - while(true) - { - int s; - - s = sqlite3_step(stmt); - if (s == SQLITE_ROW) - { - device_dbo *new_device = device_db_select_mapper(stmt); - row++; - - all_devices = (device_dbo**)realloc(all_devices, sizeof(device_dbo*) * (row + 1)); - all_devices[row - 1] = new_device; - } - else - { - if (s == SQLITE_DONE) - { - break; - } - else - { - LOG_ERROR << "Error Selecting devices from database"; - sqlite3_finalize(stmt); - return nullptr; - } - } - } - - sqlite3_finalize(stmt); - all_devices[row] = nullptr; - - return all_devices; -} - -bool -device_dbo::update() -{ - sqlite3_stmt *stmt; - - sqlite3_prepare_v2(globals::db, "UPDATE devices set name = ?2, ip = ?3, active = ?4, port = ?5, relay_count = ?6 WHERE id = ?1;", -1, &stmt, nullptr); - - return device_db_update_insert(this, stmt); -} - -bool -device_dbo::insert() -{ - sqlite3_stmt *stmt; - - sqlite3_prepare_v2(globals::db, "INSERT INTO devices(id, name, ip, active, port, relay_count) values (?1, ?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr); - - return device_db_update_insert(this, stmt); -} - -bool -device_dbo::remove() -{ - sqlite3_stmt *stmt; - int rc; - - sqlite3_prepare_v2(globals::db, "DELETE FROM devices WHERE id=?1;", -1, &stmt, nullptr); - sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC); - rc = sqlite3_step(stmt); - sqlite3_finalize(stmt); - - return rc == SQLITE_DONE; - -} - -Json::Value -device_dbo::to_json() -{ - Json::Value device_json; - device_json["name"] = this->name; - device_json["id"] = this->id; - device_json["ip"] = this->ip; - device_json["port"] = this->port; - device_json["relay_count"] = this->relay_count; - device_json["active"] = this->active; - - return device_json; -} - -device_dbo** -device_dbo::get_all() -{ - sqlite3_stmt *stmt; - - sqlite3_prepare_v2(globals::db, "SELECT * FROM devices;", -1, &stmt, nullptr); - - return device_db_select(stmt); -} - -device_dbo** -device_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func) -{ - helpers::sql_filter_builder *filters[1]; - helpers::sql_filter_builder filter - { - key, - value, - bind_func, - ";" - }; - filters[0] = &filter; - sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM devices WHERE", filters); - - return device_db_select(stmt); -} - -device_dbo** -device_dbo::get_by(helpers::sql_filter_builder **filters) -{ - sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM devices WHERE", filters); - - return device_db_select(stmt); -} - -void -device_dbo::free_list(device_dbo **devices_list) -{ - for(int i = 0; devices_list[i] != nullptr; i++) - { - free(devices_list[i]); - } - free(devices_list); -} - diff --git a/models/relay_dbo.cc b/models/relay_dbo.cc index 5b1b636..5343788 100644 --- a/models/relay_dbo.cc +++ b/models/relay_dbo.cc @@ -5,7 +5,7 @@ #include #include "relay_dbo.h" #include "globals.h" -#include "device_dbo.h" +#include "controller_dbo.h" static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt) { @@ -15,7 +15,7 @@ static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt) sqlite3_bind_int(stmt, 2, relay->number); sqlite3_bind_text(stmt, 3, relay->name, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 4, relay->active_schedule_id, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 5, relay->device_id, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 5, relay->controller_id, -1, SQLITE_STATIC); rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) @@ -41,8 +41,8 @@ relay_db_select_mapper(sqlite3_stmt *stmt) case 'a': // active_schedule_id strncpy(new_relay->active_schedule_id, (const char*)sqlite3_column_text(stmt, i), 33); break; - case 'd': // device_id - strncpy(new_relay->device_id, (const char*)sqlite3_column_text(stmt, i), 33); + case 'd': // controller_id + strncpy(new_relay->controller_id, (const char*)sqlite3_column_text(stmt, i), 33); break; case 'i': new_relay->id = sqlite3_column_int(stmt, i); @@ -111,7 +111,7 @@ relay_dbo::update() { sqlite3_stmt *stmt; - sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, active_schedule_id = ?4, device_id = ?5 WHERE id = ?1;", -1, &stmt, nullptr); + sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, active_schedule_id = ?4, controller_id = ?5 WHERE id = ?1;", -1, &stmt, nullptr); return relay_db_update_insert(this, stmt); } @@ -121,7 +121,7 @@ relay_dbo::insert() { sqlite3_stmt *stmt; - sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, active_schedule_id, device_id) values (?2, ?3, ?4, ?5);", -1, &stmt, nullptr); + sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, active_schedule_id, controller_id) values (?2, ?3, ?4, ?5);", -1, &stmt, nullptr); return relay_db_update_insert(this, stmt); } @@ -149,7 +149,7 @@ relay_dbo::to_json() relay_json["name"] = this->name; relay_json["number"] = this->number; relay_json["active_schedule_id"] = this->active_schedule_id; - relay_json["device_id"] = this->device_id; + relay_json["controller_id"] = this->controller_id; return relay_json; } @@ -189,7 +189,7 @@ relay_dbo::get_by(helpers::sql_filter_builder **filters) } relay_dbo* -relay_dbo::get_relay_for_device(const char *device_id, int relay_num) +relay_dbo::get_relay_for_controller(const char *controller_id, int relay_num) { helpers::sql_filter_builder *filters[2]; helpers::sql_filter_builder filter( @@ -199,8 +199,8 @@ relay_dbo::get_relay_for_device(const char *device_id, int relay_num) "AND" ); helpers::sql_filter_builder filter2( - "device_id", - (void*)device_id, + "controller_id", + (void*)controller_id, (intptr_t)sqlite3_bind_text, ";" ); @@ -215,12 +215,12 @@ relay_dbo::get_relay_for_device(const char *device_id, int relay_num) } bool -relay_dbo::valid_num_for_device(const char *search_device_id, int relay_num) +relay_dbo::valid_num_for_controller(const char *search_controller_id, int relay_num) { - device_dbo **devices = device_dbo::get_by_simple("id", search_device_id, (intptr_t)&sqlite3_bind_text); + controller_dbo **controllers = controller_dbo::get_by_simple("id", search_controller_id, (intptr_t)&sqlite3_bind_text); - bool valid_id_and_num = devices[0] && devices[0]->relay_count > relay_num; - device_dbo::free_list(devices); + bool valid_id_and_num = controllers[0] && controllers[0]->relay_count > relay_num; + controller_dbo::free_list(controllers); return valid_id_and_num; } diff --git a/models/relay_dbo.h b/models/relay_dbo.h index 7aef19f..25f1678 100644 --- a/models/relay_dbo.h +++ b/models/relay_dbo.h @@ -13,7 +13,7 @@ public: int id; char name[128]; int number; - char device_id[33]; + char controller_id[33]; char active_schedule_id[33]; bool @@ -38,10 +38,10 @@ public: get_by(helpers::sql_filter_builder **filters); static relay_dbo* - get_relay_for_device(const char *device_id, int relay_num); + get_relay_for_controller(const char *controller_id, int relay_num); static bool - valid_num_for_device(const char *search_device_id, int relay_num); + valid_num_for_controller(const char *search_controller_id, int relay_num); static relay_dbo** get_all();