diff --git a/controller.ini b/controller.ini index 95e2198..f6a83ae 100644 --- a/controller.ini +++ b/controller.ini @@ -3,7 +3,7 @@ name = new emgauwa device discovery-port = 4421 relay-count = 10 database = controller_db.lmdb -log-level = info +log-level = debug [relay-0] driver = piface diff --git a/handlers/loop.c b/handlers/loop.c index 8ea01e6..d548521 100644 --- a/handlers/loop.c +++ b/handlers/loop.c @@ -15,12 +15,14 @@ void handler_loop(controller_t *controller) { + time_t timestamp = time(NULL); + struct tm *time_struct = localtime(×tamp); LOG_DEBUG("===== IDLE LOOP START =====\n"); for(uint_fast8_t i = 0; i < controller->relay_count; ++i) { relay_t *relay = controller->relays[i]; int is_active = 0; - if(relay_is_active(relay, time(NULL))) + if(relay_is_active(relay, time_struct)) { LOG_DEBUG("relay %d is active\n", i); is_active = 1; diff --git a/helpers/get_day_of_week.c b/helpers/get_day_of_week.c deleted file mode 100644 index 0214f63..0000000 --- a/helpers/get_day_of_week.c +++ /dev/null @@ -1,12 +0,0 @@ -#include - -#include - -int -helper_get_weekday(const time_t timestamp_now) -{ - struct tm *now = localtime(×tamp_now); - int wday_sun_sat = now->tm_wday; - int wday_mon_sun = (wday_sun_sat + 6) % 7; - return wday_mon_sun; -} diff --git a/helpers/get_weekday.c b/helpers/get_weekday.c new file mode 100644 index 0000000..68f029e --- /dev/null +++ b/helpers/get_weekday.c @@ -0,0 +1,11 @@ +#include + +#include + +int +helper_get_weekday(const struct tm *time_struct) +{ + int wday_sun_sat = time_struct->tm_wday; + int wday_mon_sun = (wday_sun_sat + 6) % 7; + return wday_mon_sun; +} diff --git a/include/helpers.h b/include/helpers.h index 4d59c84..bb9edc0 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -1,6 +1,7 @@ #ifndef CONTROLLER_HELPERS_H #define CONTROLLER_HELPERS_H +#include #include #include @@ -32,6 +33,6 @@ void helper_parse_cli(int argc, const char **argv, config_t *config); int -helper_get_weekday(const time_t timestamp_now); +helper_get_weekday(const struct tm *time_struct); #endif /* CONTROLLER_HELPERS_H */ diff --git a/include/models/period.h b/include/models/period.h index 38a7249..3c87544 100644 --- a/include/models/period.h +++ b/include/models/period.h @@ -14,6 +14,6 @@ period_t* period_create(uint16_t start, uint16_t end); int -period_includes_time(period_t *period, uint16_t timestamp); +period_includes_time(period_t *period, struct tm *time_struct); #endif /* CONTROLLER_PERIOD_H */ diff --git a/include/models/relay.h b/include/models/relay.h index 34f08ba..34ee22c 100644 --- a/include/models/relay.h +++ b/include/models/relay.h @@ -51,7 +51,7 @@ int relay_save(relay_t *relay, MDB_env *mdb_env); int -relay_is_active(relay_t *relay, time_t timestamp_now); +relay_is_active(relay_t *relay, struct tm *time_struct); void relay_free(relay_t *relay); diff --git a/logger.c b/logger.c index fb26d56..ac636d4 100644 --- a/logger.c +++ b/logger.c @@ -46,8 +46,7 @@ logger_log(FILE *stream, log_level_t level, const char *filename, int line, cons } char timestamp_str[32]; - time_t rawtime; - time(&rawtime); + time_t rawtime = time(NULL); strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime)); fprintf(stream, "%s %s:%d:%s " COLOR_NONE, timestamp_str, filename, line, func); diff --git a/models/period.c b/models/period.c index 75a4981..d3a7369 100644 --- a/models/period.c +++ b/models/period.c @@ -15,11 +15,14 @@ period_create(uint16_t start, uint16_t end) } int -period_includes_time(period_t *period, uint16_t timestamp) +period_includes_time(period_t *period, struct tm *time_struct) { uint16_t start = period->start; uint16_t end = period->end; + time_t timestamp = time_struct->tm_hour * 60; + timestamp += time_struct->tm_min; + // "normal" timespan if(start < end) { diff --git a/models/relay.c b/models/relay.c index 59474ba..4d7c0b0 100644 --- a/models/relay.c +++ b/models/relay.c @@ -34,22 +34,17 @@ relay_set_name(relay_t *relay, const char *name) } int -relay_is_active(relay_t *relay, time_t timestamp_now) +relay_is_active(relay_t *relay, struct tm *time_struct) { - schedule_t *schedule = relay->schedules[helper_get_weekday(timestamp_now)]; + schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)]; 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)) + if(period_includes_time(schedule->periods[i], time_struct)) { return 1; }