From eea0a66b844aaeac6c4aa99c11892973ced4b92e Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Mon, 21 Apr 2025 00:29:53 +0200 Subject: [PATCH] aqhome-react: fixed unit timeprogram. --- apps/aqhome-react/units/u_timeprogram.c | 68 ++++++++++++++----------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/apps/aqhome-react/units/u_timeprogram.c b/apps/aqhome-react/units/u_timeprogram.c index e9b4056..ae1aeba 100644 --- a/apps/aqhome-react/units/u_timeprogram.c +++ b/apps/aqhome-react/units/u_timeprogram.c @@ -29,8 +29,8 @@ #define AQHOMEREACT_UNIT_TIMEPROGRAM_OUTSLOT_OUTPUT 0 -#define AQHOMEREACT_UNIT_TIMEPROGRAM_CREATEINTERVAL_MIN 60 /* 1h */ -#define AQHOMEREACT_UNIT_TIMEPROGRAM_CREATEINTERVAL_SEC (61*60) /* 61m */ +#define AQHOMEREACT_UNIT_TIMEPROGRAM_CREATEFORNEXT_MINS 60 /* 1h */ +#define AQHOMEREACT_UNIT_TIMEPROGRAM_CREATEEVERY_SECS (15*60) /* 15m */ #define AQHOMEREACT_UNIT_TIMEPROGRAM_CHECKINTERVAL_SEC 60 /* 1m */ @@ -51,7 +51,7 @@ static int _cbProcess(AQHREACT_UNIT *unit); static int _readRules(AQHREACT_UNIT *unit); static int _checkAndAddActions(AQHREACT_UNIT *unit, AQHREACT_UNIT_TIMEPROGRAM *xunit); -static int _addActionsForNextMinutes(GWEN_TIMESTAMP *ts, int minutes, +static int _addActionsForNextMinutes(AQHREACT_UNIT_TIMEPROGRAM *xunit, int minutes, const AQHREACT_PRGRULE_LIST *ruleList, MODULE_TIMER_ACTION_LIST *actionList); static int _handlePendingActions(AQHREACT_UNIT *unit, AQHREACT_UNIT_TIMEPROGRAM *xunit); @@ -194,9 +194,9 @@ int _checkAndAddActions(AQHREACT_UNIT *unit, AQHREACT_UNIT_TIMEPROGRAM *xunit) int rv; now=time(NULL); - if (now-xunit->lastCreateTime>AQHOMEREACT_UNIT_TIMEPROGRAM_CREATEINTERVAL_SEC) { - rv=_addActionsForNextMinutes(xunit->nextAddTime, - AQHOMEREACT_UNIT_TIMEPROGRAM_CREATEINTERVAL_MIN, + if ((now-xunit->lastCreateTime)>AQHOMEREACT_UNIT_TIMEPROGRAM_CREATEEVERY_SECS) { + rv=_addActionsForNextMinutes(xunit, + AQHOMEREACT_UNIT_TIMEPROGRAM_CREATEFORNEXT_MINS, xunit->ruleList, xunit->actionList); if (rv<0) { @@ -208,7 +208,7 @@ int _checkAndAddActions(AQHREACT_UNIT *unit, AQHREACT_UNIT_TIMEPROGRAM *xunit) xunit->lastCreateTime=now; } - if (now-xunit->lastCheckTime>AQHOMEREACT_UNIT_TIMEPROGRAM_CHECKINTERVAL_SEC) { + if ((now-xunit->lastCheckTime)>AQHOMEREACT_UNIT_TIMEPROGRAM_CHECKINTERVAL_SEC) { rv=_handlePendingActions(unit, xunit); if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); @@ -250,7 +250,7 @@ int _handlePendingActions(AQHREACT_UNIT *unit, AQHREACT_UNIT_TIMEPROGRAM *xunit) nextAction=ModuleTimerAction_List_Next(action); if (action->triggerTimevalue); + DBG_ERROR(NULL, "Sending output value %.2f", action->value); AQHREACT_Unit_OutputDoubleData(unit, outputPort, action->value); ModuleTimerAction_free(action); valueSent=1; @@ -265,37 +265,45 @@ int _handlePendingActions(AQHREACT_UNIT *unit, AQHREACT_UNIT_TIMEPROGRAM *xunit) -int _addActionsForNextMinutes(GWEN_TIMESTAMP *ts, int minutes, const AQHREACT_PRGRULE_LIST *ruleList, MODULE_TIMER_ACTION_LIST *actionList) +int _addActionsForNextMinutes(AQHREACT_UNIT_TIMEPROGRAM *xunit, + int minutes, + const AQHREACT_PRGRULE_LIST *ruleList, + MODULE_TIMER_ACTION_LIST *actionList) { int actionsAdded=0; int i; + GWEN_TIMESTAMP *tsWork; + tsWork=GWEN_Timestamp_NowInLocalTime(); for (i=0; inextAddTime)==1) { + int64_t triggerTime; + const AQHREACT_PRGRULE *rule; - triggerTime=GWEN_Timestamp_toInt64(ts); + triggerTime=GWEN_Timestamp_toInt64(tsWork); - rule=AQHREACT_PrgRule_List_First(ruleList); - while(rule) { - if (AQHREACT_PrgRule_Matches(rule, - GWEN_Timestamp_GetMinute(ts), - GWEN_Timestamp_GetHour(ts), - GWEN_Timestamp_GetDay(ts), - GWEN_Timestamp_GetMonth(ts), - GWEN_Timestamp_GetWeekDay(ts))>0) { - MODULE_TIMER_ACTION *act; + rule=AQHREACT_PrgRule_List_First(ruleList); + while(rule) { + if (AQHREACT_PrgRule_Matches(rule, + GWEN_Timestamp_GetMinute(tsWork), + GWEN_Timestamp_GetHour(tsWork), + GWEN_Timestamp_GetDay(tsWork), + GWEN_Timestamp_GetMonth(tsWork), + GWEN_Timestamp_GetWeekDay(tsWork))>0) { + MODULE_TIMER_ACTION *act; - DBG_INFO(NULL, "- rule matches for %s, creating action", GWEN_Timestamp_GetString(ts)); - act=ModuleTimerAction_new(triggerTime, AQHREACT_PrgRule_GetValue(rule)); - ModuleTimerAction_List_Add(act, actionList); - actionsAdded=1; - } - rule=AQHREACT_PrgRule_List_Next(rule); - } /* while */ - - GWEN_Timestamp_AddSeconds(ts, 60); /* next min */ + DBG_ERROR(NULL, "- rule matches for %s, creating action", GWEN_Timestamp_GetString(tsWork)); + act=ModuleTimerAction_new(triggerTime, AQHREACT_PrgRule_GetValue(rule)); + ModuleTimerAction_List_Add(act, actionList); + actionsAdded=1; + } + rule=AQHREACT_PrgRule_List_Next(rule); + } /* while */ + } + GWEN_Timestamp_AddSeconds(tsWork, 60); /* next min */ } /* for */ + GWEN_Timestamp_free(xunit->nextAddTime); + xunit->nextAddTime=tsWork; return actionsAdded?1:0; }