/****************************************************************************
 * 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_passthrough.h"

#include <gwenhywfar/debug.h>



/* ------------------------------------------------------------------------------------------------
 * defines
 * ------------------------------------------------------------------------------------------------
 */



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void _cbInputData(AQHREACT_UNIT *unit, AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject);



/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

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

  unit=AQHREACT_Unit_new(aqh);
  AQHREACT_Unit_SetTypeName(unit, "passthrough");
  AQHREACT_Unit_SetDescription(unit, "Generic passthrough unit");
  AQHREACT_Unit_SetInputDataFn(unit, _cbInputData);

  port=AQHREACT_Port_new();
  AQHREACT_Port_SetName(port, "output");
  AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_PASSTHROUGH_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_PASSTHROUGH_OUTSLOT_OUTPUT);
  AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
  AQHREACT_Unit_AddInputPort(unit, port);

  return unit;
}




void _cbInputData(AQHREACT_UNIT *unit, AQHREACT_PORT *port, const AQHREACT_DATAOBJECT *dataObject)
{
  if (unit && port && dataObject && AQHREACT_Port_GetIdForUnit(port)==AQHOMEREACT_UNIT_PASSTHROUGH_INSLOT_INPUT) {
    AQHREACT_PORT *outputPort;

    outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_PASSTHROUGH_OUTSLOT_OUTPUT);
    if (outputPort)
      AQHREACT_Unit_OutputData(unit, outputPort, dataObject);
  }
}




