controller-legacy/models/schedule.c

95 lines
2.3 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*
2020-04-24 22:48:59 +00:00
schedule_create(uuid_t id, uint8_t weekday, uint16_t length, uint16_t *periods_blob)
2020-04-13 22:50:55 +00:00
{
schedule_t *new_schedule = malloc(sizeof(schedule_t));
memmove(new_schedule->id, id, sizeof(uuid_t));
2020-04-24 22:48:59 +00:00
new_schedule->weekday = weekday;
2020-04-13 22:50:55 +00:00
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)
{
2020-04-24 13:08:26 +00:00
LOG_DEBUG("schedule is NULL\n");
2020-04-13 22:50:55 +00:00
return;
}
2020-04-24 22:48:59 +00:00
char uuid_str[UUID_STR_LEN];
2020-04-13 22:50:55 +00:00
uuid_unparse(schedule->id, uuid_str);
2020-04-24 13:08:26 +00:00
LOG_DEBUG("(1/3) %s @ %p\n", uuid_str, (void*)schedule);
2020-04-24 22:48:59 +00:00
LOG_DEBUG("(2/4) period count: %3d\n", schedule->length);
LOG_DEBUG("(3/4) weekday: %3d\n", schedule->weekday);
2020-04-13 22:50:55 +00:00
// 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
);
}
2020-04-24 22:48:59 +00:00
LOG_DEBUG("(4/4) periods: %s\n", periods_debug_str);
free(periods_debug_str);
2020-04-13 22:50:55 +00:00
}