121 lines
3.7 KiB
C
121 lines
3.7 KiB
C
/****************************************************************************
|
|
* This file is part of the project AqHome.
|
|
* AqHome (c) by 2023 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 "./c_getdatapoints.h"
|
|
#include "./aqhome_data_p.h"
|
|
#include "aqhome/ipc/data/ipc_data.h"
|
|
#include "aqhome/ipc/data/msg_data_multidata.h"
|
|
#include "aqhome/ipc/data/msg_data_getdata.h"
|
|
#include "aqhome/ipc/endpoint_ipc.h"
|
|
#include "aqhome/ipc/msg_ipc_result.h"
|
|
#include "aqhome/ipc/msg_ipc_tag16.h"
|
|
|
|
#include <gwenhywfar/debug.h>
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* defines
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
#define AQHOMEDATA_HANDLEGETDATAPOINTS_MAXTABLEENTRIES 2048
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* forward declarations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
static int _getAndSendDataPoints(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, const AQH_VALUE *value, uint64_t tsBegin, uint64_t tsEnd);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* implementations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
|
|
void AqHomeData_HandleGetDataPoints(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, GWEN_MSG *recvdMsg)
|
|
{
|
|
GWEN_MSG *outMsg;
|
|
int resultCode=AQH_MSG_IPC_SUCCESS;
|
|
|
|
if (AQH_IpcEndpoint_GetPermissions(ep) & AQH_IPCENDPOINT_PERMS_READDATA) {
|
|
AQH_VALUE *value;
|
|
char *valueName;
|
|
uint64_t tsBegin;
|
|
uint64_t tsEnd;
|
|
|
|
AQH_GetDataDataIpcMsg_Parse(recvdMsg, 0);
|
|
valueName=AQH_Tag16IpcMsg_GetTagDataAsNewString(recvdMsg, AQH_MSGDATA_GETDATA_TAGS_NAME, NULL);
|
|
tsBegin=AQH_Tag16IpcMsg_GetTagDataAsUint64(recvdMsg, AQH_MSGDATA_GETDATA_TAGS_BEGIN, 0);
|
|
tsEnd=AQH_Tag16IpcMsg_GetTagDataAsUint64(recvdMsg, AQH_MSGDATA_GETDATA_TAGS_END, 0);
|
|
|
|
value=AQH_Storage_GetValueByNameForSystem(aqh->storage, valueName);
|
|
if (value) {
|
|
resultCode=_getAndSendDataPoints(aqh, ep, value, tsBegin, tsEnd);
|
|
if (resultCode==AQH_MSG_IPC_SUCCESS)
|
|
return;
|
|
}
|
|
else {
|
|
DBG_INFO(NULL, "Value \"%s\" does not exist", valueName);
|
|
resultCode=AQH_MSG_IPC_ERROR_NOTFOUND;
|
|
}
|
|
free(valueName);
|
|
}
|
|
else {
|
|
DBG_ERROR(AQH_LOGDOMAIN, "No permissions to read data");
|
|
resultCode=AQH_MSG_IPC_ERROR_PERMS;
|
|
}
|
|
|
|
outMsg=AQH_ResultIpcMsg_new(AQH_MSGTYPE_IPC_DATA_RESULT, resultCode);
|
|
GWEN_MsgEndpoint_AddSendMessage(ep, outMsg);
|
|
}
|
|
|
|
|
|
|
|
int _getAndSendDataPoints(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, const AQH_VALUE *value, uint64_t tsBegin, uint64_t tsEnd)
|
|
{
|
|
uint64_t valueId;
|
|
uint64_t *tablePtr;
|
|
|
|
valueId=AQH_Value_GetId(value);
|
|
|
|
tablePtr=AQH_Storage_GetDataPoints(aqh->storage, valueId, tsBegin, tsEnd, AQHOMEDATA_HANDLEGETDATAPOINTS_MAXTABLEENTRIES);
|
|
if (tablePtr) {
|
|
int numTableEntries;
|
|
int numDataPoints;
|
|
GWEN_MSG *outMsg;
|
|
|
|
numTableEntries=(int)(tablePtr[0]);
|
|
numDataPoints=numTableEntries/2;
|
|
outMsg=AQH_MultiDataDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_GETDATA_RSP, value, &(tablePtr[1]), numDataPoints);
|
|
GWEN_MsgEndpoint_AddSendMessage(ep, outMsg);
|
|
free(tablePtr);
|
|
return AQH_MSG_IPC_SUCCESS;
|
|
}
|
|
else {
|
|
DBG_INFO(NULL, "No matching datapoints for value \"%s\"", AQH_Value_GetNameForSystem(value));
|
|
return AQH_MSG_IPC_ERROR_NODATA;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|