#include #include #include struct crontask { void (*run)(); int last_run; // Number of seconds after start to wait until running int interval; }; #define TASKCOUNT 3 static struct crontask tasks[TASKCOUNT] = { {&temperature_update_sisa, 0, 60}, {&humidity_update_sisa, 15, 60}, {&temperature_update_ulko, 30, 60} }; /* Public functions */ void cron_initialize() { int i; for (i = 0; i < TASKCOUNT; i++) { tasks[i].last_run += time(NULL) - tasks[i].interval; } } int cron_runtasks() { // Returns time of next event int i; int nextevent = -1; for (i = 0; i < TASKCOUNT; i++) { if (tasks[i].last_run + tasks[i].interval <= time(NULL)) { tasks[i].last_run = time(NULL); tasks[i].run(); } int ne = tasks[i].last_run + tasks[i].interval; if (nextevent < 0 || ne < nextevent) nextevent = ne; } if (nextevent < 0) fprintf(stderr, "Nothing to run!\n"); return nextevent; }