155 lines
4.7 KiB
C
155 lines
4.7 KiB
C
/****************************************************************************
|
|
* 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_valueset.h"
|
|
|
|
#include "aqhome/ipc/data/msg_data_set.h"
|
|
#include "aqhome/ipc/data/ipc_data.h"
|
|
|
|
#include <gwenhywfar/debug.h>
|
|
#include <gwenhywfar/text.h>
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* defines
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
#define AQHOMEREACT_UNIT_VALUESET_INSLOT_VALUE 0
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* forward declarations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
static void _cbInputData(AQHREACT_UNIT *unit, int slotIdForUnit, const AQHREACT_DATAOBJECT *dataObject);
|
|
static GWEN_MSG *_mkSetDataMsgString(AQHREACT_UNIT *unit, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject);
|
|
static GWEN_MSG *_mkSetDataMsgDouble(AQHREACT_UNIT *unit, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* implementations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
AQHREACT_UNIT *AqHomeReact_UnitValueSet_new(AQHOME_REACT *aqh)
|
|
{
|
|
AQHREACT_UNIT *unit;
|
|
AQHREACT_INPUT_SLOT *inputSlot;
|
|
AQHREACT_PARAM *param;
|
|
|
|
unit=AQHREACT_Unit_new(aqh);
|
|
AQHREACT_Unit_SetName(unit, "valueset");
|
|
AQHREACT_Unit_SetDescription(unit, "Set value by value path");
|
|
AQHREACT_Unit_SetInputDataFn(unit, _cbInputData);
|
|
|
|
inputSlot=AQHREACT_InputSlot_new();
|
|
AQHREACT_InputSlot_SetName(inputSlot, "input");
|
|
AQHREACT_InputSlot_SetIdForUnit(inputSlot, AQHOMEREACT_UNIT_VALUESET_INSLOT_VALUE);
|
|
AQHREACT_InputSlot_SetAcceptedDataType(inputSlot, AQHREACT_DATAOBJECTTYPE_STRING);
|
|
AQHREACT_Unit_AddInputSlot(unit, inputSlot);
|
|
|
|
param=AQHREACT_Param_new();
|
|
AQHREACT_Param_SetName(param, AQHOMEREACT_UNIT_VALUESET_PARAM_VALUENAME);
|
|
AQHREACT_Param_SetDataType(param, AQHREACT_DATAOBJECTTYPE_STRING);
|
|
AQHREACT_Unit_AddParam(unit, param);
|
|
|
|
return unit;
|
|
}
|
|
|
|
|
|
|
|
void _cbInputData(AQHREACT_UNIT *unit, int slotIdForUnit, const AQHREACT_DATAOBJECT *dataObject)
|
|
{
|
|
if (unit && dataObject && slotIdForUnit==AQHOMEREACT_UNIT_VALUESET_INSLOT_VALUE) {
|
|
const char *sValueName;
|
|
|
|
sValueName=AQHREACT_Unit_GetParamValueString(unit, AQHOMEREACT_UNIT_VALUESET_PARAM_VALUENAME, NULL);
|
|
if (sValueName && *sValueName) {
|
|
GWEN_MSG_ENDPOINT *brokerEndpoint;
|
|
|
|
brokerEndpoint=AqHomeReact_GetBrokerEndpoint(AQHREACT_Unit_GetAqHomeReact(unit));
|
|
if (brokerEndpoint) {
|
|
GWEN_MSG *msgOut;
|
|
|
|
switch(AQHREACT_DataObject_GetDataType(dataObject)) {
|
|
case AQHREACT_DATAOBJECTTYPE_DOUBLE:
|
|
msgOut=_mkSetDataMsgDouble(unit, sValueName, dataObject);
|
|
break;
|
|
case AQHREACT_DATAOBJECTTYPE_STRING:
|
|
msgOut=_mkSetDataMsgString(unit, sValueName, dataObject);
|
|
break;
|
|
default:
|
|
DBG_INFO(NULL, "Unhandled data type (%d)", AQHREACT_DataObject_GetDataType(dataObject));
|
|
msgOut=NULL;
|
|
break;
|
|
}
|
|
if (msgOut) {
|
|
DBG_INFO(NULL, "Sending data for value \"%s\"", sValueName);
|
|
GWEN_MsgEndpoint_AddSendMessage(brokerEndpoint, msgOut);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
GWEN_MSG *_mkSetDataMsgString(AQHREACT_UNIT *unit, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject)
|
|
{
|
|
GWEN_MSG *msgOut;
|
|
AQH_VALUE *v;
|
|
|
|
v=AQH_Value_new();
|
|
AQH_Value_SetNameForSystem(v, sValueName);
|
|
|
|
msgOut=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, v, AQHREACT_DataObject_GetStringData(dataObject));
|
|
AQH_Value_free(v);
|
|
return msgOut;
|
|
}
|
|
|
|
|
|
|
|
GWEN_MSG *_mkSetDataMsgDouble(AQHREACT_UNIT *unit, const char *sValueName, const AQHREACT_DATAOBJECT *dataObject)
|
|
{
|
|
GWEN_MSG *msgOut;
|
|
AQH_VALUE *v;
|
|
double data;
|
|
GWEN_BUFFER *buf;
|
|
int rv;
|
|
|
|
v=AQH_Value_new();
|
|
AQH_Value_SetNameForSystem(v, sValueName);
|
|
|
|
data=AQHREACT_DataObject_GetDoubleData(dataObject);
|
|
buf=GWEN_Buffer_new(0, 64, 0, 1);
|
|
rv=GWEN_Text_DoubleToBuffer(data, buf);
|
|
if (rv<0) {
|
|
GWEN_Buffer_free(buf);
|
|
AQH_Value_free(v);
|
|
return NULL;
|
|
}
|
|
|
|
msgOut=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, v, GWEN_Buffer_GetStart(buf));
|
|
GWEN_Buffer_free(buf);
|
|
AQH_Value_free(v);
|
|
return msgOut;
|
|
}
|
|
|
|
|
|
|
|
|
|
|