2019-11-15 00:23:43 +00:00
|
|
|
#include <stdlib.h>
|
2020-04-13 22:50:55 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <uuid/uuid.h>
|
2019-11-15 00:23:43 +00:00
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
#include <logger.h>
|
2020-04-24 22:48:59 +00:00
|
|
|
#include <helpers.h>
|
2020-01-07 01:23:16 +00:00
|
|
|
#include <models/relay.h>
|
2019-11-15 00:23:43 +00:00
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
relay_t*
|
|
|
|
relay_create(uint8_t number)
|
2019-11-15 00:23:43 +00:00
|
|
|
{
|
2020-04-13 22:50:55 +00:00
|
|
|
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);
|
|
|
|
|
2020-04-24 22:48:59 +00:00
|
|
|
for(int i = 0; i < 7; ++i)
|
|
|
|
{
|
|
|
|
new_relay->schedules[i] = schedule_create(off_id, i, 0, NULL);
|
|
|
|
}
|
2020-04-13 22:50:55 +00:00
|
|
|
|
|
|
|
return new_relay;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-23 23:33:48 +00:00
|
|
|
relay_set_name(relay_t *relay, const char *name)
|
2020-04-13 22:50:55 +00:00
|
|
|
{
|
|
|
|
strncpy(relay->name, name, MAX_NAME_LENGTH);
|
|
|
|
relay->name[MAX_NAME_LENGTH] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-05-06 23:41:16 +00:00
|
|
|
relay_is_active(relay_t *relay, struct tm *time_struct)
|
2020-04-13 22:50:55 +00:00
|
|
|
{
|
2020-05-06 23:41:16 +00:00
|
|
|
schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)];
|
2020-04-24 22:48:59 +00:00
|
|
|
if(schedule->length == 0)
|
2020-04-13 22:50:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:48:59 +00:00
|
|
|
for(uint16_t i = 0; i < schedule->length; ++i)
|
2020-04-13 22:50:55 +00:00
|
|
|
{
|
2020-05-06 23:41:16 +00:00
|
|
|
if(period_includes_time(schedule->periods[i], time_struct))
|
2020-04-13 22:50:55 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
relay_debug(relay_t *relay)
|
|
|
|
{
|
|
|
|
if(relay == NULL)
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_DEBUG("relay is NULL\n");
|
2020-04-13 22:50:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_DEBUG("(1/3) %d @ %p\n", relay->number, (void*)relay);
|
|
|
|
LOG_DEBUG("(2/3) name: %s\n", relay->name);
|
2020-04-24 22:48:59 +00:00
|
|
|
LOG_DEBUG("(3/3) schedules @ %p:\n", (void*)relay->schedules);
|
|
|
|
for(int i = 0; i < 7; ++i)
|
|
|
|
{
|
|
|
|
schedule_debug(relay->schedules[i]);
|
|
|
|
}
|
2020-04-13 22:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
relay_free(relay_t *relay)
|
|
|
|
{
|
2020-04-24 22:48:59 +00:00
|
|
|
for(int i = 0; i < 7; ++i)
|
2020-04-13 22:50:55 +00:00
|
|
|
{
|
2020-04-24 22:48:59 +00:00
|
|
|
schedule_free(relay->schedules[i]);
|
2020-04-13 22:50:55 +00:00
|
|
|
}
|
|
|
|
free(relay);
|
2019-11-15 00:23:43 +00:00
|
|
|
}
|