diff --git a/controllers/api_v1_controllers_relays.cc b/controllers/api_v1_controllers_relays.cc index 34f57dd..f341dc3 100644 --- a/controllers/api_v1_controllers_relays.cc +++ b/controllers/api_v1_controllers_relays.cc @@ -84,6 +84,8 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req, strncpy(relay->active_schedule_id, body["active_schedule"].asCString(), 32); strncpy(relay->controller_id, controller_id.c_str(), 32); + relay->reload_active_schedule(); + db_action_result = relay->insert(); } diff --git a/models/relay_dbo.cc b/models/relay_dbo.cc index 3979e98..b6397b8 100644 --- a/models/relay_dbo.cc +++ b/models/relay_dbo.cc @@ -82,18 +82,9 @@ relay_db_select(sqlite3_stmt *stmt) if (s == SQLITE_ROW) { relay_dbo *new_relay = relay_db_select_mapper(stmt); - schedule_dbo **schedules = schedule_dbo::get_by_simple("id", new_relay->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(new_relay->active_schedule_id, "off"); - } + new_relay->reload_active_schedule(); - new_relay->active_schedule = schedules[0]; - - free(schedules); row++; all_relays = (relay_dbo**)realloc(all_relays, sizeof(relay_dbo*) * (row + 1)); @@ -119,6 +110,23 @@ 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_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() { @@ -244,7 +252,7 @@ relay_dbo::free_list(relay_dbo **relays_list) { for(int i = 0; relays_list[i] != nullptr; i++) { - free(relays_list[i]); + delete relays_list[i]; } free(relays_list); } \ No newline at end of file diff --git a/models/relay_dbo.h b/models/relay_dbo.h index 5ead660..bbbeb8a 100644 --- a/models/relay_dbo.h +++ b/models/relay_dbo.h @@ -18,6 +18,9 @@ public: char active_schedule_id[33]; schedule_dbo *active_schedule; + void + reload_active_schedule(); + bool update();