add: schedules
This commit is contained in:
parent
2d24339421
commit
cac5781222
21 changed files with 1030 additions and 39 deletions
|
@ -3,25 +3,27 @@
|
|||
//
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "device_dbo.h"
|
||||
#include "globals.h"
|
||||
#include <macros.h>
|
||||
|
||||
bool device_dbo::save()
|
||||
static bool device_db_update_insert(device_dbo *device, sqlite3_stmt *stmt)
|
||||
{
|
||||
int rc;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(globals::db, "INSERT INTO devices(id, name, ip, active) values (?1, ?2, ?3, ?4);", -1, &stmt, nullptr);
|
||||
//sqlite3_prepare_v2(globals::db, "UPDATE devices set name = ?2, ip = ?3, active = ?4 WHERE id = ?1;", -1, &stmt, nullptr);
|
||||
|
||||
sqlite3_bind_text(stmt, 1, this->id, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, this->name, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 3, this->ip, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(stmt, 4, this->active);
|
||||
//sqlite3_bind_int(stmt, 2, 21);
|
||||
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) {
|
||||
if (rc != SQLITE_DONE)
|
||||
{
|
||||
printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db));
|
||||
return false;
|
||||
}
|
||||
|
@ -30,3 +32,162 @@ bool device_dbo::save()
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
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
|
||||
new_device->active = 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
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";
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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_one_by(const char *key, const char *value)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
char* sql;
|
||||
|
||||
asprintf(&sql, "SELECT * FROM devices WHERE %s=?1;", key);
|
||||
|
||||
sqlite3_prepare_v2(globals::db, sql, -1, &stmt, nullptr);
|
||||
sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
|
||||
|
||||
return device_db_select(stmt)[0];
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,18 +3,39 @@
|
|||
|
||||
#include <string>
|
||||
#include <sqlite3.h>
|
||||
#include <json/value.h>
|
||||
|
||||
class device_dbo
|
||||
{
|
||||
public:
|
||||
char id[23];
|
||||
|
||||
char id[33];
|
||||
char name[128];
|
||||
char ip[17];
|
||||
bool active;
|
||||
int port;
|
||||
int relay_count;
|
||||
|
||||
bool save();
|
||||
bool
|
||||
update();
|
||||
|
||||
bool
|
||||
insert();
|
||||
|
||||
bool
|
||||
remove();
|
||||
|
||||
Json::Value
|
||||
to_json();
|
||||
|
||||
static void
|
||||
free_list(device_dbo **devices_list);
|
||||
|
||||
static device_dbo*
|
||||
get_one_by(const char *key, const char *value);
|
||||
|
||||
static device_dbo**
|
||||
get_all();
|
||||
};
|
||||
|
||||
|
||||
|
|
41
models/period.cc
Normal file
41
models/period.cc
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include <stdio.h>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#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[6], end[6];
|
||||
|
||||
sprintf(start, "%02d:%02d", (int)(this->start / 60), this->start % 60);
|
||||
sprintf(end, "%02d:%02d", (int)(this->end / 60), this->end % 60);
|
||||
|
||||
result["start"] = std::string(start);
|
||||
result["end"] = std::string(end);
|
||||
|
||||
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;
|
||||
}
|
23
models/period.h
Normal file
23
models/period.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#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
|
73
models/period_list.cc
Normal file
73
models/period_list.cc
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include <stdio.h>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <drogon/drogon.h>
|
||||
#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, int 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;
|
||||
}
|
29
models/period_list.h
Normal file
29
models/period_list.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#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;
|
||||
int length;
|
||||
|
||||
period_list();
|
||||
period_list(const uint16_t *periods_blob);
|
||||
period_list(period **periods, int 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
|
|
@ -1,5 +1,182 @@
|
|||
//
|
||||
// Created by tobias on 09/07/19.
|
||||
// Created by tobias on 08/07/19.
|
||||
//
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string.h>
|
||||
#include <trantor/utils/Logger.h>
|
||||
#include "schedule_dbo.h"
|
||||
#include "globals.h"
|
||||
#include "period.h"
|
||||
#include <macros.h>
|
||||
|
||||
static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt)
|
||||
{
|
||||
int rc;
|
||||
uint16_t *periods_blob = schedule->periods->to_db_blob();
|
||||
|
||||
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, sizeof(uint16_t) * ((periods_blob[0] * 2) + 1), SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
{
|
||||
printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db));
|
||||
free(periods_blob);
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
free(periods_blob);
|
||||
|
||||
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
|
||||
auto periods_blob = (const uint16_t*)sqlite3_column_blob(stmt, i);
|
||||
new_schedule->periods = new period_list(periods_blob);
|
||||
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";
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return rc == SQLITE_DONE;
|
||||
|
||||
}
|
||||
|
||||
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_one_by(const char *key, const char *value)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
char* sql;
|
||||
|
||||
asprintf(&sql, "SELECT * FROM schedules WHERE %s=?1;", key);
|
||||
|
||||
sqlite3_prepare_v2(globals::db, sql, -1, &stmt, nullptr);
|
||||
sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
|
||||
|
||||
return schedule_db_select(stmt)[0];
|
||||
}
|
||||
|
||||
void
|
||||
schedule_dbo::free_list(schedule_dbo **schedules_list)
|
||||
{
|
||||
for(int i = 0; schedules_list[i] != nullptr; i++)
|
||||
{
|
||||
delete schedules_list[i]->periods;
|
||||
delete schedules_list[i];
|
||||
}
|
||||
free(schedules_list);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,40 @@
|
|||
//
|
||||
// Created by tobias on 09/07/19.
|
||||
//
|
||||
|
||||
#ifndef EMGAUWA_CORE_SCHEDULE_DBO_H
|
||||
#define EMGAUWA_CORE_SCHEDULE_DBO_H
|
||||
|
||||
#include <string>
|
||||
#include <sqlite3.h>
|
||||
#include <json/value.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();
|
||||
|
||||
Json::Value
|
||||
to_json();
|
||||
|
||||
static void
|
||||
free_list(schedule_dbo **schedules_list);
|
||||
|
||||
static schedule_dbo*
|
||||
get_one_by(const char *key, const char *value);
|
||||
|
||||
static schedule_dbo**
|
||||
get_all();
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue