add: weekly schedule support

This commit is contained in:
Tobias Reisinger 2020-04-23 17:00:12 +02:00
parent b3f75f4004
commit 44a83cd2c5
11 changed files with 220 additions and 79 deletions

View file

@ -11,10 +11,16 @@ static bool relay_db_update_insert(relay_dbo *relay, sqlite3_stmt *stmt)
{
int rc;
uint32_t schedules_ids[7];
for(int i = 0; i < 7; ++i)
{
schedules_ids[i] = relay->schedules[i]->id;
}
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_int(stmt, 4, relay->active_schedule_id);
sqlite3_bind_blob(stmt, 4, schedules_ids, sizeof(uint32_t) * 7, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 5, relay->controller_id, sizeof(uuid_t), SQLITE_STATIC);
sqlite3_bind_text(stmt, 6, relay->tag, -1, SQLITE_STATIC);
@ -34,15 +40,13 @@ static relay_dbo*
relay_db_select_mapper(sqlite3_stmt *stmt)
{
auto *new_relay = new relay_dbo();
const char* new_tag;
const char *new_tag;
const uint32_t *new_relays_ids;
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_dbid
new_relay->active_schedule_id = sqlite3_column_int(stmt, i);
break;
case 'c': // controller_id
uuid_copy(new_relay->controller_id, (const unsigned char*)sqlite3_column_blob(stmt, i));
break;
@ -62,6 +66,13 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
break;
}
break;
case 's': // schedules_ids
new_relays_ids = (const uint32_t*)sqlite3_column_blob(stmt, i);
for(int i = 0; i < 7; ++i)
{
new_relay->schedules[i] = schedule_dbo::get_by_id_or_off(new_relays_ids[i]);
}
break;
case 't': // tag
new_tag = (const char*)sqlite3_column_text(stmt, i);
new_relay->tag[0] = '\0';
@ -93,8 +104,6 @@ relay_db_select(sqlite3_stmt *stmt)
{
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));
@ -120,35 +129,12 @@ relay_db_select(sqlite3_stmt *stmt)
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_int, 0);
if(!schedules[0])
{
free(schedules);
uuid_t off_uuid;
memset(off_uuid, 0, sizeof(uuid_t));
memcpy(off_uuid, "off", 3);
schedules = schedule_dbo::get_by_simple("uid", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
if(schedules[0])
{
this->active_schedule_id = schedules[0]->id;
}
}
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, tag = ?6 WHERE id = ?1;", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, schedules_ids = ?4, controller_id = ?5, tag = ?6 WHERE id = ?1;", -1, &stmt, nullptr);
return relay_db_update_insert(this, stmt);
}
@ -158,7 +144,7 @@ relay_dbo::insert()
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, active_schedule_id, controller_id, tag) values (?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr);
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, schedules_ids, controller_id, tag) values (?2, ?3, ?4, ?5, ?6);", -1, &stmt, nullptr);
return relay_db_update_insert(this, stmt);
}
@ -181,16 +167,26 @@ relay_dbo::remove()
Json::Value
relay_dbo::to_json()
{
this->active_schedule = this->schedules[helpers::get_day_of_week()];
char controller_id_str[37];
uuid_unparse(this->controller_id, controller_id_str);
char active_schedule_uid_str[37];
schedule_dbo::unparse_uid(this->active_schedule->uid, active_schedule_uid_str);
Json::Value schedules_json(Json::arrayValue);
for(int i = 0; i < 7; ++i)
{
schedules_json.append(this->schedules[i]->to_json());
}
Json::Value relay_json;
relay_json["name"] = this->name;
relay_json["number"] = this->number;
relay_json["active_schedule_id"] = active_schedule_uid_str;
relay_json["controller_id"] = controller_id_str;
relay_json["active_schedule"] = this->active_schedule->to_json();
relay_json["schedules"] = schedules_json;
relay_json["tag"] = this->tag;
return relay_json;

View file

@ -19,6 +19,7 @@ public:
int active_schedule_id;
char tag[64];
schedule_dbo *active_schedule;
schedule_dbo *schedules[7];
void
reload_active_schedule();
@ -41,6 +42,9 @@ public:
static relay_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
static relay_dbo*
get_by_id_or_off(const int id);
static relay_dbo**
get_by(helpers::sql_filter_builder **filters);

View file

@ -183,6 +183,29 @@ schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_fu
return schedule_db_select(stmt);
}
schedule_dbo*
schedule_dbo::get_by_id_or_off(const int id)
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0);
if(!schedules[0])
{
free(schedules);
uuid_t off_uuid;
memset(off_uuid, 0, sizeof(uuid_t));
memcpy(off_uuid, "off", 3);
schedules = schedule_dbo::get_by_simple("uid", off_uuid, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
if(!schedules[0])
{
LOG_FATAL << "schedule with uid 'off' not found";
exit(1);
}
}
schedule_dbo *schedule = schedules[0];
free(schedules);
return schedule;
}
schedule_dbo**
schedule_dbo::get_by(helpers::sql_filter_builder **filters)
{

View file

@ -38,6 +38,9 @@ public:
static schedule_dbo**
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
static schedule_dbo*
get_by_id_or_off(const int id);
static schedule_dbo**
get_by(helpers::sql_filter_builder **filters);