85 lines
1.7 KiB
C
85 lines
1.7 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <uuid/uuid.h>
|
|
|
|
#include <logger.h>
|
|
#include <helpers.h>
|
|
#include <models/relay.h>
|
|
|
|
relay_t*
|
|
relay_create(uint8_t number)
|
|
{
|
|
relay_t *new_relay = malloc(sizeof(relay_t));
|
|
|
|
new_relay->number = number;
|
|
new_relay->name[0] = '\0';
|
|
|
|
uuid_t off_id;
|
|
memset(off_id, 0, sizeof(uuid_t));
|
|
memcpy(off_id, "off", 3);
|
|
|
|
for(int i = 0; i < 7; ++i)
|
|
{
|
|
new_relay->schedules[i] = schedule_create(off_id, i, 0, NULL);
|
|
}
|
|
|
|
return new_relay;
|
|
}
|
|
|
|
void
|
|
relay_set_name(relay_t *relay, const char *name)
|
|
{
|
|
strncpy(relay->name, name, MAX_NAME_LENGTH);
|
|
relay->name[MAX_NAME_LENGTH] = '\0';
|
|
}
|
|
|
|
int
|
|
relay_is_active(relay_t *relay, time_t timestamp_now)
|
|
{
|
|
schedule_t *schedule = relay->schedules[helper_get_weekday(timestamp_now)];
|
|
if(schedule->length == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
// we don't need days. reduce to hours, minutes and seconds
|
|
timestamp_now %= SECONDS_PER_DAY;
|
|
// finally remove seconds
|
|
timestamp_now /= SECONDS_PER_MINUTE;
|
|
|
|
for(uint16_t i = 0; i < schedule->length; ++i)
|
|
{
|
|
if(period_includes_time(schedule->periods[i], timestamp_now))
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
relay_debug(relay_t *relay)
|
|
{
|
|
if(relay == NULL)
|
|
{
|
|
LOG_DEBUG("relay is NULL\n");
|
|
return;
|
|
}
|
|
LOG_DEBUG("(1/3) %d @ %p\n", relay->number, (void*)relay);
|
|
LOG_DEBUG("(2/3) name: %s\n", relay->name);
|
|
LOG_DEBUG("(3/3) schedules @ %p:\n", (void*)relay->schedules);
|
|
for(int i = 0; i < 7; ++i)
|
|
{
|
|
schedule_debug(relay->schedules[i]);
|
|
}
|
|
}
|
|
|
|
void
|
|
relay_free(relay_t *relay)
|
|
{
|
|
for(int i = 0; i < 7; ++i)
|
|
{
|
|
schedule_free(relay->schedules[i]);
|
|
}
|
|
free(relay);
|
|
}
|