/****************************************************************************
 * 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_varchanges.h"
#include "./u_passthrough.h"

#include <gwenhywfar/debug.h>



AQHREACT_UNIT *AqHomeReact_UnitVarChanges_new(AQHOME_REACT *aqh)
{
  AQHREACT_UNIT *unit;

  unit=AqHomeReact_UnitPassthrough_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "varchanges");
  AQHREACT_Unit_SetDescription(unit, "Propagates changes of values on the data server");

  return unit;
}



void AqHomeReact_UnitVarChanges_ValueUpdated(AQHREACT_UNIT *unit, const AQH_VALUE *value, uint64_t timestamp, double data)
{
  AQHREACT_PORT *outputPort;

  outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_PASSTHROUGH_OUTSLOT_OUTPUT);
  if (outputPort) {
    AQHREACT_DATAOBJECT *dataObject;

    DBG_DEBUG(NULL, "Value \"%s\" changed", AQH_Value_GetNameForSystem(value));
    dataObject=AQHREACT_DataObject_new();
    AQHREACT_DataObject_SetDataType(dataObject, AQHREACT_DATAOBJECTTYPE_DOUBLE);
    AQHREACT_DataObject_SetTimestamp(dataObject, timestamp);
    AQHREACT_DataObject_SetDoubleData(dataObject, data);
    AQHREACT_DataObject_SetSystemValueId(dataObject, AQH_Value_GetNameForSystem(value));
    AQHREACT_DataObject_SetValueId(dataObject, AQH_Value_GetId(value));
    AQHREACT_Unit_OutputData(unit, outputPort, dataObject);
    AQHREACT_DataObject_free(dataObject);
  }
}






