/****************************************************************************
 * 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 "./devicesdump.h"
#include "./server_p.h"
#include "aqhome/aqhome.h"

#include <gwenhywfar/debug.h>



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

static void _dumpDevice(const AQHNODE_DEVICE *dev, GWEN_BUFFER *dbuf, int indent);
static void _dumpValue(const AQHNODE_VALUE *value, GWEN_BUFFER *dbuf, int indent);



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

void AQH_NodeServer_DumpDevices(const AQHNODE_DEVICE_LIST *devList, GWEN_BUFFER *dbuf)
{

  if (devList && AQHNODE_Device_List_GetCount(devList)) {
    const AQHNODE_DEVICE *dev;

    GWEN_Buffer_AppendString(dbuf, "Devices:\n");
    dev=AQHNODE_Device_List_First(devList);
    while(dev) {
      _dumpDevice(dev, dbuf, 2);
      dev=AQHNODE_Device_List_Next(dev);
    }
  }

}



void _dumpDevice(const AQHNODE_DEVICE *dev, GWEN_BUFFER *dbuf, int indent)
{
  const char *name;
  const char *driver;
  uint32_t manufacturer;
  uint16_t deviceType;
  uint16_t deviceVersion;
  const AQHNODE_VALUE_LIST *valueList;

  name=AQHNODE_Device_GetName(dev);
  driver=AQHNODE_Device_GetDriver(dev);
  manufacturer=AQHNODE_Device_GetManufacturer(dev);
  deviceType=AQHNODE_Device_GetDeviceType(dev);
  deviceVersion=AQHNODE_Device_GetDeviceVersion(dev);

  GWEN_Buffer_FillWithBytes(dbuf, ' ', indent);
  GWEN_Buffer_AppendArgs(dbuf, "Device: %s (%s, %08x, %04x, %04x)\n",
			 name?name:"<empty name>",
			 driver?driver:"<empty driver>",
			 manufacturer,
			 deviceType,
			 deviceVersion);
  valueList=AQHNODE_Device_GetValueList(dev);
  if (valueList && AQHNODE_Value_List_GetCount(valueList)) {
    const AQHNODE_VALUE *value;

    value=AQHNODE_Value_List_First(valueList);
    while(value) {
      _dumpValue(value, dbuf, indent+2);
      value=AQHNODE_Value_List_Next(value);
    }
  }
}



void _dumpValue(const AQHNODE_VALUE *value, GWEN_BUFFER *dbuf, int indent)
{
  int id;
  const char *name;
  const char *descr;
  int valueType;
  int dataType;
  int modality;
  const char *units;
  int denom;

  id=AQHNODE_Value_GetId(value);
  name=AQHNODE_Value_GetName(value);
  descr=AQHNODE_Value_GetDescription(value);
  valueType=AQHNODE_Value_GetValueType(value);
  dataType=AQHNODE_Value_GetDataType(value);
  modality=AQHNODE_Value_GetModality(value);
  units=AQHNODE_Value_GetValueUnits(value);
  denom=AQHNODE_Value_GetDenom(value);

  GWEN_Buffer_FillWithBytes(dbuf, ' ', indent);
  GWEN_Buffer_AppendArgs(dbuf, "Value: %d[%02x] (%s, %s, %s, %s, %s, %s, %d)\n",
			 id, id,
			 name?name:"<empty name>",
			 AQH_ValueType_toString(valueType),
			 AQH_ValueDataType_toString(dataType),
			 AQH_ValueModality_toString(modality),
			 units?units:"<empty units>",
                         descr?descr:"<empty descr>",
                         denom);
}







