Replaced u_hold with more generic u_stabilize.
This commit is contained in:
213
apps/aqhome-react/units/u_stabilize.c
Normal file
213
apps/aqhome-react/units/u_stabilize.c
Normal file
@@ -0,0 +1,213 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2024 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "./u_stabilize.h"
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* defines
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define AQHOMEREACT_UNIT_STABILIZE_INSLOT_INPUT 0
|
||||
#define AQHOMEREACT_UNIT_STABILIZE_INSLOT_TIMER 1
|
||||
|
||||
#define AQHOMEREACT_UNIT_STABILIZE_OUTSLOT_OUTPUT 0
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* type declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
typedef struct AQHREACT_UNIT_STABILIZE AQHREACT_UNIT_STABILIZE;
|
||||
struct AQHREACT_UNIT_STABILIZE {
|
||||
uint64_t tsHoldUntil;
|
||||
int lastProcessedState;
|
||||
int currentOutState;
|
||||
};
|
||||
GWEN_INHERIT(AQHREACT_UNIT, AQHREACT_UNIT_STABILIZE)
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
|
||||
static int _cbProcessFn(AQHREACT_UNIT *unit);
|
||||
static int _checkState(AQHREACT_UNIT *unit);
|
||||
static int _handleStateNoChange(AQHREACT_UNIT *unit, AQHREACT_UNIT_STABILIZE *xunit, int newState);
|
||||
static void _startTimer(AQHREACT_UNIT *unit, AQHREACT_UNIT_STABILIZE *xunit, int newState);
|
||||
static void _setOutput(AQHREACT_UNIT *unit, AQHREACT_UNIT_STABILIZE *xunit, int newState);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitStabilize_new(void)
|
||||
{
|
||||
AQHREACT_UNIT_STABILIZE *xunit;
|
||||
AQHREACT_UNIT *unit;
|
||||
AQHREACT_OUTPUT_SLOT *outputSlot;
|
||||
AQHREACT_INPUT_SLOT *inputSlot;
|
||||
|
||||
unit=AQHREACT_Unit_new();
|
||||
GWEN_NEW_OBJECT(AQHREACT_UNIT_STABILIZE, xunit);
|
||||
GWEN_INHERIT_SETDATA(AQHREACT_UNIT, AQHREACT_UNIT_STABILIZE, unit, xunit, _freeData);
|
||||
|
||||
AQHREACT_Unit_SetName(unit, "stabilize");
|
||||
AQHREACT_Unit_SetDescription(unit, "Stabilize signal changes (only propagate changes stable for some time)");
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessFn);
|
||||
|
||||
outputSlot=AQHREACT_OutputSlot_new();
|
||||
AQHREACT_OutputSlot_SetName(outputSlot, "output");
|
||||
AQHREACT_OutputSlot_SetIdForUnit(outputSlot, AQHOMEREACT_UNIT_STABILIZE_OUTSLOT_OUTPUT);
|
||||
AQHREACT_OutputSlot_SetEmittedDataType(outputSlot, AQHREACT_DATAOBJECTTYPE_DOUBLE);
|
||||
AQHREACT_Unit_AddOutputSlot(unit, outputSlot);
|
||||
|
||||
inputSlot=AQHREACT_InputSlot_new();
|
||||
AQHREACT_InputSlot_SetName(inputSlot, "input");
|
||||
AQHREACT_InputSlot_SetIdForUnit(inputSlot, AQHOMEREACT_UNIT_STABILIZE_INSLOT_INPUT);
|
||||
AQHREACT_InputSlot_SetAcceptedDataType(inputSlot, AQHREACT_DATAOBJECTTYPE_DOUBLE);
|
||||
AQHREACT_Unit_AddInputSlot(unit, inputSlot);
|
||||
|
||||
inputSlot=AQHREACT_InputSlot_new();
|
||||
AQHREACT_InputSlot_SetName(inputSlot, "timer");
|
||||
AQHREACT_InputSlot_SetIdForUnit(inputSlot, AQHOMEREACT_UNIT_STABILIZE_INSLOT_TIMER);
|
||||
AQHREACT_InputSlot_SetAcceptedDataType(inputSlot, AQHREACT_DATAOBJECTTYPE_DOUBLE);
|
||||
AQHREACT_Unit_AddInputSlot(unit, inputSlot);
|
||||
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _freeData(void *bp, void *p)
|
||||
{
|
||||
AQHREACT_UNIT_STABILIZE *xunit;
|
||||
|
||||
xunit=(AQHREACT_UNIT_STABILIZE*) p;
|
||||
GWEN_FREE_OBJECT(xunit);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessFn(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
int rv;
|
||||
|
||||
rv=_checkState(unit);
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputSlots(unit);
|
||||
return rv;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _checkState(AQHREACT_UNIT *unit)
|
||||
{
|
||||
AQHREACT_UNIT_STABILIZE *xunit;
|
||||
int result=0;
|
||||
|
||||
xunit=GWEN_INHERIT_GETDATA(AQHREACT_UNIT, AQHREACT_UNIT_STABILIZE, unit);
|
||||
if (xunit) {
|
||||
AQHREACT_INPUT_SLOT *dataSlot;
|
||||
|
||||
dataSlot=AQHREACT_Unit_GetInputSlotByIdForUnit(unit, AQHOMEREACT_UNIT_STABILIZE_INSLOT_INPUT);
|
||||
if (dataSlot) {
|
||||
AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
dataObject=AQHREACT_InputSlot_GetCurrentDataObject(dataSlot);
|
||||
if (dataObject) {
|
||||
int newState;
|
||||
|
||||
newState=(AQHREACT_DataObject_GetDoubleData(dataObject)>0.0)?1:0;
|
||||
if (newState!=xunit->lastProcessedState) {
|
||||
_startTimer(unit, xunit, newState);
|
||||
result=1;
|
||||
}
|
||||
else {
|
||||
if (_handleStateNoChange(unit, xunit, newState)>0)
|
||||
result=1;
|
||||
}
|
||||
xunit->lastProcessedState=newState;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _handleStateNoChange(AQHREACT_UNIT *unit, AQHREACT_UNIT_STABILIZE *xunit, int newState)
|
||||
{
|
||||
int result=0;
|
||||
uint64_t now;
|
||||
|
||||
now=(uint64_t) time(NULL);
|
||||
if (now>xunit->tsHoldUntil) {
|
||||
/* timeout, switch output */
|
||||
_setOutput(unit, xunit, newState);
|
||||
result=1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _startTimer(AQHREACT_UNIT *unit, AQHREACT_UNIT_STABILIZE *xunit, int newState)
|
||||
{
|
||||
int holdTime;
|
||||
uint64_t now;
|
||||
|
||||
now=(uint64_t) time(NULL);
|
||||
|
||||
DBG_INFO(NULL, "Starting timeout counter (%d)", newState);
|
||||
if (newState)
|
||||
holdTime=AQHREACT_Unit_GetParamValueDouble(unit, AQHOMEREACT_UNIT_STABILIZE_PARAM_HOLDTIME_HIGH, 0.0);
|
||||
else
|
||||
holdTime=AQHREACT_Unit_GetParamValueDouble(unit, AQHOMEREACT_UNIT_STABILIZE_PARAM_HOLDTIME_LOW, 30.0);
|
||||
if (holdTime<=0.0)
|
||||
_setOutput(unit, xunit, newState);
|
||||
else
|
||||
xunit->tsHoldUntil=now+holdTime;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _setOutput(AQHREACT_UNIT *unit, AQHREACT_UNIT_STABILIZE *xunit, int newState)
|
||||
{
|
||||
/* timeout, switch output */
|
||||
DBG_INFO(NULL, "Switch output to %s", newState?"ON":"OFF");
|
||||
AQHREACT_Unit_OutputDoubleData(unit, AQHOMEREACT_UNIT_STABILIZE_OUTSLOT_OUTPUT, newState?1.0:0.0);
|
||||
xunit->currentOutState=newState;
|
||||
xunit->tsHoldUntil=0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user