controller-legacy/models/schedule.c

90 lines
2.2 KiB
C
Raw Normal View History

2020-04-13 22:50:55 +00:00
#include <stdlib.h>
#include <string.h>
#include <logger.h>
#include <models/schedule.h>
schedule_t*
schedule_create(uuid_t id, uint16_t length, uint16_t *periods_blob)
{
schedule_t *new_schedule = malloc(sizeof(schedule_t));
memmove(new_schedule->id, id, sizeof(uuid_t));
new_schedule->length = length;
new_schedule->periods = NULL;
if(length)
{
new_schedule->periods = malloc(sizeof(period_t*) * length);
for(uint16_t i = 0; i < length; ++i)
{
uint16_t start = periods_blob[0 + (i * 2)];
uint16_t end = periods_blob[1 + (i * 2)];
new_schedule->periods[i] = period_create(start, end);
}
}
return new_schedule;
}
uint16_t*
schedule_periods_to_blob(schedule_t *schedule)
{
uint16_t *periods_blob = malloc(sizeof(uint16_t) * ((2 * schedule->length) + 1));
periods_blob[0] = schedule->length;
for(uint16_t i = 0; i < schedule->length; ++i)
{
periods_blob[1 + (i * 2)] = schedule->periods[i]->start;
periods_blob[2 + (i * 2)] = schedule->periods[i]->end;
}
return periods_blob;
}
void
schedule_free(schedule_t *schedule)
{
for(uint16_t i = 0; i < schedule->length; ++i)
{
free(schedule->periods[i]);
}
free(schedule->periods);
free(schedule);
}
void
schedule_debug(schedule_t *schedule)
{
if(schedule == NULL)
{
LOG_DEBUG("schedule is NULL");
return;
}
char uuid_str[37];
uuid_unparse(schedule->id, uuid_str);
LOG_DEBUG("(1/3) %s @ %p", uuid_str, (void*)schedule);
2020-04-13 22:50:55 +00:00
LOG_DEBUG("(2/3) period count: %3d", schedule->length);
// one block: "HH:MM-HH:MM, " --> size: 13 (14 with '\0')
char *periods_debug_str = malloc(sizeof(char) * ((schedule->length * 13) + 1));
periods_debug_str[0] = '\0';
for(uint16_t i = 0; i < schedule->length; ++i)
{
sprintf(
periods_debug_str + (13 * i),
"%02d:%02d-%02d:%02d, ",
schedule->periods[i]->start / 60,
schedule->periods[i]->start % 60,
schedule->periods[i]->end / 60,
schedule->periods[i]->end % 60
);
}
LOG_DEBUG("(3/3) periods: %s", periods_debug_str);
}