2019-07-12 19:05:56 +00:00
|
|
|
#include <cstdio>
|
2019-07-19 09:41:39 +00:00
|
|
|
#include <cstring>
|
2019-07-20 12:51:45 +00:00
|
|
|
#include <cstring>
|
2019-07-19 09:41:39 +00:00
|
|
|
#include <trantor/utils/Logger.h>
|
2019-07-20 21:33:17 +00:00
|
|
|
#include <helpers.h>
|
2019-07-12 19:05:56 +00:00
|
|
|
#include "device_dbo.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
2019-07-19 09:41:39 +00:00
|
|
|
static bool device_db_update_insert(device_dbo *device, sqlite3_stmt *stmt)
|
2019-07-12 19:05:56 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2019-07-19 09:41:39 +00:00
|
|
|
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);
|
2019-07-12 19:05:56 +00:00
|
|
|
|
|
|
|
rc = sqlite3_step(stmt);
|
2019-07-19 09:41:39 +00:00
|
|
|
if (rc != SQLITE_DONE)
|
|
|
|
{
|
2019-07-12 19:05:56 +00:00
|
|
|
printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-19 09:41:39 +00:00
|
|
|
|
|
|
|
static device_dbo*
|
|
|
|
device_db_select_mapper(sqlite3_stmt *stmt)
|
|
|
|
{
|
|
|
|
auto *new_device = (device_dbo*)malloc(sizeof(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
|
2019-07-20 12:51:45 +00:00
|
|
|
new_device->active = (bool)sqlite3_column_int(stmt, i);
|
2019-07-19 09:41:39 +00:00
|
|
|
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;
|
2019-07-20 12:51:45 +00:00
|
|
|
default: // ignore columns not implemented
|
|
|
|
break;
|
2019-07-19 09:41:39 +00:00
|
|
|
}
|
|
|
|
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;
|
2019-07-20 12:51:45 +00:00
|
|
|
default: // ignore columns not implemented
|
|
|
|
break;
|
2019-07-19 09:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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";
|
2019-07-19 12:42:36 +00:00
|
|
|
sqlite3_finalize(stmt);
|
2019-07-19 09:41:39 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 12:42:36 +00:00
|
|
|
sqlite3_finalize(stmt);
|
2019-07-19 09:41:39 +00:00
|
|
|
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);
|
2019-07-19 12:42:36 +00:00
|
|
|
sqlite3_finalize(stmt);
|
2019-07-19 09:41:39 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-20 21:33:17 +00:00
|
|
|
device_dbo**
|
|
|
|
device_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_func)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
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);
|
2019-07-19 09:41:39 +00:00
|
|
|
|
2019-07-20 21:33:17 +00:00
|
|
|
return device_db_select(stmt);
|
2019-07-19 09:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|