add: filters

add: schedule api calls
This commit is contained in:
Tobias Reisinger 2019-07-20 14:51:45 +02:00
parent a2dfcebf3f
commit a38a6e63b3
23 changed files with 171 additions and 95 deletions

View file

@ -1,10 +1,6 @@
//
// Created by tobias on 08/07/19.
//
#include <cstdio>
#include <cstring>
#include <string.h>
#include <cstring>
#include <trantor/utils/Logger.h>
#include "device_dbo.h"
#include "globals.h"
@ -42,7 +38,7 @@ device_db_select_mapper(sqlite3_stmt *stmt)
switch(name[0])
{
case 'a': // active
new_device->active = sqlite3_column_int(stmt, i);
new_device->active = (bool)sqlite3_column_int(stmt, i);
break;
case 'i':
switch(name[1])
@ -53,6 +49,8 @@ device_db_select_mapper(sqlite3_stmt *stmt)
case 'p': // ip
strncpy(new_device->ip, (const char*)sqlite3_column_text(stmt, i), 16);
break;
default: // ignore columns not implemented
break;
}
break;
case 'n': // name
@ -64,6 +62,8 @@ device_db_select_mapper(sqlite3_stmt *stmt)
case 'r': // relay_count
new_device->relay_count = sqlite3_column_int(stmt, i);
break;
default: // ignore columns not implemented
break;
}
}
return new_device;

View file

@ -1,15 +0,0 @@
{
//rdbms:server type, postgresql,mysql or sqlite3
"rdbms":"postgresql",
//filename:sqlite3 db file name
//"filename":"",
//host:server address,localhost by default;
"host":"127.0.0.1",
//port:server port, 5432 by default;
"port":5432,
//dbname:Database name;
"dbname":"",
"user":"",
"passwd":"",
"tables":[]
}

View file

@ -1,4 +1,4 @@
#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <cstdint>
#include <cstdlib>
@ -17,8 +17,8 @@ period::to_json()
char start_str[6], end_str[6];
sprintf(start_str, "%02d:%02d", (int)(this->start / 60), this->start % 60);
sprintf(end_str, "%02d:%02d", (int)(this->end / 60), this->end % 60);
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);

View file

@ -1,4 +1,4 @@
#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <cstdint>
#include <cstdlib>
@ -23,7 +23,7 @@ period_list::period_list(const uint16_t* periods_blob)
}
}
period_list::period_list(period **periods, int length)
period_list::period_list(period **periods, uint16_t length)
{
this->periods = periods;
this->length = length;

View file

@ -8,11 +8,11 @@ class period_list
{
public:
period **periods;
int length;
uint16_t length;
period_list();
period_list(const uint16_t *periods_blob);
period_list(period **periods, int length);
explicit period_list(const uint16_t *periods_blob);
period_list(period **periods, uint16_t length);
~period_list();
void
@ -25,5 +25,4 @@ public:
to_db_blob();
};
#endif //EMGAUWA_CORE_PERIOD_LIST_H

View file

@ -4,7 +4,7 @@
#include <cstdio>
#include <cstring>
#include <string.h>
#include <cstring>
#include <trantor/utils/Logger.h>
#include "schedule_dbo.h"
#include "globals.h"
@ -14,10 +14,11 @@ 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 = 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, sizeof(uint16_t) * ((periods_blob[0] * 2) + 1), SQLITE_STATIC);
sqlite3_bind_blob(stmt, 3, periods_blob, blob_size, SQLITE_STATIC);
rc = sqlite3_step(stmt);
@ -26,7 +27,7 @@ static bool schedule_db_update_insert(schedule_dbo *schedule, sqlite3_stmt *stmt
if (rc != SQLITE_DONE)
{
LOG_ERROR << "ERROR inserting data: " << sqlite3_errmsg(globals::db);
LOG_ERROR << "ERROR inserting/updating data: " << sqlite3_errmsg(globals::db);
return false;
}
@ -51,8 +52,9 @@ schedule_db_select_mapper(sqlite3_stmt *stmt)
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);
new_schedule->periods = new period_list((const uint16_t*)sqlite3_column_blob(stmt, i));
break;
default: // ignore columns not implemented
break;
}
}
@ -106,7 +108,7 @@ schedule_dbo::update()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "UPDATE schedules set name = ?2, periods = ?3 WHERE id = ?1;", -1, &stmt, nullptr);
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);
}