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-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);
|
|
|
|
|
|
|
|
new_relay->schedule = schedule_create(off_id, 0, NULL);
|
|
|
|
|
|
|
|
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
|
|
|
|
relay_is_active(relay_t *relay, time_t timestamp_now)
|
|
|
|
{
|
|
|
|
if(relay->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 < relay->schedule->length; ++i)
|
|
|
|
{
|
|
|
|
if(period_includes_time(relay->schedule->periods[i], timestamp_now))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//struct tm time_start, time_now, time_end;
|
|
|
|
//localtime_r(×tamp_start, &time_start);
|
|
|
|
//localtime_r(×tamp_now, &time_now);
|
|
|
|
//localtime_r(×tamp_end, &time_end);
|
|
|
|
//LOG_DEBUG("%02d:%02d - %02d:%02d - %02d:%02d", time_start.tm_hour, time_start.tm_min, time_now.tm_hour, time_now.tm_min, time_end.tm_hour, time_end.tm_min);
|
|
|
|
|
|
|
|
void
|
|
|
|
relay_debug(relay_t *relay)
|
|
|
|
{
|
|
|
|
if(relay == NULL)
|
|
|
|
{
|
|
|
|
LOG_DEBUG("relay is NULL");
|
|
|
|
return;
|
|
|
|
}
|
2020-04-23 14:59:46 +00:00
|
|
|
LOG_DEBUG("(1/3) %d @ %p", relay->number, (void*)relay);
|
2020-04-13 22:50:55 +00:00
|
|
|
LOG_DEBUG("(2/3) name: %s", relay->name);
|
|
|
|
LOG_DEBUG("(3/3) schedule:");
|
|
|
|
schedule_debug(relay->schedule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
relay_free(relay_t *relay)
|
|
|
|
{
|
|
|
|
if(relay->schedule)
|
|
|
|
{
|
|
|
|
schedule_free(relay->schedule);
|
|
|
|
}
|
|
|
|
free(relay);
|
2019-11-15 00:23:43 +00:00
|
|
|
}
|