fix: timezone issue
This commit is contained in:
parent
ad7a6028b3
commit
27cec1d8cc
10 changed files with 27 additions and 28 deletions
|
@ -3,7 +3,7 @@ name = new emgauwa device
|
||||||
discovery-port = 4421
|
discovery-port = 4421
|
||||||
relay-count = 10
|
relay-count = 10
|
||||||
database = controller_db.lmdb
|
database = controller_db.lmdb
|
||||||
log-level = info
|
log-level = debug
|
||||||
|
|
||||||
[relay-0]
|
[relay-0]
|
||||||
driver = piface
|
driver = piface
|
||||||
|
|
|
@ -15,12 +15,14 @@
|
||||||
void
|
void
|
||||||
handler_loop(controller_t *controller)
|
handler_loop(controller_t *controller)
|
||||||
{
|
{
|
||||||
|
time_t timestamp = time(NULL);
|
||||||
|
struct tm *time_struct = localtime(×tamp);
|
||||||
LOG_DEBUG("===== IDLE LOOP START =====\n");
|
LOG_DEBUG("===== IDLE LOOP START =====\n");
|
||||||
for(uint_fast8_t i = 0; i < controller->relay_count; ++i)
|
for(uint_fast8_t i = 0; i < controller->relay_count; ++i)
|
||||||
{
|
{
|
||||||
relay_t *relay = controller->relays[i];
|
relay_t *relay = controller->relays[i];
|
||||||
int is_active = 0;
|
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);
|
LOG_DEBUG("relay %d is active\n", i);
|
||||||
is_active = 1;
|
is_active = 1;
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include <helpers.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
11
helpers/get_weekday.c
Normal file
11
helpers/get_weekday.c
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include <helpers.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef CONTROLLER_HELPERS_H
|
#ifndef CONTROLLER_HELPERS_H
|
||||||
#define CONTROLLER_HELPERS_H
|
#define CONTROLLER_HELPERS_H
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
#include <confini.h>
|
#include <confini.h>
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
|
@ -32,6 +33,6 @@ void
|
||||||
helper_parse_cli(int argc, const char **argv, config_t *config);
|
helper_parse_cli(int argc, const char **argv, config_t *config);
|
||||||
|
|
||||||
int
|
int
|
||||||
helper_get_weekday(const time_t timestamp_now);
|
helper_get_weekday(const struct tm *time_struct);
|
||||||
|
|
||||||
#endif /* CONTROLLER_HELPERS_H */
|
#endif /* CONTROLLER_HELPERS_H */
|
||||||
|
|
|
@ -14,6 +14,6 @@ period_t*
|
||||||
period_create(uint16_t start, uint16_t end);
|
period_create(uint16_t start, uint16_t end);
|
||||||
|
|
||||||
int
|
int
|
||||||
period_includes_time(period_t *period, uint16_t timestamp);
|
period_includes_time(period_t *period, struct tm *time_struct);
|
||||||
|
|
||||||
#endif /* CONTROLLER_PERIOD_H */
|
#endif /* CONTROLLER_PERIOD_H */
|
||||||
|
|
|
@ -51,7 +51,7 @@ int
|
||||||
relay_save(relay_t *relay, MDB_env *mdb_env);
|
relay_save(relay_t *relay, MDB_env *mdb_env);
|
||||||
|
|
||||||
int
|
int
|
||||||
relay_is_active(relay_t *relay, time_t timestamp_now);
|
relay_is_active(relay_t *relay, struct tm *time_struct);
|
||||||
|
|
||||||
void
|
void
|
||||||
relay_free(relay_t *relay);
|
relay_free(relay_t *relay);
|
||||||
|
|
3
logger.c
3
logger.c
|
@ -46,8 +46,7 @@ logger_log(FILE *stream, log_level_t level, const char *filename, int line, cons
|
||||||
}
|
}
|
||||||
|
|
||||||
char timestamp_str[32];
|
char timestamp_str[32];
|
||||||
time_t rawtime;
|
time_t rawtime = time(NULL);
|
||||||
time(&rawtime);
|
|
||||||
strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
|
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);
|
fprintf(stream, "%s %s:%d:%s " COLOR_NONE, timestamp_str, filename, line, func);
|
||||||
|
|
|
@ -15,11 +15,14 @@ period_create(uint16_t start, uint16_t end)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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 start = period->start;
|
||||||
uint16_t end = period->end;
|
uint16_t end = period->end;
|
||||||
|
|
||||||
|
time_t timestamp = time_struct->tm_hour * 60;
|
||||||
|
timestamp += time_struct->tm_min;
|
||||||
|
|
||||||
// "normal" timespan
|
// "normal" timespan
|
||||||
if(start < end)
|
if(start < end)
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,22 +34,17 @@ relay_set_name(relay_t *relay, const char *name)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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)
|
if(schedule->length == 0)
|
||||||
{
|
{
|
||||||
return 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)
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue