add: device relay get one

fix: stuff
This commit is contained in:
Tobias Reisinger 2019-07-21 00:29:05 +02:00
parent ac61c26d56
commit a399050892
7 changed files with 64 additions and 9 deletions

View file

@ -1,5 +1,6 @@
#include <netdb.h> #include <netdb.h>
#include <models/relay_dbo.h> #include <models/relay_dbo.h>
#include <helpers.h>
#include "api_v1_devices.h" #include "api_v1_devices.h"
using namespace api::v1; using namespace api::v1;
@ -9,8 +10,7 @@ devices::get_relays_all(const HttpRequestPtr &req, std::function<void(const Http
const std::string& device_id) const std::string& device_id)
{ {
relay_dbo **all_device_relays = relay_dbo::get_by_simple("device_id", (void *) device_id.c_str(), relay_dbo **all_device_relays = relay_dbo::get_by_simple("device_id", (void *) device_id.c_str(), (intptr_t) sqlite3_bind_text);
(intptr_t) sqlite3_bind_text);
Json::Value all_relays_json(Json::arrayValue); Json::Value all_relays_json(Json::arrayValue);
for(int i = 0; all_device_relays[i] != nullptr; i++) for(int i = 0; all_device_relays[i] != nullptr; i++)
@ -30,7 +30,25 @@ devices::get_relays_one_by_id_and_num(const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id,
int relay_num) int relay_num)
{ {
relay_dbo **relays = relay_dbo::get_by_simple("id", (void *) (intptr_t) relay_num, (intptr_t) &sqlite3_bind_int); helpers::sql_filter_builder *filters[2];
helpers::sql_filter_builder filter
{
"number",
(void*)(intptr_t)relay_num,
(intptr_t)&sqlite3_bind_int,
"AND"
};
helpers::sql_filter_builder filter2
{
"device_id",
(void*)device_id.c_str(),
(intptr_t)sqlite3_bind_text,
";"
};
filters[0] = &filter;
filters[1] = &filter2;
auto relays = relay_dbo::get_by(filters);
if(relays[0]) if(relays[0])
{ {

View file

@ -187,6 +187,14 @@ device_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func
return device_db_select(stmt); 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 void
device_dbo::free_list(device_dbo **devices_list) device_dbo::free_list(device_dbo **devices_list)
{ {

View file

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <sqlite3.h> #include <sqlite3.h>
#include <json/value.h> #include <json/value.h>
#include <helpers.h>
class device_dbo class device_dbo
{ {
@ -34,6 +35,9 @@ public:
static device_dbo** static device_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func); get_by_simple(const char *key, const void *value, intptr_t bind_func);
static device_dbo**
get_by(helpers::sql_filter_builder **filters);
static device_dbo** static device_dbo**
get_all(); get_all();
}; };

View file

@ -47,7 +47,7 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
new_relay->id = sqlite3_column_int(stmt, i); new_relay->id = sqlite3_column_int(stmt, i);
break; break;
case 'n': case 'n':
switch(name[0]) switch(name[1])
{ {
case 'a': // name case 'a': // name
strncpy(new_relay->name, (const char*)sqlite3_column_text(stmt, i), 127); strncpy(new_relay->name, (const char*)sqlite3_column_text(stmt, i), 127);
@ -93,9 +93,8 @@ relay_db_select(sqlite3_stmt *stmt)
} }
else else
{ {
LOG_ERROR << "Error Selecting relays from database"; LOG_ERROR << "Error Selecting relays from database: " << sqlite3_errstr(s);
sqlite3_finalize(stmt); break;
return nullptr;
} }
} }
} }
@ -145,8 +144,11 @@ Json::Value
relay_dbo::to_json() relay_dbo::to_json()
{ {
Json::Value relay_json; Json::Value relay_json;
// relay_json["id"] = this->id;
relay_json["name"] = this->name; relay_json["name"] = this->name;
relay_json["id"] = this->id; relay_json["number"] = this->number;
relay_json["active_schedule_id"] = this->active_schedule_id;
relay_json["device_id"] = this->device_id;
return relay_json; return relay_json;
} }
@ -178,6 +180,13 @@ relay_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
return relay_db_select(stmt); return relay_db_select(stmt);
} }
relay_dbo**
relay_dbo::get_by(helpers::sql_filter_builder **filters)
{
sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM relays WHERE", filters);
return relay_db_select(stmt);
}
void void
relay_dbo::free_list(relay_dbo **relays_list) relay_dbo::free_list(relay_dbo **relays_list)
{ {
@ -187,4 +196,3 @@ relay_dbo::free_list(relay_dbo **relays_list)
} }
free(relays_list); free(relays_list);
} }

View file

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <sqlite3.h> #include <sqlite3.h>
#include <json/value.h> #include <json/value.h>
#include <helpers.h>
class relay_dbo class relay_dbo
{ {
@ -33,6 +34,9 @@ public:
static relay_dbo** static relay_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func); get_by_simple(const char *key, const void *value, intptr_t bind_func);
static relay_dbo**
get_by(helpers::sql_filter_builder **filters);
static relay_dbo** static relay_dbo**
get_all(); get_all();
}; };

View file

@ -177,6 +177,15 @@ schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_fu
";" ";"
}; };
filters[0] = &filter; filters[0] = &filter;
sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters);
return schedule_db_select(stmt);
}
schedule_dbo**
schedule_dbo::get_by(helpers::sql_filter_builder **filters)
{
sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters); sqlite3_stmt *stmt = helpers::create_sql_filtered_query("SELECT * FROM schedules WHERE", filters);
return schedule_db_select(stmt); return schedule_db_select(stmt);

View file

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <sqlite3.h> #include <sqlite3.h>
#include <json/value.h> #include <json/value.h>
#include <helpers.h>
#include "period.h" #include "period.h"
#include "period_list.h" #include "period_list.h"
@ -35,6 +36,9 @@ public:
static schedule_dbo** static schedule_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func); get_by_simple(const char *key, const void *value, intptr_t bind_func);
static schedule_dbo**
get_by(helpers::sql_filter_builder **filters);
static schedule_dbo** static schedule_dbo**
get_all(); get_all();
}; };