init rewrite

This commit is contained in:
Tobias Reisinger 2020-05-05 11:42:02 +02:00
parent 9a44bc494e
commit 6d828fcffc
100 changed files with 50541 additions and 2707 deletions

View file

@ -1,246 +0,0 @@
#include <cstdio>
#include <cstring>
#include <trantor/utils/Logger.h>
#include <sys/socket.h>
#include <unistd.h>
#include <helpers.h>
#include "controller_dbo.h"
#include "globals.h"
controller_dbo::~controller_dbo()
{
if(this->relays)
{
relay_dbo::free_list(this->relays);
}
}
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);
new_controller->relays = relay_dbo::get_by_simple("controller_id", new_controller->id, (intptr_t)&sqlite3_bind_text);
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;
controller_json["relays"] = Json::arrayValue;
for(int i = 0; this->relays[i] != nullptr; i++)
{
controller_json["relays"].append(this->relays[i]->to_json());
}
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);
}
bool
controller_dbo::command(int command_code, const char *payload)
{
char port_str[6];
sprintf(port_str, "%d", this->port);
int controller_socket = helpers::open_tcp_connection(this->ip, port_str);
if(!controller_socket)
{
LOG_ERROR << "Can't open command socket " << this->ip << ":" << port_str;
return false;
}
LOG_DEBUG << "Commanding (" << command_code << ") " << payload;
send(controller_socket, &command_code, 1, 0);
send(controller_socket, payload, strlen(payload), 0);
close(controller_socket);
return true;
}
void
controller_dbo::free_list(controller_dbo **controllers_list)
{
for(int i = 0; controllers_list[i] != nullptr; i++)
{
delete controllers_list[i];
}
free(controllers_list);
}

View file

@ -1,53 +0,0 @@
#ifndef EMGAUWA_CORE_controller_DBO_H
#define EMGAUWA_CORE_controller_DBO_H
#include <string>
#include <sqlite3.h>
#include <json/value.h>
#include <helpers.h>
#include "relay_dbo.h"
class controller_dbo
{
public:
char id[33];
char name[128];
char ip[17];
bool active;
int port;
int relay_count;
relay_dbo **relays;
~controller_dbo();
bool
update();
bool
insert();
bool
remove();
Json::Value
to_json();
static controller_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func);
static controller_dbo**
get_by(helpers::sql_filter_builder **filters);
static controller_dbo**
get_all();
bool
command(int command_code, const char *payload);
static void
free_list(controller_dbo **controllers_list);
};
#endif //EMGAUWA_CORE_controller_DBO_H

View file

@ -0,0 +1,114 @@
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <models/junction_relay_schedule.h>
#include <logger.h>
#include <database.h>
int
junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
{
int rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "INSERT INTO junction_relay_schedule(weekday, schedule_id, relay_id) values (?1, ?2, ?3);", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, weekday);
sqlite3_bind_int(stmt, 2, schedule_id);
sqlite3_bind_int(stmt, 3, relay_id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
LOG_ERROR("error inserting data: %s", sqlite3_errmsg(global_database));
return false;
}
sqlite3_finalize(stmt);
return true;
}
int
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
{
int result = 0;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT schedule_id FROM junction_relay_schedule WHERE weekday=?1 AND relay_id=?2 LIMIT 1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, weekday);
sqlite3_bind_int(stmt, 2, relay_id);
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
result = sqlite3_column_int(stmt, 0);
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR("error reading from database: %s", sqlite3_errstr(s));
break;
}
}
}
sqlite3_finalize(stmt);
return result;
}
int
junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(global_database, "DELETE FROM junction_relay_schedule WHERE weekday=?1 AND schedule_id=?2 AND relay_id=?3;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, weekday);
sqlite3_bind_int(stmt, 2, schedule_id);
sqlite3_bind_int(stmt, 3, relay_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
int
junction_relay_schedule_remove_for_relay(int relay_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(global_database, "DELETE FROM junction_relay_schedule WHERE relay_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, relay_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
int
junction_relay_schedule_remove_for_schedule(int schedule_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(global_database, "DELETE FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, schedule_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}

175
models/junction_tag.c Normal file
View file

@ -0,0 +1,175 @@
#include <stdlib.h>
#include <stddef.h>
#include <sqlite3.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <database.h>
int
junction_tag_insert(int tag_id, int relay_id, int schedule_id)
{
int rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "INSERT INTO junction_tag(tag_id, schedule_id, relay_id) values (?1, ?2, ?3);", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, tag_id);
sqlite3_bind_int(stmt, 2, schedule_id);
sqlite3_bind_int(stmt, 3, relay_id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
printf("ERROR inserting data: %s\n", sqlite3_errmsg(global_database));
return false;
}
sqlite3_finalize(stmt);
return true;
}
static int*
get_ids(sqlite3_stmt *stmt)
{
int *ids = malloc(sizeof(int));
int new_id;
int row = 0;
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
new_id = sqlite3_column_int(stmt, 0);
row++;
ids = (int*)realloc(ids, sizeof(int) * (row + 1));
ids[row - 1] = new_id;
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR("error selecting relays from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return NULL;
}
}
}
sqlite3_finalize(stmt);
ids[row] = 0;
return ids;
}
int*
junction_tag_get_relays_for_tag_id(int tag_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT relay_id FROM junction_tag WHERE tag_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, tag_id);
return get_ids(stmt);
}
int*
junction_tag_get_schedules_for_tag_id(int tag_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT schedule_id FROM junction_tag WHERE tag_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, tag_id);
return get_ids(stmt);
}
int*
junction_tag_get_tags_for_relay_id(int relay_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT tag_id FROM junction_tag WHERE relay_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, relay_id);
return get_ids(stmt);
}
int*
junction_tag_get_tags_for_schedule_id(int schedule_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT tag_id FROM junction_tag WHERE schedule_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, schedule_id);
return get_ids(stmt);
}
int
junction_tag_remove(int tag_id, int relay_id, int schedule_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(global_database, "DELETE FROM junction_tag WHERE tag_id=?1 AND schedule_id=?2 AND relay_id=?3;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, tag_id);
sqlite3_bind_int(stmt, 2, schedule_id);
sqlite3_bind_int(stmt, 3, relay_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
int
junction_tag_remove_for_tag(int tag_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(global_database, "DELETE FROM junction_tag WHERE tag_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, tag_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
int
junction_tag_remove_for_relay(int relay_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(global_database, "DELETE FROM junction_tag WHERE relay_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, relay_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
int
junction_tag_remove_for_schedule(int schedule_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(global_database, "DELETE FROM junction_tag WHERE schedule_id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, schedule_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}

37
models/period.c Normal file
View file

@ -0,0 +1,37 @@
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <models/period.h>
int
period_helper_parse_hhmm(const char *hhmm_str, uint16_t *hhmm)
{
if(strlen(hhmm_str) != 5)
{
return 1;
}
if(hhmm_str[2] != ':')
{
return 1;
}
for(int i = 0; i < 5; ++i)
{
if(i != 2)
{
if(hhmm_str[i] < '0' || hhmm_str[i] > '9')
{
return 1;
}
}
}
uint16_t tmp_h, tmp_m;
tmp_h = (uint16_t)strtol(&hhmm_str[0], NULL, 10);
tmp_m = (uint16_t)strtol(&hhmm_str[3], NULL, 10);
*hhmm = (tmp_h * 60) + tmp_m;
return 0;
}

View file

@ -1,39 +0,0 @@
#include <cstdio>
#include <cstdint>
#include "period.h"
period::period(uint16_t start, uint16_t end)
{
this->start = start;
this->end = end;
}
Json::Value
period::to_json()
{
Json::Value result;
char start_str[8], end_str[8];
sprintf(start_str, "%02d:%02d", this->start / 60, this->start % 60);
sprintf(end_str, "%02d:%02d", this->end / 60, this->end % 60);
result["start"] = std::string(start_str);
result["end"] = std::string(end_str);
return result;
}
bool
period::is_in_period(uint16_t timestamp)
{
if(this->start < this->end)
{
return this->start <= timestamp and timestamp <= this->end;
}
if(this->start > this->end)
{
return this->end <= timestamp and timestamp <= this->start;
}
return this->start == timestamp;
}

View file

@ -1,23 +0,0 @@
#ifndef EMGAUWA_CORE_PERIOD_H
#define EMGAUWA_CORE_PERIOD_H
#include <json/json.h>
class period
{
public:
uint16_t start;
uint16_t end;
period(uint16_t start, uint16_t end);
Json::Value
to_json();
bool
is_in_period(uint16_t timestamp);
};
#endif //EMGAUWA_CORE_PERIOD_H

View file

@ -1,70 +0,0 @@
#include <cstdint>
#include <cstdlib>
#include "period_list.h"
period_list::period_list()
{
this->periods = (period**)malloc(0);
this->length = 0;
}
period_list::period_list(const uint16_t* periods_blob)
{
this->length = periods_blob[0];
this->periods = (period**)malloc(sizeof(period*) * this->length);
for(int i = 0; i < length; i++)
{
auto new_period = new period(periods_blob[(i * 2) + 1], periods_blob[(i * 2) + 2]);
periods[i] = new_period;
}
}
period_list::period_list(period **periods, uint16_t length)
{
this->periods = periods;
this->length = length;
}
period_list::~period_list()
{
for(int i = 0; i < length; i++)
{
delete this->periods[i];
}
free(this->periods);
}
void
period_list::add_period(uint16_t start, uint16_t end)
{
this->length++;
this->periods = (period**)realloc(this->periods, sizeof(period*) * this->length);
this->periods[this->length - 1] = new period(start, end);
}
Json::Value
period_list::to_json()
{
Json::Value result(Json::arrayValue);
for(int i = 0; i < this->length; i++)
{
result.append(this->periods[i]->to_json());
}
return result;
}
uint16_t*
period_list::to_db_blob()
{
auto result = (uint16_t*)malloc(sizeof(uint16_t) * ((this->length * 2) + 1));
result[0] = this->length;
for(int i = 0; i < this->length; i++)
{
result[(i * 2) + 1] = this->periods[i]->start;
result[(i * 2) + 2] = this->periods[i]->end;
}
return result;
}

View file

@ -1,28 +0,0 @@
#ifndef EMGAUWA_CORE_PERIOD_LIST_H
#define EMGAUWA_CORE_PERIOD_LIST_H
#include <json/json.h>
#include "period.h"
class period_list
{
public:
period **periods;
uint16_t length;
period_list();
explicit period_list(const uint16_t *periods_blob);
period_list(period **periods, uint16_t length);
~period_list();
void
add_period(uint16_t start, uint16_t end);
Json::Value
to_json();
uint16_t*
to_db_blob();
};
#endif //EMGAUWA_CORE_PERIOD_LIST_H

View file

@ -1,257 +0,0 @@
#include <cstdio>
#include <cstring>
#include <trantor/utils/Logger.h>
#include <helpers.h>
#include "relay_dbo.h"
#include "globals.h"
#include "controller_dbo.h"
#include "schedule_dbo.h"
static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
{
int rc;
sqlite3_bind_int(stmt, 1, relay->id);
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->controller_id, -1, SQLITE_STATIC);
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 relay_dbo*
relay_db_select_mapper(sqlite3_stmt *stmt)
{
auto *new_relay = new relay_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_schedule_id
strncpy(new_relay->active_schedule_id, (const char*)sqlite3_column_text(stmt, i), 32);
break;
case 'c': // controller_id
strncpy(new_relay->controller_id, (const char*)sqlite3_column_text(stmt, i), 32);
break;
case 'i':
new_relay->id = sqlite3_column_int(stmt, i);
break;
case 'n':
switch(name[1])
{
case 'a': // name
strncpy(new_relay->name, (const char*)sqlite3_column_text(stmt, i), 127);
break;
case 'u': // number
new_relay->number = sqlite3_column_int(stmt, i);
break;
default:
break;
}
default: // ignore columns not implemented
break;
}
}
return new_relay;
}
static relay_dbo**
relay_db_select(sqlite3_stmt *stmt)
{
auto **all_relays = (relay_dbo**)malloc(sizeof(relay_dbo*));
int row = 0;
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
relay_dbo *new_relay = relay_db_select_mapper(stmt);
new_relay->reload_active_schedule();
row++;
all_relays = (relay_dbo**)realloc(all_relays, sizeof(relay_dbo*) * (row + 1));
all_relays[row - 1] = new_relay;
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR << "Error Selecting relays from database: " << sqlite3_errstr(s);
break;
}
}
}
sqlite3_finalize(stmt);
all_relays[row] = nullptr;
return all_relays;
}
void
relay_dbo::reload_active_schedule()
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", this->active_schedule_id, (intptr_t)&sqlite3_bind_text);
if(!schedules[0])
{
free(schedules);
schedules = schedule_dbo::get_by_simple("id", "off", (intptr_t)&sqlite3_bind_text);
strcpy(this->active_schedule_id, "off");
}
this->active_schedule = schedules[0];
free(schedules);
}
bool
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);
return relay_db_update_insert(this, stmt);
}
bool
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);
return relay_db_update_insert(this, stmt);
}
bool
relay_dbo::remove()
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM relays WHERE id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, this->id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
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();
return relay_json;
}
relay_dbo**
relay_dbo::get_all()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT * FROM relays;", -1, &stmt, nullptr);
return relay_db_select(stmt);
}
relay_dbo**
relay_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 relays WHERE", filters);
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);
}
relay_dbo*
relay_dbo::get_relay_for_controller(const char *controller_id, int relay_num)
{
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(
"controller_id",
(void*)controller_id,
(intptr_t)sqlite3_bind_text,
";"
);
filters[0] = &filter;
filters[1] = &filter2;
auto relays = relay_dbo::get_by(filters);
relay_dbo *relay = relays[0];
free(relays);
return relay;
}
bool
relay_dbo::valid_num_for_controller(const char *search_controller_id, int relay_num)
{
controller_dbo **controllers = controller_dbo::get_by_simple("id", search_controller_id, (intptr_t)&sqlite3_bind_text);
bool valid_id_and_num = controllers[0] && controllers[0]->relay_count > relay_num;
controller_dbo::free_list(controllers);
return valid_id_and_num;
}
void
relay_dbo::free_list(relay_dbo **relays_list)
{
for(int i = 0; relays_list[i] != nullptr; i++)
{
delete relays_list[i];
}
free(relays_list);
}

View file

@ -1,56 +0,0 @@
#ifndef EMGAUWA_CORE_RELAY_DBO_H
#define EMGAUWA_CORE_RELAY_DBO_H
#include <string>
#include <sqlite3.h>
#include <json/value.h>
#include <helpers.h>
#include "schedule_dbo.h"
class relay_dbo
{
public:
int id;
char name[128];
int number;
char controller_id[33];
char active_schedule_id[33];
schedule_dbo *active_schedule;
void
reload_active_schedule();
bool
update();
bool
insert();
bool
remove();
Json::Value
to_json();
static void
free_list(relay_dbo **relays_list);
static relay_dbo**
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*
get_relay_for_controller(const char *controller_id, int relay_num);
static bool
valid_num_for_controller(const char *search_controller_id, int relay_num);
static relay_dbo**
get_all();
};
#endif //EMGAUWA_CORE_RELAY_DBO_H

325
models/schedule.c Normal file
View file

@ -0,0 +1,325 @@
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <cJSON.h>
#include <logger.h>
#include <database.h>
#include <models/schedule.h>
#include <models/junction_tag.h>
#include <models/tag.h>
static int
db_update_insert(schedule_t *schedule, sqlite3_stmt *stmt)
{
int rc;
uint16_t *periods_blob = schedule_periods_to_blob(schedule);
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
sqlite3_bind_int(stmt, 1, schedule->id);
sqlite3_bind_blob(stmt, 2, schedule->uid, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, schedule->name, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 4, periods_blob, blob_size, SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
free(periods_blob);
return rc != SQLITE_DONE;
}
static schedule_t*
schedule_db_select_mapper(sqlite3_stmt *stmt)
{
const uint16_t *periods_blob;
schedule_t *new_schedule = malloc(sizeof(schedule_t));
for(int i = 0; i < sqlite3_column_count(stmt); i++)
{
const char *name = sqlite3_column_name(stmt, i);
switch(name[0])
{
case 'i': // id
new_schedule->id = sqlite3_column_int(stmt, i);
break;
case 'n': // name
strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127);
new_schedule->name[127] = '\0';
break;
case 'p': // periods
periods_blob = sqlite3_column_blob(stmt, i);
new_schedule->periods_count = periods_blob[0];
new_schedule->periods = malloc(sizeof(period_t) * periods_blob[0]);
for(int i = 0; i < periods_blob[0]; ++i)
{
new_schedule->periods[i].start = periods_blob[(i * 2) + 1];
new_schedule->periods[i].end = periods_blob[(i * 2) + 2];
}
break;
case 'u': // uid
uuid_copy(new_schedule->uid, (const unsigned char*)sqlite3_column_blob(stmt, i));
break;
default: // ignore columns not implemented
break;
}
}
return new_schedule;
}
static schedule_t**
schedule_db_select(sqlite3_stmt *stmt)
{
schedule_t **all_schedules = malloc(sizeof(schedule_t*));
int row = 0;
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
schedule_t *new_schedule = schedule_db_select_mapper(stmt);
row++;
all_schedules = (schedule_t**)realloc(all_schedules, sizeof(schedule_t*) * (row + 1));
all_schedules[row - 1] = new_schedule;
}
else
{
if(s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR("srror selecting schedules from database: %s\n", sqlite3_errstr(s));
break;
}
}
}
sqlite3_finalize(stmt);
all_schedules[row] = NULL;
return all_schedules;
}
int
schedule_save(schedule_t *schedule)
{
sqlite3_stmt *stmt;
if(schedule->id)
{
sqlite3_prepare_v2(global_database, "UPDATE schedules SET uid = ?2, name = ?3, periods = ?4 WHERE id=?1;", -1, &stmt, NULL);
}
else
{
sqlite3_prepare_v2(global_database, "INSERT INTO schedules(uid, name, periods) values (?2, ?3, ?4);", -1, &stmt, NULL);
}
int result = db_update_insert(schedule, stmt);
if(result)
{
if(schedule->id)
{
LOG_ERROR("error inserting data: %s\n", sqlite3_errmsg(global_database));
}
else
{
LOG_ERROR("error updating data: %s\n", sqlite3_errmsg(global_database));
}
}
else
{
schedule->id = sqlite3_last_insert_rowid(global_database);
}
return result;
}
void
schedule_free(schedule_t *schedule)
{
free(schedule->periods);
free(schedule);
}
void
schedule_free_list(schedule_t **schedules)
{
for(int i = 0; schedules[i] != NULL; ++i)
{
schedule_free(schedules[i]);
}
free(schedules);
}
uint16_t*
schedule_periods_to_blob(schedule_t *schedule)
{
uint16_t *blob = malloc(sizeof(uint16_t) * ((schedule->periods_count * 2) + 1));
blob[0] = schedule->periods_count;
for(int i = 0; i < schedule->periods_count; i++)
{
blob[(i * 2) + 1] = schedule->periods[i].start;
blob[(i * 2) + 2] = schedule->periods[i].end;
}
return blob;
}
cJSON*
schedule_to_json(schedule_t *schedule)
{
cJSON *json = cJSON_CreateObject();
cJSON *json_name = cJSON_CreateString(schedule->name);
if(json_name == NULL)
{
cJSON_Delete(json);
return NULL;
}
cJSON_AddItemToObject(json, "name", json_name);
char uuid_str[UUID_STR_LEN];
schedule_uid_unparse(schedule->uid, uuid_str);
cJSON *json_id = cJSON_CreateString(uuid_str);
if(json_name == NULL)
{
cJSON_Delete(json);
return NULL;
}
cJSON_AddItemToObject(json, "id", json_id);
cJSON *json_periods = cJSON_CreateArray();
if(json_periods == NULL)
{
cJSON_Delete(json);
return NULL;
}
cJSON_AddItemToObject(json, "periods", json_periods);
for(int i = 0; i < schedule->periods_count; ++i)
{
cJSON *json_period = cJSON_CreateObject();
if (json_period == NULL)
{
continue;
}
char start_str[8], end_str[8];
period_t *period = &schedule->periods[i];
sprintf(start_str, "%02d:%02d", period->start / 60, period->start % 60);
sprintf(end_str, "%02d:%02d", period->end / 60, period->end % 60);
cJSON *json_period_start = cJSON_CreateString(start_str);
if (json_period_start == NULL)
{
LOG_DEBUG("failed to add start period from string '%s'\n", start_str);
cJSON_Delete(json_period);
continue;
}
cJSON_AddItemToObject(json_period, "start", json_period_start);
cJSON *json_period_end = cJSON_CreateString(end_str);
if (json_period_end == NULL)
{
LOG_DEBUG("failed to add end period from string '%s'\n", end_str);
cJSON_Delete(json_period);
continue;
}
cJSON_AddItemToObject(json_period, "end", json_period_end);
cJSON_AddItemToArray(json_periods, json_period);
}
cJSON *json_tags = cJSON_CreateArray();
int *tags_ids = junction_tag_get_tags_for_schedule_id(schedule->id);
if(tags_ids != NULL)
{
for(int i = 0; tags_ids[i] != 0; ++i)
{
char *tag = tag_get_tag(tags_ids[i]);
if(tag == NULL)
{
continue;
}
cJSON *json_tag = cJSON_CreateString(tag);
if (json_tag == NULL)
{
LOG_DEBUG("failed to add tag from string '%s'\n", tag);
free(tag);
continue;
}
cJSON_AddItemToArray(json_tags, json_tag);
free(tag);
}
free(tags_ids);
}
cJSON_AddItemToObject(json, "tags", json_tags);
return json;
}
schedule_t**
schedule_get_all()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT * FROM schedules;", -1, &stmt, NULL);
return schedule_db_select(stmt);
}
int
schedule_uid_parse(const char *uid_str, uuid_t result)
{
if(strcmp("off", uid_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "off", 3);
return 0;
}
if(strcmp("on", uid_str) == 0)
{
memset(result, 0, sizeof(uuid_t));
memcpy(result, "on", 2);
return 0;
}
if(uuid_parse(uid_str, result))
{
return 1;
}
return 0;
}
void
schedule_uid_unparse(const uuid_t uid, char *result)
{
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
if(uuid_compare(uid, tmp_uuid) == 0)
{
strcpy(result, "off");
return;
}
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "on", 2);
if(uuid_compare(uid, tmp_uuid) == 0)
{
strcpy(result, "on");
return;
}
uuid_unparse(uid, result);
}

View file

@ -1,197 +0,0 @@
#include <cstring>
#include <trantor/utils/Logger.h>
#include <helpers.h>
#include "schedule_dbo.h"
#include "globals.h"
#include "period.h"
static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt)
{
int rc;
uint16_t *periods_blob = schedule->periods->to_db_blob();
int blob_size = (int)sizeof(uint16_t) * ((periods_blob[0] * 2) + 1);
sqlite3_bind_text(stmt, 1, schedule->id, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, schedule->name, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 3, periods_blob, blob_size, SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
free(periods_blob);
if (rc != SQLITE_DONE)
{
LOG_ERROR << "ERROR inserting/updating data: " << sqlite3_errmsg(globals::db);
return false;
}
return true;
}
static schedule_dbo*
schedule_db_select_mapper(sqlite3_stmt *stmt)
{
auto new_schedule = new schedule_dbo();
for(int i = 0; i < sqlite3_column_count(stmt); i++)
{
const char *name = sqlite3_column_name(stmt, i);
switch(name[0])
{
case 'i': // id
strncpy(new_schedule->id, (const char*)sqlite3_column_text(stmt, i), 32);
new_schedule->id[32] = '\0';
break;
case 'n': // name
strncpy(new_schedule->name, (const char*)sqlite3_column_text(stmt, i), 127);
new_schedule->name[127] = '\0';
break;
case 'p': // periods
new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i));
break;
default: // ignore columns not implemented
break;
}
}
return new_schedule;
}
static schedule_dbo**
schedule_db_select(sqlite3_stmt *stmt)
{
auto **all_schedules = (schedule_dbo**)malloc(sizeof(schedule_dbo*));
int row = 0;
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
schedule_dbo *new_schedule = schedule_db_select_mapper(stmt);
row++;
all_schedules = (schedule_dbo**)realloc(all_schedules, sizeof(schedule_dbo*) * (row + 1));
all_schedules[row - 1] = new_schedule;
}
else
{
if(s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR << "Error Selecting schedules from database: " << sqlite3_errstr(s);
break;
}
}
}
sqlite3_finalize(stmt);
all_schedules[row] = nullptr;
return all_schedules;
}
bool
schedule_dbo::update()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "UPDATE schedules SET name = ?2, periods = ?3 WHERE id=?1;", -1, &stmt, nullptr);
return schedule_db_update_insert(this, stmt);
}
bool
schedule_dbo::insert()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO schedules(id, name, periods) values (?1, ?2, ?3);", -1, &stmt, nullptr);
return schedule_db_update_insert(this, stmt);
}
bool
schedule_dbo::remove()
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM schedules 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;
}
schedule_dbo::~schedule_dbo()
{
delete this->periods;
}
Json::Value
schedule_dbo::to_json()
{
Json::Value schedule_json;
schedule_json["name"] = this->name;
schedule_json["id"] = this->id;
schedule_json["periods"] = this->periods->to_json();
return schedule_json;
}
schedule_dbo**
schedule_dbo::get_all()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT * FROM schedules;", -1, &stmt, nullptr);
return schedule_db_select(stmt);
}
schedule_dbo**
schedule_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 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);
return schedule_db_select(stmt);
}
void
schedule_dbo::free_list(schedule_dbo **schedules_list)
{
for(int i = 0; schedules_list[i] != nullptr; i++)
{
delete schedules_list[i];
}
free(schedules_list);
}

View file

@ -1,47 +0,0 @@
#ifndef EMGAUWA_CORE_SCHEDULE_DBO_H
#define EMGAUWA_CORE_SCHEDULE_DBO_H
#include <string>
#include <sqlite3.h>
#include <json/value.h>
#include <helpers.h>
#include "period.h"
#include "period_list.h"
class schedule_dbo
{
public:
char id[33];
char name[128];
period_list *periods;
bool
update();
bool
insert();
bool
remove();
~schedule_dbo();
Json::Value
to_json();
static void
free_list(schedule_dbo **schedules_list);
static schedule_dbo**
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**
get_all();
};
#endif //EMGAUWA_CORE_SCHEDULE_DBO_H

133
models/tag.c Normal file
View file

@ -0,0 +1,133 @@
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <logger.h>
#include <database.h>
#include <models/tag.h>
int
tag_save(int id, const char *tag)
{
int rc;
sqlite3_stmt *stmt;
if(id)
{
sqlite3_prepare_v2(global_database, "UPDATE tags SET tag = ?2 WHERE id = ?1;", -1, &stmt, NULL);
}
else
{
sqlite3_prepare_v2(global_database, "INSERT INTO tags(tag) values (?2);", -1, &stmt, NULL);
}
sqlite3_bind_int(stmt, 1, id);
sqlite3_bind_text(stmt, 2, tag, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
LOG_ERROR("error saving tag: %s\n", sqlite3_errmsg(global_database));
return false;
}
sqlite3_finalize(stmt);
return 1;
}
char*
tag_get_tag(int id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT tag FROM tags WHERE id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, id);
char *result = NULL;
while(1)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
const char *found_tag = (const char *)sqlite3_column_text(stmt, 0);
result = (char*)malloc(sizeof(char) * (strlen(found_tag) + 1));
strcpy(result, found_tag);
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR("error selecting tags from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return NULL;
}
}
}
sqlite3_finalize(stmt);
return result;
}
int
tag_get_id(const char *tag)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT id FROM tags WHERE tag=?1;", -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, tag, -1, SQLITE_STATIC);
int result = 0;
while(1)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
result = sqlite3_column_int(stmt, 0);
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR("error selecting tags from database: %s\n", sqlite3_errstr(s));
sqlite3_finalize(stmt);
return 0;
}
}
}
sqlite3_finalize(stmt);
return result;
}
int
tag_remove(int id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(global_database, "DELETE FROM tags WHERE id=?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}