/****************************************************************************
 * 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 <gwenhywfar/debug.h>



AQHREACT_UNIT *AqHomeReact_UnitVarChanges_new(AQH_OBJECT *aqh)
{
  AQHREACT_UNIT *unit;
  AQHREACT_PORT *port;

  unit=AQHREACT_Unit_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "varchanges");
  AQHREACT_Unit_SetDescription(unit, "Propagates changes of values (server or local)");

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "intOutput");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_VARCHANGES_OUTSLOT_OUTINT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_INT);
  AQHREACT_Unit_AddOutputPort(unit, port);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "doubleOutput");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_VARCHANGES_OUTSLOT_OUTDOUBLE);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddOutputPort(unit, port);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "stringOutput");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_VARCHANGES_OUTSLOT_OUTSTRING);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_STRING);
  AQHREACT_Unit_AddOutputPort(unit, port);

  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_VARCHANGES_OUTSLOT_OUTDOUBLE);
  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);
  }
}



void AqHomeReact_UnitVarChanges_DoubleVarUpdated(AQHREACT_UNIT *unit, const char *varName, uint64_t timestamp, double data)
{
  AQHREACT_PORT *outputPort;

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

    DBG_DEBUG(NULL, "Variable \"%s\" changed (double)", varName);
    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, varName);
    AQHREACT_Unit_OutputData(unit, outputPort, dataObject);
    AQHREACT_DataObject_free(dataObject);
  }
}



void AqHomeReact_UnitVarChanges_StringVarUpdated(AQHREACT_UNIT *unit, const char *varName, uint64_t timestamp, const char *data)
{
  AQHREACT_PORT *outputPort;

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

    DBG_DEBUG(NULL, "Variable \"%s\" changed (string)", varName);
    dataObject=AQHREACT_DataObject_new();
    AQHREACT_DataObject_SetDataType(dataObject, AQHREACT_DATAOBJECTTYPE_STRING);
    AQHREACT_DataObject_SetTimestamp(dataObject, timestamp);
    AQHREACT_DataObject_SetStringData(dataObject, data);
    AQHREACT_DataObject_SetSystemValueId(dataObject, varName);
    AQHREACT_Unit_OutputData(unit, outputPort, dataObject);
    AQHREACT_DataObject_free(dataObject);
  }
}



void AqHomeReact_UnitVarChanges_IntVarUpdated(AQHREACT_UNIT *unit, const char *varName, uint64_t timestamp, int data)
{
  AQHREACT_PORT *outputPort;

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

    DBG_DEBUG(NULL, "Variable \"%s\" changed (int)", varName);
    dataObject=AQHREACT_DataObject_new();
    AQHREACT_DataObject_SetDataType(dataObject, AQHREACT_DATAOBJECTTYPE_INT);
    AQHREACT_DataObject_SetTimestamp(dataObject, timestamp);
    AQHREACT_DataObject_SetIntData(dataObject, data);
    AQHREACT_DataObject_SetSystemValueId(dataObject, varName);
    AQHREACT_Unit_OutputData(unit, outputPort, dataObject);
    AQHREACT_DataObject_free(dataObject);
  }
}







