/****************************************************************************
 * 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>

#include <time.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(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT_STABILIZE *xunit;
  AQHREACT_UNIT *unit;
  AQHREACT_PORT *port;
  AQHREACT_PARAM *param;

  unit=AQHREACT_Unit_new(aqh);
  GWEN_NEW_OBJECT(AQHREACT_UNIT_STABILIZE, xunit);
  GWEN_INHERIT_SETDATA(AQHREACT_UNIT, AQHREACT_UNIT_STABILIZE, unit, xunit, _freeData);

  AQHREACT_Unit_SetTypeName(unit, "stabilize");
  AQHREACT_Unit_SetDescription(unit, "Stabilize signal changes (only propagate changes stable for some time)");
  AQHREACT_Unit_SetProcessFn(unit, _cbProcessFn);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "output");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_STABILIZE_OUTSLOT_OUTPUT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddOutputPort(unit, port);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "input");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_STABILIZE_INSLOT_INPUT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddInputPort(unit, port);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "timer");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_STABILIZE_INSLOT_TIMER);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddInputPort(unit, port);

  param=AQHREACT_Param_new();
  AQHREACT_Param_SetName(param, AQHOMEREACT_UNIT_STABILIZE_PARAM_HOLDTIME_HIGH);
  AQHREACT_Param_SetDataType(param, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddParam(unit, param);

  param=AQHREACT_Param_new();
  AQHREACT_Param_SetName(param, AQHOMEREACT_UNIT_STABILIZE_PARAM_HOLDTIME_LOW);
  AQHREACT_Param_SetDataType(param, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddParam(unit, param);

  return unit;
}



void _freeData(GWEN_UNUSED 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_ClearChangeFlagsInUnitAndInputPorts(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_PORT *port;

    port=AQHREACT_Unit_GetInputPortByIdForUnit(unit, AQHOMEREACT_UNIT_STABILIZE_INSLOT_INPUT);
    if (port) {
      AQHREACT_DATAOBJECT *dataObject;

      dataObject=AQHREACT_Port_GetCurrentDataObject(port);
      if (dataObject) {
        double doubleData;
        int newState;

        doubleData=AQHREACT_DataObject_GetDoubleData(dataObject);
        newState=(doubleData>0.0)?1:0;
        DBG_DEBUG(NULL, "Got dataObject (%f), new state is %d (previously: %d)", doubleData, newState, xunit->lastProcessedState);
        if (newState!=xunit->lastProcessedState) {
	  DBG_INFO(NULL, "State changed (%d->%d)[%f]", xunit->lastProcessedState, newState, doubleData);
          _startTimer(unit, xunit, newState);
          result=1;
        }
        else {
          DBG_DEBUG(NULL, "Stage unchanged (%d)", newState);
          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 (xunit->tsHoldUntil && 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);
  
  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) {
    DBG_INFO(NULL, "Timeout is zero, immediately setting output (%d)", newState);
    _setOutput(unit, xunit, newState);
  }
  else {
    DBG_INFO(NULL, "Starting timeout counter (%d, t=%d)", newState, holdTime);
    xunit->tsHoldUntil=now+holdTime;
  }
}



void _setOutput(AQHREACT_UNIT *unit, AQHREACT_UNIT_STABILIZE *xunit, int newState)
{
  AQHREACT_PORT *outputPort;

  outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_STABILIZE_OUTSLOT_OUTPUT);
  if (outputPort) {
    /* timeout, switch output */
    DBG_INFO(NULL, "Switch output to %s", newState?"ON":"OFF");
    AQHREACT_Unit_OutputDoubleData(unit, outputPort, newState?1.0:0.0);
    xunit->currentOutState=newState;
    xunit->tsHoldUntil=0;
  }
}







