add: tag support for controller and relay
add: static on schedule
This commit is contained in:
parent
9a44bc494e
commit
636fdf8df1
11 changed files with 96 additions and 18 deletions
|
@ -22,7 +22,8 @@ controllers::get_all(const HttpRequestPtr &req, std::function<void(const HttpRes
|
|||
}
|
||||
|
||||
void
|
||||
controllers::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& controller_id)
|
||||
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);
|
||||
|
||||
|
@ -42,6 +43,28 @@ controllers::get_one_by_id(const HttpRequestPtr &req, std::function<void(const H
|
|||
controller_dbo::free_list(controllers);
|
||||
}
|
||||
|
||||
void
|
||||
controllers::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
const std::string &controller_tag)
|
||||
{
|
||||
controller_dbo **controllers = controller_dbo::get_by_simple("tag", controller_tag.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)
|
||||
{
|
||||
|
|
|
@ -7,20 +7,22 @@ namespace api::v1
|
|||
{
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
METHOD_ADD(controllers::post_discover, "/discover", Post, Options);
|
||||
METHOD_ADD(controllers::get_all, "/", Get, Options);
|
||||
METHOD_ADD(controllers::get_one_by_id, "/{1}", Get, Options);
|
||||
METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete, Options);
|
||||
METHOD_ADD(controllers::put_one_by_id, "/{1}", Put, Options, "filters::json_required", "filters::controllers::valid_json");
|
||||
METHOD_ADD(controllers::post_discover, "/discover", Post, Options);
|
||||
METHOD_ADD(controllers::get_all, "/", Get, Options);
|
||||
METHOD_ADD(controllers::get_one_by_id, "/{1}", Get, Options);
|
||||
METHOD_ADD(controllers::get_one_by_tag, "/tag/{1}", Get, Options);
|
||||
METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete, Options);
|
||||
METHOD_ADD(controllers::put_one_by_id, "/{1}", Put, Options, "filters::json_required", "filters::controllers::valid_json");
|
||||
|
||||
METHOD_ADD(controllers::get_relays_all, "/{1}/relays/", Get, Options);
|
||||
METHOD_ADD(controllers::get_relays_one_by_id_and_num, "/{1}/relays/{2}", Get, Options);
|
||||
METHOD_ADD(controllers::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, Options, "filters::json_required", "filters::relays::valid_json");
|
||||
METHOD_ADD(controllers::get_relays_all, "/{1}/relays/", Get, Options);
|
||||
METHOD_ADD(controllers::get_relays_one_by_id_and_num, "/{1}/relays/{2}", Get, Options);
|
||||
METHOD_ADD(controllers::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, Options, "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& controller_id);
|
||||
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 put_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
||||
|
||||
|
|
|
@ -21,3 +21,25 @@ relays::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponse
|
|||
|
||||
relay_dbo::free_list(all_relays);
|
||||
}
|
||||
|
||||
void
|
||||
relays::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
const std::string &relay_tag)
|
||||
{
|
||||
relay_dbo **relays = relay_dbo::get_by_simple("tag", relay_tag.c_str(), (intptr_t)&sqlite3_bind_text);
|
||||
|
||||
if(relays[0])
|
||||
{
|
||||
auto resp = HttpResponse::newHttpJsonResponse(relays[0]->to_json());
|
||||
|
||||
callback(resp);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k404NotFound);
|
||||
|
||||
callback(resp);
|
||||
}
|
||||
relay_dbo::free_list(relays);
|
||||
}
|
|
@ -7,9 +7,11 @@ namespace api::v1
|
|||
{
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
METHOD_ADD(relays::get_all, "/", Get, Options);
|
||||
METHOD_ADD(relays::get_all, "/", Get, Options);
|
||||
METHOD_ADD(relays::get_one_by_tag, "/tag/{1}", Get, Options);
|
||||
METHOD_LIST_END
|
||||
|
||||
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
|
||||
static void get_one_by_tag(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& relay_tag);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
|
|||
void
|
||||
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
|
||||
{
|
||||
if(strcmp(schedule_id.c_str(), "off") == 0)
|
||||
if(strcmp(schedule_id.c_str(), "off") == 0 || strcmp(schedule_id.c_str(), "on") == 0)
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k403Forbidden);
|
||||
|
@ -109,7 +109,7 @@ void
|
|||
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
const std::string &schedule_id)
|
||||
{
|
||||
if(strcmp(schedule_id.c_str(), "off") == 0)
|
||||
if(strcmp(schedule_id.c_str(), "off") == 0 || strcmp(schedule_id.c_str(), "on") == 0)
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k403Forbidden);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <config.h>
|
||||
|
||||
#include <sql/migration_0.h>
|
||||
#include <sql/migration_1.h>
|
||||
|
||||
int
|
||||
helpers::migrate_sql()
|
||||
|
@ -32,6 +33,7 @@ helpers::migrate_sql()
|
|||
switch(version_num)
|
||||
{
|
||||
case 0:
|
||||
LOG_INFO << "Migrating LEVEL 0";
|
||||
rc = sqlite3_exec(globals::db, (const char *)sql_migration_0_sql, nullptr, nullptr, &err_msg);
|
||||
if(rc != 0)
|
||||
{
|
||||
|
@ -39,6 +41,15 @@ helpers::migrate_sql()
|
|||
break;
|
||||
}
|
||||
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:
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ static bool controller_db_update_insert(controller_dbo *controller, sqlite3_stmt
|
|||
sqlite3_bind_int(stmt, 4, controller->active);
|
||||
sqlite3_bind_int(stmt, 5, controller->port);
|
||||
sqlite3_bind_int(stmt, 6, controller->relay_count);
|
||||
sqlite3_bind_text(stmt, 7, controller->tag, -1, SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
|
@ -72,6 +73,9 @@ controller_db_select_mapper(sqlite3_stmt *stmt)
|
|||
case 'r': // relay_count
|
||||
new_controller->relay_count = sqlite3_column_int(stmt, i);
|
||||
break;
|
||||
case 't': // tag
|
||||
strncpy(new_controller->tag, (const char*)sqlite3_column_text(stmt, i), 63);
|
||||
break;
|
||||
default: // ignore columns not implemented
|
||||
break;
|
||||
}
|
||||
|
@ -126,7 +130,7 @@ 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);
|
||||
sqlite3_prepare_v2(globals::db, "UPDATE controllers set name = ?2, ip = ?3, active = ?4, port = ?5, relay_count = ?6, tag = ?7 WHERE id = ?1;", -1, &stmt, nullptr);
|
||||
|
||||
return controller_db_update_insert(this, stmt);
|
||||
}
|
||||
|
@ -136,7 +140,7 @@ 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);
|
||||
sqlite3_prepare_v2(globals::db, "INSERT INTO controllers(id, name, ip, active, port, relay_count, tag) values (?1, ?2, ?3, ?4, ?5, ?6, ?7);", -1, &stmt, nullptr);
|
||||
|
||||
return controller_db_update_insert(this, stmt);
|
||||
}
|
||||
|
@ -163,9 +167,9 @@ controller_dbo::to_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;
|
||||
controller_json["tag"] = this->tag;
|
||||
|
||||
|
||||
controller_json["relays"] = Json::arrayValue;
|
||||
|
|
|
@ -17,6 +17,7 @@ public:
|
|||
bool active;
|
||||
int port;
|
||||
int relay_count;
|
||||
char tag[64];
|
||||
relay_dbo **relays;
|
||||
|
||||
~controller_dbo();
|
||||
|
|
|
@ -16,6 +16,7 @@ static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
|
|||
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->controller_id, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 6, relay->tag, -1, SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
|
@ -59,6 +60,9 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
|
|||
default:
|
||||
break;
|
||||
}
|
||||
case 't': // tag
|
||||
strncpy(new_relay->tag, (const char*)sqlite3_column_text(stmt, i), 63);
|
||||
break;
|
||||
default: // ignore columns not implemented
|
||||
break;
|
||||
}
|
||||
|
@ -131,7 +135,7 @@ relay_dbo::update()
|
|||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
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);
|
||||
sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, active_schedule_id = ?4, controller_id = ?5, tag = ?6 WHERE id = ?1;", -1, &stmt, nullptr);
|
||||
|
||||
return relay_db_update_insert(this, stmt);
|
||||
}
|
||||
|
@ -141,7 +145,7 @@ relay_dbo::insert()
|
|||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, active_schedule_id, controller_id) values (?2, ?3, ?4, ?5);", -1, &stmt, nullptr);
|
||||
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, active_schedule_id, controller_id, tag) values (?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr);
|
||||
|
||||
return relay_db_update_insert(this, stmt);
|
||||
}
|
||||
|
@ -165,12 +169,12 @@ Json::Value
|
|||
relay_dbo::to_json()
|
||||
{
|
||||
Json::Value relay_json;
|
||||
// relay_json["id"] = this->id;
|
||||
relay_json["name"] = this->name;
|
||||
relay_json["number"] = this->number;
|
||||
relay_json["active_schedule_id"] = this->active_schedule_id;
|
||||
relay_json["controller_id"] = this->controller_id;
|
||||
relay_json["active_schedule"] = this->active_schedule->to_json();
|
||||
relay_json["tag"] = this->tag;
|
||||
|
||||
return relay_json;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ public:
|
|||
int number;
|
||||
char controller_id[33];
|
||||
char active_schedule_id[33];
|
||||
char tag[64];
|
||||
schedule_dbo *active_schedule;
|
||||
|
||||
void
|
||||
|
|
8
sql/migration_1.sql
Normal file
8
sql/migration_1.sql
Normal file
|
@ -0,0 +1,8 @@
|
|||
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');
|
||||
|
Loading…
Reference in a new issue