fix: rename "device" to "controller"
This commit is contained in:
parent
9981eeabcb
commit
368df26ad7
12 changed files with 362 additions and 362 deletions
68
controllers/api_v1_controllers.cc
Normal file
68
controllers/api_v1_controllers.cc
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include <netdb.h>
|
||||
#include <models/controller_dbo.h>
|
||||
#include "api_v1_controllers.h"
|
||||
using namespace api::v1;
|
||||
|
||||
void
|
||||
controllers::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&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<void(const HttpResponsePtr &)> &&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<void(const HttpResponsePtr &)> &&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);
|
||||
}
|
|
@ -5,28 +5,28 @@ namespace api
|
|||
{
|
||||
namespace v1
|
||||
{
|
||||
class devices:public drogon::HttpController<devices>
|
||||
class controllers:public drogon::HttpController<controllers>
|
||||
{
|
||||
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<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& device_id);
|
||||
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& device_id);
|
||||
static void get_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);
|
||||
|
||||
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& device_id);
|
||||
static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& device_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& device_id, int relay_num);
|
||||
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
||||
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 put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num);
|
||||
};
|
||||
}
|
||||
}
|
|
@ -3,11 +3,11 @@
|
|||
#include <config.h>
|
||||
#include <helpers.h>
|
||||
#include <cmath>
|
||||
#include <models/device_dbo.h>
|
||||
#include "api_v1_devices.h"
|
||||
#include <models/controller_dbo.h>
|
||||
#include "api_v1_controllers.h"
|
||||
using namespace api::v1;
|
||||
|
||||
void devices::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
|
||||
void controllers::post_discover(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&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::function<void(const
|
|||
uint8_t discover_answer_buf[1];
|
||||
uint8_t discover_header_buf[1];
|
||||
|
||||
device_dbo **known_devices = device_dbo::get_all();
|
||||
controller_dbo **known_controllers = controller_dbo::get_all();
|
||||
|
||||
while(true)
|
||||
{
|
||||
|
@ -105,36 +105,36 @@ void devices::post_discover(const HttpRequestPtr &req, std::function<void(const
|
|||
|
||||
bool found_discovered_in_list = false;
|
||||
|
||||
for(int i = 0; known_devices[i] != nullptr; i++)
|
||||
for(int i = 0; known_controllers[i] != nullptr; i++)
|
||||
{
|
||||
if(!found_discovered_in_list)
|
||||
{
|
||||
if(strcmp(known_devices[i]->id, 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::function<void(const
|
|||
close(client_fd);
|
||||
}
|
||||
}
|
||||
for(int i = 0; known_devices[i] != nullptr; i++)
|
||||
for(int i = 0; known_controllers[i] != nullptr; i++)
|
||||
{
|
||||
known_devices[i]->active = 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);
|
||||
|
||||
}
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
#include <netdb.h>
|
||||
#include <models/device_dbo.h>
|
||||
#include "api_v1_devices.h"
|
||||
using namespace api::v1;
|
||||
|
||||
void
|
||||
devices::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&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<void(const HttpResponsePtr &)> &&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<void(const HttpResponsePtr &)> &&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);
|
||||
}
|
|
@ -1,36 +1,36 @@
|
|||
#include <netdb.h>
|
||||
#include <models/relay_dbo.h>
|
||||
#include <helpers.h>
|
||||
#include <models/device_dbo.h>
|
||||
#include "api_v1_devices.h"
|
||||
#include <models/controller_dbo.h>
|
||||
#include "api_v1_controllers.h"
|
||||
|
||||
using namespace api::v1;
|
||||
|
||||
void
|
||||
devices::get_relays_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
const std::string& device_id)
|
||||
controllers::get_relays_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&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<void(const HttpResponsePtr &)> &&callback, const std::string& device_id,
|
||||
controllers::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||
std::function<void(const HttpResponsePtr &)> &&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<void(const HttpResponsePtr &)> &&callback, const std::string& device_id,
|
||||
controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
||||
std::function<void(const HttpResponsePtr &)> &&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();
|
||||
}
|
||||
|
|
|
@ -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<void (const HttpResponsePtr &)> &&callback);
|
||||
|
|
2
main.cc
2
main.cc
|
@ -1,7 +1,7 @@
|
|||
#include <drogon/drogon.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <models/device_dbo.h>
|
||||
#include <models/controller_dbo.h>
|
||||
#include <csignal>
|
||||
|
||||
#include "globals.h"
|
||||
|
|
207
models/controller_dbo.cc
Normal file
207
models/controller_dbo.cc
Normal file
|
@ -0,0 +1,207 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstring>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <helpers.h>
|
||||
#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);
|
||||
}
|
||||
|
|
@ -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 <string>
|
||||
#include <sqlite3.h>
|
||||
#include <json/value.h>
|
||||
#include <helpers.h>
|
||||
|
||||
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
|
|
@ -1,207 +0,0 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstring>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include <helpers.h>
|
||||
#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);
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@
|
|||
#include <helpers.h>
|
||||
#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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue