add: forward schedule updates to relays
This commit is contained in:
parent
574a384d2f
commit
606f2483a5
5 changed files with 75 additions and 18 deletions
|
@ -30,15 +30,13 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id)
|
|||
return true;
|
||||
}
|
||||
|
||||
int
|
||||
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
|
||||
static int*
|
||||
get_ids(sqlite3_stmt *stmt)
|
||||
{
|
||||
int result = 0;
|
||||
sqlite3_stmt *stmt;
|
||||
int *ids = malloc(sizeof(int));
|
||||
int new_id;
|
||||
|
||||
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);
|
||||
int row = 0;
|
||||
|
||||
while(true)
|
||||
{
|
||||
|
@ -47,7 +45,11 @@ junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
|
|||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
result = sqlite3_column_int(stmt, 0);
|
||||
new_id = sqlite3_column_int(stmt, 0);
|
||||
row++;
|
||||
|
||||
ids = (int*)realloc(ids, sizeof(int) * (row + 1));
|
||||
ids[row - 1] = new_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -57,17 +59,46 @@ junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
|
|||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("error reading from database: %s", sqlite3_errstr(s));
|
||||
break;
|
||||
LOG_ERROR("error selecting junction ids from database: %s\n", sqlite3_errstr(s));
|
||||
sqlite3_finalize(stmt);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
ids[row] = 0;
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
int
|
||||
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id)
|
||||
{
|
||||
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);
|
||||
|
||||
int *id_list = get_ids(stmt);
|
||||
int result = id_list[0];
|
||||
|
||||
free(id_list);
|
||||
return result;
|
||||
}
|
||||
|
||||
int*
|
||||
junction_relay_schedule_get_relays_ids(int schedule_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(global_database, "SELECT relay_id FROM junction_relay_schedule WHERE schedule_id=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_int(stmt, 1, schedule_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
}
|
||||
|
||||
int
|
||||
junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id)
|
||||
{
|
||||
|
|
|
@ -134,7 +134,10 @@ schedule_save(schedule_t *schedule)
|
|||
}
|
||||
else
|
||||
{
|
||||
schedule->id = sqlite3_last_insert_rowid(global_database);
|
||||
if(!schedule->id)
|
||||
{
|
||||
schedule->id = sqlite3_last_insert_rowid(global_database);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue