#942 Add weeks and week days to the schedule API

* add og_schedule_create_weekdays()
* add og_schedule_create_weeks()
* add og_schedule_create_days()
master
Javier Sánchez Parra 2020-03-06 14:16:17 +01:00 committed by OpenGnSys Support Team
parent 54c7ca3bb3
commit 130b6ffae8
2 changed files with 171 additions and 115 deletions

View File

@ -1554,10 +1554,12 @@ struct og_computer {
#define OG_REST_PARAM_TASK (1UL << 31) #define OG_REST_PARAM_TASK (1UL << 31)
#define OG_REST_PARAM_TIME_YEARS (1UL << 32) #define OG_REST_PARAM_TIME_YEARS (1UL << 32)
#define OG_REST_PARAM_TIME_MONTHS (1UL << 33) #define OG_REST_PARAM_TIME_MONTHS (1UL << 33)
#define OG_REST_PARAM_TIME_DAYS (1UL << 34) #define OG_REST_PARAM_TIME_WEEKS (1UL << 34)
#define OG_REST_PARAM_TIME_HOURS (1UL << 35) #define OG_REST_PARAM_TIME_WEEK_DAYS (1UL << 35)
#define OG_REST_PARAM_TIME_AM_PM (1UL << 36) #define OG_REST_PARAM_TIME_DAYS (1UL << 36)
#define OG_REST_PARAM_TIME_MINUTES (1UL << 37) #define OG_REST_PARAM_TIME_HOURS (1UL << 37)
#define OG_REST_PARAM_TIME_AM_PM (1UL << 38)
#define OG_REST_PARAM_TIME_MINUTES (1UL << 39)
enum og_rest_method { enum og_rest_method {
OG_METHOD_GET = 0, OG_METHOD_GET = 0,
@ -1793,6 +1795,12 @@ static int og_json_parse_time_params(json_t *element,
} else if (!strcmp(key, "months")) { } else if (!strcmp(key, "months")) {
err = og_json_parse_uint(value, &params->time.months); err = og_json_parse_uint(value, &params->time.months);
params->flags |= OG_REST_PARAM_TIME_MONTHS; params->flags |= OG_REST_PARAM_TIME_MONTHS;
} else if (!strcmp(key, "weeks")) {
err = og_json_parse_uint(value, &params->time.weeks);
params->flags |= OG_REST_PARAM_TIME_WEEKS;
} else if (!strcmp(key, "week_days")) {
err = og_json_parse_uint(value, &params->time.week_days);
params->flags |= OG_REST_PARAM_TIME_WEEK_DAYS;
} else if (!strcmp(key, "days")) { } else if (!strcmp(key, "days")) {
err = og_json_parse_uint(value, &params->time.days); err = og_json_parse_uint(value, &params->time.days);
params->flags |= OG_REST_PARAM_TIME_DAYS; params->flags |= OG_REST_PARAM_TIME_DAYS;
@ -3742,12 +3750,15 @@ static int og_dbi_schedule_create(struct og_dbi *dbi,
result = dbi_conn_queryf(dbi->conn, result = dbi_conn_queryf(dbi->conn,
"INSERT INTO programaciones (tipoaccion," "INSERT INTO programaciones (tipoaccion,"
" identificador, nombrebloque, annos, meses," " identificador, nombrebloque, annos, meses,"
" diario, horas, ampm, minutos, suspendida) VALUES (%d," " semanas, dias, diario, horas, ampm, minutos,"
" %s, '%s', %d, %d, %d, %d, %d, %d, %d)", type, " suspendida) VALUES (%d, %s, '%s', %d, %d,"
params->task_id, params->name, params->time.years, " %d, %d, %d, %d, %d, %d, %d)", type,
params->time.months, params->time.days, params->task_id, params->name,
params->time.hours, params->time.am_pm, params->time.years, params->time.months,
params->time.minutes, suspended); params->time.weeks, params->time.week_days,
params->time.days, params->time.hours,
params->time.am_pm, params->time.minutes,
suspended);
if (!result) { if (!result) {
dbi_conn_error(dbi->conn, &msglog); dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
@ -3946,6 +3957,8 @@ static int og_cmd_schedule_create(json_t *element, struct og_msg_params *params)
OG_REST_PARAM_NAME | OG_REST_PARAM_NAME |
OG_REST_PARAM_TIME_YEARS | OG_REST_PARAM_TIME_YEARS |
OG_REST_PARAM_TIME_MONTHS | OG_REST_PARAM_TIME_MONTHS |
OG_REST_PARAM_TIME_WEEKS |
OG_REST_PARAM_TIME_WEEK_DAYS |
OG_REST_PARAM_TIME_DAYS | OG_REST_PARAM_TIME_DAYS |
OG_REST_PARAM_TIME_HOURS | OG_REST_PARAM_TIME_HOURS |
OG_REST_PARAM_TIME_MINUTES | OG_REST_PARAM_TIME_MINUTES |

View File

@ -128,13 +128,9 @@ static void get_last_week(struct tm *tm, int *days, int *j)
days[(*j)++] = tm->tm_mday; days[(*j)++] = tm->tm_mday;
syslog(LOG_ERR, "TM_WDAY: %d", tm->tm_wday);//XXX
syslog(LOG_ERR, "TM_MDAY: %d", tm->tm_mday);//XXX
/* Last day of this week? */ /* Last day of this week? */
if (tm->tm_wday == 1) { if (tm->tm_wday == 1)
syslog(LOG_ERR, "break week");//XXX
break; break;
}
tm->tm_mday--; tm->tm_mday--;
} }
@ -199,17 +195,140 @@ static void og_schedule_remove_duplicates()
} }
} }
static void og_schedule_create_weekdays(int month, int year,
int *hours, int minutes, int week_days,
uint32_t task_id, uint32_t schedule_id)
{
struct og_schedule *schedule;
int month_days[5];
int n_month_days;
uint32_t wday;
struct tm tm;
int k, l;
for (wday = 0; wday < 7; wday++) {
if (!((1 << wday) & week_days))
continue;
memset(&tm, 0, sizeof(tm));
tm.tm_mon = month;
tm.tm_year = year;
n_month_days = 0;
memset(month_days, 0, sizeof(month_days));
get_days_from_weekday(&tm, wday, month_days, &n_month_days);
for (k = 0; month_days[k] != 0 && k < n_month_days; k++) {
for (l = 0; hours[l] != 0 && l < 31; l++) {
schedule = (struct og_schedule *)
calloc(1, sizeof(struct og_schedule));
if (!schedule)
return;
memset(&tm, 0, sizeof(tm));
tm.tm_year = year;
tm.tm_mon = month;
tm.tm_mday = month_days[k];
tm.tm_hour = hours[l] - 1;
tm.tm_min = minutes;
schedule->seconds = mktime(&tm);
schedule->task_id = task_id;
schedule->schedule_id = schedule_id;
og_schedule_add(schedule);
}
}
}
}
static void og_schedule_create_weeks(int month, int year,
int *hours, int minutes, int weeks,
uint32_t task_id, uint32_t schedule_id)
{
struct og_schedule *schedule;
int month_days[7];
int n_month_days;
struct tm tm;
int week;
int k, l;
for (week = 0; week < 5; week++) {
if (!((1 << week) & weeks))
continue;
memset(&tm, 0, sizeof(tm));
tm.tm_mon = month;
tm.tm_year = year;
n_month_days = 0;
memset(month_days, 0, sizeof(month_days));
if (week == 5)
get_last_week(&tm, month_days, &n_month_days);
else
get_days_from_week(&tm, week, month_days, &n_month_days);
for (k = 0; month_days[k] != 0 && k < n_month_days; k++) {
for (l = 0; hours[l] != 0 && l < 31; l++) {
schedule = (struct og_schedule *)
calloc(1, sizeof(struct og_schedule));
if (!schedule)
return;
memset(&tm, 0, sizeof(tm));
tm.tm_year = year;
tm.tm_mon = month;
tm.tm_mday = month_days[k];
tm.tm_hour = hours[l] - 1;
tm.tm_min = minutes;
schedule->seconds = mktime(&tm);
schedule->task_id = task_id;
schedule->schedule_id = schedule_id;
og_schedule_add(schedule);
}
}
}
}
static void og_schedule_create_days(int month, int year,
int *hours, int minutes, int *days,
uint32_t task_id, uint32_t schedule_id)
{
struct og_schedule *schedule;
struct tm tm;
int k, l;
for (k = 0; days[k] != 0 && k < 31; k++) {
for (l = 0; hours[l] != 0 && l < 31; l++) {
schedule = (struct og_schedule *)
calloc(1, sizeof(struct og_schedule));
if (!schedule)
return;
memset(&tm, 0, sizeof(tm));
tm.tm_year = year;
tm.tm_mon = month;
tm.tm_mday = days[k];
tm.tm_hour = hours[l] - 1;
tm.tm_min = minutes;
schedule->seconds = mktime(&tm);
schedule->task_id = task_id;
schedule->schedule_id = schedule_id;
og_schedule_add(schedule);
}
}
}
void og_schedule_create(unsigned int schedule_id, unsigned int task_id, void og_schedule_create(unsigned int schedule_id, unsigned int task_id,
struct og_schedule_time *time) struct og_schedule_time *time)
{ {
struct og_schedule *schedule; int year, month, minutes;
int months[12] = {}; int months[12] = {};
int years[12] = {}; int years[12] = {};
int hours[12] = {}; int hours[12] = {};
int days[31] = {}; int days[31] = {};
struct tm tm = {}; int i, j;
int i, j, k, l = 0;
int minutes;
og_parse_years(time->years, years); og_parse_years(time->years, years);
og_parse_months(time->months, months); og_parse_months(time->months, months);
@ -219,105 +338,29 @@ void og_schedule_create(unsigned int schedule_id, unsigned int task_id,
for (i = 0; years[i] != 0 && i < 12; i++) { for (i = 0; years[i] != 0 && i < 12; i++) {
for (j = 0; months[j] != 0 && j < 12; j++) { for (j = 0; months[j] != 0 && j < 12; j++) {
memset(&tm, 0, sizeof(tm)); month = months[j] - 1;
tm.tm_year = years[i]; year = years[i];
tm.tm_mon = months[j] - 1;
if (time->week_days) {
for (int wday = 0; wday < 7; wday++) {
if ((1 << wday) & time->week_days) {
int specific_month_days[5] = {};
int n_month_days = 0;
get_days_from_weekday(&tm,
wday,
specific_month_days,
&n_month_days);
for (k = 0; specific_month_days[k] != 0 && k < n_month_days; k++) { if (time->week_days)
for (l = 0; hours[l] != 0 && l < 31; l++) { og_schedule_create_weekdays(month, year,
schedule = (struct og_schedule *) hours, minutes,
calloc(1, sizeof(struct og_schedule)); time->week_days,
if (!schedule) task_id,
return; schedule_id);
memset(&tm, 0, sizeof(tm)); if (time->weeks)
tm.tm_year = years[i]; og_schedule_create_weeks(month, year,
tm.tm_mon = months[j] - 1; hours, minutes,
tm.tm_mday = specific_month_days[k]; time->weeks,
tm.tm_hour = hours[l] - 1; task_id,
tm.tm_min = minutes; schedule_id);
schedule->seconds = mktime(&tm); if (time->days)
schedule->task_id = task_id; og_schedule_create_days(month, year,
schedule->schedule_id = schedule_id; hours, minutes,
og_schedule_add(schedule); days,
} task_id,
} schedule_id);
}
}
}
if (time->weeks) {
for (int week = 0; week < 5; week++) {
if ((1 << week) & time->weeks) {
int specific_month_days[7] = {};
int n_month_days = 0;
if (week == 5)
get_last_week(&tm,
specific_month_days,
&n_month_days);
else
get_days_from_week(&tm,
week,
specific_month_days,
&n_month_days);
for (k = 0; specific_month_days[k] != 0 && k < n_month_days; k++) {
for (l = 0; hours[l] != 0 && l < 31; l++) {
schedule = (struct og_schedule *)
calloc(1, sizeof(struct og_schedule));
if (!schedule)
return;
memset(&tm, 0, sizeof(tm));
tm.tm_year = years[i];
tm.tm_mon = months[j] - 1;
tm.tm_mday = specific_month_days[k];
tm.tm_hour = hours[l] - 1;
tm.tm_min = minutes;
schedule->seconds = mktime(&tm);
schedule->task_id = task_id;
schedule->schedule_id = schedule_id;
og_schedule_add(schedule);
}
}
}
}
}
if (time->days) {
for (k = 0; days[k] != 0 && k < 31; k++) {
for (l = 0; hours[l] != 0 && l < 31; l++) {
schedule = (struct og_schedule *)
calloc(1, sizeof(struct og_schedule));
if (!schedule)
return;
memset(&tm, 0, sizeof(tm));
tm.tm_year = years[i];
tm.tm_mon = months[j] - 1;
tm.tm_mday = days[k];
tm.tm_hour = hours[l] - 1;
tm.tm_min = minutes;
schedule->seconds = mktime(&tm);
schedule->task_id = task_id;
schedule->schedule_id = schedule_id;
og_schedule_add(schedule);
}
}
}
} }
} }